rework registry

This commit is contained in:
Géry Debongnie
2019-02-02 16:51:24 +01:00
parent c9f2a6147c
commit e26cf25bd3
10 changed files with 57 additions and 63 deletions
+4
View File
@@ -31,6 +31,10 @@ interface Meta<T extends WEnv> {
const patch = init([sdListeners, sdAttrs]); const patch = init([sdListeners, sdAttrs]);
export interface Type<T> extends Function {
new (...args: any[]): T;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Widget // Widget
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
+23
View File
@@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// Registry code
//------------------------------------------------------------------------------
export class Registry<T> {
private map: { [key: string]: T } = {};
add(key: string, item: T): Registry<T> {
if (key in this.map) {
throw new Error(`Key ${key} already exists!`);
}
this.map[key] = item;
return this;
}
get(key: string): T {
return this.map[key];
}
}
//------------------------------------------------------------------------------
// Main registry instance
//------------------------------------------------------------------------------
export const registry = new Registry();
+9 -7
View File
@@ -7,11 +7,12 @@ import {
INotificationManager, INotificationManager,
NotificationManager NotificationManager
} from "./core/notifications"; } from "./core/notifications";
import { registry, Registry } from "./services/registry"; import { actionRegistry } from "./registries";
import { Registry } from "./core/registry";
import { IRouter, Router } from "./core/router"; import { IRouter, Router } from "./core/router";
import { CRM } from "./widgets/crm"; import { CRM } from "./widgets/crm";
import { Discuss } from "./widgets/discuss"; import { Discuss } from "./widgets/discuss";
import { Widget, Type } from "./core/widget";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Types // Types
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -26,9 +27,11 @@ export interface Env extends WEnv {
actionManager: IActionManager; actionManager: IActionManager;
ajax: IAjax; ajax: IAjax;
notifications: INotificationManager; notifications: INotificationManager;
registry: Registry;
router: IRouter; router: IRouter;
// registries
actionRegistry: Registry<Type<Widget<Env, any>>>;
// data // data
menus: Menu[]; menus: Menu[];
@@ -56,14 +59,13 @@ export interface Env extends WEnv {
*/ */
export const makeEnvironment = memoize(async function(): Promise<Env> { export const makeEnvironment = memoize(async function(): Promise<Env> {
// main application registry // main application registry
registry.add("action", "discuss", Discuss); actionRegistry.add("discuss", Discuss).add("crm", CRM);
registry.add("action", "crm", CRM);
// services // services
const qweb = new QWeb(); const qweb = new QWeb();
const router = new Router(); const router = new Router();
const ajax = new Ajax(); const ajax = new Ajax();
const actionManager = new ActionManager(router, registry); const actionManager = new ActionManager(router, actionRegistry);
const notifications = new NotificationManager(); const notifications = new NotificationManager();
// demo data // demo data
@@ -89,7 +91,7 @@ export const makeEnvironment = memoize(async function(): Promise<Env> {
actionManager, actionManager,
ajax, ajax,
notifications, notifications,
registry, actionRegistry,
router, router,
menus, menus,
+1 -1
View File
@@ -2,7 +2,7 @@
import { debounce } from "./core/utils"; import { debounce } from "./core/utils";
import { makeEnvironment } from "./env"; import { makeEnvironment } from "./env";
import { Root } from "./root"; import { Root } from "./widgets/root";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Application bootstrapping // Application bootstrapping
+5
View File
@@ -0,0 +1,5 @@
import { Registry } from "./core/registry";
import { Widget, Type } from "./core/widget";
import { Env } from "./env";
export const actionRegistry: Registry<Type<Widget<Env, any>>> = new Registry();
+5 -6
View File
@@ -1,8 +1,7 @@
import { EventBus } from "../core/event_bus"; import { EventBus } from "../core/event_bus";
import { Widget } from "../core/widget"; import { Widget, Type } from "../core/widget";
import { Env } from "../env"; import { Env } from "../env";
import { Registry } from "./registry"; import { Registry } from "../core/registry";
import { Type } from "../types";
import { IRouter, Query } from "../core/router"; import { IRouter, Query } from "../core/router";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -51,10 +50,10 @@ export interface IActionManager {
export class ActionManager extends EventBus implements IActionManager { export class ActionManager extends EventBus implements IActionManager {
router: IRouter; router: IRouter;
registry: Registry; registry: Registry<Type<Widget<Env, any>>>;
stack: ActionStack; stack: ActionStack;
constructor(router: IRouter, registry: Registry) { constructor(router: IRouter, registry: Registry<Type<Widget<Env, any>>>) {
super(); super();
this.router = router; this.router = router;
this.registry = registry; this.registry = registry;
@@ -71,7 +70,7 @@ export class ActionManager extends EventBus implements IActionManager {
// this is an action ID // this is an action ID
let name = request === 1 ? "discuss" : "crm"; let name = request === 1 ? "discuss" : "crm";
let title = request === 1 ? "Discuss" : "CRM"; let title = request === 1 ? "Discuss" : "CRM";
let Widget = this.registry.getAction(name); let Widget = this.registry.get(name);
this.stack = [ this.stack = [
{ {
id: 1, id: 1,
-35
View File
@@ -1,35 +0,0 @@
import { Widget } from "../core/widget";
import { Env } from "../env";
import { Type } from "../types";
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
type ActionWidget = Type<Widget<Env, {}>>;
//------------------------------------------------------------------------------
// Registry code
//------------------------------------------------------------------------------
export class Registry {
registries: { [key: string]: { [key: string]: any } } = {
action: {}
};
add(type: "action", name: string, action: ActionWidget): Registry {
if (name in this.registries[type]) {
throw new Error(`Key ${name} already exists!`);
}
this.registries[type][name] = action;
return this;
}
getAction(name: string): ActionWidget {
return this.registries.action[name];
}
}
//------------------------------------------------------------------------------
// Main registry instance
//------------------------------------------------------------------------------
export const registry = new Registry();
-3
View File
@@ -1,3 +0,0 @@
export interface Type<T> extends Function {
new (...args: any[]): T;
}
@@ -1,11 +1,11 @@
import { Widget } from "./core/widget"; import { Widget } from "../core/widget";
import { Env } from "./env"; import { Env } from "../env";
import { ActionStack } from "./services/action_manager"; import { ActionStack } from "../services/action_manager";
import { INotification } from "./core/notifications"; import { INotification } from "../core/notifications";
import { Action } from "./widgets/action"; import { Action } from "./action";
import { HomeMenu } from "./widgets/home_menu"; import { HomeMenu } from "./home_menu";
import { Navbar } from "./widgets/navbar"; import { Navbar } from "./navbar";
import { Notification } from "./widgets/notification"; import { Notification } from "./notification";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Types // Types
+2 -3
View File
@@ -3,7 +3,7 @@ import { idGenerator } from "../src/ts/core/utils";
import { WEnv } from "../src/ts/core/widget"; import { WEnv } from "../src/ts/core/widget";
import { Env } from "../src/ts/env"; import { Env } from "../src/ts/env";
import { IAjax, RPCQuery } from "../src/ts/core/ajax"; import { IAjax, RPCQuery } from "../src/ts/core/ajax";
import { Registry } from "../src/ts/services/registry"; import { Registry } from "../src/ts/core/registry";
import { NotificationManager } from "../src/ts/core/notifications"; import { NotificationManager } from "../src/ts/core/notifications";
import { IActionManager, ActionEvent } from "../src/ts/services/action_manager"; import { IActionManager, ActionEvent } from "../src/ts/services/action_manager";
@@ -30,11 +30,10 @@ export function makeTestEnv(): Env {
const router = new MockRouter(); const router = new MockRouter();
const notifications = new NotificationManager(); const notifications = new NotificationManager();
let { qweb, getID } = makeTestWEnv(); let { qweb, getID } = makeTestWEnv();
const registry = new Registry();
return { return {
qweb, qweb,
getID, getID,
registry, actionRegistry: new Registry(),
ajax, ajax,
actionManager, actionManager,
notifications, notifications,