UNPKG

@nimel/directorr-appinitializer

Version:

Simplification of the application initialization process

78 lines (69 loc) 3.64 kB
/* (c) Nikita Melnikov - MIT Licensed 2020 - now */ import { makeObservable, observable } from 'mobx'; import { action, createActionAndEffect, isStoreReady, isStoreError } from '@nimel/directorr'; /****************************************************************************** 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); } const initStoreAction = action('AppInitStore.INIT_STORES'); const [initStoreErrorAction, initStoreErrorEffect] = createActionAndEffect('AppInitStore.INIT_STORES_ERROR'); const [initStoreSuccessAction, initStoreSuccessEffect] = createActionAndEffect('AppInitStore.INIT_STORES_SUCCESS'); const [isReadyAction, isReadyEffect] = createActionAndEffect('AppInitStore.IS_READY'); class AppInitStore { constructor() { this.isInitComplated = false; this.loadStores = (stores) => ({ stores }); this.toSuccess = () => { this.isInitComplated = true; }; makeObservable(this); } } AppInitStore.afterware = ({ type, payload }, dispatchType, directorr) => { if (type === initStoreAction.type) { const { stores } = payload; directorr.addStores(stores); const waitStores = directorr.waitStoresState(stores, isStoreReady); const waitStoreWithError = directorr.findStoreState(isStoreError); void Promise.race([waitStores, waitStoreWithError]).then(store => { if (store) { waitStores.cancel(); dispatchType(initStoreErrorAction.type, { store, stores }); } else { waitStoreWithError.cancel(); dispatchType(initStoreSuccessAction.type, { stores }); } }); } }; __decorate([ 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); export { AppInitStore, initStoreAction, initStoreErrorAction, initStoreErrorEffect, initStoreSuccessAction, initStoreSuccessEffect, isReadyAction, isReadyEffect };