New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

microcosm

Package Overview
Dependencies
Maintainers
4
Versions
233
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microcosm - npm Package Compare versions

Comparing version 12.1.3 to 12.2.0-beta

6

CHANGELOG.md
# Changelog
## 12.1.3
## 12.2.0 (Beta)
- Fix issue where pushing actions inside of other actions would cause
a rollback to initial state.
- Added method for removing actions from history, history `append`
event.

@@ -8,0 +8,0 @@ ## 12.1.2

@@ -114,2 +114,197 @@ 'use strict';

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; };
var SPLITTER = '.';
function castPath(value) {
if (value == null) {
return [];
} else if (Array.isArray(value)) {
return value;
}
return typeof value === 'string' ? value.split(SPLITTER) : [value];
}
/**
* Shallow copy an object
*/
function clone(target) {
if (Array.isArray(target)) {
return target.slice(0);
} else if (isObject(target) === false) {
return target;
}
var copy = {};
for (var key in target) {
copy[key] = target[key];
}
return copy;
}
/**
* Merge any number of objects into a provided object.
*/
function merge() {
var copy = null;
var subject = null;
for (var i = 0, len = arguments.length; i < len; i++) {
copy = copy || arguments[i];
subject = subject || copy;
var next = arguments[i];
for (var key in next) {
if (copy[key] !== next[key]) {
if (copy === subject) {
copy = clone(subject);
}
copy[key] = next[key];
}
}
}
return copy;
}
function backfill(subject, properties) {
if (subject == null) {
return properties;
}
var copy = subject;
for (var key in properties) {
if (copy.hasOwnProperty(key) === false) {
if (copy === subject) {
copy = clone(subject);
}
copy[key] = properties[key];
}
}
return copy;
}
/**
* Basic prototypal inheritence
*/
function inherit(Child, Ancestor, proto) {
Child.__proto__ = Ancestor;
Child.prototype = merge(Object.create(Ancestor.prototype), {
constructor: Child
}, proto);
return Child;
}
/**
* Retrieve a value from an object. If no key is provided, just return the
* object.
*/
function get(object, key, fallback) {
if (object == null) {
return fallback;
}
var path = castPath(key);
for (var i = 0, len = path.length; i < len; i++) {
var value = object == null ? undefined : object[path[i]];
if (value === undefined) {
i = len;
value = fallback;
}
object = value;
}
return object;
}
/**
* Non-destructively assign a value to a provided object at a given key. If the
* value is the same, don't do anything. Otherwise return a new object.
*/
function set(object, key, value) {
// Ensure we're working with a key path, like: ['a', 'b', 'c']
var path = castPath(key);
var len = path.length;
if (len <= 0) {
return value;
}
if (get(object, path) === value) {
return object;
}
var root = clone(object);
var node = root;
// For each key in the path...
for (var i = 0; i < len; i++) {
var _key = path[i];
var next = value;
// Are we at the end?
if (i < len - 1) {
// No: Check to see if the key is already assigned,
if (_key in node) {
// If yes, clone that value
next = clone(node[_key]);
} else {
// Otherwise assign an object so that we can keep drilling down
next = {};
}
}
// Assign the value, then continue on to the next iteration of the loop
// using the next step down
node[_key] = next;
node = node[_key];
}
return root;
}
function isPromise(obj) {
var type = typeof obj === 'undefined' ? 'undefined' : _typeof(obj);
return !!obj && (type === 'object' || type === 'function') && typeof obj.then === 'function';
}
function isObject(target) {
return !!target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object';
}
function createOrClone(target, options, repo) {
if (typeof target === 'function') {
return new target(options, repo);
}
return Object.create(target);
}
/**
* A helper combination of get and set
*/
function update(state, path, fn, fallback) {
if (typeof fn !== 'function') {
return set(state, path, fn);
}
var last = get(state, path, fallback);
var next = fn(last);
return set(state, path, next);
}
var uid = 0;

@@ -206,9 +401,9 @@ var FALLBACK = '_action';

