From add380f59be1735f97fa12619d04fa4234c9aaa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 21 Mar 2019 16:33:08 +0100 Subject: [PATCH] ref: move PureWidget back to core, use updateEnv --- examples/web/static/src/ts/ui/root.ts | 3 +-- examples/web/static/src/ts/widget.ts | 34 +++------------------------ src/component.ts | 18 ++++++++++++++ src/index.ts | 11 +++++++-- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/examples/web/static/src/ts/ui/root.ts b/examples/web/static/src/ts/ui/root.ts index d3d52d16..e6bb3215 100644 --- a/examples/web/static/src/ts/ui/root.ts +++ b/examples/web/static/src/ts/ui/root.ts @@ -40,8 +40,7 @@ export class Root extends Widget { applyMobileInterface() { const isMobile = window.innerWidth <= 768; if (isMobile !== this.env.isMobile) { - this.env.isMobile = isMobile; - this.render(); + this.updateEnv({ isMobile }); } } diff --git a/examples/web/static/src/ts/widget.ts b/examples/web/static/src/ts/widget.ts index 10f62567..4421ed81 100644 --- a/examples/web/static/src/ts/widget.ts +++ b/examples/web/static/src/ts/widget.ts @@ -1,38 +1,10 @@ -import { Component } from "../../../../../src/component"; +import { Component, PureComponent } from "../../../../../src/component"; import { Env } from "./env"; //------------------------------------------------------------------------------ // Widget classes //------------------------------------------------------------------------------ -export class Widget extends Component { - constructor(parent, props) { - super(parent, props); - (this.__widget__).isMobile = this.env.isMobile; - } - async updateProps(nextProps: P): Promise { - if ((this.__widget__).isMobile !== this.env.isMobile) { - (this.__widget__).isMobile = this.env.isMobile; - return this._updateProps(nextProps); - } - return super.updateProps(nextProps); - } -} +export class Widget extends Component {} -export class PureWidget extends Widget { - shouldUpdate(nextProps: P): boolean { - for (let k in nextProps) { - if (nextProps[k] !== this.props[k]) { - return true; - } - } - return false; - } - async updateState(nextState: Partial) { - for (let k in nextState) { - if (nextState[k] !== this.state[k]) { - return super.updateState(nextState); - } - } - } -} +export class PureWidget extends PureComponent {} diff --git a/src/component.ts b/src/component.ts index 373f7e96..d46d834c 100644 --- a/src/component.ts +++ b/src/component.ts @@ -407,3 +407,21 @@ export class Component< } } } + +export class PureComponent extends Component { + shouldUpdate(nextProps: P): boolean { + for (let k in nextProps) { + if (nextProps[k] !== this.props[k]) { + return true; + } + } + return false; + } + async updateState(nextState: Partial) { + for (let k in nextState) { + if (nextState[k] !== this.state[k]) { + return super.updateState(nextState); + } + } + } +} diff --git a/src/index.ts b/src/index.ts index 163c0545..11bb49b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,13 @@ import { QWeb } from "./qweb"; import { EventBus } from "./event_bus"; -import { Component } from "./component"; +import { Component, PureComponent } from "./component"; import { Store, StoreMixin } from "./store"; -export const core = { QWeb, EventBus, Component, Store, StoreMixin }; +export const core = { + QWeb, + EventBus, + Component, + PureComponent, + Store, + StoreMixin +};