declarative-promise
Version:
Behaves kinda like the es6 Promise api, but it doesn't actually *do* anything. It just produces a description of what is to be done, by someone else. At the moment, it's designed to work with the [redux-fetch](https://github.com/ashaffer/redux-fetch) mi
61 lines (48 loc) • 2.16 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = (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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Declarative promise
*/
var DeclarativePromise = (function () {
function DeclarativePromise(action, root) {
_classCallCheck(this, DeclarativePromise);
action.meta = action.meta || {};
action.meta.steps = action.meta.steps || [];
this.action = action;
this.root = root || this;
}
_createClass(DeclarativePromise, [{
key: "step",
value: function step(success, failure) {
var q = new DeclarativePromise({ success: success, failure: failure }, this.root);
this.action.meta.steps.push(q);
return q;
}
}, {
key: "toJSON",
value: function toJSON(_recurse) {
// Always toJSON starting at the root
// of the promise tree
if (!_recurse) {
return this.root.toJSON(true);
}
return _extends({}, this.action, {
meta: _extends({}, this.action.meta, {
steps: this.action.meta.steps.map(function (step) {
return step.toJSON(true);
})
})
});
}
}]);
return DeclarativePromise;
})();
/**
* Exports
*/
exports.default = DeclarativePromise;