* 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.
*
*
* a Microcosm. Each node in the tree is an action. Branches are
* changes over time.
* @constructor
*/
function History(limit) {
Emitter.call(this);
function History(limit) {
this.ids = 0;

@@ -219,8 +414,15 @@ this.size = 0;

this.genesis = new Action(BIRTH, 'resolve', this);
this.append(START, 'resolve');
this.begin();
}
History.prototype = {
inherit(History, Emitter, {
/**
* Setup the head and root action for a history. This effectively
* starts or restarts history.
*/
begin: function begin() {
this.head = this.root = null;
this.append(START, 'resolve');
},
getId: function getId() {

@@ -258,16 +460,9 @@ return ++this.ids;

action.parent = this.size ? this.head : this.genesis;
if (this.size > 0) {
// To keep track of children, maintain a pointer to the first
// child ever produced. We might checkout another child later,
// so we can't use next
if (this.head.first) {
this.head.next.sibling = action;
} else {
this.head.first = action;
}
this.head.lead(action);
} else {
// Always have a parent node, no matter what
var birth = new Action(BIRTH, 'resolve', this);
birth.adopt(action);
this.head.next = action;
} else {
this.root = action;

@@ -279,6 +474,49 @@ }

this.invoke('createInitialSnapshot', action);
this._emit('append', action);
return this.head;
},
/**
* Remove an action from history, connecting adjacent actions
* together to bridge the gap.
* @param {Action} action - Action to remove from history
*/
remove: function remove(action) {
if (action.isDisconnected()) {
return;
}
var next = action.next;
var parent = action.parent;
this.clean(action);
if (this.size <= 0) {
this.begin();
return;
} else if (!next) {
next = this.head = parent;
} else if (action === this.root) {
this.root = next;
}
if (!action.disabled) {
this.reconcile(next);
}
},
/**
* The actual clean up operation that purges an action from both
* history, and removes all snapshots within tracking repos.
* @param {Action} action - Action to clean up
*/
clean: function clean(action) {
this.size -= 1;
this.invoke('clean', action);
action.remove();
},
reconcile: function reconcile(action) {

@@ -300,3 +538,3 @@

this.invoke('prepareRelease');
this.invoke('update');

@@ -311,3 +549,3 @@ this.invoke('release', action);

size -= 1;
this.invoke('removeSnapshot', root.parent);
this.invoke('clean', root.parent);
root = root.next;

@@ -339,3 +577,6 @@ }

// Toggle actions in bulk, then reconcile from the first action
/**
* Toggle actions in bulk, then reconcile from the first action
* @param {Action[]} - A list of actions to toggle
*/
toggle: function toggle(actions) {

@@ -367,200 +608,5 @@ var list = [].concat(actions);

}
};
});
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; };
var SPLITTER = '.';
function castPath(value) {
if (value == null) {
return [];
} else if (Array.isArray(value)) {
return value;
}
return typeof value === 'string' ? value.split(SPLITTER) : [value];
}
/**
* Shallow copy an object
*/
function clone(target) {
if (Array.isArray(target)) {
return target.slice(0);
} else if (isObject(target) === false) {
return target;
}
var copy = {};
for (var key in target) {
copy[key] = target[key];
}
return copy;
}
/**
* Merge any number of objects into a provided object.
*/
function merge() {
var copy = null;
var subject = null;
for (var i = 0, len = arguments.length; i < len; i++) {
copy = copy || arguments[i];
subject = subject || copy;
var next = arguments[i];
for (var key in next) {
if (copy[key] !== next[key]) {
if (copy === subject) {
copy = clone(subject);
}
copy[key] = next[key];
}
}
}
return copy;
}
function backfill(subject, properties) {
if (subject == null) {
return properties;
}
var copy = subject;
for (var key in properties) {
if (copy.hasOwnProperty(key) === false) {
if (copy === subject) {
copy = clone(subject);
}
copy[key] = properties[key];
}
}
return copy;
}
/**
* Basic prototypal inheritence
*/
function inherit(Child, Ancestor, proto) {
Child.__proto__ = Ancestor;
Child.prototype = merge(Object.create(Ancestor.prototype), {
constructor: Child
}, proto);
return Child;
}
/**
* Retrieve a value from an object. If no key is provided, just return the
* object.
*/
function get(object, key, fallback) {
if (object == null) {
return fallback;
}
var path = castPath(key);
for (var i = 0, len = path.length; i < len; i++) {
var value = object == null ? undefined : object[path[i]];
if (value === undefined) {
i = len;
value = fallback;
}
object = value;
}
return object;
}
/**
* Non-destructively assign a value to a provided object at a given key. If the
* value is the same, don't do anything. Otherwise return a new object.
*/
function set(object, key, value) {
// Ensure we're working with a key path, like: ['a', 'b', 'c']
var path = castPath(key);
var len = path.length;
if (len <= 0) {
return value;
}
if (get(object, path) === value) {
return object;
}
var root = clone(object);
var node = root;
// For each key in the path...
for (var i = 0; i < len; i++) {
var _key = path[i];
var next = value;
// Are we at the end?
if (i < len - 1) {
// No: Check to see if the key is already assigned,
if (_key in node) {
// If yes, clone that value
next = clone(node[_key]);
} else {
// Otherwise assign an object so that we can keep drilling down
next = {};
}
}
// Assign the value, then continue on to the next iteration of the loop
// using the next step down
node[_key] = next;
node = node[_key];
}
return root;
}
function isPromise(obj) {
var type = typeof obj === 'undefined' ? 'undefined' : _typeof(obj);
return !!obj && (type === 'object' || type === 'function') && typeof obj.then === 'function';
}
function isObject(target) {
return !!target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object';
}
function createOrClone(target, options, repo) {
if (typeof target === 'function') {
return new target(options, repo);
}
return Object.create(target);
}
/**
* A helper combination of get and set
*/
function update(state, path, fn, fallback) {
if (typeof fn !== 'function') {
return set(state, path, fn);
}
var last = get(state, path, fallback);
var next = fn(last);
return set(state, path, next);
}
/**
* Actions move through a specific set of states. This manifest

@@ -628,2 +674,3 @@ * controls how they should behave.

this.timestamp = Date.now();
this.children = [];

@@ -670,4 +717,82 @@ if (status) {

},
/**
* Remove the grandparent of this action, cutting off history.
*/
prune: function prune() {
this.parent.parent = null;
},
/**
* Set the next action after this one in the historical tree of
* actions.
* @param {?Action} child Action to follow this one
*/
lead: function lead(child) {
this.next = child;
if (child) {
this.adopt(child);
}
},
/**
* Add an action to the list of children
* @param {Action} child Action to include in child list
*/
adopt: function adopt(child) {
var index = this.children.indexOf(child);
if (index < 0) {
this.children.push(child);
}
child.parent = this;
},
/**
* Remove a child action
* @param {Action} child Action to remove
*/
abandon: function abandon(child) {
var index = this.children.indexOf(child);
if (index >= 0) {
this.children.splice(index, 1);
child.parent = null;
}
},
/**
* Connect the parent node to the next node. Removing this action
* from history.
*/
remove: function remove() {
/**
* If an action the oldest child of a parent, pass on the lead
* role to the next child.
*/
if (this.parent.next === this) {
this.parent.lead(this.next);
}
this.parent.abandon(this);
// Remove all relations
this.next = null;
},
/**
* Indicates if an action is currently connected within
* a history.
* @returns {Boolean} Is the action connected to a parent?
*/
isDisconnected: function isDisconnected() {
return this.parent == null;
}

