move registries into env, reorganize some code

This commit is contained in:
Géry Debongnie
2019-03-07 12:15:12 +01:00
parent 4b02b74440
commit c3c060a028
17 changed files with 194 additions and 178 deletions
+42 -9
View File
@@ -1,15 +1,27 @@
import { WEnv } from "./core/component";
import { QWeb } from "./core/qweb_vdom";
import { Registry } from "./core/registry";
import { idGenerator } from "./core/utils";
import { Notification, RPC, Services, Store } from "./store/store";
import { RPC } from "./services/ajax";
import { IRouter } from "./services/router";
import { ControllerWidget, Store, Notification } from "./store/store";
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
export interface Services {
rpc: RPC;
router: IRouter;
}
export interface Env extends WEnv {
services: Services;
// registries
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
// commands
activateMenuItem(menuId: number): void;
addNotification(notif: Partial<Notification>): number;
@@ -24,30 +36,51 @@ export interface Env extends WEnv {
isMobile: boolean;
}
export interface InitData {
services: Services;
templates: string;
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
}
//------------------------------------------------------------------------------
// Environment
//------------------------------------------------------------------------------
export function makeEnv(store: Store, templates: string): Env {
export function makeEnv(data: InitData): Env {
const qweb = new QWeb();
qweb.addTemplate("default", "<div/>");
qweb.loadTemplates(templates);
qweb.loadTemplates(data.templates);
function throwNonImplementedError(): any {
throw new Error("Not implemented. This feature requires a store.");
}
const env: Env = {
qweb,
getID: idGenerator(),
services: store.services,
services: data.services,
actionRegistry: data.actionRegistry,
viewRegistry: data.viewRegistry,
activateMenuItem: store.activateMenuItem.bind(store),
addNotification: store.addNotification.bind(store),
closeNotification: store.closeNotification.bind(store),
toggleHomeMenu: store.toggleHomeMenu.bind(store),
activateMenuItem: throwNonImplementedError,
addNotification: throwNonImplementedError,
closeNotification: throwNonImplementedError,
toggleHomeMenu: throwNonImplementedError,
rpc: store.rpc.bind(store),
rpc: throwNonImplementedError,
debug: false,
isMobile: window.innerWidth <= 768
};
return env;
}
export function linkStoreToEnv(env: Env, store: Store) {
env.activateMenuItem = store.activateMenuItem.bind(store);
env.addNotification = store.addNotification.bind(store);
env.closeNotification = store.closeNotification.bind(store);
env.toggleHomeMenu = store.toggleHomeMenu.bind(store);
env.rpc = store.rpc.bind(store);
}
+4 -3
View File
@@ -1,6 +1,6 @@
///<amd-module name="main" />
import { makeEnv } from "./env";
import { makeEnv, linkStoreToEnv } from "./env";
import { loadMenus, loadTemplates } from "./loaders";
import { actionRegistry, viewRegistry } from "./registries";
import { rpc } from "./services/ajax";
@@ -20,8 +20,9 @@ document.addEventListener("DOMContentLoaded", async function() {
const templates = await loadTemplates();
const menuInfo = loadMenus();
const store = new Store(services, menuInfo, actionRegistry, viewRegistry);
const env = makeEnv(store, templates);
const env = makeEnv({ services, templates, actionRegistry, viewRegistry });
const store = new Store(env, menuInfo);
linkStoreToEnv(env, store);
// Creating root widget
const rootWidget = new Root(env, store);
@@ -82,7 +82,7 @@ export function actionManagerMixin<T extends ReturnType<typeof rpcMixin>>(
doActWindowAction(descr: ActWindowActionDescription) {
const tag = descr.views[0][1];
let View = this.viewRegistry.get(tag);
let View = this.env.viewRegistry.get(tag);
if (!View) {
this.addNotification({
title: "Invalid View type",
@@ -105,7 +105,7 @@ export function actionManagerMixin<T extends ReturnType<typeof rpcMixin>>(
doClientAction(descr: ClientActionDescription): Controller["create"] {
let key = descr.tag;
let ActionWidget = this.actionRegistry.get(key);
let ActionWidget = this.env.actionRegistry.get(key);
if (!ActionWidget) {
this.addNotification({
title: "Invalid Client Action",
+1 -1
View File
@@ -41,7 +41,7 @@ export function rpcMixin<T extends Type<BaseStore>>(Base: T) {
this.trigger("rpc_status", "loading");
}
this.counter++;
const result = await this.services.rpc(request.route, request.params);
const result = await this.env.services.rpc(request.route, request.params);
this.counter--;
if (this.counter === 0) {
this.trigger("rpc_status", "notloading");
+22 -42
View File
@@ -1,9 +1,8 @@
import { EventBus } from "../core/event_bus";
import { Registry } from "../core/registry";
import { idGenerator } from "../core/utils";
import { RPC } from "../services/ajax";
import { IRouter, Query } from "../services/router";
import { actionManagerMixin, ControllerWidget } from "./action_manager_mixin";
import { Env } from "../env";
import { Query } from "../services/router";
import { actionManagerMixin } from "./action_manager_mixin";
import { rpcMixin } from "./rpc_mixin";
import { MenuItem } from "./store";
@@ -27,6 +26,14 @@ export interface MenuItem {
children: MenuItem[];
}
export interface Notification {
id: number;
title: string;
message: string;
type: "notification" | "warning";
sticky: boolean;
}
export interface MenuInfo {
menus: { [key: number]: MenuItem | undefined };
@@ -40,19 +47,6 @@ export interface State {
notifications: Notification[];
}
export interface Services {
rpc: RPC;
router: IRouter;
}
export interface Notification {
id: number;
title: string;
message: string;
type: "notification" | "warning";
sticky: boolean;
}
//------------------------------------------------------------------------------
// Store
//------------------------------------------------------------------------------
@@ -63,23 +57,14 @@ export class BaseStore extends EventBus {
notifications: []
};
menuInfo: MenuInfo;
services: Services;
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
env: Env;
currentQuery: Query;
generateID = idGenerator();
constructor(
services: Services,
menuInfo: MenuInfo,
actionRegistry: Registry<ControllerWidget>,
viewRegistry: Registry<ControllerWidget>
) {
constructor(env: Env, menuInfo: MenuInfo) {
super();
this.services = services;
this.env = env;
this.menuInfo = menuInfo;
this.actionRegistry = actionRegistry;
this.viewRegistry = viewRegistry;
this.currentQuery = {};
}
@@ -94,7 +79,7 @@ export class BaseStore extends EventBus {
}
this.update({ inHome: !this.state.inHome });
if (this.state.inHome) {
this.services.router.navigate({ home: true });
this.env.services.router.navigate({ home: true });
} else {
this.updateQuery(this.currentQuery);
}
@@ -102,7 +87,7 @@ export class BaseStore extends EventBus {
updateQuery(query: Query) {
this.currentQuery = query;
this.services.router.navigate(query);
this.env.services.router.navigate(query);
}
addNotification(notif: Partial<Notification>): number {
@@ -129,22 +114,17 @@ export class BaseStore extends EventBus {
}
export class Store extends actionManagerMixin(rpcMixin(BaseStore)) {
constructor(
services: Services,
menuInfo: MenuInfo,
actionRegistry: Registry<ControllerWidget>,
viewRegistry: Registry<ControllerWidget>
) {
super(services, menuInfo, actionRegistry, viewRegistry);
const query = this.services.router.getQuery();
constructor(env: Env, menuInfo: MenuInfo) {
super(env, menuInfo);
const query = this.env.services.router.getQuery();
let { app, actionId } = this.getAppAndAction(query);
this.state.currentApp = app;
if (!actionId) {
this.state.inHome = true;
this.services.router.navigate({ home: true });
this.env.services.router.navigate({ home: true });
}
this.services.router.on("query_changed", this, this.updateAction);
this.updateAction(this.services.router.getQuery());
this.env.services.router.on("query_changed", this, this.updateAction);
this.updateAction(this.env.services.router.getQuery());
}
private updateAction(query: Query) {
+2 -2
View File
@@ -1,11 +1,11 @@
import { debounce } from "../core/utils";
import { Env } from "../env";
import { Controller } from "../store/action_manager_mixin";
import { State, Store } from "../store/store";
import { Widget } from "../widget";
import { HomeMenu } from "./home_menu";
import { Navbar } from "./navbar";
import { Notification } from "./notification";
import { Widget } from "../widget";
import { Controller } from "../store/action_manager_mixin";
//------------------------------------------------------------------------------
// Root Widget
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget";
export class FormView extends Widget<{}, { info: any }> {
export class FormView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>form view: <span t-esc="props.info"/></div>`;
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget";
export class KanbanView extends Widget<{}, { info: any }> {
export class KanbanView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>kanban view: <span t-esc="props.info"/></div>`;
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget";
export class ListView extends Widget<{}, { info: any }> {
export class ListView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>list view: <span t-esc="props.info"/></div>`;
}
+28 -29
View File
@@ -1,39 +1,20 @@
import { readFile } from "fs";
import { BaseMenuItem, getMenuInfo } from "../../src/ts/loaders";
import { actionRegistry, viewRegistry } from "../../src/ts/registries";
import { ActionDescription } from "../../src/ts/store/action_manager_mixin";
import { MenuInfo } from "../../src/ts/store/store";
import { Registry } from "../../src/ts/core/registry";
export interface TestData {
menuInfo: MenuInfo;
actions: ActionDescription[];
actionRegistry: typeof actionRegistry;
viewRegistry: typeof viewRegistry;
templates: string;
}
let templates: string;
export async function makeTestData(): Promise<TestData> {
if (!templates) {
templates = await loadTemplates();
}
const _actionRegistry: typeof actionRegistry = new Registry();
(<any>_actionRegistry).map = Object.assign({}, (<any>actionRegistry).map);
const _viewRegistry: typeof actionRegistry = new Registry();
(<any>_viewRegistry).map = Object.assign({}, (<any>actionRegistry).map);
export function makeTestData(): TestData {
return {
menuInfo: makeMenuInfo(),
actions: makeActionData(),
actionRegistry: _actionRegistry,
viewRegistry: _viewRegistry,
templates
actions: makeActionData()
};
}
export function makeMenuInfo(): MenuInfo {
function makeMenuInfo(): MenuInfo {
const items: BaseMenuItem[] = [
{
id: 96,
@@ -215,10 +196,28 @@ function makeActionData(): ActionDescription[] {
];
}
function loadTemplates(): Promise<string> {
return new Promise((resolve, reject) => {
readFile("web/static/src/xml/templates.xml", "utf-8", (err, result) => {
resolve(result);
});
});
}
// function loadTemplates(): Promise<string> {
// return new Promise((resolve, reject) => {
// readFile("web/static/src/xml/templates.xml", "utf-8", (err, result) => {
// resolve(result);
// });
// });
// }
// export async function makeTestData(): Promise<TestData> {
// if (!templates) {
// templates = await loadTemplates();
// }
// const _actionRegistry: typeof actionRegistry = new Registry();
// (<any>_actionRegistry).map = Object.assign({}, (<any>actionRegistry).map);
// const _viewRegistry: typeof actionRegistry = new Registry();
// (<any>_viewRegistry).map = Object.assign({}, (<any>actionRegistry).map);
// return {
// menuInfo: makeMenuInfo(),
// actions: makeActionData(),
// actionRegistry: _actionRegistry,
// viewRegistry: _viewRegistry,
// templates
// };
// }
+52 -37
View File
@@ -1,13 +1,17 @@
import { readFileSync } from "fs";
import { WEnv } from "../../src/ts/core/component";
import { QWeb } from "../../src/ts/core/qweb_vdom";
import { Registry } from "../../src/ts/core/registry";
import { idGenerator } from "../../src/ts/core/utils";
import { Env, makeEnv } from "../../src/ts/env";
import { IRouter } from "../../src/ts/services/router";
import { MenuInfo, Store } from "../../src/ts/store/store";
import { Env, InitData, linkStoreToEnv, makeEnv } from "../../src/ts/env";
import {
actionRegistry as AR,
viewRegistry as VR
} from "../../src/ts/registries";
import { Store } from "../../src/ts/store/store";
import { MockRouter } from "./mock_router";
import { MockServer } from "./mock_server";
import { TestData } from "./test_data";
import { makeTestData, TestData } from "./test_data";
//------------------------------------------------------------------------------
// Types
@@ -17,11 +21,16 @@ export interface TestEnv extends Env {
store: Store;
}
export interface TestInfo extends Partial<TestData> {
export interface TestInfo extends Partial<InitData>, Partial<TestData> {
mockRPC?(this: MockServer, route: string, params: any): Promise<any>;
router?: IRouter;
}
//------------------------------------------------------------------------------
// Code
//------------------------------------------------------------------------------
const TEMPLATES = readFileSync("web/static/src/xml/templates.xml", "utf-8");
export function makeTestWEnv(): WEnv {
return {
qweb: new QWeb(),
@@ -29,44 +38,50 @@ export function makeTestWEnv(): WEnv {
};
}
//------------------------------------------------------------------------------
// Code
//------------------------------------------------------------------------------
export function makeTestEnv(info: TestInfo = {}): TestEnv {
const templates = info.templates || "";
const menuInfo: MenuInfo = info.menuInfo || {
menus: {},
actionMap: {},
roots: []
};
const actionRegistry = info.actionRegistry || new Registry();
const viewRegistry = info.viewRegistry || new Registry();
const actions = info.actions || [];
const templates = info.templates || TEMPLATES;
const testData: TestData = makeTestData();
if (info.menuInfo) {
testData.menuInfo = info.menuInfo;
}
if (info.actions) {
testData.actions = info.actions;
}
const data: TestData = {
menuInfo,
actions,
const actionRegistry = info.actionRegistry || cloneRegistry(AR);
const viewRegistry = info.viewRegistry || cloneRegistry(VR);
let rpc = info.services && info.services.rpc;
if (!rpc) {
const mockServer = new MockServer(testData);
rpc = function(route: string, params: any): Promise<any> {
if (info.mockRPC) {
return info.mockRPC.call(mockServer, route, params);
}
return mockServer.rpc(route, params);
};
}
const router = (info.services && info.services.router) || new MockRouter();
const initData: InitData = {
services: {
rpc,
router
},
actionRegistry,
viewRegistry,
templates
};
const mockServer = new MockServer(data);
function rpc(route: string, params: any): Promise<any> {
if (info.mockRPC) {
return info.mockRPC.call(mockServer, route, params);
}
return mockServer.rpc(route, params);
}
const services = {
rpc,
router: info.router || new MockRouter()
};
const store = new Store(services, menuInfo, actionRegistry, viewRegistry);
const env = makeEnv(store, templates);
const env = makeEnv(initData);
const store = new Store(env, testData.menuInfo);
linkStoreToEnv(env, store);
const testEnv = Object.assign({ store }, env);
return testEnv;
}
function cloneRegistry<T>(registry: Registry<T>): Registry<T> {
const clone: Registry<T> = new Registry();
(<any>clone).map = Object.assign({}, (<any>registry).map);
return clone;
}
@@ -1,5 +1,5 @@
import { makeTestData, makeTestEnv } from "../helpers";
import { Registry } from "../../src/ts/core/registry";
import { makeTestEnv } from "../helpers";
//------------------------------------------------------------------------------
// Tests
@@ -7,9 +7,7 @@ import { Registry } from "../../src/ts/core/registry";
test("does not reload action if already done", async () => {
const routes: string[] = [];
const data = await makeTestData();
const testEnv = makeTestEnv({
...data,
mockRPC(route, params) {
routes.push(route);
return this.rpc(route, params);
@@ -28,9 +26,7 @@ test("does not reload action if already done", async () => {
});
test("display a warning if client action is not in registry", async () => {
const data = await makeTestData();
data.actionRegistry = new Registry();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv({ actionRegistry: new Registry() });
await testEnv.store.doAction(131);
@@ -40,9 +36,7 @@ test("display a warning if client action is not in registry", async () => {
});
test("display a warning if view is not in registry", async () => {
const data = await makeTestData();
data.viewRegistry = new Registry();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv({ viewRegistry: new Registry() });
await testEnv.store.doAction(250);
+21 -17
View File
@@ -1,4 +1,9 @@
import { makeTestData, makeTestEnv, nextMicroTick } from "../helpers";
import { Store } from "../../src/ts/store/store";
import { makeTestEnv, nextMicroTick, TestInfo } from "../helpers";
function makeTestStore(info?: TestInfo): Store {
return makeTestEnv(info).store;
}
//------------------------------------------------------------------------------
// Tests
@@ -8,18 +13,18 @@ describe("rpc", () => {
test("properly translate query in route", async () => {
expect.assertions(1);
const store = makeTestEnv({
const store = makeTestStore({
mockRPC(route, params) {
expect(route).toBe("/web/dataset/call_kw/test/hey");
return this.rpc(route, params);
}
}).store;
});
await store.rpc({ model: "test", method: "hey" });
});
test("trigger proper events", async () => {
const store = makeTestEnv().store;
const store = makeTestStore();
const events: string[] = [];
store.on("rpc_status", null, s => {
events.push(s);
@@ -34,7 +39,7 @@ describe("rpc", () => {
describe("notifications", () => {
test("can subscribe and add notification", () => {
const store = makeTestEnv().store;
const store = makeTestStore();
expect(store.state.notifications.length).toBe(0);
const id = store.addNotification({
title: "test",
@@ -45,7 +50,7 @@ describe("notifications", () => {
});
test("can close a notification", () => {
const store = makeTestEnv().store;
const store = makeTestStore();
const id = store.addNotification({
title: "test",
@@ -59,7 +64,7 @@ describe("notifications", () => {
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
const store = makeTestEnv().store;
const store = makeTestStore();
store.addNotification({ title: "test", message: "message" });
@@ -71,7 +76,7 @@ describe("notifications", () => {
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
const store = makeTestEnv().store;
const store = makeTestStore();
store.addNotification({
title: "test",
@@ -88,32 +93,32 @@ describe("notifications", () => {
describe("state transitions", () => {
test("toggle menu", async () => {
const data = await makeTestData();
const store = makeTestEnv(data).store;
const env = makeTestEnv();
const store = env.store;
expect(store.state.inHome).toBe(true);
store.toggleHomeMenu();
// should still be in home menu since no app is currently active
expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true });
expect(env.services.router.getQuery()).toEqual({ home: true });
const promise = store.activateMenuItem(96);
expect(store.services.router.getQuery()).toEqual({ home: true });
expect(env.services.router.getQuery()).toEqual({ home: true });
await promise;
store.activateController(store.lastController!);
expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({
expect(env.services.router.getQuery()).toEqual({
action_id: "131",
menu_id: "96"
});
store.toggleHomeMenu();
expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true });
expect(env.services.router.getQuery()).toEqual({ home: true });
store.toggleHomeMenu();
expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({
expect(env.services.router.getQuery()).toEqual({
action_id: "131",
menu_id: "96"
});
@@ -121,8 +126,7 @@ describe("state transitions", () => {
test("document title", async () => {
document.title = "Odoo";
const data = await makeTestData();
const store = makeTestEnv(data).store;
const store = makeTestStore();
expect(store.state.inHome).toBe(true);
expect(document.title).toBe("Odoo");
const promise = store.activateMenuItem(96);
+3 -4
View File
@@ -1,5 +1,5 @@
import { HomeMenu } from "../../src/ts/ui/home_menu";
import { makeTestData, makeTestEnv, makeTestFixture } from "../helpers";
import { makeTestEnv, makeTestFixture } from "../helpers";
//------------------------------------------------------------------------------
// Setup and helpers
@@ -20,9 +20,8 @@ afterEach(() => {
//------------------------------------------------------------------------------
test("can be rendered", async () => {
const data = await makeTestData();
const testEnv = makeTestEnv(data);
const props = { menuInfo: data.menuInfo };
const testEnv = makeTestEnv();
const props = { menuInfo: testEnv.store.menuInfo };
const homeMenu = new HomeMenu(testEnv, props);
await homeMenu.mount(fixture);
expect(fixture.innerHTML).toMatchSnapshot();
+3 -4
View File
@@ -11,12 +11,11 @@ let env: helpers.TestEnv;
let props: Props;
let menuInfo: MenuInfo;
beforeEach(async () => {
beforeEach(() => {
fixture = helpers.makeTestFixture();
const data = await helpers.makeTestData();
env = helpers.makeTestEnv(data);
env = helpers.makeTestEnv();
props = { inHome: false, app: null };
menuInfo = helpers.makeMenuInfo();
menuInfo = env.store.menuInfo;
});
afterEach(() => {
+1 -2
View File
@@ -11,8 +11,7 @@ let env: helpers.TestEnv;
beforeEach(async () => {
fixture = helpers.makeTestFixture();
const data = await helpers.makeTestData();
env = helpers.makeTestEnv(data);
env = helpers.makeTestEnv();
});
afterEach(() => {
+7 -14
View File
@@ -1,7 +1,7 @@
import { Registry } from "../../src/ts/core/registry";
import { Root } from "../../src/ts/ui/root";
import * as helpers from "../helpers";
import { makeTestData, makeTestEnv } from "../helpers";
import { Registry } from "../../src/ts/core/registry";
import { makeTestEnv } from "../helpers";
//------------------------------------------------------------------------------
// Setup and helpers
@@ -22,17 +22,15 @@ afterEach(() => {
//------------------------------------------------------------------------------
test("can be rendered (in home menu)", async () => {
const data = await makeTestData();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv();
const root = new Root(testEnv, testEnv.store);
await root.mount(fixture);
expect(fixture.innerHTML).toMatchSnapshot();
});
test("if url has action_id, will render action and navigate to proper menu_id", async () => {
const data = await makeTestData();
const router = new helpers.MockRouter({ action_id: "131" });
const testEnv = makeTestEnv({ ...data, router });
const testEnv = makeTestEnv({ services: <any>{ router } });
await helpers.nextTick();
const root = new Root(testEnv, testEnv.store);
@@ -46,8 +44,7 @@ test("if url has action_id, will render action and navigate to proper menu_id",
});
test("start with no action => clicks on client action => discuss is rendered", async () => {
const data = await makeTestData();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv();
const root = new Root(testEnv, testEnv.store);
await root.mount(fixture);
@@ -64,9 +61,7 @@ test("start with no action => clicks on client action => discuss is rendered", a
});
test("clicks on client action with invalid key => empty widget is rendered + warning", async () => {
const data = await makeTestData();
data.actionRegistry = new Registry();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv({ actionRegistry: new Registry() });
const root = new Root(testEnv, testEnv.store);
await root.mount(fixture);
@@ -78,9 +73,7 @@ test("clicks on client action with invalid key => empty widget is rendered + war
});
test("open act window action with invalid viewtype => empty widget is rendered + warning", async () => {
const data = await makeTestData();
data.viewRegistry = new Registry();
const testEnv = makeTestEnv(data);
const testEnv = makeTestEnv({ viewRegistry: new Registry() });
const root = new Root(testEnv, testEnv.store);
await root.mount(fixture);