UNPKG

5.54 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9var __importDefault = (this && this.__importDefault) || function (mod) {
10 return (mod && mod.__esModule) ? mod : { "default": mod };
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const di = __importStar(require("./global-injector"));
14const orchestrator_1 = __importDefault(require("orchestrator"));
15const events_1 = require("events");
16const injector_1 = require("./injector");
17process.hrtime = process.hrtime || require('browser-process-hrtime');
18class ExtendableEvent {
19 constructor() {
20 this.promises = [];
21 }
22 waitUntil(p) {
23 this.promises.push(p);
24 }
25 complete() {
26 const done = () => {
27 this._done = true;
28 };
29 return Promise.all(this.promises).then(done, done);
30 }
31 get done() { return this._done; }
32}
33exports.ExtendableEvent = ExtendableEvent;
34class Module extends injector_1.Injector {
35 constructor(name, dep) {
36 super(moduleInjector);
37 this.name = name;
38 this.dep = dep;
39 this.emitter = new events_1.EventEmitter();
40 this.activateEvent = new ExtendableEvent();
41 this.readyEvent = new ExtendableEvent();
42 var existingModule = moduleInjector.resolve(name);
43 if (existingModule && typeof (existingModule.dep) != 'undefined' && existingModule.dep.length && typeof (dep) != 'undefined' && dep.length)
44 throw new Error('the module ' + existingModule.name + ' can be registered only once with dependencies');
45 if (existingModule) {
46 if (typeof (dep) != 'undefined') {
47 delete Module.o.tasks[name + '#activate'];
48 delete Module.o.tasks[name + '#ready'];
49 delete Module.o.tasks[name];
50 existingModule.dep = dep;
51 moduleInjector.unregister(name);
52 Module.registerModule(existingModule);
53 }
54 return existingModule;
55 }
56 Module.registerModule(this);
57 this.emitter.setMaxListeners(0);
58 }
59 static registerModule(m) {
60 var emitter = m.emitter;
61 if (typeof m.dep == 'undefined')
62 m.dep = [];
63 Module.o.add(m.name + '#activate', m.dep.map(dep => dep.name + '#activate'), function () {
64 emitter.emit('activate', m.activateEvent);
65 return m.activateEvent.complete();
66 });
67 Module.o.add(m.name + '#ready', [m.name + '#activate'].concat(m.dep.map(dep => dep.name + '#ready')), function () {
68 emitter.emit('ready', m.readyEvent);
69 return m.readyEvent.complete();
70 });
71 Module.o.add(m.name, [m.name + '#ready'], function () { });
72 moduleInjector.register(m.name, m);
73 }
74 ready(toInject, f) {
75 if (!f)
76 return (f) => this.ready(toInject, f);
77 if (this.readyEvent.done)
78 this.injectWithName(toInject, f)();
79 else
80 this.emitter.on('ready', this.injectWithName(toInject, f));
81 return this;
82 }
83 readyAsync(toInject, f) {
84 if (!f)
85 return (f) => this.readyAsync(toInject, f);
86 if (this.readyEvent.done)
87 return this.injectWithNameAsync(toInject, f.bind(this.readyEvent));
88 else
89 this.emitter.on('ready', (ev) => { this.injectWithNameAsync(toInject, f.bind(ev)); });
90 return this;
91 }
92 activate(toInject, f) {
93 if (!f)
94 return (f) => this.activate(toInject, f);
95 if (this.activateEvent.done)
96 this.injectWithName(toInject, f)();
97 else
98 this.emitter.on('activate', this.injectWithName(toInject, f));
99 return this;
100 }
101 activateAsync(toInject, f) {
102 if (!f)
103 return (f) => this.activateAsync(toInject, f);
104 if (this.readyEvent.done)
105 return this.injectWithNameAsync(toInject, f.bind(this.readyEvent));
106 else
107 this.emitter.on('activate', (ev) => { this.injectWithNameAsync(toInject, f.bind(ev)); });
108 return this;
109 }
110 activateNew(...toInject) {
111 return (ctor) => {
112 return this.activate(toInject, function (...args) {
113 return new ctor(...args);
114 });
115 };
116 }
117 activateNewAsync(...toInject) {
118 return function (ctor) {
119 return this.activateAsync(toInject, function (...args) {
120 return new ctor(...args);
121 });
122 };
123 }
124 readyNew(...toInject) {
125 return (ctor) => {
126 return this.ready(toInject, function (...args) {
127 return new ctor(...args);
128 });
129 };
130 }
131 readyNewAsync(...toInject) {
132 return function (ctor) {
133 return this.readyAsync(toInject, function (...args) {
134 return new ctor(...args);
135 });
136 };
137 }
138 start(toInject, f) {
139 if (arguments.length > 0)
140 Module.o.on('stop', this.injectWithName(toInject, f));
141 else
142 Module.o.start(this.name);
143 }
144}
145exports.Module = Module;
146Module.o = new orchestrator_1.default();
147var moduleInjector = di.resolve('$modules');
148if (!moduleInjector) {
149 moduleInjector = new injector_1.Injector();
150 di.register('$modules', moduleInjector);
151}
152//# sourceMappingURL=module.js.map
\No newline at end of file