UNPKG

@akala/core

Version:
145 lines 5.23 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const di = __importStar(require("./global-injector")); const orchestrator_1 = __importDefault(require("orchestrator")); const events_1 = require("events"); const injector_1 = require("./injector"); process.hrtime = process.hrtime || require('browser-process-hrtime'); class ExtendableEvent { constructor() { this.promises = []; } waitUntil(p) { this.promises.push(p); } complete() { return Promise.all(this.promises).finally(() => this._done = true); } get done() { return this._done; } } exports.ExtendableEvent = ExtendableEvent; class Module extends injector_1.Injector { constructor(name, dep) { super(moduleInjector); this.name = name; this.dep = dep; this.emitter = new events_1.EventEmitter(); this.activateEvent = new ExtendableEvent(); this.readyEvent = new ExtendableEvent(); var existingModule = moduleInjector.resolve(name); if (existingModule && typeof (dep) != 'undefined') throw new Error('the module can be registered only once with dependencies'); if (existingModule) { if (typeof (dep) != 'undefined') { delete Module.o.tasks[name + '#activate']; delete Module.o.tasks[name + '#ready']; delete Module.o.tasks[name]; existingModule.dep = dep; moduleInjector.unregister(name); Module.registerModule(existingModule); } return existingModule; } Module.registerModule(this); this.emitter.setMaxListeners(0); } static registerModule(m) { var emitter = m.emitter; if (typeof m.dep == 'undefined') m.dep = []; Module.o.add(m.name + '#activate', m.dep.map(dep => dep + '#activate'), function (done) { emitter.emit('activate', m.activateEvent); m.activateEvent.complete().then(() => { done(); }, done); }); Module.o.add(m.name + '#ready', [m.name + '#activate'].concat(m.dep.map(dep => dep + '#ready')), function (done) { emitter.emit('ready', m.readyEvent); m.readyEvent.complete().then(() => { done(); }, done); }); Module.o.add(m.name, [m.name + '#ready'], function () { }); moduleInjector.register(m.name, m); } ready(toInject, f) { if (!f) return (f) => this.ready(toInject, f); if (this.readyEvent.done) this.injectWithName(toInject, f)(); else this.emitter.on('ready', this.injectWithName(toInject, f)); return this; } readyAsync(toInject, f) { if (!f) return (f) => this.readyAsync(toInject, f); if (this.readyEvent.done) return this.injectWithNameAsync(toInject, f.bind(this.readyEvent)); else this.emitter.on('ready', (ev) => { this.injectWithNameAsync(toInject, f.bind(ev)); }); return this; } activate(toInject, f) { if (!f) return (f) => this.activate(toInject, f); if (this.activateEvent.done) this.injectWithName(toInject, f)(); else this.emitter.on('activate', this.injectWithName(toInject, f)); return this; } activateAsync(toInject, f) { if (!f) return (f) => this.activateAsync(toInject, f); if (this.readyEvent.done) return this.injectWithNameAsync(toInject, f.bind(this.readyEvent)); else this.emitter.on('activate', (ev) => { this.injectWithNameAsync(toInject, f.bind(ev)); }); return this; } activateNew(...toInject) { return (ctor) => { return this.activate(toInject, ctor.bind(ctor)); }; } activateNewAsync(...toInject) { return function (ctor) { return this.activateAsync(toInject, ctor.bind(ctor)); }; } readyNew(...toInject) { return (ctor) => { return this.ready(toInject, ctor.bind(ctor)); }; } readyNewAsync(...toInject) { return function (ctor) { return this.readyAsync(toInject, ctor.bind(ctor)); }; } start(toInject, f) { if (arguments.length > 0) Module.o.on('stop', this.injectWithName(toInject, f)); else Module.o.start(this.name); } } exports.Module = Module; Module.o = new orchestrator_1.default(); var moduleInjector = di.resolve('$modules'); if (!moduleInjector) { moduleInjector = new injector_1.Injector(); di.register('$modules', moduleInjector); } //# sourceMappingURL=module.js.map