UNPKG

@malagu/core

Version:
108 lines 4.23 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractApplicationStateService = exports.AbstractApplication = exports.ApplicationProps = exports.ApplicationStateService = exports.Application = exports.ApplicationLifecycle = void 0; const promise_util_1 = require("../utils/promise-util"); const emitter_1 = require("../utils/emitter"); const logger_protocol_1 = require("../logger/logger-protocol"); const annotation_1 = require("../annotation"); exports.ApplicationLifecycle = Symbol('ApplicationLifecycle'); exports.Application = Symbol('Application'); exports.ApplicationStateService = Symbol('ApplicationStateService'); exports.ApplicationProps = Symbol('ApplicationProps'); class AbstractApplication { /** * Initialize and start the frontend application. */ async doStart() { for (const lifecycle of this.lifecycles) { if (lifecycle.initialize) { try { lifecycle.initialize(); } catch (error) { this.logger.error('Could not initialize lifecycle', error); } } } // TODO for (const lifecycle of this.lifecycles) { if (lifecycle.onStart) { try { await lifecycle.onStart(this); } catch (error) { this.logger.error('Could not start lifecycle', error); } } } } /** * Stop the frontend application lifecycle. */ doStop() { for (const lifecycle of this.lifecycles) { if (lifecycle.onStop) { try { lifecycle.onStop(this); } catch (error) { this.logger.error('Could not stop lifecycle', error); } } } } } __decorate([ (0, annotation_1.Autowired)(exports.ApplicationLifecycle), (0, annotation_1.Optional)(), __metadata("design:type", Array) ], AbstractApplication.prototype, "lifecycles", void 0); __decorate([ (0, annotation_1.Autowired)(logger_protocol_1.Logger), __metadata("design:type", Object) ], AbstractApplication.prototype, "logger", void 0); exports.AbstractApplication = AbstractApplication; class AbstractApplicationStateService { constructor() { this._state = 'init'; this.deferred = {}; this.stateChanged = new emitter_1.Emitter(); } get state() { return this._state; } set state(state) { if (state !== this._state) { this.deferred[this._state] = new promise_util_1.Deferred(); this._state = state; if (this.deferred[state] === undefined) { this.deferred[state] = new promise_util_1.Deferred(); } this.deferred[state].resolve(); this.stateChanged.fire(state); } } get onStateChanged() { return this.stateChanged.event; } reachedState(state) { if (this.deferred[state] === undefined) { this.deferred[state] = new promise_util_1.Deferred(); } return this.deferred[state].promise; } reachedAnyState(...states) { return Promise.race(states.map(s => this.reachedState(s))); } } exports.AbstractApplicationStateService = AbstractApplicationStateService; //# sourceMappingURL=application-protocol.js.map