typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
61 lines • 1.92 kB
JavaScript
import { ActionStatus } from "./ActionTypes";
import { Bluebird as Promise } from '../util';
/**
* Wraps an action providing tracking events and data
*/
export 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 = ActionStatus.Started;
const InternalActionFactory = require("../internal/InternalActionFactory").InternalActionFactory, internalActions = new InternalActionFactory(store);
this._promise = new Promise((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
Promise
.resolve(result)
.then(resolve)
.catch(reject);
}
catch (err) {
reject(err);
}
}).finally(() => {
// FINALLY NOTIFY INTERNAL STATE
this.status = ActionStatus.Finished;
internalActions.setPendingAction(Object.assign({}, this));
});
}
/**
* Get the underlying promise
*
* @returns {Promise<T>}
*/
get promise() {
return this._promise;
}
}
//# sourceMappingURL=ActionTracker.js.map