UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

65 lines 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionTracker = void 0; const ActionTypes_1 = require("./ActionTypes"); const util_1 = require("../util"); /** * Wraps an action providing tracking events and data */ class ActionTracker { /** * Create new action tracker * * @param leaf * @param name * @param action * @param id * @param store */ constructor(id, leaf, name, action, store) { this.id = id; this.leaf = leaf; this.name = name; this.action = action; this.store = store; /** * Action Status * * @type {ActionStatus} */ this.status = ActionTypes_1.ActionStatus.Started; const InternalActionFactory = require("../internal/InternalActionFactory").InternalActionFactory, internalActions = new InternalActionFactory(store); this._promise = new util_1.Bluebird((resolve, reject) => { Object.assign(this, { reject, resolve }); try { internalActions.setPendingAction(this); const dispatch = (action) => store === null || store === void 0 ? void 0 : store.dispatch(action), getState = () => store === null || store === void 0 ? void 0 : store.getState(), result = action(dispatch, getState); // UNWRAP PROMISE util_1.Bluebird .resolve(result) .then(resolve) .catch(reject); } catch (err) { reject(err); } }).finally(() => { // FINALLY NOTIFY INTERNAL STATE this.status = ActionTypes_1.ActionStatus.Finished; internalActions.setPendingAction(Object.assign({}, this)); }); } /** * Get the underlying promise * * @returns {Promise<T>} */ get promise() { return this._promise; } } exports.ActionTracker = ActionTracker; //# sourceMappingURL=ActionTracker.js.map