microcosm
Advanced tools
Comparing version 12.1.0-rc to 12.1.0-rc-2
# Changelog | ||
## Edge | ||
## 12.1.0 (not released) | ||
- Add `onOpen` callback props to `ActionForm` and `ActionButton` | ||
- Replaced internal references of `behavior` with `command` | ||
- Cleaned up some internal operations they no longer expose actions in history | ||
@@ -8,0 +9,0 @@ ## 12.0.0 |
321
microcosm.js
@@ -98,6 +98,97 @@ 'use strict'; | ||
var uid = 0; | ||
var FALLBACK = '_action'; | ||
var toString = function toString() { | ||
return this.done; | ||
}; | ||
/** | ||
* Uniquely tag a function. This is used to identify actions. | ||
* @param {Function} fn The target function to add action identifiers to. | ||
* @param {String} [name] An override to use instead of `fn.name`. | ||
* @return {Function} The tagged function (same as `fn`). | ||
*/ | ||
function tag(fn, name) { | ||
if (fn == null) { | ||
throw new Error('Unable to identify ' + fn + ' action'); | ||
} | ||
if (fn.done) { | ||
return fn; | ||
} | ||
if (typeof fn === 'string') { | ||
name = fn; | ||
fn = function fn(n) { | ||
return n; | ||
}; | ||
} | ||
/** | ||
* Auto-increment a stepper suffix to prevent two actions with the | ||
* same name from colliding. | ||
*/ | ||
uid += 1; | ||
/** | ||
* Function.name lacks legacy support. For these browsers, fallback | ||
* to a consistent name: | ||
*/ | ||
var symbol = name || (fn.name || FALLBACK) + '.' + uid; | ||
fn.open = symbol + '.open'; | ||
fn.loading = symbol + '.loading'; | ||
fn.update = fn.loading; | ||
fn.done = symbol; // intentional for string actions | ||
fn.resolve = fn.done; | ||
fn.error = symbol + '.error'; | ||
fn.reject = fn.error; | ||
fn.cancel = symbol + '.cancel'; | ||
fn.cancelled = fn.cancel; | ||
// The default state is done | ||
fn.toString = toString; | ||
return fn; | ||
} | ||
function sandbox(data, deserialize) { | ||
return function (action, repo) { | ||
var payload = data; | ||
if (deserialize) { | ||
try { | ||
payload = repo.deserialize(data); | ||
} catch (error) { | ||
action.reject(error); | ||
} | ||
} | ||
action.resolve(payload); | ||
}; | ||
} | ||
var RESET = tag(function reset(data, deserialize) { | ||
return sandbox(data, deserialize); | ||
}, '$reset'); | ||
var PATCH = tag(function patch(data, deserialize) { | ||
return sandbox(data, deserialize); | ||
}, '$patch'); | ||
var BIRTH = '$birth'; | ||
var START = '$start'; | ||
/** | ||
* The central tree data structure that is used to calculate state for | ||
* a Microcosm. Each node in the tree represents an action. Branches | ||
* are changes over time. | ||
* | ||
* | ||
*/ | ||
@@ -111,3 +202,5 @@ | ||
this.append('_root').resolve(); | ||
this.genesis = new Action(BIRTH, 'resolve', this); | ||
this.append(START, 'resolve'); | ||
} | ||
@@ -137,11 +230,7 @@ | ||
checkout: function checkout(action) { | ||
if (action == null) { | ||
throw new TypeError('Unable to checkout ' + action + ' action'); | ||
} | ||
this.head = action || this.head; | ||
this.head = action; | ||
this.setActiveBranch(); | ||
this.reconcile(action); | ||
this.reconcile(this.head); | ||
@@ -151,7 +240,7 @@ return this; | ||
append: function append(command, status) { | ||
var action = new Action(command, this, status); | ||
var action = new Action(command, status, this); | ||
action.parent = this.size ? this.head : this.genesis; | ||
if (this.size > 0) { | ||
action.parent = this.head; | ||
// To keep track of children, maintain a pointer to the first | ||
@@ -177,2 +266,3 @@ // child ever produced. We might checkout another child later, | ||
reconcile: function reconcile(action) { | ||
var focus = action; | ||
@@ -195,16 +285,15 @@ | ||
archive: function archive() { | ||
var action = this.root; | ||
var size = this.size; | ||
var root = this.root; | ||
while (this.size > this.limit && action.disposable) { | ||
this.size -= 1; | ||
while (size > this.limit && root.disposable) { | ||
size -= 1; | ||
this.invoke('clean', root.parent); | ||
root = root.next; | ||
} | ||
if (action.parent) { | ||
this.invoke('clean', action.parent); | ||
action.parent = null; | ||
} | ||
root.prune(); | ||
action = action.next; | ||
} | ||
this.root = action; | ||
this.root = root; | ||
this.size = size; | ||
}, | ||
@@ -227,2 +316,14 @@ setActiveBranch: function setActiveBranch() { | ||
}, | ||
// Toggle actions in bulk, then reconcile from the first action | ||
toggle: function toggle(actions) { | ||
var list = [].concat(actions); | ||
list.forEach(function (action) { | ||
return action.toggle('silently'); | ||
}); | ||
this.reconcile(list[0]); | ||
}, | ||
map: function map(fn, scope) { | ||
@@ -247,84 +348,2 @@ var size = this.size; | ||
var uid = 0; | ||
var FALLBACK = '_action'; | ||
var toString = function toString() { | ||
return this.done; | ||
}; | ||
/** | ||
* Uniquely tag a function. This is used to identify actions. | ||
* @param {Function} fn The target function to add action identifiers to. | ||
* @param {String} [name] An override to use instead of `fn.name`. | ||
* @return {Function} The tagged function (same as `fn`). | ||
*/ | ||
function tag(fn, name) { | ||
if (fn == null) { | ||
throw new Error('Unable to identify ' + fn + ' action'); | ||
} | ||
if (fn.done) { | ||
return fn; | ||
} | ||
if (typeof fn === 'string') { | ||
name = fn; | ||
fn = function fn(n) { | ||
return n; | ||
}; | ||
} | ||
/** | ||
* Auto-increment a stepper suffix to prevent two actions with the | ||
* same name from colliding. | ||
*/ | ||
uid += 1; | ||
/** | ||
* Function.name lacks legacy support. For these browsers, fallback | ||
* to a consistent name: | ||
*/ | ||
var symbol = name || (fn.name || FALLBACK) + '.' + uid; | ||
fn.open = symbol + '.open'; | ||
fn.loading = symbol + '.loading'; | ||
fn.update = fn.loading; | ||
fn.done = symbol; // intentional for string actions | ||
fn.resolve = fn.done; | ||
fn.error = symbol + '.error'; | ||
fn.reject = fn.error; | ||
fn.cancel = symbol + '.cancel'; | ||
fn.cancelled = fn.cancel; | ||
// The default state is done | ||
fn.toString = toString; | ||
return fn; | ||
} | ||
/** | ||
* Actions move through a specific set of states. This manifest | ||
* controls how they should behave. | ||
*/ | ||
var ACTION_STATES = [{ key: 'open', disposable: false, once: true, listener: 'onOpen' }, { key: 'update', disposable: false, once: false, listener: 'onUpdate' }, { key: 'resolve', disposable: true, once: true, listener: 'onDone' }, { key: 'reject', disposable: true, once: true, listener: 'onError' }, { key: 'cancel', disposable: true, once: true, listener: 'onCancel' }]; | ||
// For nested registrations, track our aliases | ||
var ACTION_ALIASES = { | ||
inactive: 'inactive', | ||
open: 'open', | ||
update: 'loading', | ||
loading: 'update', | ||
done: 'resolve', | ||
resolve: 'done', | ||
reject: 'error', | ||
error: 'reject', | ||
cancel: 'cancelled', | ||
cancelled: 'cancel' | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -526,6 +545,34 @@ | ||
/** | ||
* Actions encapsulate the process of resolving an action | ||
* creator. Create an action using `Microcosm::push`: | ||
* Actions move through a specific set of states. This manifest | ||
* controls how they should behave. | ||
*/ | ||
function Action(command, history, status) { | ||
var ACTION_STATES = { | ||
inactive: { disposable: false, once: true, listener: 'onInactive' }, | ||
open: { disposable: false, once: true, listener: 'onOpen' }, | ||
update: { disposable: false, once: false, listener: 'onUpdate' }, | ||
resolve: { disposable: true, once: true, listener: 'onDone' }, | ||
reject: { disposable: true, once: true, listener: 'onError' }, | ||
cancel: { disposable: true, once: true, listener: 'onCancel' } | ||
}; | ||
// For nested registrations, track our aliases | ||
var ACTION_ALIASES = { | ||
inactive: 'inactive', | ||
open: 'open', | ||
update: 'loading', | ||
loading: 'update', | ||
done: 'resolve', | ||
resolve: 'done', | ||
reject: 'error', | ||
error: 'reject', | ||
cancel: 'cancelled', | ||
cancelled: 'cancel' | ||
}; | ||
/** | ||
* Actions encapsulate the process of resolving an action creator. Create an | ||
* action using `Microcosm::push`: | ||
*/ | ||
function Action(command, status, history) { | ||
Emitter.call(this); | ||
@@ -537,5 +584,6 @@ | ||
this.command = tag(command); | ||
this.timestamp = Date.now(); | ||
if (status) { | ||
this.status = status; | ||
this.setStatus(status); | ||
} | ||
@@ -557,6 +605,8 @@ } | ||
}, | ||
toggle: function toggle() { | ||
toggle: function toggle(silent) { | ||
this.disabled = !this.disabled; | ||
this.history.reconcile(this); | ||
if (!silent) { | ||
this.history.reconcile(this); | ||
} | ||
@@ -572,2 +622,10 @@ return this; | ||
}).then(pass, fail); | ||
}, | ||
setStatus: function setStatus(status) { | ||
this.status = status; | ||
this.disposable = ACTION_STATES[status].disposable; | ||
}, | ||
prune: function prune() { | ||
this.parent.parent = null; | ||
} | ||
@@ -579,9 +637,7 @@ }); | ||
*/ | ||
ACTION_STATES.forEach(function (_ref) { | ||
var key = _ref.key, | ||
disposable = _ref.disposable, | ||
once = _ref.once, | ||
listener = _ref.listener; | ||
Object.keys(ACTION_STATES).forEach(function (key) { | ||
var _ACTION_STATES$key = ACTION_STATES[key], | ||
once = _ACTION_STATES$key.once, | ||
listener = _ACTION_STATES$key.listener; | ||
/** | ||
@@ -591,6 +647,6 @@ * Create a method to update the action status. For example: | ||
*/ | ||
Action.prototype[key] = function (payload) { | ||
if (!this.disposable) { | ||
this.status = key; | ||
this.disposable = disposable; | ||
this.setStatus(key); | ||
@@ -643,28 +699,2 @@ if (arguments.length) { | ||
function sandbox(data, deserialize) { | ||
return function (action, repo) { | ||
var payload = data; | ||
if (deserialize) { | ||
try { | ||
payload = repo.deserialize(data); | ||
} catch (error) { | ||
action.reject(error); | ||
} | ||
} | ||
action.resolve(payload); | ||
}; | ||
} | ||
var RESET = tag(function reset(data, deserialize) { | ||
return sandbox(data, deserialize); | ||
}, 'reset'); | ||
var PATCH = tag(function patch(data, deserialize) { | ||
return sandbox(data, deserialize); | ||
}, 'patch'); | ||
var ADD_DOMAIN = 'addDomain'; | ||
function MetaDomain(_, repo) { | ||
@@ -818,5 +848,7 @@ this.repo = repo; | ||
set: function set(action, state) { | ||
this.pool[action.id] = state; | ||
}, | ||
remove: function remove(action) { | ||
delete this.pool[action.id]; | ||
@@ -1054,5 +1086,6 @@ } | ||
this.initial = set(this.initial, key, domain.getInitialState()); | ||
this.push(ADD_DOMAIN, domain, key); | ||
} | ||
this.history.checkout(); | ||
return domain; | ||
@@ -1059,0 +1092,0 @@ }, |
@@ -1,1 +0,1 @@ | ||
"use strict";function t(){this._events=null}function e(t){this.ids=0,this.size=0,this.limit=Math.max(1,t||1),this.repos=[],this.append("_root").resolve()}function n(t,e){if(null==t)throw new Error("Unable to identify "+t+" action");if(t.done)return t;"string"==typeof t&&(e=t,t=function(t){return t}),A+=1;var n=e||(t.name||j)+"."+A;return t.open=n+".open",t.loading=n+".loading",t.update=t.loading,t.done=n,t.resolve=t.done,t.error=n+".error",t.reject=t.error,t.cancel=n+".cancel",t.cancelled=t.cancel,t.toString=S,t}function i(t){return null==t?[]:Array.isArray(t)?t:"string"==typeof t?t.split(R):[t]}function r(t){if(Array.isArray(t))return t.slice(0);if(l(t)===!1)return t;var e={};for(var n in t)e[n]=t[n];return e}function o(){for(var t=null,e=null,n=0,i=arguments.length;n<i;n++){t=t||arguments[n],e=e||t;var o=arguments[n];for(var s in o)t[s]!==o[s]&&(t===e&&(t=r(e)),t[s]=o[s])}return t}function s(t,e){if(null==t)return e;var n=t;for(var i in e)n.hasOwnProperty(i)===!1&&(n===t&&(n=r(t)),n[i]=e[i]);return n}function a(t,e,n){return t.__proto__=e,t.prototype=o(Object.create(e.prototype),{constructor:t},n),t}function h(t,e,n){if(null==t)return n;for(var r=i(e),o=0,s=r.length;o<s;o++){var a=null==t?void 0:t[r[o]];void 0===a&&(o=s,a=n),t=a}return t}function u(t,e,n){var o=i(e),s=o.length;if(s<=0)return n;if(h(t,o)===n)return t;for(var a=r(t),u=a,c=0;c<s;c++){var l=o[c],f=n;c<s-1&&(f=l in u?r(u[l]):{}),u[l]=f,u=u[l]}return a}function c(t){var e=void 0===t?"undefined":I(t);return!!t&&("object"===e||"function"===e)&&"function"==typeof t.then}function l(t){return!!t&&"object"===(void 0===t?"undefined":I(t))}function f(t,e,n){return"function"==typeof t?new t(e,n):Object.create(t)}function p(t,e,n,i){return"function"!=typeof n?u(t,e,n):u(t,e,n(h(t,e,i)))}function d(i,r,o){t.call(this),this.history=r||new e,this.id=this.history.getId(),this.command=n(i),o&&(this.status=o)}function v(t,e){return function(n,i){var r=t;if(e)try{r=i.deserialize(t)}catch(t){n.reject(t)}n.resolve(r)}}function y(t,e){this.repo=e}function m(t,e,n){var i=null,r=n?E[n]:T;if(null==r)throw new ReferenceError("Invalid action status "+n);return t&&e&&(i=l(t[e])?t[e][r]||t[e][n]:t[e[r]]),i}function g(t,e){for(var n=e.command,i=e.status,r=[],o=0,s=t.length;o<s;o++){var a=t[o],h=a[0],u=a[1];if(u.register){var c=m(u.register(),n,i);c&&r.push({key:h,domain:u,handler:c})}}return r}function b(t){this.repo=t,this.domains=[],this.registry={},this.meta=this.add(null,y)}function w(){this.pool={}}function _(t,e){return function(n){var i=n.command,r=n.status,o=n.payload,s=m(e.register(),i,r);s&&s.call(e,t,o)}}function x(t,e,n){var i=f(e,n,t);return i.setup&&i.setup(t,n),i.register&&t.on("effect",_(t,i)),i.teardown&&t.on("teardown",i.teardown,i),i}function k(t,e,n){return c(e)?(t.open(),e.then(function(e){return global.setTimeout(function(){return t.resolve(e)},0)},function(e){return global.setTimeout(function(){return t.reject(e)},0)}),t):"function"==typeof e?(e(t,n),t):t.resolve(e)}function z(n,i,r){t.call(this),n=n||{},this.parent=n.parent||null,this.history=this.parent?this.parent.history:new e(n.maxHistory),this.history.addRepo(this),this.realm=new b(this),this.archive=new w,this.initial=this.parent?this.parent.initial:{},this.state=this.parent?this.parent.state:this.initial,this.follower=!!this.parent,this.setup(n),i&&this.reset(i,r)}Object.defineProperty(exports,"__esModule",{value:!0}),t.prototype={on:function(t,e,n,i){return null==this._events&&(this._events=[]),this._events.push({event:t,fn:e,scope:n,once:i}),this},once:function(t,e,n){return this.on(t,e,n,!0)},off:function(t,e,n){if(null==this._events)return this;for(var i=null==e,r=0;r<this._events.length;){var o=this._events[r];o.event===t&&(i||o.fn===e&&o.scope===n)?this._events.splice(r,1):r+=1}return this},removeAllListeners:function(){this._events=null},_emit:function(t,e){if(null==this._events)return this;for(var n=0;n<this._events.length;){var i=this._events[n];i.event===t&&(i.fn.call(i.scope||this,e),i.once)?this._events.splice(n,1):n+=1}return this}},e.prototype={getId:function(){return++this.ids},addRepo:function(t){this.repos.push(t)},removeRepo:function(t){var e=this.repos.indexOf(t);~e&&this.repos.splice(e,1)},invoke:function(t,e){for(var n=this.repos,i=0,r=n.length;i<r;i++)n[i][t](e)},checkout:function(t){if(null==t)throw new TypeError("Unable to checkout "+t+" action");return this.head=t,this.setActiveBranch(),this.reconcile(t),this},append:function(t,e){var n=new d(t,this,e);return this.size>0?(n.parent=this.head,this.head.first?this.head.next.sibling=n:this.head.first=n,this.head.next=n):this.root=n,this.head=n,this.size+=1,this.head},reconcile:function(t){for(var e=t;e&&(this.invoke("reconcile",e),e!==this.head);)e=e.next;this.archive(),this.invoke("release",t)},archive:function(){for(var t=this.root;this.size>this.limit&&t.disposable;)this.size-=1,t.parent&&(this.invoke("clean",t.parent),t.parent=null),t=t.next;this.root=t},setActiveBranch:function(){for(var t=this.head,e=1;t!==this.root;){var n=t.parent;n.next=t,t=n,e+=1}this.size=e},map:function(t,e){for(var n=this.size,i=Array(n),r=this.head;n--;)i[n]=t.call(e,r),r=r.parent;return i},toArray:function(){return this.map(function(t){return t})}};var A=0,j="_action",S=function(){return this.done},O=[{key:"open",disposable:!1,once:!0,listener:"onOpen"},{key:"update",disposable:!1,once:!1,listener:"onUpdate"},{key:"resolve",disposable:!0,once:!0,listener:"onDone"},{key:"reject",disposable:!0,once:!0,listener:"onError"},{key:"cancel",disposable:!0,once:!0,listener:"onCancel"}],E={inactive:"inactive",open:"open",update:"loading",loading:"update",done:"resolve",resolve:"done",reject:"error",error:"reject",cancel:"cancelled",cancelled:"cancel"},I="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},R=".";a(d,t,{status:"inactive",payload:void 0,disabled:!1,disposable:!1,parent:null,first:null,next:null,sibling:null,is:function(t){return this.command[this.status]===this.command[t]},toggle:function(){return this.disabled=!this.disabled,this.history.reconcile(this),this},then:function(t,e){var n=this;return new Promise(function(t,e){n.onDone(t),n.onError(e)}).then(t,e)}}),O.forEach(function(t){var e=t.key,n=t.disposable,i=t.once,r=t.listener;d.prototype[e]=function(t){return this.disposable||(this.status=e,this.disposable=n,arguments.length&&(this.payload=t),this.history.reconcile(this),this._emit(e,this.payload)),this},d.prototype[r]=function(t,n){return t&&(i&&this.is(e)?t.call(n,this.payload):this.once(e,t,n)),this}}),Object.defineProperty(d.prototype,"children",{get:function(){for(var t=[],e=this.first;e;)t.unshift(e),e=e.sibling;return t}});var P=n(function(t,e){return v(t,e)},"reset"),D=n(function(t,e){return v(t,e)},"patch"),M="addDomain";y.prototype={reset:function(t,e){return this.patch(this.repo.getInitialState(),e)},patch:function(t,e){return this.repo.realm.prune(t,e)},register:function(){var t;return t={},t[P]=this.reset,t[D]=this.patch,t}};var T="done";b.prototype={register:function(t){var e=t.command[t.status];return void 0===this.registry[e]&&(this.registry[e]=g(this.domains,t)),this.registry[e]},add:function(t,e,n){var r=f(e,n,this.repo);return this.domains.push([i(t),r]),this.registry={},r.setup&&r.setup(this.repo,n),r.teardown&&this.repo.on("teardown",r.teardown,r),r},reduce:function(t,e,n){for(var i=e,r=1,o=this.domains.length;r<o;r++){var s=this.domains[r],a=s[0],h=s[1];i=t.call(n,i,a,h)}return i},invoke:function(t,e,n){return this.reduce(function(n,i,r){return r[t]?u(n,i,r[t](h(e,i))):n},n)},prune:function(t,e){return this.reduce(function(t,n){var i=h(e,n);return void 0===i?t:u(t,n,i)},t)}},w.prototype={get:function(t){return this.pool[t.id]},has:function(t){return this.pool.hasOwnProperty(t.id)},set:function(t,e){this.pool[t.id]=e},remove:function(t){delete this.pool[t.id]}},a(z,t,{setup:function(){},teardown:function(){this._emit("teardown",this),this.history.removeRepo(this),this.removeAllListeners()},getInitialState:function(){return this.initial},recall:function(t){return this.archive.get(t)},save:function(t,e){return this.archive.set(t,e)},clean:function(t){this.archive.remove(t)},dispatch:function(t,e){for(var n=this.realm.register(e),i=t,r=0,o=n.length;r<o;r++){var s=n[r],a=s.key,c=s.domain,l=s.handler,f=h(t,a);i=u(i,a,l.call(c,f,e.payload))}return i},reconcile:function(t){if(this.follower)return this;var e=s(this.recall(t.parent),this.initial);return this.parent&&(e=o(e,this.parent.recall(t))),t.disabled||(e=this.dispatch(e,t)),this.save(t,e),this},release:function(t){var e=this.follower?this.parent.state:this.recall(this.history.head);e!==this.state&&(this.state=e,this._emit("change",e)),this._emit("effect",t)},append:function(t,e){return this.history.append(t,e)},push:function(t){for(var e=this.append(t),n=arguments.length,i=Array(n>1?n-1:0),r=1;r<n;r++)i[r-1]=arguments[r];return k(e,e.command.apply(null,i),this),e},prepare:function(){for(var t=this,e=arguments.length,n=Array(e),i=0;i<e;i++)n[i]=arguments[i];return function(){for(var e=arguments.length,i=Array(e),r=0;r<e;r++)i[r]=arguments[r];return t.push.apply(t,n.concat(i))}},addDomain:function(t,e,n){var i=this.realm.add(t,e,n);return this.follower=!1,i.getInitialState&&(this.initial=u(this.initial,t,i.getInitialState()),this.push(M,i,t)),i},addEffect:function(t,e){return x(this,t,e)},reset:function(t,e){return this.push(P,t,e)},patch:function(t,e){return this.push(D,t,e)},deserialize:function(t){var e=t;return this.parent?e=this.parent.deserialize(t):"string"==typeof e&&(e=JSON.parse(e)),this.realm.invoke("deserialize",e,e)},serialize:function(){var t=this.parent?this.parent.serialize():{};return this.realm.invoke("serialize",this.state,t)},toJSON:function(){return this.serialize()},checkout:function(t){return this.history.checkout(t),this},fork:function(){return new z({parent:this})}}),exports.default=z,exports.Microcosm=z,exports.Action=d,exports.History=e,exports.tag=n,exports.get=h,exports.set=u,exports.update=p,exports.merge=o,exports.inherit=a,exports.getRegistration=m; | ||
"use strict";function t(){this._events=null}function e(t,e){if(null==t)throw new Error("Unable to identify "+t+" action");if(t.done)return t;"string"==typeof t&&(e=t,t=function(t){return t}),S+=1;var n=e||(t.name||A)+"."+S;return t.open=n+".open",t.loading=n+".loading",t.update=t.loading,t.done=n,t.resolve=t.done,t.error=n+".error",t.reject=t.error,t.cancel=n+".cancel",t.cancelled=t.cancel,t.toString=j,t}function n(t,e){return function(n,i){var r=t;if(e)try{r=i.deserialize(t)}catch(t){n.reject(t)}n.resolve(r)}}function i(t){this.ids=0,this.size=0,this.limit=Math.max(1,t||1),this.repos=[],this.genesis=new v(E,"resolve",this),this.append(R,"resolve")}function r(t){return null==t?[]:Array.isArray(t)?t:"string"==typeof t?t.split(D):[t]}function s(t){if(Array.isArray(t))return t.slice(0);if(f(t)===!1)return t;var e={};for(var n in t)e[n]=t[n];return e}function o(){for(var t=null,e=null,n=0,i=arguments.length;n<i;n++){t=t||arguments[n],e=e||t;var r=arguments[n];for(var o in r)t[o]!==r[o]&&(t===e&&(t=s(e)),t[o]=r[o])}return t}function a(t,e){if(null==t)return e;var n=t;for(var i in e)n.hasOwnProperty(i)===!1&&(n===t&&(n=s(t)),n[i]=e[i]);return n}function h(t,e,n){return t.__proto__=e,t.prototype=o(Object.create(e.prototype),{constructor:t},n),t}function u(t,e,n){if(null==t)return n;for(var i=r(e),s=0,o=i.length;s<o;s++){var a=null==t?void 0:t[i[s]];void 0===a&&(s=o,a=n),t=a}return t}function c(t,e,n){var i=r(e),o=i.length;if(o<=0)return n;if(u(t,i)===n)return t;for(var a=s(t),h=a,c=0;c<o;c++){var l=i[c],f=n;c<o-1&&(f=l in h?s(h[l]):{}),h[l]=f,h=h[l]}return a}function l(t){var e=void 0===t?"undefined":P(t);return!!t&&("object"===e||"function"===e)&&"function"==typeof t.then}function f(t){return!!t&&"object"===(void 0===t?"undefined":P(t))}function p(t,e,n){return"function"==typeof t?new t(e,n):Object.create(t)}function d(t,e,n,i){return"function"!=typeof n?c(t,e,n):c(t,e,n(u(t,e,i)))}function v(n,r,s){t.call(this),this.history=s||new i,this.id=this.history.getId(),this.command=e(n),this.timestamp=Date.now(),r&&this.setStatus(r)}function y(t,e){this.repo=e}function g(t,e,n){var i=null,r=n?M[n]:B;if(null==r)throw new ReferenceError("Invalid action status "+n);return t&&e&&(i=f(t[e])?t[e][r]||t[e][n]:t[e[r]]),i}function m(t,e){for(var n=e.command,i=e.status,r=[],s=0,o=t.length;s<o;s++){var a=t[s],h=a[0],u=a[1];if(u.register){var c=g(u.register(),n,i);c&&r.push({key:h,domain:u,handler:c})}}return r}function b(t){this.repo=t,this.domains=[],this.registry={},this.meta=this.add(null,y)}function w(){this.pool={}}function _(t,e){return function(n){var i=n.command,r=n.status,s=n.payload,o=g(e.register(),i,r);o&&o.call(e,t,s)}}function x(t,e,n){var i=p(e,n,t);return i.setup&&i.setup(t,n),i.register&&t.on("effect",_(t,i)),i.teardown&&t.on("teardown",i.teardown,i),i}function z(t,e,n){return l(e)?(t.open(),e.then(function(e){return global.setTimeout(function(){return t.resolve(e)},0)},function(e){return global.setTimeout(function(){return t.reject(e)},0)}),t):"function"==typeof e?(e(t,n),t):t.resolve(e)}function k(e,n,r){t.call(this),e=e||{},this.parent=e.parent||null,this.history=this.parent?this.parent.history:new i(e.maxHistory),this.history.addRepo(this),this.realm=new b(this),this.archive=new w,this.initial=this.parent?this.parent.initial:{},this.state=this.parent?this.parent.state:this.initial,this.follower=!!this.parent,this.setup(e),n&&this.reset(n,r)}Object.defineProperty(exports,"__esModule",{value:!0}),t.prototype={on:function(t,e,n,i){return null==this._events&&(this._events=[]),this._events.push({event:t,fn:e,scope:n,once:i}),this},once:function(t,e,n){return this.on(t,e,n,!0)},off:function(t,e,n){if(null==this._events)return this;for(var i=null==e,r=0;r<this._events.length;){var s=this._events[r];s.event===t&&(i||s.fn===e&&s.scope===n)?this._events.splice(r,1):r+=1}return this},removeAllListeners:function(){this._events=null},_emit:function(t,e){if(null==this._events)return this;for(var n=0;n<this._events.length;){var i=this._events[n];i.event===t&&(i.fn.call(i.scope||this,e),i.once)?this._events.splice(n,1):n+=1}return this}};var S=0,A="_action",j=function(){return this.done},O=e(function(t,e){return n(t,e)},"$reset"),I=e(function(t,e){return n(t,e)},"$patch"),E="$birth",R="$start";i.prototype={getId:function(){return++this.ids},addRepo:function(t){this.repos.push(t)},removeRepo:function(t){var e=this.repos.indexOf(t);~e&&this.repos.splice(e,1)},invoke:function(t,e){for(var n=this.repos,i=0,r=n.length;i<r;i++)n[i][t](e)},checkout:function(t){return this.head=t||this.head,this.setActiveBranch(),this.reconcile(this.head),this},append:function(t,e){var n=new v(t,e,this);return n.parent=this.size?this.head:this.genesis,this.size>0?(this.head.first?this.head.next.sibling=n:this.head.first=n,this.head.next=n):this.root=n,this.head=n,this.size+=1,this.head},reconcile:function(t){for(var e=t;e&&(this.invoke("reconcile",e),e!==this.head);)e=e.next;this.archive(),this.invoke("release",t)},archive:function(){for(var t=this.size,e=this.root;t>this.limit&&e.disposable;)t-=1,this.invoke("clean",e.parent),e=e.next;e.prune(),this.root=e,this.size=t},setActiveBranch:function(){for(var t=this.head,e=1;t!==this.root;){var n=t.parent;n.next=t,t=n,e+=1}this.size=e},toggle:function(t){var e=[].concat(t);e.forEach(function(t){return t.toggle("silently")}),this.reconcile(e[0])},map:function(t,e){for(var n=this.size,i=Array(n),r=this.head;n--;)i[n]=t.call(e,r),r=r.parent;return i},toArray:function(){return this.map(function(t){return t})}};var P="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},D=".",$={inactive:{disposable:!1,once:!0,listener:"onInactive"},open:{disposable:!1,once:!0,listener:"onOpen"},update:{disposable:!1,once:!1,listener:"onUpdate"},resolve:{disposable:!0,once:!0,listener:"onDone"},reject:{disposable:!0,once:!0,listener:"onError"},cancel:{disposable:!0,once:!0,listener:"onCancel"}},M={inactive:"inactive",open:"open",update:"loading",loading:"update",done:"resolve",resolve:"done",reject:"error",error:"reject",cancel:"cancelled",cancelled:"cancel"};h(v,t,{status:"inactive",payload:void 0,disabled:!1,disposable:!1,parent:null,first:null,next:null,sibling:null,is:function(t){return this.command[this.status]===this.command[t]},toggle:function(t){return this.disabled=!this.disabled,t||this.history.reconcile(this),this},then:function(t,e){var n=this;return new Promise(function(t,e){n.onDone(t),n.onError(e)}).then(t,e)},setStatus:function(t){this.status=t,this.disposable=$[t].disposable},prune:function(){this.parent.parent=null}}),Object.keys($).forEach(function(t){var e=$[t],n=e.once,i=e.listener;v.prototype[t]=function(e){return this.disposable||(this.setStatus(t),arguments.length&&(this.payload=e),this.history.reconcile(this),this._emit(t,this.payload)),this},v.prototype[i]=function(e,i){return e&&(n&&this.is(t)?e.call(i,this.payload):this.once(t,e,i)),this}}),Object.defineProperty(v.prototype,"children",{get:function(){for(var t=[],e=this.first;e;)t.unshift(e),e=e.sibling;return t}}),y.prototype={reset:function(t,e){return this.patch(this.repo.getInitialState(),e)},patch:function(t,e){return this.repo.realm.prune(t,e)},register:function(){var t;return t={},t[O]=this.reset,t[I]=this.patch,t}};var B="done";b.prototype={register:function(t){var e=t.command[t.status];return void 0===this.registry[e]&&(this.registry[e]=m(this.domains,t)),this.registry[e]},add:function(t,e,n){var i=p(e,n,this.repo);return this.domains.push([r(t),i]),this.registry={},i.setup&&i.setup(this.repo,n),i.teardown&&this.repo.on("teardown",i.teardown,i),i},reduce:function(t,e,n){for(var i=e,r=1,s=this.domains.length;r<s;r++){var o=this.domains[r],a=o[0],h=o[1];i=t.call(n,i,a,h)}return i},invoke:function(t,e,n){return this.reduce(function(n,i,r){return r[t]?c(n,i,r[t](u(e,i))):n},n)},prune:function(t,e){return this.reduce(function(t,n){var i=u(e,n);return void 0===i?t:c(t,n,i)},t)}},w.prototype={get:function(t){return this.pool[t.id]},has:function(t){return this.pool.hasOwnProperty(t.id)},set:function(t,e){this.pool[t.id]=e},remove:function(t){delete this.pool[t.id]}},h(k,t,{setup:function(){},teardown:function(){this._emit("teardown",this),this.history.removeRepo(this),this.removeAllListeners()},getInitialState:function(){return this.initial},recall:function(t){return this.archive.get(t)},save:function(t,e){return this.archive.set(t,e)},clean:function(t){this.archive.remove(t)},dispatch:function(t,e){for(var n=this.realm.register(e),i=t,r=0,s=n.length;r<s;r++){var o=n[r],a=o.key,h=o.domain,l=o.handler,f=u(t,a);i=c(i,a,l.call(h,f,e.payload))}return i},reconcile:function(t){if(this.follower)return this;var e=a(this.recall(t.parent),this.initial);return this.parent&&(e=o(e,this.parent.recall(t))),t.disabled||(e=this.dispatch(e,t)),this.save(t,e),this},release:function(t){var e=this.follower?this.parent.state:this.recall(this.history.head);e!==this.state&&(this.state=e,this._emit("change",e)),this._emit("effect",t)},append:function(t,e){return this.history.append(t,e)},push:function(t){for(var e=this.append(t),n=arguments.length,i=Array(n>1?n-1:0),r=1;r<n;r++)i[r-1]=arguments[r];return z(e,e.command.apply(null,i),this),e},prepare:function(){for(var t=this,e=arguments.length,n=Array(e),i=0;i<e;i++)n[i]=arguments[i];return function(){for(var e=arguments.length,i=Array(e),r=0;r<e;r++)i[r]=arguments[r];return t.push.apply(t,n.concat(i))}},addDomain:function(t,e,n){var i=this.realm.add(t,e,n);return this.follower=!1,i.getInitialState&&(this.initial=c(this.initial,t,i.getInitialState())),this.history.checkout(),i},addEffect:function(t,e){return x(this,t,e)},reset:function(t,e){return this.push(O,t,e)},patch:function(t,e){return this.push(I,t,e)},deserialize:function(t){var e=t;return this.parent?e=this.parent.deserialize(t):"string"==typeof e&&(e=JSON.parse(e)),this.realm.invoke("deserialize",e,e)},serialize:function(){var t=this.parent?this.parent.serialize():{};return this.realm.invoke("serialize",this.state,t)},toJSON:function(){return this.serialize()},checkout:function(t){return this.history.checkout(t),this},fork:function(){return new k({parent:this})}}),exports.default=k,exports.Microcosm=k,exports.Action=v,exports.History=i,exports.tag=e,exports.get=u,exports.set=c,exports.update=d,exports.merge=o,exports.inherit=h,exports.getRegistration=g; |
{ | ||
"name": "microcosm", | ||
"version": "12.1.0-rc", | ||
"version": "12.1.0-rc-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", |
@@ -24,3 +24,3 @@ # [![Microcosm](http://code.viget.com/microcosm/assets/microcosm.svg)](http://code.viget.com/microcosm/) | ||
// Domains define how a Microcosm should turn actions into new state | ||
repo.addDomain('counter', { | ||
repo.addDomain('users', { | ||
getInitialState () { | ||
@@ -27,0 +27,0 @@ return {} |
196070
1327