@difizen/mana-core
Version:
93 lines (92 loc) • 5.92 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ApplicationStateService = exports.ApplicationState = void 0;
var _manaCommon = require("@difizen/mana-common");
var _manaObservable = require("@difizen/mana-observable");
var _manaSyringe = require("@difizen/mana-syringe");
var _debug = require("../common/debug");
var _dec, _dec2, _class, _class2, _descriptor;
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
var ApplicationState = exports.ApplicationState = /*#__PURE__*/function (ApplicationState) {
ApplicationState["Default"] = "Default";
ApplicationState["Initialized"] = "Initialized";
ApplicationState["Started"] = "Started";
ApplicationState["ViewStarted"] = "ViewStarted";
ApplicationState["Ready"] = "Ready";
ApplicationState["Closing"] = "Closing";
return ApplicationState;
}({});
var ApplicationStateService = exports.ApplicationStateService = (_dec = (0, _manaSyringe.singleton)(), _dec2 = (0, _manaObservable.prop)(), _dec(_class = (_class2 = /*#__PURE__*/function () {
function ApplicationStateService(debugService) {
_classCallCheck(this, ApplicationStateService);
_initializerDefineProperty(this, "state", _descriptor, this);
this.deferred = {};
this.stateChanged = new _manaCommon.Emitter();
this.debugService = debugService;
}
ApplicationStateService = (0, _manaSyringe.inject)(_debug.DebugService)(ApplicationStateService, undefined, 0) || ApplicationStateService;
_createClass(ApplicationStateService, [{
key: "setState",
value: function setState(state) {
if (state !== this.state) {
var oldState = this.state;
var oldDeferred = this.getStateDeferred(oldState);
oldDeferred.resolve();
var deferred = this.getStateDeferred(state);
this.state = state;
deferred.resolve();
this.deferred[state].resolve();
this.debugService("Changed application state from '".concat(oldState, "' to '").concat(this.state, "'."));
}
}
}, {
key: "onStateChanged",
get: function get() {
return this.stateChanged.event;
}
}, {
key: "getStateDeferred",
value: function getStateDeferred(state) {
var deferred = this.deferred[state];
if (deferred === undefined) {
deferred = new _manaCommon.Deferred();
this.deferred[state] = deferred;
}
return deferred;
}
}, {
key: "reachedState",
value: function reachedState(state) {
return this.getStateDeferred(state).promise;
}
}, {
key: "reachedAnyState",
value: function reachedAnyState() {
var _this = this;
for (var _len = arguments.length, states = new Array(_len), _key = 0; _key < _len; _key++) {
states[_key] = arguments[_key];
}
return Promise.race(states.map(function (s) {
return _this.reachedState(s);
}));
}
}]);
return ApplicationStateService;
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "state", [_dec2], {
configurable: true,
enumerable: true,
writable: true,
initializer: function initializer() {
return ApplicationState.Default;
}
})), _class2)) || _class);