UNPKG

@nimel/directorr-appinitializer

Version:

Simplification of the application initialization process

113 lines (101 loc) 5.57 kB
/* (c) Nikita Melnikov - MIT Licensed 2020 - now */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('mobx'), require('@nimel/directorr')) : typeof define === 'function' && define.amd ? define(['exports', 'mobx', '@nimel/directorr'], factory) : (global = global || self, factory(global.index = {}, global.mobx, global.directorr)); }(this, (function (exports, mobx, directorr) { 'use strict'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __decorate(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; } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } var _a, _b, _c; var initStoreAction = directorr.action('AppInitStore.INIT_STORES'); var initStoreErrorAction = (_a = __read(directorr.createActionAndEffect('AppInitStore.INIT_STORES_ERROR'), 2), _a[0]), initStoreErrorEffect = _a[1]; var initStoreSuccessAction = (_b = __read(directorr.createActionAndEffect('AppInitStore.INIT_STORES_SUCCESS'), 2), _b[0]), initStoreSuccessEffect = _b[1]; var isReadyAction = (_c = __read(directorr.createActionAndEffect('AppInitStore.IS_READY'), 2), _c[0]), isReadyEffect = _c[1]; var AppInitStore = /** @class */ (function () { function AppInitStore() { var _this = this; this.isInitComplated = false; this.loadStores = function (stores) { return ({ stores: stores }); }; this.toSuccess = function () { _this.isInitComplated = true; }; mobx.makeObservable(this); } AppInitStore.afterware = function (_a, dispatchType, directorr$1) { var type = _a.type, payload = _a.payload; if (type === initStoreAction.type) { var stores_1 = payload.stores; directorr$1.addStores(stores_1); var waitStores_1 = directorr$1.waitStoresState(stores_1, directorr.isStoreReady); var waitStoreWithError_1 = directorr$1.findStoreState(directorr.isStoreError); void Promise.race([waitStores_1, waitStoreWithError_1]).then(function (store) { if (store) { waitStores_1.cancel(); dispatchType(initStoreErrorAction.type, { store: store, stores: stores_1 }); } else { waitStoreWithError_1.cancel(); dispatchType(initStoreSuccessAction.type, { stores: stores_1 }); } }); } }; __decorate([ mobx.observable, __metadata("design:type", Object) ], AppInitStore.prototype, "isInitComplated", void 0); __decorate([ initStoreAction, __metadata("design:type", Object) ], AppInitStore.prototype, "loadStores", void 0); __decorate([ isReadyEffect, __metadata("design:type", Object) ], AppInitStore.prototype, "toSuccess", void 0); return AppInitStore; }()); exports.AppInitStore = AppInitStore; exports.initStoreAction = initStoreAction; exports.initStoreErrorAction = initStoreErrorAction; exports.initStoreErrorEffect = initStoreErrorEffect; exports.initStoreSuccessAction = initStoreSuccessAction; exports.initStoreSuccessEffect = initStoreSuccessEffect; exports.isReadyAction = isReadyAction; exports.isReadyEffect = isReadyEffect; Object.defineProperty(exports, '__esModule', { value: true }); })));