From d99ea4c9f49e90e5acb4f305b1af0bc7f409747e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 15 Mar 2019 11:54:59 +0100 Subject: [PATCH] rename updateState into setState --- demo/static/src/ts/discuss/clock.ts | 2 +- demo/static/src/ts/discuss/counter.ts | 2 +- demo/static/src/ts/discuss/discuss.ts | 8 ++--- demo/static/src/ts/ui/root.ts | 2 +- demo/static/src/ts/widget.ts | 4 +-- demo/static/src/xml/templates.xml | 2 +- doc/component.md | 2 +- src/component.ts | 4 +-- tests/component.test.ts | 52 +++++++++++++-------------- 9 files changed, 39 insertions(+), 39 deletions(-) diff --git a/demo/static/src/ts/discuss/clock.ts b/demo/static/src/ts/discuss/clock.ts index 45fc6a67..9007e09a 100644 --- a/demo/static/src/ts/discuss/clock.ts +++ b/demo/static/src/ts/discuss/clock.ts @@ -33,7 +33,7 @@ export class Clock extends Widget<{}, State> { } updateTime() { - this.updateState({ currentTime: new Date().toLocaleTimeString() }); + this.setState({ currentTime: new Date().toLocaleTimeString() }); } startClock() { diff --git a/demo/static/src/ts/discuss/counter.ts b/demo/static/src/ts/discuss/counter.ts index 4c783cae..555c0af1 100644 --- a/demo/static/src/ts/discuss/counter.ts +++ b/demo/static/src/ts/discuss/counter.ts @@ -34,6 +34,6 @@ export class Counter extends Widget { } increment(delta: number) { - this.updateState({ counter: this.state.counter + delta }); + this.setState({ counter: this.state.counter + delta }); } } diff --git a/demo/static/src/ts/discuss/discuss.ts b/demo/static/src/ts/discuss/discuss.ts index f4db91f9..641d1f27 100644 --- a/demo/static/src/ts/discuss/discuss.ts +++ b/demo/static/src/ts/discuss/discuss.ts @@ -22,25 +22,25 @@ export class Discuss extends Widget<{}, State> { resetCounter(ev: MouseEvent) { if (this.refs.counter instanceof Counter) { - this.refs.counter.updateState({ counter: 3 }); + this.refs.counter.setState({ counter: 3 }); } } resetCounterAsync(ev: MouseEvent) { setTimeout(() => { if (this.refs.counter2 instanceof Counter) { - this.refs.counter2.updateState({ counter: 300 }); + this.refs.counter2.setState({ counter: 300 }); } }, 3000); } toggle() { - this.updateState({ validcounter: !this.state.validcounter }); + this.setState({ validcounter: !this.state.validcounter }); } toggleColor() { const newColor = this.state.color === "red" ? "blue" : "red"; - this.updateState({ color: newColor }); + this.setState({ color: newColor }); } addNotif(sticky: boolean) { diff --git a/demo/static/src/ts/ui/root.ts b/demo/static/src/ts/ui/root.ts index 09a9694f..a4daa9b1 100644 --- a/demo/static/src/ts/ui/root.ts +++ b/demo/static/src/ts/ui/root.ts @@ -24,7 +24,7 @@ export class Root extends Widget { } mounted() { - this.store.on("state_updated", this, this.updateState); + this.store.on("state_updated", this, this.setState); this.store.on("rpc_status", this, this.toggleLoadingIndicator); this.store.on("update_action", this, this.applyController); if (this.store.lastController) { diff --git a/demo/static/src/ts/widget.ts b/demo/static/src/ts/widget.ts index 76905330..6ff84d27 100644 --- a/demo/static/src/ts/widget.ts +++ b/demo/static/src/ts/widget.ts @@ -28,10 +28,10 @@ export class PureWidget extends Widget { } return false; } - async updateState(nextState: Partial) { + async setState(nextState: Partial) { for (let k in nextState) { if (nextState[k] !== this.state[k]) { - return super.updateState(nextState); + return super.setState(nextState); } } } diff --git a/demo/static/src/xml/templates.xml b/demo/static/src/xml/templates.xml index cd4bf7f2..6effdcee 100644 --- a/demo/static/src/xml/templates.xml +++ b/demo/static/src/xml/templates.xml @@ -114,7 +114,7 @@ - + diff --git a/doc/component.md b/doc/component.md index cbc6ad5f..fb9aa880 100644 --- a/doc/component.md +++ b/doc/component.md @@ -24,7 +24,7 @@ export class Counter extends Component { } increment(delta) { - this.updateState({ counter: this.state.counter + delta }); + this.setState({ counter: this.state.counter + delta }); } } ``` diff --git a/src/component.ts b/src/component.ts index 49f4b1ff..87dbc34e 100644 --- a/src/component.ts +++ b/src/component.ts @@ -205,10 +205,10 @@ export class Component< * * Notes: * - it checks if we do not add extra keys to the state. - * - it is ok to call updateState before the widget is started. In that + * - it is ok to call setState before the widget is started. In that * case, it will simply update the state and will not rerender */ - async updateState(nextState: Partial) { + async setState(nextState: Partial) { if (Object.keys(nextState).length === 0) { return; } diff --git a/tests/component.test.ts b/tests/component.test.ts index 4beb011c..19b5201b 100644 --- a/tests/component.test.ts +++ b/tests/component.test.ts @@ -52,7 +52,7 @@ class Counter extends Widget { }; inc() { - this.updateState({ counter: this.state.counter + 1 }); + this.setState({ counter: this.state.counter + 1 }); } } @@ -103,11 +103,11 @@ describe("basic widget properties", () => { ); }); - test("updateState before first render does not trigger a render", async () => { + test("setState before first render does not trigger a render", async () => { let renderCalls = 0; class TestW extends Widget { async willStart() { - this.updateState({}); + this.setState({}); } async _render() { renderCalls++; @@ -119,10 +119,10 @@ describe("basic widget properties", () => { expect(renderCalls).toBe(1); }); - test("updateState does not allow adding extra keys", async () => { + test("setState does not allow adding extra keys", async () => { const widget = new Widget(env); try { - await widget.updateState({ extra: 1 }); + await widget.setState({ extra: 1 }); } catch (e) { expect(e.message).toMatch("Invalid key:"); } @@ -253,7 +253,7 @@ describe("lifecycle hooks", () => { const widget = new ParentWidget(env); await widget.mount(fixture); expect(hookCounter).toBe(0); // sub widget not created yet - await widget.updateState({ ok: true }); + await widget.setState({ ok: true }); expect(hookCounter).toBe(2); }); @@ -313,7 +313,7 @@ describe("lifecycle hooks", () => { const widget = new ParentWidget(env); await widget.mount(fixture); expect(steps).toEqual(["init", "willstart", "mounted"]); - await widget.updateState({ ok: false }); + await widget.setState({ ok: false }); expect(steps).toEqual([ "init", "willstart", @@ -568,9 +568,9 @@ describe("composition", () => { expect(fixture.innerHTML).toBe( "
1
" ); - await widget.updateState({ ok: false }); + await widget.setState({ ok: false }); expect(fixture.innerHTML).toBe("
"); - await widget.updateState({ ok: true }); + await widget.setState({ ok: true }); expect(fixture.innerHTML).toBe( "
0
" ); @@ -592,10 +592,10 @@ describe("composition", () => { ); const counter = children(widget)[0]; expect(counter.__widget__.isMounted).toBe(true); - await widget.updateState({ ok: false }); + await widget.setState({ ok: false }); expect(fixture.innerHTML).toBe("
"); expect(counter.__widget__.isMounted).toBe(false); - await widget.updateState({ ok: true }); + await widget.setState({ ok: true }); expect(counter.__widget__.isMounted).toBe(true); expect(fixture.innerHTML).toBe( "
1
" @@ -615,9 +615,9 @@ describe("composition", () => { await widget.mount(fixture); const input = fixture.getElementsByTagName("input")[0]; input.value = "test"; - await widget.updateState({ ok: false }); + await widget.setState({ ok: false }); expect(fixture.innerHTML).toBe("
"); - await widget.updateState({ ok: true }); + await widget.setState({ ok: true }); expect(fixture.innerHTML).toBe("
"); const input2 = fixture.getElementsByTagName("input")[0]; expect(input).toBe(input2); @@ -682,7 +682,7 @@ describe("composition", () => { } const parent = new Parent(env); await parent.mount(fixture); - await parent.updateState({ numbers: [1, 3] }); + await parent.setState({ numbers: [1, 3] }); expect(normalize(fixture.innerHTML)).toBe( normalize(`
@@ -713,7 +713,7 @@ describe("composition", () => { const parent = new Parent(env); await parent.mount(fixture); const child = children(parent)[0]; - await parent.updateState({ flag: true }); + await parent.setState({ flag: true }); expect(children(parent)[0]).toBe(child); expect(child.__widget__.isDestroyed).toBe(false); expect(normalize(fixture.innerHTML)).toBe( @@ -795,7 +795,7 @@ describe("props evaluation (with t-props directive)", () => {
`; - widgets = { child: Child } + widgets = { child: Child }; } class Child extends Widget { inlineTemplate = ` @@ -848,10 +848,10 @@ describe("other directives with t-widget", () => { expect(fixture.innerHTML).toBe("
hey
"); - await widget.updateState({ flag: false }); + await widget.setState({ flag: false }); expect(fixture.innerHTML).toBe("
"); - await widget.updateState({ flag: true }); + await widget.setState({ flag: true }); expect(fixture.innerHTML).toBe("
hey
"); }); @@ -874,7 +874,7 @@ describe("other directives with t-widget", () => { expect(normalize(fixture.innerHTML)).toBe("
somediv
"); - await widget.updateState({ flag: false }); + await widget.setState({ flag: false }); expect(normalize(fixture.innerHTML)).toBe("
hey
"); }); }); @@ -910,7 +910,7 @@ describe("random stuff/miscellaneous", () => { const widget = new Parent(env); await widget.mount(fixture); expect(fixture.innerHTML).toBe("
abc
"); - await widget.updateState({ flag: true }); + await widget.setState({ flag: true }); expect(fixture.innerHTML).toBe("
abcdef
"); }); @@ -979,10 +979,10 @@ describe("async rendering", () => { const w = new W(env); await w.mount(fixture); expect(n).toBe(0); - w.updateState({ val: 2 }); + w.setState({ val: 2 }); expect(n).toBe(1); await nextTick(); - w.updateState({ val: 3 }); + w.setState({ val: 3 }); expect(n).toBe(2); def.resolve(); await nextTick(); @@ -1019,10 +1019,10 @@ describe("async rendering", () => { const parent = new Parent(env); await parent.mount(fixture); expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("
"); - parent.updateState({ flagA: true }); + parent.setState({ flagA: true }); await nextTick(); expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("
"); - parent.updateState({ flagB: true }); + parent.setState({ flagB: true }); await nextTick(); expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("
"); defB.resolve(); @@ -1065,12 +1065,12 @@ describe("async rendering", () => { expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe( "
a1
" ); - parent.updateState({ valA: 2 }); + parent.setState({ valA: 2 }); await nextTick(); expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe( "
a1
" ); - parent.updateState({ flagB: true }); + parent.setState({ flagB: true }); await nextTick(); expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe( "
a1
"