-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.d.ts
107 lines (95 loc) · 3.35 KB
/
types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {NsEventListener, NsEventEmitter, EventListener} from "./lib/namespaced-emitter";
export interface ModuleDefn extends Object {
/**
* The name of the application module
*/
name: string;
/**
* The module impmentation
*/
module?: any | null | undefined;
/**
* Initialize this module and return the actual module.
* This module can by any object, value, service, function, etc.
* @param {AppContext} context The application context
*/
async initialize(context: AppContext): Promise<any> | any;
}
/**
* Application context provides access to other modules installed as part of the application. It provides
* API to register modules a rudimentary dependency management for modules that depend on other modules
* Usage
* @example
* <code>
* const context = createAppContext();
* context.registerModule({
* name: "mymodule",
* async initialize(ctx) {
* const [a, b] = await ctx.dependency(["a", "b"]);
* const service = await createMyService(a, b);
* return service;
* }
* })
* </code>
*
*/
export interface AppContext extends Object {
/**
* Register a module with the application context. Module defination has a name and initialize(context)
* async function (Returns a promise) The promise can resolve to any object or service.
* @param {ModuleDefn} moduleDefn The module definition with following structure
* @example
* <code>
* const defn = {
* name: "my-module" // A unique name
* async initialize(ctx) {}
* }
*
* context.register(defn)
* </code>
*/
async register(module: ModuleDefn): Promise<ModuleDefn>;
/**
* Gets a module by name or null if the module does not exist
* @param {String} name The module name with which it was registered
* @return {any} The module or null of module was not found
*/
getModule(name: string): any;
/**
* Register to be nodified when a dependent module was registered and initialized
* @param {String|[String]} name A string or an arry of string module names
* @param {Function} handler The handler to be called when all the dependencies are satisfied.
* The handler is called with the dependencies as arguments in the order they were specified in
* the first argument
* @return {Promise} only if handler is not specifed which resolves to all the dependencies
* @example
* <code>
* context.dependency(["foo", "bar"], (foo, bar) => {
* // do something with foo and bar
* });
* // OR
* const [foo, bar] = await context.dependency(["foo", "bar"]);
* </code>
*/
dependency(name: string|Array<string>, handler?: function(...*)): Promise<any|Array<any>> | void;
/**
* Register for a context event
* @see #once
* @param {String} event The event name.
* @param {Function} handler The handler to call
* @return {Function} The unsubscribe function
*/
on(event: string, handler: NsEventListener|EventListener): Function;
/**
* Register for a context event to be called only once
* @param {string} event The event name e.g. module:init or module:
* @param handler The event handler
* @return {Function} The unsubscribe function
*/
once(event: string, handler: NsEventListener|EventListener ): Function;
emit(event: string, ...args: any[]): void;
}
export interface AppContextModule {
create(): AppContext;
createNsEmitter(separator?: string): NsEventEmitter;
}