microcosm
Advanced tools
Comparing version 12.7.0-alpha.2 to 12.7.0-alpha.3
@@ -14,3 +14,50 @@ # Changelog | ||
actions that return promises. | ||
- Action status changing methods are auto-bound, and will warn when a | ||
completed action attempts to move into a new state (strict mode only) | ||
### Auto-bound action status methods | ||
Action status methods like `action.resolve()` and `action.reject()` | ||
are auto-bound. They can be passed directly into a callback without | ||
needing to wrap them in an anonymous function. | ||
This is particularly useful when working with AJAX libraries. For | ||
example, when working with `superagent`: | ||
Instead of: | ||
```javascript | ||
import superagent from 'superagent' | ||
function getPlanets () { | ||
return action => { | ||
let request = superagent.get('/planets') | ||
request.on('request', (data) => action.open(data)) | ||
request.on('progress', (data) => action.update(data)) | ||
request.on('abort', (data) => action.cancel(data)) | ||
request.then((data) => action.resolve(data), (error) => action.reject(error)) | ||
} | ||
} | ||
``` | ||
You can do: | ||
```javascript | ||
import superagent from 'superagent' | ||
function getPlanets () { | ||
return action => { | ||
let request = superagent.get('/planets') | ||
request.on('request', action.open) | ||
request.on('progress', action.update) | ||
request.on('abort', action.cancel) | ||
request.then(action.resolve, action.reject) | ||
} | ||
} | ||
``` | ||
## 12.6.1 | ||
@@ -17,0 +64,0 @@ |
# Actions | ||
1. [Overview](#overview) | ||
2. [Writing action creators](#writing-action-creators) | ||
2. [Writing Action Creators](#writing-action-creators) | ||
3. [Dispatching to Domains](#dispatching-to-domains) | ||
@@ -162,2 +162,27 @@ 4. [How this works](#how-this-works) | ||
### Action status methods are auto-bound | ||
Action status methods like `action.resolve()` and `action.reject()` | ||
are auto-bound. They can be passed directly into a callback without | ||
needing to wrap them in an anonymous function. | ||
This is particularly useful when working with AJAX libraries. For | ||
example, when working with `superagent`: | ||
```javascript | ||
import superagent from 'superagent' | ||
function getPlanets () { | ||
return action => { | ||
let request = superagent.get('/planets') | ||
request.on('request', action.open) | ||
request.on('progress', action.update) | ||
request.on('abort', action.cancel) | ||
request.then(action.resolve, action.reject) | ||
} | ||
} | ||
``` | ||
## Dispatching to Domains | ||
@@ -179,7 +204,9 @@ | ||
return { | ||
[getPlanet.open] : this.setLoading, | ||
[getPlanet.loading] : this.setProgress, | ||
[getPlanet.done] : this.addPlanet, | ||
[getPlanet.error] : this.setError, | ||
[getPlanet.cancelled] : this.setCancelled | ||
[getPlanet]: { | ||
open : this.setLoading, | ||
update : this.setProgress, | ||
done : this.addPlanet, | ||
error : this.setError, | ||
cancel : this.setCancelled | ||
} | ||
} | ||
@@ -186,0 +213,0 @@ } |
203
microcosm.js
@@ -530,63 +530,2 @@ 'use strict'; | ||
/** | ||
* Open state. The action has started, but has received no response. | ||
* @param {*} [nextPayload] | ||
*/ | ||
open: function open(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('open', false); | ||
return this; | ||
}, | ||
/** | ||
* Update state. The action has received an update, such as loading progress. | ||
* @param {*} [nextPayload] | ||
*/ | ||
update: function update$$1(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('update', false); | ||
return this; | ||
}, | ||
/** | ||
* Resolved state. The action has completed successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
resolve: function resolve(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('resolve', true); | ||
return this; | ||
}, | ||
/** | ||
* Failure state. The action did not complete successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
reject: function reject(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('reject', true); | ||
return this; | ||
}, | ||
/** | ||
* Cancelled state. The action was halted, like aborting an HTTP | ||
* request. | ||
* @param {*} [nextPayload] | ||
*/ | ||
cancel: function cancel(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('cancel', true); | ||
return this; | ||
}, | ||
/** | ||
* Subscribe to when an action opens. | ||
@@ -768,30 +707,2 @@ * @param {Function} callback | ||
/** | ||
* Reset the payload | ||
* @private | ||
*/ | ||
_setPayload: function _setPayload(payload) { | ||
if (arguments.length) { | ||
this.payload = payload; | ||
} | ||
}, | ||
/** | ||
* Change the status of the action | ||
* @private | ||
* @param {string} status The next action status | ||
* @param {boolean} complete Should the action be complete? | ||
*/ | ||
_setStatus: function _setStatus(status, complete) { | ||
if (!this.complete) { | ||
this.status = status; | ||
this.complete = complete; | ||
this._emit('change', this); | ||
this._emit(status, this.payload); | ||
} | ||
}, | ||
/** | ||
* If an action is already a given status, invoke the | ||
@@ -816,6 +727,108 @@ * provided callback. Otherwise setup a one-time listener | ||
Object.defineProperty(Action.prototype, 'type', { | ||
get: function get$$1() { | ||
return this.command[this.status]; | ||
/** | ||
* Complete actions can never change. This is a courtesy warning | ||
* @param {Action} action | ||
* @param {string} method | ||
* @return A function that will warn developers that they can not | ||
* update this action | ||
*/ | ||
function createCompleteWarning(action, method) { | ||
return function () { | ||
}; | ||
} | ||
/** | ||
* Used to autobind action resolution methods. | ||
* @param {Action} action | ||
* @param {string} status | ||
* @param {boolean} complete | ||
* @return A function that will update the provided action with a new state | ||
*/ | ||
function createActionUpdater(action, status, complete) { | ||
return function (payload) { | ||
action.status = status; | ||
action.complete = complete; | ||
// Check arguments, we want to allow payloads that are undefined | ||
if (arguments.length > 0) { | ||
action.payload = payload; | ||
} | ||
action._emit('change', action); | ||
action._emit(status, action.payload); | ||
return action; | ||
}; | ||
} | ||
/** | ||
* If the action is complete, return a warning that the action can no longer be updated. | ||
* Otherwise return an autobound updater function. | ||
* @param {Action} action | ||
* @param {string} status | ||
* @param {boolean} complete | ||
*/ | ||
function warnOrUpdate(action, status, complete) { | ||
return action.complete ? createCompleteWarning(action, status) : createActionUpdater(action, status, complete); | ||
} | ||
Object.defineProperties(Action.prototype, { | ||
type: { | ||
get: function get$$1() { | ||
return this.command[this.status]; | ||
} | ||
}, | ||
/** | ||
* Open state. The action has started, but has received no response. | ||
* @param {*} [nextPayload] | ||
*/ | ||
open: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'open', false); | ||
} | ||
}, | ||
/** | ||
* Update state. The action has received an update, such as loading progress. | ||
* @param {*} [nextPayload] | ||
*/ | ||
update: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'update', false); | ||
} | ||
}, | ||
/** | ||
* Resolved state. The action has completed successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
resolve: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'resolve', true); | ||
} | ||
}, | ||
/** | ||
* Failure state. The action did not complete successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
reject: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'reject', true); | ||
} | ||
}, | ||
/** | ||
* Cancelled state. The action was halted, like aborting an HTTP | ||
* request. | ||
* @param {*} [nextPayload] | ||
*/ | ||
cancel: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'cancel', true); | ||
} | ||
} | ||
}); | ||
@@ -852,5 +865,7 @@ | ||
var BIRTH = function $birth() {}; | ||
var BIRTH = function $birth() { | ||
}; | ||
var START = function $start() {}; | ||
var START = function $start() { | ||
}; | ||
@@ -857,0 +872,0 @@ var ADD_DOMAIN = function $addDomain(domain) { |
@@ -1,1 +0,1 @@ | ||
"use strict";function t(t){return""===t||null===t||void 0===t}function n(n){return Array.isArray(n)?n:t(n)?[]:d(n)?n.split(B):[n]}function e(t){var e=t;return Array.isArray(t)===!1&&(e=(""+e).split(T)),e.map(n)}function i(t){return t.join(B)}function r(t){return t.map(i).join(T)}function s(t){if(Array.isArray(t))return t.slice(0);if(l(t)===!1)return t;var n={};for(var e in t)n[e]=t[e];return n}function o(){for(var t=null,n=null,e=0,i=arguments.length;e<i;e++){t=t||arguments[e],n=n||t;var r=arguments[e];for(var o in r)t[o]!==r[o]&&(t===n&&(t=s(n)),t[o]=r[o])}return t}function a(t,n,e){return t.__proto__=n,t.prototype=o(Object.create(n.prototype),{constructor:t.prototype.constructor},e),t}function h(t,e,i){if(null==t)return i;for(var r=n(e),s=0,o=r.length;s<o;s++){var a=null==t?void 0:t[r[s]];if(void 0===a)return i;t=a}return t}function c(t,e,i){for(var r=n(e),s=0,o=r.length;s<o;s++){var e=r[s];if(!t||L.call(t,e)===!1)return!1;t=t[e]}return!0}function u(t,e,i){var r=n(e),o=r.length;if(o<=0)return i;if(h(t,r)===i)return t;for(var a=s(t),c=a,u=0;u<o;u++){var f=r[u],l=i;u<o-1&&(l=f in c?s(c[f]):{}),c[f]=l,c=c[f]}return a}function f(t){return(l(t)||p(t))&&p(t.then)}function l(t){return!!t&&"object"===(void 0===t?"undefined":C(t))}function p(t){return!!t&&"function"==typeof t}function d(t){return"string"==typeof t}function v(t){return"GeneratorFunction"===h(t,J,"")}function g(t,n,e){return p(t)?new t(n,e):Object.create(t)}function y(t,e,i,r){var s=n(e);return p(i)===!1?u(t,s,i):u(t,s,i(h(t,s,r)))}function m(t,n,e,i){this.event=t,this.fn=n,this.scope=e,this.once=i}function w(){this.a=[]}function x(t,n){if(t.b===!0)return t;d(t)&&(n=t,t=function(t){return t}),Q+=1;var e=n||(t.name||$)+"."+Q;return t.open=e+".open",t.loading=e+".loading",t.update=t.loading,t.done=e,t.resolve=t.done,t.error=e+".error",t.reject=t.error,t.cancel=e+".cancel",t.cancelled=t.cancel,t.toString=function(){return e},t.b=!0,t}function b(t,n){w.call(this),this.id=q++,this.command=x(t),this.timestamp=Date.now(),this.children=[],n&&this[n]()}function z(t,n){return function(e,i){var r=t;if(n)try{r=i.deserialize(t)}catch(t){throw e.reject(t),t}var s=i.domains.sanitize(r);e.resolve(s)}}function S(t){w.call(this),this.size=0,this.limit=Math.max(1,t||1),this.begin()}function A(){this.pool={}}function j(t,n){this.repo=n}function k(t,n,e){var i=V[e],r=t[n],s=n[e];return l(r)?r[i]||r[e]:t[s]}function O(t){this.repo=t,this.domains=[],this.registry={},this.add([],j)}function D(t){this.repo=t,this.effects=[]}function E(t,n,e){this.id=t,this.key=n,this.edges=[],e&&(this.parent=e,e.connect(this))}function P(t,n){w.call(this),this.id=t,this.keyPaths=e(n)}function I(t){this.snapshot=t,this.nodes={}}function _(t,n,e){function i(n){var e=s.next(n);e.done?t.resolve(n):r(e.value)}function r(n){n.onDone(i),n.onCancel(t.cancel,t),n.onError(t.reject,t)}t.open();var s=n(e);return i(),t}function H(t,n,e,i){var r=n.apply(null,e);return f(r)?(t.open.apply(t,e),r.then(function(n){return global.setTimeout(function(){return t.resolve(n)},0)},function(n){return global.setTimeout(function(){return t.reject(n)},0)}),t):v(r)?_(t,r,i):p(r)?(r(t,i),t):t.resolve(r)}function N(t,n,e){w.call(this);var i=o(N.defaults,this.constructor.defaults,t);this.parent=i.parent,this.initial=this.parent?this.parent.initial:{},this.state=this.parent?this.parent.state:this.initial,this.history=this.parent?this.parent.history:new S(i.maxHistory),this.archive=new A,this.domains=new O(this),this.effects=new D(this),this.changes=new I(this.state),this.history.on("append",this.createSnapshot,this),this.history.on("update",this.updateSnapshot,this),this.history.on("remove",this.removeSnapshot,this),this.history.on("reconcile",this.dispatchEffect,this),this.history.on("release",this.release,this),this.setup(i),n&&this.reset(n,e)}Object.defineProperty(exports,"__esModule",{value:!0});var B=".",T=",",C="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},L=Object.prototype.hasOwnProperty,M="function"==typeof Symbol?Symbol:{},J=M.toStringTag||"@@toStringTag";w.prototype={on:function(t,n,e){var i=new m(t,n,e,!1);return this.a.push(i),this},once:function(t,n,e){var i=new m(t,n,e,!0);return this.a.push(i),this},off:function(t,n,e){for(var i=null==n,r=0;r<this.a.length;){var s=this.a[r];s.event===t&&(i||s.fn===n&&s.scope===e)?this.a.splice(r,1):r+=1}return this},removeAllListeners:function(){this.a.length=0},c:function(t){for(var n=0,e=arguments.length,i=Array(e>1?e-1:0),r=1;r<e;r++)i[r-1]=arguments[r];for(;n<this.a.length;){var s=this.a[n];s.event===t&&(s.fn.apply(s.scope||this,i),s.once)?this.a.splice(n,1):n+=1}return this},d:function(t){for(var n=0;n<this.a.length;)t!==this.a[n].scope?n+=1:this.a.splice(n,1)}};var Q=0,$="_action",q=0;a(b,w,{status:"inactive",payload:void 0,disabled:!1,complete:!1,parent:null,next:null,open:function(t){return this.e.apply(this,arguments),this.f("open",!1),this},update:function(t){return this.e.apply(this,arguments),this.f("update",!1),this},resolve:function(t){return this.e.apply(this,arguments),this.f("resolve",!0),this},reject:function(t){return this.e.apply(this,arguments),this.f("reject",!0),this},cancel:function(t){return this.e.apply(this,arguments),this.f("cancel",!0),this},onOpen:function(t,n){return this.g("open",t,n),this},onUpdate:function(t,n){return t&&this.on("update",t,n),this},onDone:function(t,n){return this.g("resolve",t,n),this},onError:function(t,n){return this.g("reject",t,n),this},onCancel:function(t,n){return this.g("cancel",t,n),this},is:function(t){return this.command[this.status]===this.command[t]},toggle:function(t){return this.disabled=!this.disabled,t||this.c("change",this),this},then:function(t,n){var e=this;return new Promise(function(t,n){e.onDone(t),e.onError(n)}).then(t,n)},isDisconnected:function(){return!this.parent},prune:function(){this.parent.parent=null},lead:function(t){this.next=t,t&&this.adopt(t)},adopt:function(t){this.children.indexOf(t)<0&&this.children.push(t),t.parent=this},remove:function(){this.parent.abandon(this),this.removeAllListeners()},abandon:function(t){var n=this.children.indexOf(t);n>=0&&(this.children.splice(n,1),t.parent=null),this.next===t&&this.lead(t.next)},e:function(t){arguments.length&&(this.payload=t)},f:function(t,n){this.complete||(this.status=t,this.complete=n,this.c("change",this),this.c(t,this.payload))},g:function(t,n,e){n&&(this.is(t)?n.call(e,this.payload):this.once(t,n,e))}}),Object.defineProperty(b.prototype,"type",{get:function(){return this.command[this.status]}});var F=x(function(t,n){return z(t,n)},"$reset"),G=x(function(t,n){return z(t,n)},"$patch"),R=function(){},U=function(){},K=function(t){return t};a(S,w,{checkout:function(t){return this.head=t||this.head,this.setActiveBranch(),this.reconcile(this.head),this},toggle:function(t){var n=[].concat(t);n.forEach(function(t){return t.toggle("silently")}),this.reconcile(n[0])},toArray:function(){return this.map(function(t){return t})},map:function(t,n){for(var e=this.size,i=Array(e),r=this.head;e--;)i[e]=t.call(n,r),r=r.parent;return i},wait:function(){var t=this.toArray(),n=t.length;return new Promise(function(e,i){function r(){n-=1,n<=0&&e()}t.map(function(t){t.onDone(r),t.onCancel(r),t.onError(i)})})},then:function(t,n){return this.wait().then(t,n)},begin:function(){this.head=this.root=null,this.append(U,"resolve")},append:function(t,n){var e=new b(t,n);return this.size>0?this.head.lead(e):(new b(R,"resolve").adopt(e),this.root=e),this.head=e,this.size+=1,this.c("append",e),e.on("change",this.reconcile,this),this.head},remove:function(t){if(!t.isDisconnected()){var n=t.next,e=t.parent;if(this.clean(t),this.size<=0)return void this.begin();n?t===this.root&&(this.root=n):n=this.head=e,t.disabled||this.reconcile(n)}},clean:function(t){this.size-=1,this.c("remove",t),t.remove()},reconcile:function(t){for(var n=t;n&&(this.c("update",n),n!==this.head);)n=n.next;this.archive(),this.c("reconcile",t),this.c("release")},archive:function(){for(var t=this.size,n=this.root;t>this.limit&&n.complete;)t-=1,this.c("remove",n.parent),n=n.next;n.prune(),this.root=n,this.size=t},setActiveBranch:function(){for(var t=this.head,n=1;t!==this.root;){var e=t.parent;e.next=t,t=e,n+=1}this.size=n}}),A.prototype={create:function(t){this.set(t,this.get(t.parent))},get:function(t,n){var e=this.pool[t.id];return void 0===e?n:e},set:function(t,n){this.pool[t.id]=n},remove:function(t){delete this.pool[t.id]}},j.prototype={reset:function(t,n){var e=this.repo.domains.sanitize(n);return o(t,this.repo.getInitialState(),e)},patch:function(t,n){return o(t,this.repo.domains.sanitize(n))},addDomain:function(t){return o(this.repo.getInitialState(),t)},register:function(){var t;return t={},t[F]=this.reset,t[G]=this.patch,t[K]=this.addDomain,t}};var V={inactive:"inactive",open:"open",update:"loading",loading:"update",done:"resolve",resolve:"done",reject:"error",error:"reject",cancel:"cancelled",cancelled:"cancel"};O.prototype={getHandlers:function(t){for(var n=t.command,e=t.status,i=[],r=0,s=this.domains.length;r<s;r++){var o=this.domains[r],a=o[0],h=o[1];if(h.register){var c=k(h.register(),n,e);c&&i.push({key:a,domain:h,handler:c})}}return i},register:function(t){var n=t.type;return this.registry[n]||(this.registry[n]=this.getHandlers(t)),this.registry[n]},add:function(t,e,i){var r=g(e,i,this.repo);return this.domains.push([n(t),r]),this.registry={},r.setup&&r.setup(this.repo,i),r},reduce:function(t,n,e){for(var i=n,r=1,s=this.domains.length;r<s;r++){var o=this.domains[r],a=o[0],h=o[1];i=t.call(e,i,a,h)}return i},sanitize:function(t){for(var n={},e=0,i=this.domains.length;e<i;e++){var r=this.domains[e],s=r[0];s.length&&c(t,s)&&(n=u(n,s,h(t,s)))}return n},dispatch:function(t,n){for(var e=this.register(n),i=0,r=e.length;i<r;i++){var s=e[i],o=s.key,a=s.domain,c=s.handler,f=h(t,o);t=u(t,o,c.call(a,f,n.payload))}return t},deserialize:function(t){return this.reduce(function(n,e,i){return i.deserialize?u(n,e,i.deserialize(h(t,e))):n},t)},serialize:function(t,n){return this.reduce(function(n,e,i){return i.serialize?u(n,e,i.serialize(h(t,e))):n},n)},teardown:function(){for(var t=0,n=this.domains.length;t<n;t++){var e=this.domains[t],i=(e[0],e[1]);i.teardown&&i.teardown(this.repo)}}},D.prototype={add:function(t,n){var e=g(t,n,this.repo);return e.setup&&e.setup(this.repo,n),this.effects.push(e),e},teardown:function(){for(var t=0,n=this.effects.length;t<n;t++){var e=this.effects[t];e.teardown&&e.teardown(this.repo)}},dispatch:function(t){for(var n=t.command,e=t.payload,i=t.status,r=0,s=this.effects.length;r<s;r++){var o=this.effects[r];if(o.register){var a=k(o.register(),n,i);a&&a.call(o,this.repo,e)}}}},E.prototype={parent:null,connect:function(t){t!==this&&this.edges.indexOf(t)<0&&this.edges.push(t)},disconnect:function(t){var n=this.edges.indexOf(t);~n&&this.edges.splice(n,1)},isAlone:function(){return this.edges.length<=0},orphan:function(){this.parent&&this.parent.disconnect(this)}},P.getId=function(t){return"query:"+r(e(t))},a(P,w,{extract:function(t){for(var n=this.keyPaths.length,e=Array(n),i=0;i<n;i++)e[i]=h(t,this.keyPaths[i]);return e},trigger:function(t){var n=this.extract(t);this.c.apply(this,["change"].concat(n))},isEmpty:function(){return this.a.length<=0}});var W="";I.prototype={on:function(t,n,i){for(var r=e(t),s=P.getId(t),o=this.addQuery(s,r),a=0;a<r.length;a++)this.addBranch(r[a],o);return o.on("change",n,i),o},off:function(t,n,e){var i=P.getId(t),r=this.nodes[i];r&&(r.off("change",n,e),r.isEmpty()&&this.prune(r))},update:function(t){var n=this.snapshot;this.snapshot=t;var e=this.nodes[W];e&&this.scan(e,n,t)},addNode:function(t,n,e){return this.nodes[t]||(this.nodes[t]=new E(t,n,e)),this.nodes[t]},addQuery:function(t,n){return this.nodes[t]||(this.nodes[t]=new P(t,n)),this.nodes[t]},remove:function(t){delete this.nodes[t.id]},prune:function(t){for(var n=t.keyPaths.map(i),e=0,r=n.length;e<r;e++){var s=this.nodes[n[e]];s.disconnect(t);do{if(!s.isAlone())break;s.orphan(),this.remove(s),s=s.parent}while(s)}this.remove(t)},addBranch:function(t,n){for(var e=this.addNode(W,"",null),i="",r=0,s=t.length;r<s;r++)i=i?i+"."+t[r]:t[r],e=this.addNode(i,t[r],e);e.connect(n)},scan:function(t,n,e){for(var i=[{node:t,last:n,next:e}],r=[];i.length;){var s=i.pop(),o=s.node,a=s.last,h=s.next;if(a!==h)for(var c=o.edges,u=0,f=c.length;u<f;u++){var l=c[u];l instanceof P&&r.indexOf(l)<0?(l.trigger(this.snapshot),r.push(l)):i.push({node:l,last:null==a?a:a[l.key],next:null==h?h:h[l.key]})}}}},N.defaults={maxHistory:0,parent:null},a(N,w,{setup:function(){},teardown:function(){},getInitialState:function(){return this.initial},recall:function(t,n){return this.archive.get(t,n)},createSnapshot:function(t){this.archive.create(t)},updateSnapshot:function(t){var n=this.recall(t.parent,this.initial);this.parent&&(n=o(n,this.parent.recall(t))),t.disabled||(n=this.domains.dispatch(n,t)),this.archive.set(t,n),this.state=n},removeSnapshot:function(t){this.archive.remove(t)},dispatchEffect:function(t){this.effects.dispatch(t)},release:function(){this.changes.update(this.state)},on:function(t,n,e){var i=t.split(":",2),r=i[0],s=i[1];switch(r){case"change":this.changes.on(s||"",n,e);break;default:w.prototype.on.apply(this,arguments)}return this},off:function(t,n,e){var i=t.split(":",2),r=i[0],s=i[1];switch(r){case"change":this.changes.off(s||"",n,e);break;default:w.prototype.off.apply(this,arguments)}return this},append:function(t,n){return this.history.append(t,n)},push:function(t){for(var n=this.append(t),e=arguments.length,i=Array(e>1?e-1:0),r=1;r<e;r++)i[r-1]=arguments[r];return H(n,n.command,i,this),n},prepare:function(){for(var t=this,n=arguments.length,e=Array(n),i=0;i<n;i++)e[i]=arguments[i];return function(){for(var n=arguments.length,i=Array(n),r=0;r<n;r++)i[r]=arguments[r];return t.push.apply(t,e.concat(i))}},addDomain:function(t,n,e){var i=this.domains.add(t,n,e);return i.getInitialState&&(this.initial=u(this.initial,t,i.getInitialState())),this.push(K,i),i},addEffect:function(t,n){return this.effects.add(t,n)},reset:function(t,n){return this.push(F,t,n)},patch:function(t,n){return this.push(G,t,n)},deserialize:function(t){var n=t;return this.parent?n=this.parent.deserialize(t):d(n)&&(n=JSON.parse(n)),this.domains.deserialize(n)},serialize:function(){var t=this.parent?this.parent.serialize():{};return this.domains.serialize(this.state,t)},toJSON:function(){return this.serialize()},checkout:function(t){return this.history.checkout(t),this},fork:function(){return new N({parent:this})},shutdown:function(){this.teardown(),this.effects.teardown(),this.domains.teardown(),this.c("teardown",this),this.history.d(this),this.removeAllListeners()}}),exports.default=N,exports.Microcosm=N,exports.Action=b,exports.History=S,exports.tag=x,exports.get=h,exports.set=u,exports.update=y,exports.merge=o,exports.inherit=a,exports.getRegistration=k; | ||
"use strict";function t(t){return""===t||null===t||void 0===t}function n(n){return Array.isArray(n)?n:t(n)?[]:d(n)?n.split(L):[n]}function e(t){var e=t;return Array.isArray(t)===!1&&(e=(""+e).split(M)),e.map(n)}function i(t){return t.join(L)}function r(t){return t.map(i).join(M)}function s(t){if(Array.isArray(t))return t.slice(0);if(l(t)===!1)return t;var n={};for(var e in t)n[e]=t[e];return n}function o(){for(var t=null,n=null,e=0,i=arguments.length;e<i;e++){t=t||arguments[e],n=n||t;var r=arguments[e];for(var o in r)t[o]!==r[o]&&(t===n&&(t=s(n)),t[o]=r[o])}return t}function a(t,n,e){return t.__proto__=n,t.prototype=o(Object.create(n.prototype),{constructor:t.prototype.constructor},e),t}function h(t,e,i){if(null==t)return i;for(var r=n(e),s=0,o=r.length;s<o;s++){var a=null==t?void 0:t[r[s]];if(void 0===a)return i;t=a}return t}function c(t,e,i){for(var r=n(e),s=0,o=r.length;s<o;s++){var e=r[s];if(!t||Q.call(t,e)===!1)return!1;t=t[e]}return!0}function u(t,e,i){var r=n(e),o=r.length;if(o<=0)return i;if(h(t,r)===i)return t;for(var a=s(t),c=a,u=0;u<o;u++){var f=r[u],l=i;u<o-1&&(l=f in c?s(c[f]):{}),c[f]=l,c=c[f]}return a}function f(t){return(l(t)||p(t))&&p(t.then)}function l(t){return!!t&&"object"===(void 0===t?"undefined":J(t))}function p(t){return!!t&&"function"==typeof t}function d(t){return"string"==typeof t}function v(t){return"GeneratorFunction"===h(t,q,"")}function g(t,n,e){return p(t)?new t(n,e):Object.create(t)}function y(t,e,i,r){var s=n(e);return p(i)===!1?u(t,s,i):u(t,s,i(h(t,s,r)))}function m(t,n,e,i){this.event=t,this.fn=n,this.scope=e,this.once=i}function w(){this.a=[]}function x(t,n){if(t.b===!0)return t;d(t)&&(n=t,t=function(t){return t}),F+=1;var e=n||(t.name||G)+"."+F;return t.open=e+".open",t.loading=e+".loading",t.update=t.loading,t.done=e,t.resolve=t.done,t.error=e+".error",t.reject=t.error,t.cancel=e+".cancel",t.cancelled=t.cancel,t.toString=function(){return e},t.b=!0,t}function b(t,n){w.call(this),this.id=R++,this.command=x(t),this.timestamp=Date.now(),this.children=[],n&&this[n]()}function z(t,n){return function(){}}function S(t,n,e){return function(i){return t.status=n,t.complete=e,arguments.length>0&&(t.payload=i),t.c("change",t),t.c(n,t.payload),t}}function A(t,n,e){return t.complete?z(t,n):S(t,n,e)}function j(t,n){return function(e,i){var r=t;if(n)try{r=i.deserialize(t)}catch(t){throw e.reject(t),t}var s=i.domains.sanitize(r);e.resolve(s)}}function k(t){w.call(this),this.size=0,this.limit=Math.max(1,t||1),this.begin()}function O(){this.pool={}}function D(t,n){this.repo=n}function E(t,n,e){var i=Y[e],r=t[n],s=n[e];return l(r)?r[i]||r[e]:t[s]}function P(t){this.repo=t,this.domains=[],this.registry={},this.add([],D)}function I(t){this.repo=t,this.effects=[]}function _(t,n,e){this.id=t,this.key=n,this.edges=[],e&&(this.parent=e,e.connect(this))}function H(t,n){w.call(this),this.id=t,this.keyPaths=e(n)}function N(t){this.snapshot=t,this.nodes={}}function B(t,n,e){function i(n){var e=s.next(n);e.done?t.resolve(n):r(e.value)}function r(n){n.onDone(i),n.onCancel(t.cancel,t),n.onError(t.reject,t)}t.open();var s=n(e);return i(),t}function T(t,n,e,i){var r=n.apply(null,e);return f(r)?(t.open.apply(t,e),r.then(function(n){return global.setTimeout(function(){return t.resolve(n)},0)},function(n){return global.setTimeout(function(){return t.reject(n)},0)}),t):v(r)?B(t,r,i):p(r)?(r(t,i),t):t.resolve(r)}function C(t,n,e){w.call(this);var i=o(C.defaults,this.constructor.defaults,t);this.parent=i.parent,this.initial=this.parent?this.parent.initial:{},this.state=this.parent?this.parent.state:this.initial,this.history=this.parent?this.parent.history:new k(i.maxHistory),this.archive=new O,this.domains=new P(this),this.effects=new I(this),this.changes=new N(this.state),this.history.on("append",this.createSnapshot,this),this.history.on("update",this.updateSnapshot,this),this.history.on("remove",this.removeSnapshot,this),this.history.on("reconcile",this.dispatchEffect,this),this.history.on("release",this.release,this),this.setup(i),n&&this.reset(n,e)}Object.defineProperty(exports,"__esModule",{value:!0});var L=".",M=",",J="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},Q=Object.prototype.hasOwnProperty,$="function"==typeof Symbol?Symbol:{},q=$.toStringTag||"@@toStringTag";w.prototype={on:function(t,n,e){var i=new m(t,n,e,!1);return this.a.push(i),this},once:function(t,n,e){var i=new m(t,n,e,!0);return this.a.push(i),this},off:function(t,n,e){for(var i=null==n,r=0;r<this.a.length;){var s=this.a[r];s.event===t&&(i||s.fn===n&&s.scope===e)?this.a.splice(r,1):r+=1}return this},removeAllListeners:function(){this.a.length=0},c:function(t){for(var n=0,e=arguments.length,i=Array(e>1?e-1:0),r=1;r<e;r++)i[r-1]=arguments[r];for(;n<this.a.length;){var s=this.a[n];s.event===t&&(s.fn.apply(s.scope||this,i),s.once)?this.a.splice(n,1):n+=1}return this},d:function(t){for(var n=0;n<this.a.length;)t!==this.a[n].scope?n+=1:this.a.splice(n,1)}};var F=0,G="_action",R=0;a(b,w,{status:"inactive",payload:void 0,disabled:!1,complete:!1,parent:null,next:null,onOpen:function(t,n){return this.e("open",t,n),this},onUpdate:function(t,n){return t&&this.on("update",t,n),this},onDone:function(t,n){return this.e("resolve",t,n),this},onError:function(t,n){return this.e("reject",t,n),this},onCancel:function(t,n){return this.e("cancel",t,n),this},is:function(t){return this.command[this.status]===this.command[t]},toggle:function(t){return this.disabled=!this.disabled,t||this.c("change",this),this},then:function(t,n){var e=this;return new Promise(function(t,n){e.onDone(t),e.onError(n)}).then(t,n)},isDisconnected:function(){return!this.parent},prune:function(){this.parent.parent=null},lead:function(t){this.next=t,t&&this.adopt(t)},adopt:function(t){this.children.indexOf(t)<0&&this.children.push(t),t.parent=this},remove:function(){this.parent.abandon(this),this.removeAllListeners()},abandon:function(t){var n=this.children.indexOf(t);n>=0&&(this.children.splice(n,1),t.parent=null),this.next===t&&this.lead(t.next)},e:function(t,n,e){n&&(this.is(t)?n.call(e,this.payload):this.once(t,n,e))}}),Object.defineProperties(b.prototype,{type:{get:function(){return this.command[this.status]}},open:{get:function(){return A(this,"open",!1)}},update:{get:function(){return A(this,"update",!1)}},resolve:{get:function(){return A(this,"resolve",!0)}},reject:{get:function(){return A(this,"reject",!0)}},cancel:{get:function(){return A(this,"cancel",!0)}}});var U=x(function(t,n){return j(t,n)},"$reset"),K=x(function(t,n){return j(t,n)},"$patch"),V=function(){},W=function(){},X=function(t){return t};a(k,w,{checkout:function(t){return this.head=t||this.head,this.setActiveBranch(),this.reconcile(this.head),this},toggle:function(t){var n=[].concat(t);n.forEach(function(t){return t.toggle("silently")}),this.reconcile(n[0])},toArray:function(){return this.map(function(t){return t})},map:function(t,n){for(var e=this.size,i=Array(e),r=this.head;e--;)i[e]=t.call(n,r),r=r.parent;return i},wait:function(){var t=this.toArray(),n=t.length;return new Promise(function(e,i){function r(){n-=1,n<=0&&e()}t.map(function(t){t.onDone(r),t.onCancel(r),t.onError(i)})})},then:function(t,n){return this.wait().then(t,n)},begin:function(){this.head=this.root=null,this.append(W,"resolve")},append:function(t,n){var e=new b(t,n);return this.size>0?this.head.lead(e):(new b(V,"resolve").adopt(e),this.root=e),this.head=e,this.size+=1,this.c("append",e),e.on("change",this.reconcile,this),this.head},remove:function(t){if(!t.isDisconnected()){var n=t.next,e=t.parent;if(this.clean(t),this.size<=0)return void this.begin();n?t===this.root&&(this.root=n):n=this.head=e,t.disabled||this.reconcile(n)}},clean:function(t){this.size-=1,this.c("remove",t),t.remove()},reconcile:function(t){for(var n=t;n&&(this.c("update",n),n!==this.head);)n=n.next;this.archive(),this.c("reconcile",t),this.c("release")},archive:function(){for(var t=this.size,n=this.root;t>this.limit&&n.complete;)t-=1,this.c("remove",n.parent),n=n.next;n.prune(),this.root=n,this.size=t},setActiveBranch:function(){for(var t=this.head,n=1;t!==this.root;){var e=t.parent;e.next=t,t=e,n+=1}this.size=n}}),O.prototype={create:function(t){this.set(t,this.get(t.parent))},get:function(t,n){var e=this.pool[t.id];return void 0===e?n:e},set:function(t,n){this.pool[t.id]=n},remove:function(t){delete this.pool[t.id]}},D.prototype={reset:function(t,n){var e=this.repo.domains.sanitize(n);return o(t,this.repo.getInitialState(),e)},patch:function(t,n){return o(t,this.repo.domains.sanitize(n))},addDomain:function(t){return o(this.repo.getInitialState(),t)},register:function(){var t;return t={},t[U]=this.reset,t[K]=this.patch,t[X]=this.addDomain,t}};var Y={inactive:"inactive",open:"open",update:"loading",loading:"update",done:"resolve",resolve:"done",reject:"error",error:"reject",cancel:"cancelled",cancelled:"cancel"};P.prototype={getHandlers:function(t){for(var n=t.command,e=t.status,i=[],r=0,s=this.domains.length;r<s;r++){var o=this.domains[r],a=o[0],h=o[1];if(h.register){var c=E(h.register(),n,e);c&&i.push({key:a,domain:h,handler:c})}}return i},register:function(t){var n=t.type;return this.registry[n]||(this.registry[n]=this.getHandlers(t)),this.registry[n]},add:function(t,e,i){var r=g(e,i,this.repo);return this.domains.push([n(t),r]),this.registry={},r.setup&&r.setup(this.repo,i),r},reduce:function(t,n,e){for(var i=n,r=1,s=this.domains.length;r<s;r++){var o=this.domains[r],a=o[0],h=o[1];i=t.call(e,i,a,h)}return i},sanitize:function(t){for(var n={},e=0,i=this.domains.length;e<i;e++){var r=this.domains[e],s=r[0];s.length&&c(t,s)&&(n=u(n,s,h(t,s)))}return n},dispatch:function(t,n){for(var e=this.register(n),i=0,r=e.length;i<r;i++){var s=e[i],o=s.key,a=s.domain,c=s.handler,f=h(t,o);t=u(t,o,c.call(a,f,n.payload))}return t},deserialize:function(t){return this.reduce(function(n,e,i){return i.deserialize?u(n,e,i.deserialize(h(t,e))):n},t)},serialize:function(t,n){return this.reduce(function(n,e,i){return i.serialize?u(n,e,i.serialize(h(t,e))):n},n)},teardown:function(){for(var t=0,n=this.domains.length;t<n;t++){var e=this.domains[t],i=(e[0],e[1]);i.teardown&&i.teardown(this.repo)}}},I.prototype={add:function(t,n){var e=g(t,n,this.repo);return e.setup&&e.setup(this.repo,n),this.effects.push(e),e},teardown:function(){for(var t=0,n=this.effects.length;t<n;t++){var e=this.effects[t];e.teardown&&e.teardown(this.repo)}},dispatch:function(t){for(var n=t.command,e=t.payload,i=t.status,r=0,s=this.effects.length;r<s;r++){var o=this.effects[r];if(o.register){var a=E(o.register(),n,i);a&&a.call(o,this.repo,e)}}}},_.prototype={parent:null,connect:function(t){t!==this&&this.edges.indexOf(t)<0&&this.edges.push(t)},disconnect:function(t){var n=this.edges.indexOf(t);~n&&this.edges.splice(n,1)},isAlone:function(){return this.edges.length<=0},orphan:function(){this.parent&&this.parent.disconnect(this)}},H.getId=function(t){return"query:"+r(e(t))},a(H,w,{extract:function(t){for(var n=this.keyPaths.length,e=Array(n),i=0;i<n;i++)e[i]=h(t,this.keyPaths[i]);return e},trigger:function(t){var n=this.extract(t);this.c.apply(this,["change"].concat(n))},isEmpty:function(){return this.a.length<=0}});var Z="";N.prototype={on:function(t,n,i){for(var r=e(t),s=H.getId(t),o=this.addQuery(s,r),a=0;a<r.length;a++)this.addBranch(r[a],o);return o.on("change",n,i),o},off:function(t,n,e){var i=H.getId(t),r=this.nodes[i];r&&(r.off("change",n,e),r.isEmpty()&&this.prune(r))},update:function(t){var n=this.snapshot;this.snapshot=t;var e=this.nodes[Z];e&&this.scan(e,n,t)},addNode:function(t,n,e){return this.nodes[t]||(this.nodes[t]=new _(t,n,e)),this.nodes[t]},addQuery:function(t,n){return this.nodes[t]||(this.nodes[t]=new H(t,n)),this.nodes[t]},remove:function(t){delete this.nodes[t.id]},prune:function(t){for(var n=t.keyPaths.map(i),e=0,r=n.length;e<r;e++){var s=this.nodes[n[e]];s.disconnect(t);do{if(!s.isAlone())break;s.orphan(),this.remove(s),s=s.parent}while(s)}this.remove(t)},addBranch:function(t,n){for(var e=this.addNode(Z,"",null),i="",r=0,s=t.length;r<s;r++)i=i?i+"."+t[r]:t[r],e=this.addNode(i,t[r],e);e.connect(n)},scan:function(t,n,e){for(var i=[{node:t,last:n,next:e}],r=[];i.length;){var s=i.pop(),o=s.node,a=s.last,h=s.next;if(a!==h)for(var c=o.edges,u=0,f=c.length;u<f;u++){var l=c[u];l instanceof H&&r.indexOf(l)<0?(l.trigger(this.snapshot),r.push(l)):i.push({node:l,last:null==a?a:a[l.key],next:null==h?h:h[l.key]})}}}},C.defaults={maxHistory:0,parent:null},a(C,w,{setup:function(){},teardown:function(){},getInitialState:function(){return this.initial},recall:function(t,n){return this.archive.get(t,n)},createSnapshot:function(t){this.archive.create(t)},updateSnapshot:function(t){var n=this.recall(t.parent,this.initial);this.parent&&(n=o(n,this.parent.recall(t))),t.disabled||(n=this.domains.dispatch(n,t)),this.archive.set(t,n),this.state=n},removeSnapshot:function(t){this.archive.remove(t)},dispatchEffect:function(t){this.effects.dispatch(t)},release:function(){this.changes.update(this.state)},on:function(t,n,e){var i=t.split(":",2),r=i[0],s=i[1];switch(r){case"change":this.changes.on(s||"",n,e);break;default:w.prototype.on.apply(this,arguments)}return this},off:function(t,n,e){var i=t.split(":",2),r=i[0],s=i[1];switch(r){case"change":this.changes.off(s||"",n,e);break;default:w.prototype.off.apply(this,arguments)}return this},append:function(t,n){return this.history.append(t,n)},push:function(t){for(var n=this.append(t),e=arguments.length,i=Array(e>1?e-1:0),r=1;r<e;r++)i[r-1]=arguments[r];return T(n,n.command,i,this),n},prepare:function(){for(var t=this,n=arguments.length,e=Array(n),i=0;i<n;i++)e[i]=arguments[i];return function(){for(var n=arguments.length,i=Array(n),r=0;r<n;r++)i[r]=arguments[r];return t.push.apply(t,e.concat(i))}},addDomain:function(t,n,e){var i=this.domains.add(t,n,e);return i.getInitialState&&(this.initial=u(this.initial,t,i.getInitialState())),this.push(X,i),i},addEffect:function(t,n){return this.effects.add(t,n)},reset:function(t,n){return this.push(U,t,n)},patch:function(t,n){return this.push(K,t,n)},deserialize:function(t){var n=t;return this.parent?n=this.parent.deserialize(t):d(n)&&(n=JSON.parse(n)),this.domains.deserialize(n)},serialize:function(){var t=this.parent?this.parent.serialize():{};return this.domains.serialize(this.state,t)},toJSON:function(){return this.serialize()},checkout:function(t){return this.history.checkout(t),this},fork:function(){return new C({parent:this})},shutdown:function(){this.teardown(),this.effects.teardown(),this.domains.teardown(),this.c("teardown",this),this.history.d(this),this.removeAllListeners()}}),exports.default=C,exports.Microcosm=C,exports.Action=b,exports.History=k,exports.tag=x,exports.get=h,exports.set=u,exports.update=y,exports.merge=o,exports.inherit=a,exports.getRegistration=E; |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.2", | ||
"version": "12.7.0-alpha.3", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -21,4 +21,5 @@ "main": "microcosm.js", | ||
"dependencies": { | ||
"form-serialize": "0.7.1" | ||
"form-serialize": "0.7.1", | ||
"rollup-plugin-strip": "^1.1.1" | ||
} | ||
} |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.2", | ||
"version": "12.7.0-alpha.3", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -21,4 +21,5 @@ "main": "microcosm.js", | ||
"dependencies": { | ||
"form-serialize": "0.7.1" | ||
"form-serialize": "0.7.1", | ||
"rollup-plugin-strip": "^1.1.1" | ||
} | ||
} |
@@ -533,63 +533,2 @@ 'use strict'; | ||
/** | ||
* Open state. The action has started, but has received no response. | ||
* @param {*} [nextPayload] | ||
*/ | ||
open: function open(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('open', false); | ||
return this; | ||
}, | ||
/** | ||
* Update state. The action has received an update, such as loading progress. | ||
* @param {*} [nextPayload] | ||
*/ | ||
update: function update$$1(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('update', false); | ||
return this; | ||
}, | ||
/** | ||
* Resolved state. The action has completed successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
resolve: function resolve(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('resolve', true); | ||
return this; | ||
}, | ||
/** | ||
* Failure state. The action did not complete successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
reject: function reject(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('reject', true); | ||
return this; | ||
}, | ||
/** | ||
* Cancelled state. The action was halted, like aborting an HTTP | ||
* request. | ||
* @param {*} [nextPayload] | ||
*/ | ||
cancel: function cancel(nextPayload) { | ||
this._setPayload.apply(this, arguments); | ||
this._setStatus('cancel', true); | ||
return this; | ||
}, | ||
/** | ||
* Subscribe to when an action opens. | ||
@@ -773,30 +712,2 @@ * @param {Function} callback | ||
/** | ||
* Reset the payload | ||
* @private | ||
*/ | ||
_setPayload: function _setPayload(payload) { | ||
if (arguments.length) { | ||
this.payload = payload; | ||
} | ||
}, | ||
/** | ||
* Change the status of the action | ||
* @private | ||
* @param {string} status The next action status | ||
* @param {boolean} complete Should the action be complete? | ||
*/ | ||
_setStatus: function _setStatus(status, complete) { | ||
if (!this.complete) { | ||
this.status = status; | ||
this.complete = complete; | ||
this._emit('change', this); | ||
this._emit(status, this.payload); | ||
} | ||
}, | ||
/** | ||
* If an action is already a given status, invoke the | ||
@@ -822,6 +733,109 @@ * provided callback. Otherwise setup a one-time listener | ||
Object.defineProperty(Action.prototype, 'type', { | ||
get: function get$$1() { | ||
return this.command[this.status]; | ||
/** | ||
* Complete actions can never change. This is a courtesy warning | ||
* @param {Action} action | ||
* @param {string} method | ||
* @return A function that will warn developers that they can not | ||
* update this action | ||
*/ | ||
function createCompleteWarning(action, method) { | ||
return function () { | ||
console.warn('Action "' + (action.command.name || action.type) + '" is already in ' + ('the ' + action.status + ' state. Calling ' + method + '() will not change it.')); | ||
}; | ||
} | ||
/** | ||
* Used to autobind action resolution methods. | ||
* @param {Action} action | ||
* @param {string} status | ||
* @param {boolean} complete | ||
* @return A function that will update the provided action with a new state | ||
*/ | ||
function createActionUpdater(action, status, complete) { | ||
return function (payload) { | ||
action.status = status; | ||
action.complete = complete; | ||
// Check arguments, we want to allow payloads that are undefined | ||
if (arguments.length > 0) { | ||
action.payload = payload; | ||
} | ||
action._emit('change', action); | ||
action._emit(status, action.payload); | ||
return action; | ||
}; | ||
} | ||
/** | ||
* If the action is complete, return a warning that the action can no longer be updated. | ||
* Otherwise return an autobound updater function. | ||
* @param {Action} action | ||
* @param {string} status | ||
* @param {boolean} complete | ||
*/ | ||
function warnOrUpdate(action, status, complete) { | ||
return action.complete ? createCompleteWarning(action, status) : createActionUpdater(action, status, complete); | ||
} | ||
Object.defineProperties(Action.prototype, { | ||
type: { | ||
get: function get$$1() { | ||
return this.command[this.status]; | ||
} | ||
}, | ||
/** | ||
* Open state. The action has started, but has received no response. | ||
* @param {*} [nextPayload] | ||
*/ | ||
open: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'open', false); | ||
} | ||
}, | ||
/** | ||
* Update state. The action has received an update, such as loading progress. | ||
* @param {*} [nextPayload] | ||
*/ | ||
update: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'update', false); | ||
} | ||
}, | ||
/** | ||
* Resolved state. The action has completed successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
resolve: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'resolve', true); | ||
} | ||
}, | ||
/** | ||
* Failure state. The action did not complete successfully. | ||
* @param {*} [nextPayload] | ||
*/ | ||
reject: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'reject', true); | ||
} | ||
}, | ||
/** | ||
* Cancelled state. The action was halted, like aborting an HTTP | ||
* request. | ||
* @param {*} [nextPayload] | ||
*/ | ||
cancel: { | ||
get: function get$$1() { | ||
return warnOrUpdate(this, 'cancel', true); | ||
} | ||
} | ||
}); | ||
@@ -828,0 +842,0 @@ |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.2", | ||
"version": "12.7.0-alpha.3", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -21,4 +21,5 @@ "main": "microcosm.js", | ||
"dependencies": { | ||
"form-serialize": "0.7.1" | ||
"form-serialize": "0.7.1", | ||
"rollup-plugin-strip": "^1.1.1" | ||
} | ||
} |
323040
4570
2
+ Addedrollup-plugin-strip@^1.1.1
+ Addedestree-walker@0.6.1(transitive)
+ Addedmagic-string@0.25.9(transitive)
+ Addedrollup-plugin-strip@1.2.2(transitive)
+ Addedrollup-pluginutils@2.8.2(transitive)
+ Addedsourcemap-codec@1.4.8(transitive)