microcosm
Advanced tools
Comparing version 12.7.0-alpha.1 to 12.7.0-alpha.2
@@ -12,2 +12,4 @@ # Changelog | ||
change event, even if state didn't change. | ||
- The first argument of `repo.push` is passed into the `open` state of | ||
actions that return promises. | ||
@@ -14,0 +16,0 @@ ## 12.6.1 |
@@ -68,10 +68,19 @@ # Actions | ||
```javascript | ||
function readPlanets () { | ||
function getPlanet (id) { | ||
// Using your favorite promise-based ajax library (maybe axios or fetch?) | ||
return ajax.get('/planets') | ||
return ajax.get(`/planets/${id}`) | ||
} | ||
repo.push(readPlanets) | ||
repo.push(getPlanet, 'mars') | ||
``` | ||
When returning a Promise: | ||
1. The action is opened with the first argument of the parameters | ||
passed to `repo.push`. In this case: `'mars'` | ||
2. When the request finishes, the action is resolved with the payload | ||
passed into the Promise's resolve callback | ||
3. If the request fails, the action is rejected with whatever error | ||
was passed with the Promise's reject callback | ||
### Return a function | ||
@@ -78,0 +87,0 @@ |
@@ -50,3 +50,4 @@ # AJAX | ||
1. Mark the action as `open`. This gives domain handlers a way to | ||
subscribe to a loading state. | ||
subscribe to a loading state. The payload of the action will be | ||
the first argument of the parameters passed into `repo.push`. | ||
2. On resolution, mark the action as `done` and update its payload to | ||
@@ -53,0 +54,0 @@ that of the resolved Promise. |
@@ -1749,3 +1749,5 @@ 'use strict'; | ||
*/ | ||
function coroutine(action, body, repo) { | ||
function coroutine(action, command, params, repo) { | ||
var body = command.apply(null, params); | ||
/** | ||
@@ -1761,3 +1763,3 @@ * Provide support for Promises: | ||
if (isPromise(body)) { | ||
action.open(); | ||
action.open.apply(action, params); | ||
@@ -1965,3 +1967,3 @@ body.then(function (result) { | ||
coroutine(action, action.command.apply(null, params), this); | ||
coroutine(action, action.command, params, this); | ||
@@ -1968,0 +1970,0 @@ return action; |
@@ -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){return f(n)?(t.open(),n.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(n)?_(t,n,e):p(n)?(n(t,e),t):t.resolve(n)}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.apply(null,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(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; |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.1", | ||
"version": "12.7.0-alpha.2", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -5,0 +5,0 @@ "main": "microcosm.js", |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.1", | ||
"version": "12.7.0-alpha.2", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -5,0 +5,0 @@ "main": "microcosm.js", |
@@ -1770,3 +1770,5 @@ 'use strict'; | ||
*/ | ||
function coroutine(action, body, repo) { | ||
function coroutine(action, command, params, repo) { | ||
var body = command.apply(null, params); | ||
/** | ||
@@ -1782,3 +1784,3 @@ * Provide support for Promises: | ||
if (isPromise(body)) { | ||
action.open(); | ||
action.open.apply(action, params); | ||
@@ -1986,3 +1988,3 @@ body.then(function (result) { | ||
coroutine(action, action.command.apply(null, params), this); | ||
coroutine(action, action.command, params, this); | ||
@@ -1989,0 +1991,0 @@ return action; |
{ | ||
"name": "microcosm", | ||
"version": "12.7.0-alpha.1", | ||
"version": "12.7.0-alpha.2", | ||
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.", | ||
@@ -5,0 +5,0 @@ "main": "microcosm.js", |
319966
4527