@@ -722,19 +847,2 @@ });

/**
* Get all child actions. Used by the Microcosm debugger to visualize history.
*/
Object.defineProperty(Action.prototype, 'children', {
get: function get$$1() {
var children = [];
var node = this.first;
while (node) {
children.unshift(node);
node = node.sibling;
}
return children;
}
});
function MetaDomain(_, repo) {

@@ -772,2 +880,5 @@ this.repo = repo;

/**
* Support nesting, like:
*/
if (isObject(nest)) {

@@ -1053,35 +1164,10 @@ answer = nest[alias] || nest[status];

recall: function recall(action) {
return this.archive.get(action.id);
},
save: function save(action, state) {
/**
* Create the initial state snapshot for an action. This is
* important so that, when rolling back to this action, it always
* has a state value.
* @param {Action} action - The action to generate a snapshot for
*/
createInitialSnapshot: function createInitialSnapshot(action) {
var state = this.recall(action.parent);
this.updateSnapshot(action, state);
return this.archive.set(action.id, state);
},
clean: function clean(action) {
/**
* Update the state snapshot for a given action
* @param {Action} action - The action to update the snapshot for
*/
updateSnapshot: function updateSnapshot(action, state) {
this.archive.set(action.id, state);
},
/**
* Remove the snapshot for a given action
* @param {Action} action - The action to remove the snapshot for
*/
removeSnapshot: function removeSnapshot(action) {
this.archive.remove(action.id);

@@ -1123,7 +1209,7 @@ },

this.updateSnapshot(action, next);
this.save(action, next);
return this;
},
prepareRelease: function prepareRelease() {
update: function update$$1() {
var next = this.follower ? this.parent.state : this.recall(this.history.head);

@@ -1130,0 +1216,0 @@

@@ -1,1 +0,1 @@

"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}),z+=1;var n=e||(t.name||A)+"."+z;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(R,"resolve",this),this.append(E,"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(p(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],p=n;c<o-1&&(p=l in h?s(h[l]):{}),h[l]=p,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 p(t){return!!t&&"object"===(void 0===t?"undefined":P(t))}function f(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 m(t,e,n){var i=B[n],r=t[e];return p(r)?r[i]||r[n]:t[e[i]]}function g(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=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,s=n.payload,o=m(e.register(),i,r);o&&o.call(e,t,s)}}function S(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 x(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),this.dirty=!1,n&&this.reset(n,r)}Object.defineProperty(exports,"__esModule",{value:!0}),t.prototype={on:function(t,e,n,i){return 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 z=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"),R=function(){},E=function(){};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;i<n.length;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.invoke("createInitialSnapshot",n),this.head},reconcile:function(t){for(var e=t;e&&(this.invoke("reconcile",e),e!==this.head);)e=e.next;this.archive(),this.invoke("prepareRelease"),this.invoke("release",t)},archive:function(){for(var t=this.size,e=this.root;t>this.limit&&e.disposable;)t-=1,this.invoke("removeSnapshot",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=".",M={};M.inactive={disposable:!1,once:!0,listener:"onStart"},M.open={disposable:!1,once:!0,listener:"onOpen"},M.update={disposable:!1,once:!1,listener:"onUpdate"},M.resolve={disposable:!0,once:!0,listener:"onDone"},M.reject={disposable:!0,once:!0,listener:"onError"},M.cancel={disposable:!0,once:!0,listener:"onCancel"};var B={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=M[t].disposable},prune:function(){this.parent.parent=null}}),Object.keys(M).forEach(function(t){var e=M[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}},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 i=f(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.has(t)?this.pool[t]:null},has:function(t){return this.pool.hasOwnProperty(t)},set:function(t,e){this.pool[t]=e},remove:function(t){delete this.pool[t]}},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.id)},createInitialSnapshot:function(t){var e=this.recall(t.parent);this.updateSnapshot(t,e)},updateSnapshot:function(t,e){this.archive.set(t.id,e)},removeSnapshot:function(t){this.archive.remove(t.id)},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,p=u(t,a);i=c(i,a,l.call(h,p,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.updateSnapshot(t,e),this},prepareRelease:function(){var t=this.follower?this.parent.state:this.recall(this.history.head);this.dirty=t!==this.state,this.state=t},release:function(t){this.dirty&&(this.dirty=!1,this._emit("change",this.state)),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 x(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 S(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=m;
"use strict";function t(){this._events=null}function e(t){return null==t?[]:Array.isArray(t)?t:"string"==typeof t?t.split(A):[t]}function n(t){if(Array.isArray(t))return t.slice(0);if(u(t)===!1)return t;var e={};for(var n in t)e[n]=t[n];return e}function i(){for(var t=null,e=null,i=0,r=arguments.length;i<r;i++){t=t||arguments[i],e=e||t;var s=arguments[i];for(var o in s)t[o]!==s[o]&&(t===e&&(t=n(e)),t[o]=s[o])}return t}function r(t,e){if(null==t)return e;var i=t;for(var r in e)i.hasOwnProperty(r)===!1&&(i===t&&(i=n(t)),i[r]=e[r]);return i}function s(t,e,n){return t.__proto__=e,t.prototype=i(Object.create(e.prototype),{constructor:t},n),t}function o(t,n,i){if(null==t)return i;for(var r=e(n),s=0,o=r.length;s<o;s++){var a=null==t?void 0:t[r[s]];void 0===a&&(s=o,a=i),t=a}return t}function a(t,i,r){var s=e(i),a=s.length;if(a<=0)return r;if(o(t,s)===r)return t;for(var h=n(t),u=h,c=0;c<a;c++){var l=s[c],f=r;c<a-1&&(f=l in u?n(u[l]):{}),u[l]=f,u=u[l]}return h}function h(t){var e=void 0===t?"undefined":S(t);return!!t&&("object"===e||"function"===e)&&"function"==typeof t.then}function u(t){return!!t&&"object"===(void 0===t?"undefined":S(t))}function c(t,e,n){return"function"==typeof t?new t(e,n):Object.create(t)}function l(t,e,n,i){return"function"!=typeof n?a(t,e,n):a(t,e,n(o(t,e,i)))}function f(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}),j+=1;var n=e||(t.name||O)+"."+j;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=D,t}function p(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 d(e){t.call(this),this.ids=0,this.size=0,this.limit=Math.max(1,e||1),this.repos=[],this.begin()}function v(e,n,i){t.call(this),this.history=i||new d,this.id=this.history.getId(),this.command=f(e),this.timestamp=Date.now(),this.children=[],n&&this.setStatus(n)}function y(t,e){this.repo=e}function m(t,e,n){var i=B[n],r=t[e];return u(r)?r[i]||r[n]:t[e[i]]}function g(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=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 x(t,e){return function(n){var i=n.command,r=n.status,s=n.payload,o=m(e.register(),i,r);o&&o.call(e,t,s)}}function _(t,e,n){var i=c(e,n,t);return i.setup&&i.setup(t,n),i.register&&t.on("effect",x(t,i)),i.teardown&&t.on("teardown",i.teardown,i),i}function k(t,e,n){return h(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(e,n,i){t.call(this),e=e||{},this.parent=e.parent||null,this.history=this.parent?this.parent.history:new d(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),this.dirty=!1,n&&this.reset(n,i)}Object.defineProperty(exports,"__esModule",{value:!0}),t.prototype={on:function(t,e,n,i){return 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="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},A=".",j=0,O="_action",D=function(){return this.done},E=f(function(t,e){return p(t,e)},"$reset"),I=f(function(t,e){return p(t,e)},"$patch"),R=function(){},P=function(){};s(d,t,{begin:function(){this.head=this.root=null,this.append(P,"resolve")},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;i<n.length;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 this.size>0?this.head.lead(n):(new v(R,"resolve",this).adopt(n),this.root=n),this.head=n,this.size+=1,this._emit("append",n),this.head},remove:function(t){if(!t.isDisconnected()){var e=t.next,n=t.parent;if(this.clean(t),this.size<=0)return void this.begin();e?t===this.root&&(this.root=e):e=this.head=n,t.disabled||this.reconcile(e)}},clean:function(t){this.size-=1,this.invoke("clean",t),t.remove()},reconcile:function(t){for(var e=t;e&&(this.invoke("reconcile",e),e!==this.head);)e=e.next;this.archive(),this.invoke("update"),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 M={};M.inactive={disposable:!1,once:!0,listener:"onStart"},M.open={disposable:!1,once:!0,listener:"onOpen"},M.update={disposable:!1,once:!1,listener:"onUpdate"},M.resolve={disposable:!0,once:!0,listener:"onDone"},M.reject={disposable:!0,once:!0,listener:"onError"},M.cancel={disposable:!0,once:!0,listener:"onCancel"};var B={inactive:"inactive",open:"open",update:"loading",loading:"update",done:"resolve",resolve:"done",reject:"error",error:"reject",cancel:"cancelled",cancelled:"cancel"};s(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=M[t].disposable},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},abandon:function(t){var e=this.children.indexOf(t);e>=0&&(this.children.splice(e,1),t.parent=null)},remove:function(){this.parent.next===this&&this.parent.lead(this.next),this.parent.abandon(this),this.next=null},isDisconnected:function(){return null==this.parent}}),Object.keys(M).forEach(function(t){var e=M[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}}),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[E]=this.reset,t[I]=this.patch,t}},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,n,i){var r=c(n,i,this.repo);return this.domains.push([e(t),r]),this.registry={},r.setup&&r.setup(this.repo,i),r.teardown&&this.repo.on("teardown",r.teardown,r),r},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]?a(n,i,r[t](o(e,i))):n},n)},prune:function(t,e){return this.reduce(function(t,n){var i=o(e,n);return void 0===i?t:a(t,n,i)},t)}},w.prototype={get:function(t){return this.has(t)?this.pool[t]:null},has:function(t){return this.pool.hasOwnProperty(t)},set:function(t,e){this.pool[t]=e},remove:function(t){delete this.pool[t]}},s(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.id)},save:function(t,e){return this.archive.set(t.id,e)},clean:function(t){this.archive.remove(t.id)},dispatch:function(t,e){for(var n=this.realm.register(e),i=t,r=0,s=n.length;r<s;r++){var h=n[r],u=h.key,c=h.domain,l=h.handler,f=o(t,u);i=a(i,u,l.call(c,f,e.payload))}return i},reconcile:function(t){if(this.follower)return this;var e=r(this.recall(t.parent),this.initial);return this.parent&&(e=i(e,this.parent.recall(t))),t.disabled||(e=this.dispatch(e,t)),this.save(t,e),this},update:function(){var t=this.follower?this.parent.state:this.recall(this.history.head);this.dirty=t!==this.state,this.state=t},release:function(t){this.dirty&&(this.dirty=!1,this._emit("change",this.state)),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=a(this.initial,t,i.getInitialState())),this.history.checkout(),i},addEffect:function(t,e){return _(this,t,e)},reset:function(t,e){return this.push(E,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 z({parent:this})}}),exports.default=z,exports.Microcosm=z,exports.Action=v,exports.History=d,exports.tag=f,exports.get=o,exports.set=a,exports.update=l,exports.merge=i,exports.inherit=s,exports.getRegistration=m;
{
"name": "microcosm",
"version": "12.1.3",
"version": "12.2.0-beta",
"description": "Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.",

@@ -5,0 +5,0 @@ "main": "microcosm.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc