From 311141a9e377a59346cbb566389bc2bde2f2c093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Tue, 5 Feb 2019 14:39:45 +0100 Subject: [PATCH] move Env definition to Widget --- web/static/src/ts/core/component.ts | 23 ++++++++ web/static/src/ts/env.ts | 54 ++--------------- web/static/src/ts/registries.ts | 4 ++ web/static/src/ts/widgets/widget.ts | 58 ++++++++++++------- web/static/tests/helpers.ts | 2 +- web/static/tests/widgets/home_menu.test.ts | 3 +- web/static/tests/widgets/notification.test.ts | 3 +- 7 files changed, 72 insertions(+), 75 deletions(-) diff --git a/web/static/src/ts/core/component.ts b/web/static/src/ts/core/component.ts index d391a460..00ed275f 100644 --- a/web/static/src/ts/core/component.ts +++ b/web/static/src/ts/core/component.ts @@ -269,3 +269,26 @@ export class Component< } } } + +export class PureComponent extends Component< + T, + Props, + State +> { + shouldUpdate(nextProps: Props): 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; + } + } + return super.updateState(nextState); + } +} diff --git a/web/static/src/ts/env.ts b/web/static/src/ts/env.ts index 4f61d77f..ad5cfcb2 100644 --- a/web/static/src/ts/env.ts +++ b/web/static/src/ts/env.ts @@ -1,52 +1,11 @@ -import { Ajax, IAjax } from "./core/ajax"; -import { WEnv } from "./core/component"; -import { - INotificationManager, - NotificationManager -} from "./core/notifications"; +import { Ajax } from "./core/ajax"; +import { NotificationManager } from "./core/notifications"; import { QWeb } from "./core/qweb_vdom"; -import { Registry } from "./core/registry"; -import { IRouter, Router } from "./core/router"; +import { Router } from "./core/router"; import { idGenerator, memoize } from "./core/utils"; import { actionRegistry } from "./registries"; -import { - ActionManager, - ActionWidget, - IActionManager -} from "./services/action_manager"; -import { CRM } from "./widgets/crm"; -import { Discuss } from "./widgets/discuss"; - -//------------------------------------------------------------------------------ -// Types -//------------------------------------------------------------------------------ - -export interface Menu { - title: string; - actionID: number; -} - -export interface Env extends WEnv { - // services - actionManager: IActionManager; - ajax: IAjax; - notifications: INotificationManager; - router: IRouter; - - // registries - actionRegistry: Registry; - - // helpers - rpc: IAjax["rpc"]; - - // configuration - debug: boolean; - isMobile: boolean; -} - -//------------------------------------------------------------------------------ -// Code -//------------------------------------------------------------------------------ +import { ActionManager } from "./services/action_manager"; +import { Env } from "./widgets/widget"; /** * makeEnvironment returns the main environment for the application. @@ -59,9 +18,6 @@ export interface Env extends WEnv { * this function will actually return the same environment. */ export const makeEnvironment = memoize(async function(): Promise { - // main application registry - actionRegistry.add("discuss", Discuss).add("crm", CRM); - // services const qweb = new QWeb(); const router = new Router(); diff --git a/web/static/src/ts/registries.ts b/web/static/src/ts/registries.ts index 7aa514fb..84d54888 100644 --- a/web/static/src/ts/registries.ts +++ b/web/static/src/ts/registries.ts @@ -1,4 +1,8 @@ import { Registry } from "./core/registry"; import { ActionWidget } from "./services/action_manager"; +import { CRM } from "./widgets/crm"; +import { Discuss } from "./widgets/discuss"; export const actionRegistry: Registry = new Registry(); + +actionRegistry.add("discuss", Discuss).add("crm", CRM); diff --git a/web/static/src/ts/widgets/widget.ts b/web/static/src/ts/widgets/widget.ts index 459e094d..d93c6099 100644 --- a/web/static/src/ts/widgets/widget.ts +++ b/web/static/src/ts/widgets/widget.ts @@ -1,24 +1,40 @@ -import { Component } from "../core/component"; -import { Env } from "../env"; +import { IAjax } from "../core/ajax"; +import { Component, PureComponent, WEnv } from "../core/component"; +import { INotificationManager } from "../core/notifications"; +import { Registry } from "../core/registry"; +import { IRouter } from "../core/router"; +import { ActionWidget, IActionManager } from "../services/action_manager"; + +//------------------------------------------------------------------------------ +// Types +//------------------------------------------------------------------------------ + +export interface Env extends WEnv { + // services + actionManager: IActionManager; + ajax: IAjax; + notifications: INotificationManager; + router: IRouter; + + // registries + actionRegistry: Registry; + + // helpers + rpc: IAjax["rpc"]; + + // configuration + debug: boolean; + isMobile: boolean; +} + +//------------------------------------------------------------------------------ +// Widget classes +//------------------------------------------------------------------------------ export class Widget extends Component {} -// TODO: move this to PureComponent in core -export class PureWidget extends Widget { - shouldUpdate(nextProps: Props): 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; - } - } - return super.updateState(nextState); - } -} +export class PureWidget extends PureComponent< + Env, + Props, + State +> {} diff --git a/web/static/tests/helpers.ts b/web/static/tests/helpers.ts index f6cf2044..6e18504f 100644 --- a/web/static/tests/helpers.ts +++ b/web/static/tests/helpers.ts @@ -1,7 +1,7 @@ import { QWeb } from "../src/ts/core/qweb_vdom"; import { idGenerator } from "../src/ts/core/utils"; import { WEnv } from "../src/ts/core/component"; -import { Env } from "../src/ts/env"; +import { Env } from "../src/ts/widgets/widget"; import { IAjax, RPCQuery } from "../src/ts/core/ajax"; import { Registry } from "../src/ts/core/registry"; import { NotificationManager } from "../src/ts/core/notifications"; diff --git a/web/static/tests/widgets/home_menu.test.ts b/web/static/tests/widgets/home_menu.test.ts index 595a0a7c..63f675b7 100644 --- a/web/static/tests/widgets/home_menu.test.ts +++ b/web/static/tests/widgets/home_menu.test.ts @@ -1,4 +1,3 @@ -import { Env } from "../../src/ts/env"; import { HomeMenu, Props } from "../../src/ts/widgets/home_menu"; import { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers"; @@ -7,7 +6,7 @@ import { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers"; //------------------------------------------------------------------------------ let fixture: HTMLElement; -let env: Env; +let env: ReturnType; let props: Props; let templates: string; diff --git a/web/static/tests/widgets/notification.test.ts b/web/static/tests/widgets/notification.test.ts index 063eaa48..3a3fb0c5 100644 --- a/web/static/tests/widgets/notification.test.ts +++ b/web/static/tests/widgets/notification.test.ts @@ -1,4 +1,3 @@ -import { Env } from "../../src/ts/env"; import { INotification } from "../../src/ts/core/notifications"; import { Notification } from "../../src/ts/widgets/notification"; import { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers"; @@ -8,7 +7,7 @@ import { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers"; //------------------------------------------------------------------------------ let fixture: HTMLElement; -let env: Env; +let env: ReturnType; let templates: string; beforeAll(async () => {