Comparing version 0.0.3 to 0.0.4
474
dist/onek.js
@@ -1,2 +0,474 @@ | ||
var t=require("react"),i=Object.prototype.hasOwnProperty;function n(t){return t.constructor===Object||null===t.constructor}function r(t,r){if(t===r)return!0;if(Array.isArray(t)&&Array.isArray(r)){var s=t.length;if(s!==r.length)return!1;for(;s--;)if(t[s]!==r[s])return!1;return!0}if(t&&r&&"object"==typeof t&&"object"==typeof r&&n(t)&&n(r)){var o=Object.keys(t),u=o.length;if(Object.keys(r).length!==u)return!1;for(;u-- >0;){var h=o[u];if(!i.call(r,h)||t[h]!==r[h])return!1}return!0}if(t instanceof Set&&r instanceof Set){if(t.size!==r.size)return!1;var f=!0;return t.forEach(function(t){r.has(t)||(f=!1)}),f}if(t instanceof Map&&r instanceof Map){if(t.size!==r.size)return!1;var e=!0;return t.forEach(function(t,i){r.get(i)!==t&&(e=!1)}),e}return!1}var s=0,o=null,u=[],h=[],f=!1,e=[],c=function(t){Promise.resolve().then(t)},a=!0,v=null;function l(t){return function(){var i=o;o=null;try{return t.apply(this,arguments)}finally{o=i}}}function y(){!f&&(e.length||h.length||v)&&(f=!0,c(p))}function p(){try{v&&v();for(var t,i=100;e.length||h.length;){for(var n=void 0;n=h.pop();)n.t();for(;e.length&&--i;){var r=e;e=[],r.forEach(function(t){return t.i()})}if(!i)throw new Error("infinite reactions loop")}for(;t=u.pop();)t.o()}finally{f=!1,e=[]}}var d=/*#__PURE__*/function(){function t(t,i){this.u=t,this.h=new Set,this.v=i?"function"==typeof i?l(i):r:void 0}var i=t.prototype;return i.l=function(t){return!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t)},i.j=function(){var t=this;this.h.forEach(function(i){return i.j(3,t)}),!s&&y()},i.S=function(t){return void 0===t&&(t=o),t&&t.g(this),this.u},i.O=function(t,i){if(o&&o instanceof w)throw new Error("changing observable inside of computed");if(arguments.length>0){if("function"!=typeof t||i||(t=t(this.u)),this.v&&this.v(this.u,t))return;this.u=t}this.j()},t}(),w=/*#__PURE__*/function(){function t(t,i){this._=t,this.u=void 0,this.h=new Set,this.m=[],this.R=[],this.A=0,this.v=i?"function"==typeof i?l(i):r:void 0}var i=t.prototype;return i.g=function(t){t.l(this)&&this.m.push(t)},i.M=function(){var t=this;this.m.forEach(function(i){i.p(t)}),this.m=[],this.R=[]},i.l=function(t){return 1!==this.A&&this.t(),!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t),this.h.size||u.push(this)},i.q=function(t){var i=this;this.h.forEach(function(n){n.j(t,i)})},i.o=function(){this.h.size||0===this.A||this.P()},i.j=function(t,i){this.A>=t||(this.v?1===this.A&&this.q(2):this.q(t),this.A=t,2===t?this.R.push(i):this.M())},i.t=function(){if(2===this.A&&(this.A=1,this.R.forEach(function(t){t.t()}),this.R=[]),1!==this.A){var t=o,i=0!==this.A,n=a||t;o=n?this:null,this.A=4;try{var r=this._();if(this.A=n?1:0,i&&this.v){if(this.v(this.u,r))return;this.q(3)}this.u=r}catch(t){throw this.P(),t}finally{o=t}}},i.P=function(){this.A=0,this.u=void 0,this.M()},i.S=function(t){if(void 0===t&&(t=o),4===this.A)throw new Error("recursive computed call");return this.t(),t&&t.g(this),this.u},t}(),b=/*#__PURE__*/function(){function t(t,i){this._=t,this.k=i,this.m=[],this.B=void 0,this.C=!1,this.D=!0}var i=t.prototype;return i.g=function(t){t.l(this)&&this.m.push(t)},i.j=function(t,i){2===t?h.push(i):(this.F(),e.push(this))},i.F=function(){var t=this;this.m.forEach(function(i){i.p(t)}),this.m=[],this.B&&this.B(),this.B=void 0},i.i=function(){this.C||(this.k?(this.F(),this.k()):this.G())},i.P=function(){this.F(),this.C=!0},i.G=function(){this.F();var t=o;o=this,++s;try{this.C=!1,this.B=this._()}finally{o=t,--s||y()}},t}(),x=[];exports.action=function(t){return function(){var i=o;o=null,++s;try{return t.apply(this,arguments)}finally{o=i,--s||y()}}},exports.computed=function(t,i){var n=new w(t,i),r=n.S.bind(n);return r.$$computed=n,r.destroy=n.P.bind(n),r},exports.configure=function(t){void 0!==t.reactionRunner&&(c=t.reactionRunner),void 0!==t.cacheOnUntrackedRead&&(a=t.cacheOnUntrackedRead),void 0!==t.onTxEnd&&(v=t.onTxEnd)},exports.observable=function(t,i){var n=new d(t,i),r=n.S.bind(n),s=n.O.bind(n);return r.$$observable=n,[r,s]},exports.reaction=function(t,i){var n=new b(t,i),r=n.P.bind(n);return r.run=n.G.bind(n),n.G(),r},exports.shallowEquals=r,exports.tx=function(t){++s;try{t()}finally{--s||y()}},exports.useObserver=function(i){var n=t.useRef(i),r=t.useMemo(function(){var t={},i=new Set,r=null,s=new b(function(){r=null==n.current?void 0:n.current()},function(){t={},i.forEach(function(t){return t()})});return{H:function(t){return i.add(t),function(){i.delete(t),0===i.size&&s.F()}},I:function(){return t},J:function(){return r},K:s}},x),s=r.K;return t.useSyncExternalStore(r.H,r.I),i?(s.G(),r.J()):(s.F(),s)},exports.utx=function(t){var i=o;o=null,++s;try{return t()}finally{o=i,--s||y()}}; | ||
var react = require('react'); | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function isPlainObject(obj) { | ||
return obj.constructor === Object || obj.constructor === null; | ||
} | ||
function shallowEquals(prev, next) { | ||
if (prev === next) { | ||
return true; | ||
} | ||
if (Array.isArray(prev) && Array.isArray(next)) { | ||
var idx = prev.length; | ||
if (idx !== next.length) { | ||
return false; | ||
} | ||
while (idx--) { | ||
if (prev[idx] !== next[idx]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev && next && typeof prev === "object" && typeof next === "object" && isPlainObject(prev) && isPlainObject(next)) { | ||
var prevKeys = Object.keys(prev); | ||
var index = prevKeys.length; | ||
if (Object.keys(next).length !== index) { | ||
return false; | ||
} | ||
while (index-- > 0) { | ||
var key = prevKeys[index]; | ||
if (!hasOwnProperty.call(next, key) || prev[key] !== next[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev instanceof Set && next instanceof Set) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var result = true; | ||
prev.forEach(function (value) { | ||
if (!next.has(value)) { | ||
result = false; | ||
} | ||
}); | ||
return result; | ||
} | ||
if (prev instanceof Map && next instanceof Map) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var _result = true; | ||
prev.forEach(function (value, key) { | ||
if (next.get(key) !== value) { | ||
_result = false; | ||
} | ||
}); | ||
return _result; | ||
} | ||
return false; | ||
} | ||
var txDepth = 0; | ||
var subscriber = null; | ||
var subscriberChecks = []; | ||
var stateActualizationQueue = []; | ||
var reactionsScheduled = false; | ||
var reactionsQueue = []; | ||
var reactionsRunner = function reactionsRunner(runner) { | ||
Promise.resolve().then(runner); | ||
}; | ||
var cacheOnUntrackedRead = true; | ||
function configure(options) { | ||
if (options.reactionRunner !== undefined) { | ||
reactionsRunner = options.reactionRunner; | ||
} | ||
if (options.cacheOnUntrackedRead !== undefined) { | ||
cacheOnUntrackedRead = options.cacheOnUntrackedRead; | ||
} | ||
} | ||
function tx(fn) { | ||
++txDepth; | ||
try { | ||
fn(); | ||
} finally { | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function utx(fn) { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function untracked(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
}; | ||
} | ||
function action(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
} | ||
function endTx() { | ||
var shouldRunReactions = reactionsQueue.length || stateActualizationQueue.length; | ||
if (!reactionsScheduled && shouldRunReactions) { | ||
reactionsScheduled = true; | ||
reactionsRunner(runReactions); | ||
} | ||
} | ||
function runReactions() { | ||
try { | ||
var i = 1000; | ||
while (reactionsQueue.length || stateActualizationQueue.length) { | ||
var comp = void 0; | ||
while (comp = stateActualizationQueue.pop()) { | ||
comp._actualizeAndRecompute(); | ||
} | ||
while (reactionsQueue.length && --i) { | ||
var reactions = reactionsQueue; | ||
reactionsQueue = []; | ||
reactions.forEach(function (r) { | ||
return r._runManager(); | ||
}); | ||
} | ||
if (!i) { | ||
throw new Error("infinite reactions loop"); | ||
} | ||
} | ||
var _computed; | ||
while (_computed = subscriberChecks.pop()) { | ||
_computed._checkSubscribers(); | ||
} | ||
} finally { | ||
reactionsScheduled = false; | ||
reactionsQueue = []; | ||
} | ||
} | ||
var Observable = /*#__PURE__*/function () { | ||
function Observable(value, checkFn) { | ||
this._value = value; | ||
this._subscribers = new Set(); | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto = Observable.prototype; | ||
_proto._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
}; | ||
_proto._notify = function _notify() { | ||
var _this = this; | ||
this._subscribers.forEach(function (subs) { | ||
return subs._notify(3 /* State.DIRTY */, _this); | ||
}); | ||
!txDepth && endTx(); | ||
}; | ||
_proto._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
_proto._setValue = function _setValue(newValue, asIs) { | ||
if (subscriber && subscriber instanceof Computed) { | ||
throw new Error("changing observable inside of computed"); | ||
} | ||
if (arguments.length > 0) { | ||
if (typeof newValue === "function" && !asIs) { | ||
newValue = newValue(this._value); | ||
} | ||
if (this._checkFn && this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._value = newValue; | ||
} | ||
this._notify(); | ||
}; | ||
return Observable; | ||
}(); | ||
function observable(value, checkFn) { | ||
var obs = new Observable(value, checkFn); | ||
var get = obs._getValue.bind(obs); | ||
var set = obs._setValue.bind(obs); | ||
get.$$observable = obs; | ||
return [get, set]; | ||
} | ||
var Computed = /*#__PURE__*/function () { | ||
function Computed(fn, checkFn) { | ||
this._fn = fn; | ||
this._value = undefined; | ||
this._subscribers = new Set(); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto2 = Computed.prototype; | ||
_proto2._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto2._removeSubscriptions = function _removeSubscriptions() { | ||
var _this2 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this2); | ||
}); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
}; | ||
_proto2._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto2._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
if (!this._subscribers.size) { | ||
subscriberChecks.push(this); | ||
} | ||
}; | ||
_proto2._notifySubscribers = function _notifySubscribers(state) { | ||
var _this3 = this; | ||
this._subscribers.forEach(function (subs) { | ||
subs._notify(state, _this3); | ||
}); | ||
}; | ||
_proto2._checkSubscribers = function _checkSubscribers() { | ||
if (!this._subscribers.size && this._state !== 0 /* State.NOT_INITIALIZED */) { | ||
this._destroy(); | ||
} | ||
}; | ||
_proto2._notify = function _notify(state, subscription) { | ||
if (this._state >= state) return; | ||
if (this._checkFn) { | ||
if (this._state === 1 /* State.CLEAN */) this._notifySubscribers(2 /* State.MAYBE_DIRTY */); | ||
} else { | ||
this._notifySubscribers(state); | ||
} | ||
this._state = state; | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
this._subscriptionsToActualize.push(subscription); | ||
} else { | ||
this._removeSubscriptions(); | ||
} | ||
}; | ||
_proto2._actualizeAndRecompute = function _actualizeAndRecompute() { | ||
if (this._state === 2 /* State.MAYBE_DIRTY */) { | ||
this._state = 1 /* State.CLEAN */; | ||
this._subscriptionsToActualize.forEach(function (subs) { | ||
subs._actualizeAndRecompute(); | ||
}); | ||
this._subscriptionsToActualize = []; | ||
} | ||
if (this._state !== 1 /* State.CLEAN */) { | ||
var oldSubscriber = subscriber; | ||
var shouldCheck = this._state !== 0 /* State.NOT_INITIALIZED */; | ||
var shouldCache = cacheOnUntrackedRead || oldSubscriber; | ||
subscriber = shouldCache ? this : null; | ||
this._state = 4 /* State.COMPUTING */; | ||
try { | ||
var newValue = this._fn(); | ||
this._state = shouldCache ? 1 /* State.CLEAN */ : 0 /* State.NOT_INITIALIZED */; | ||
if (shouldCheck && this._checkFn) { | ||
if (this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._notifySubscribers(3 /* State.DIRTY */); | ||
} | ||
this._value = newValue; | ||
} catch (e) { | ||
this._destroy(); | ||
throw e; | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
} | ||
}; | ||
_proto2._destroy = function _destroy() { | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._value = undefined; | ||
this._removeSubscriptions(); | ||
}; | ||
_proto2._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (this._state === 4 /* State.COMPUTING */) { | ||
throw new Error("recursive computed call"); | ||
} | ||
this._actualizeAndRecompute(); | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
return Computed; | ||
}(); | ||
function computed(fn, checkFn) { | ||
var comp = new Computed(fn, checkFn); | ||
var get = comp._getValue.bind(comp); | ||
get.$$computed = comp; | ||
get.destroy = comp._destroy.bind(comp); | ||
return get; | ||
} | ||
var Reaction = /*#__PURE__*/function () { | ||
function Reaction(fn, manager) { | ||
this._fn = fn; | ||
this._manager = manager; | ||
this._subscriptions = []; | ||
this._destructor = undefined; | ||
this._isDestroyed = false; | ||
} | ||
var _proto3 = Reaction.prototype; | ||
_proto3._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto3._notify = function _notify(state, subscription) { | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
stateActualizationQueue.push(subscription); | ||
} else { | ||
this._unsubscribeAndRemove(); | ||
reactionsQueue.push(this); | ||
} | ||
}; | ||
_proto3._subscribe = function _subscribe() { | ||
var _this4 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._addSubscriber(_this4); | ||
}); | ||
}; | ||
_proto3._unsubscribe = function _unsubscribe() { | ||
var _this5 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this5); | ||
}); | ||
}; | ||
_proto3._unsubscribeAndRemove = function _unsubscribeAndRemove() { | ||
this._unsubscribe(); | ||
this._subscriptions = []; | ||
this._destructor && this._destructor(); | ||
this._destructor = undefined; | ||
}; | ||
_proto3._runManager = function _runManager() { | ||
if (!this._isDestroyed) { | ||
if (this._manager) { | ||
this._unsubscribeAndRemove(); | ||
this._manager(); | ||
} else { | ||
this._run(); | ||
} | ||
} | ||
}; | ||
_proto3._destroy = function _destroy() { | ||
this._unsubscribeAndRemove(); | ||
this._isDestroyed = true; | ||
}; | ||
_proto3._run = function _run() { | ||
this._unsubscribeAndRemove(); | ||
var oldSubscriber = subscriber; | ||
subscriber = this; | ||
++txDepth; | ||
try { | ||
this._isDestroyed = false; | ||
this._destructor = this._fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
return Reaction; | ||
}(); | ||
function reaction(fn, manager) { | ||
var r = new Reaction(fn, manager); | ||
var destructor = r._destroy.bind(r); | ||
destructor.run = r._run.bind(r); | ||
r._run(); | ||
return destructor; | ||
} | ||
var EMPTY_ARRAY = []; | ||
function useObserver(renderFn) { | ||
var renderFnRef = react.useRef(renderFn); | ||
var store = react.useMemo(function () { | ||
var revision = {}; | ||
var subscribers = new Set(); | ||
var renderResult = null; | ||
var reactionBody = function reactionBody() { | ||
renderResult = renderFnRef.current == null ? void 0 : renderFnRef.current(); | ||
}; | ||
var r = new Reaction(reactionBody, function () { | ||
revision = {}; | ||
subscribers.forEach(function (notify) { | ||
return notify(); | ||
}); | ||
}); | ||
return { | ||
_subscribe: function _subscribe(notify) { | ||
if (subscribers.size === 0) { | ||
r._subscribe(); | ||
} | ||
subscribers.add(notify); | ||
return function () { | ||
subscribers["delete"](notify); | ||
if (subscribers.size === 0) { | ||
r._unsubscribe(); | ||
} | ||
}; | ||
}, | ||
_getRevision: function _getRevision() { | ||
return revision; | ||
}, | ||
_getRenderResult: function _getRenderResult() { | ||
return renderResult; | ||
}, | ||
_reaction: r | ||
}; | ||
}, EMPTY_ARRAY); | ||
var r = store._reaction; | ||
react.useSyncExternalStore(store._subscribe, store._getRevision); | ||
if (renderFn) { | ||
r._run(); | ||
return store._getRenderResult(); | ||
} else { | ||
r._unsubscribeAndRemove(); | ||
return r; | ||
} | ||
} | ||
exports.action = action; | ||
exports.computed = computed; | ||
exports.configure = configure; | ||
exports.observable = observable; | ||
exports.reaction = reaction; | ||
exports.shallowEquals = shallowEquals; | ||
exports.tx = tx; | ||
exports.useObserver = useObserver; | ||
exports.utx = utx; | ||
//# sourceMappingURL=onek.js.map |
@@ -1,2 +0,466 @@ | ||
import{useRef as t,useMemo as i,useSyncExternalStore as n}from"react";var r=Object.prototype.hasOwnProperty;function s(t){return t.constructor===Object||null===t.constructor}function u(t,i){if(t===i)return!0;if(Array.isArray(t)&&Array.isArray(i)){var n=t.length;if(n!==i.length)return!1;for(;n--;)if(t[n]!==i[n])return!1;return!0}if(t&&i&&"object"==typeof t&&"object"==typeof i&&s(t)&&s(i)){var u=Object.keys(t),o=u.length;if(Object.keys(i).length!==o)return!1;for(;o-- >0;){var h=u[o];if(!r.call(i,h)||t[h]!==i[h])return!1}return!0}if(t instanceof Set&&i instanceof Set){if(t.size!==i.size)return!1;var f=!0;return t.forEach(function(t){i.has(t)||(f=!1)}),f}if(t instanceof Map&&i instanceof Map){if(t.size!==i.size)return!1;var e=!0;return t.forEach(function(t,n){i.get(n)!==t&&(e=!1)}),e}return!1}var o=0,h=null,f=[],e=[],c=!1,a=[],v=function(t){Promise.resolve().then(t)},l=!0,y=null;function d(t){void 0!==t.reactionRunner&&(v=t.reactionRunner),void 0!==t.cacheOnUntrackedRead&&(l=t.cacheOnUntrackedRead),void 0!==t.onTxEnd&&(y=t.onTxEnd)}function w(t){++o;try{t()}finally{--o||m()}}function p(t){var i=h;h=null,++o;try{return t()}finally{h=i,--o||m()}}function b(t){return function(){var i=h;h=null;try{return t.apply(this,arguments)}finally{h=i}}}function j(t){return function(){var i=h;h=null,++o;try{return t.apply(this,arguments)}finally{h=i,--o||m()}}}function m(){!c&&(a.length||e.length||y)&&(c=!0,v(S))}function S(){try{y&&y();for(var t,i=100;a.length||e.length;){for(var n=void 0;n=e.pop();)n.t();for(;a.length&&--i;){var r=a;a=[],r.forEach(function(t){return t.i()})}if(!i)throw new Error("infinite reactions loop")}for(;t=f.pop();)t.u()}finally{c=!1,a=[]}}var g=/*#__PURE__*/function(){function t(t,i){this.o=t,this.h=new Set,this.v=i?"function"==typeof i?b(i):u:void 0}var i=t.prototype;return i.l=function(t){return!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t)},i.j=function(){var t=this;this.h.forEach(function(i){return i.j(3,t)}),!o&&m()},i.m=function(t){return void 0===t&&(t=h),t&&t.S(this),this.o},i.g=function(t,i){if(h&&h instanceof _)throw new Error("changing observable inside of computed");if(arguments.length>0){if("function"!=typeof t||i||(t=t(this.o)),this.v&&this.v(this.o,t))return;this.o=t}this.j()},t}();function O(t,i){var n=new g(t,i),r=n.m.bind(n),s=n.g.bind(n);return r.$$observable=n,[r,s]}var _=/*#__PURE__*/function(){function t(t,i){this.O=t,this.o=void 0,this.h=new Set,this._=[],this.R=[],this.A=0,this.v=i?"function"==typeof i?b(i):u:void 0}var i=t.prototype;return i.S=function(t){t.l(this)&&this._.push(t)},i.M=function(){var t=this;this._.forEach(function(i){i.p(t)}),this._=[],this.R=[]},i.l=function(t){return 1!==this.A&&this.t(),!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t),this.h.size||f.push(this)},i.P=function(t){var i=this;this.h.forEach(function(n){n.j(t,i)})},i.u=function(){this.h.size||0===this.A||this.k()},i.j=function(t,i){this.A>=t||(this.v?1===this.A&&this.P(2):this.P(t),this.A=t,2===t?this.R.push(i):this.M())},i.t=function(){if(2===this.A&&(this.A=1,this.R.forEach(function(t){t.t()}),this.R=[]),1!==this.A){var t=h,i=0!==this.A,n=l||t;h=n?this:null,this.A=4;try{var r=this.O();if(this.A=n?1:0,i&&this.v){if(this.v(this.o,r))return;this.P(3)}this.o=r}catch(t){throw this.k(),t}finally{h=t}}},i.k=function(){this.A=0,this.o=void 0,this.M()},i.m=function(t){if(void 0===t&&(t=h),4===this.A)throw new Error("recursive computed call");return this.t(),t&&t.S(this),this.o},t}();function E(t,i){var n=new _(t,i),r=n.m.bind(n);return r.$$computed=n,r.destroy=n.k.bind(n),r}var R=/*#__PURE__*/function(){function t(t,i){this.O=t,this.q=i,this._=[],this.B=void 0,this.C=!1,this.D=!0}var i=t.prototype;return i.S=function(t){t.l(this)&&this._.push(t)},i.j=function(t,i){2===t?e.push(i):(this.F(),a.push(this))},i.F=function(){var t=this;this._.forEach(function(i){i.p(t)}),this._=[],this.B&&this.B(),this.B=void 0},i.i=function(){this.C||(this.q?(this.F(),this.q()):this.G())},i.k=function(){this.F(),this.C=!0},i.G=function(){this.F();var t=h;h=this,++o;try{this.C=!1,this.B=this.O()}finally{h=t,--o||m()}},t}();function A(t,i){var n=new R(t,i),r=n.k.bind(n);return r.run=n.G.bind(n),n.G(),r}var M=[];function x(r){var s=t(r),u=i(function(){var t={},i=new Set,n=null,r=new R(function(){n=null==s.current?void 0:s.current()},function(){t={},i.forEach(function(t){return t()})});return{H:function(t){return i.add(t),function(){i.delete(t),0===i.size&&r.F()}},I:function(){return t},J:function(){return n},K:r}},M),o=u.K;return n(u.H,u.I),r?(o.G(),u.J()):(o.F(),o)}export{j as action,E as computed,d as configure,O as observable,A as reaction,u as shallowEquals,w as tx,x as useObserver,p as utx}; | ||
import { useRef, useMemo, useSyncExternalStore } from 'react'; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function isPlainObject(obj) { | ||
return obj.constructor === Object || obj.constructor === null; | ||
} | ||
function shallowEquals(prev, next) { | ||
if (prev === next) { | ||
return true; | ||
} | ||
if (Array.isArray(prev) && Array.isArray(next)) { | ||
var idx = prev.length; | ||
if (idx !== next.length) { | ||
return false; | ||
} | ||
while (idx--) { | ||
if (prev[idx] !== next[idx]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev && next && typeof prev === "object" && typeof next === "object" && isPlainObject(prev) && isPlainObject(next)) { | ||
var prevKeys = Object.keys(prev); | ||
var index = prevKeys.length; | ||
if (Object.keys(next).length !== index) { | ||
return false; | ||
} | ||
while (index-- > 0) { | ||
var key = prevKeys[index]; | ||
if (!hasOwnProperty.call(next, key) || prev[key] !== next[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev instanceof Set && next instanceof Set) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var result = true; | ||
prev.forEach(function (value) { | ||
if (!next.has(value)) { | ||
result = false; | ||
} | ||
}); | ||
return result; | ||
} | ||
if (prev instanceof Map && next instanceof Map) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var _result = true; | ||
prev.forEach(function (value, key) { | ||
if (next.get(key) !== value) { | ||
_result = false; | ||
} | ||
}); | ||
return _result; | ||
} | ||
return false; | ||
} | ||
var txDepth = 0; | ||
var subscriber = null; | ||
var subscriberChecks = []; | ||
var stateActualizationQueue = []; | ||
var reactionsScheduled = false; | ||
var reactionsQueue = []; | ||
var reactionsRunner = function reactionsRunner(runner) { | ||
Promise.resolve().then(runner); | ||
}; | ||
var cacheOnUntrackedRead = true; | ||
function configure(options) { | ||
if (options.reactionRunner !== undefined) { | ||
reactionsRunner = options.reactionRunner; | ||
} | ||
if (options.cacheOnUntrackedRead !== undefined) { | ||
cacheOnUntrackedRead = options.cacheOnUntrackedRead; | ||
} | ||
} | ||
function tx(fn) { | ||
++txDepth; | ||
try { | ||
fn(); | ||
} finally { | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function utx(fn) { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function untracked(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
}; | ||
} | ||
function action(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
} | ||
function endTx() { | ||
var shouldRunReactions = reactionsQueue.length || stateActualizationQueue.length; | ||
if (!reactionsScheduled && shouldRunReactions) { | ||
reactionsScheduled = true; | ||
reactionsRunner(runReactions); | ||
} | ||
} | ||
function runReactions() { | ||
try { | ||
var i = 1000; | ||
while (reactionsQueue.length || stateActualizationQueue.length) { | ||
var comp = void 0; | ||
while (comp = stateActualizationQueue.pop()) { | ||
comp._actualizeAndRecompute(); | ||
} | ||
while (reactionsQueue.length && --i) { | ||
var reactions = reactionsQueue; | ||
reactionsQueue = []; | ||
reactions.forEach(function (r) { | ||
return r._runManager(); | ||
}); | ||
} | ||
if (!i) { | ||
throw new Error("infinite reactions loop"); | ||
} | ||
} | ||
var _computed; | ||
while (_computed = subscriberChecks.pop()) { | ||
_computed._checkSubscribers(); | ||
} | ||
} finally { | ||
reactionsScheduled = false; | ||
reactionsQueue = []; | ||
} | ||
} | ||
var Observable = /*#__PURE__*/function () { | ||
function Observable(value, checkFn) { | ||
this._value = value; | ||
this._subscribers = new Set(); | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto = Observable.prototype; | ||
_proto._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
}; | ||
_proto._notify = function _notify() { | ||
var _this = this; | ||
this._subscribers.forEach(function (subs) { | ||
return subs._notify(3 /* State.DIRTY */, _this); | ||
}); | ||
!txDepth && endTx(); | ||
}; | ||
_proto._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
_proto._setValue = function _setValue(newValue, asIs) { | ||
if (subscriber && subscriber instanceof Computed) { | ||
throw new Error("changing observable inside of computed"); | ||
} | ||
if (arguments.length > 0) { | ||
if (typeof newValue === "function" && !asIs) { | ||
newValue = newValue(this._value); | ||
} | ||
if (this._checkFn && this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._value = newValue; | ||
} | ||
this._notify(); | ||
}; | ||
return Observable; | ||
}(); | ||
function observable(value, checkFn) { | ||
var obs = new Observable(value, checkFn); | ||
var get = obs._getValue.bind(obs); | ||
var set = obs._setValue.bind(obs); | ||
get.$$observable = obs; | ||
return [get, set]; | ||
} | ||
var Computed = /*#__PURE__*/function () { | ||
function Computed(fn, checkFn) { | ||
this._fn = fn; | ||
this._value = undefined; | ||
this._subscribers = new Set(); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto2 = Computed.prototype; | ||
_proto2._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto2._removeSubscriptions = function _removeSubscriptions() { | ||
var _this2 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this2); | ||
}); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
}; | ||
_proto2._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto2._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
if (!this._subscribers.size) { | ||
subscriberChecks.push(this); | ||
} | ||
}; | ||
_proto2._notifySubscribers = function _notifySubscribers(state) { | ||
var _this3 = this; | ||
this._subscribers.forEach(function (subs) { | ||
subs._notify(state, _this3); | ||
}); | ||
}; | ||
_proto2._checkSubscribers = function _checkSubscribers() { | ||
if (!this._subscribers.size && this._state !== 0 /* State.NOT_INITIALIZED */) { | ||
this._destroy(); | ||
} | ||
}; | ||
_proto2._notify = function _notify(state, subscription) { | ||
if (this._state >= state) return; | ||
if (this._checkFn) { | ||
if (this._state === 1 /* State.CLEAN */) this._notifySubscribers(2 /* State.MAYBE_DIRTY */); | ||
} else { | ||
this._notifySubscribers(state); | ||
} | ||
this._state = state; | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
this._subscriptionsToActualize.push(subscription); | ||
} else { | ||
this._removeSubscriptions(); | ||
} | ||
}; | ||
_proto2._actualizeAndRecompute = function _actualizeAndRecompute() { | ||
if (this._state === 2 /* State.MAYBE_DIRTY */) { | ||
this._state = 1 /* State.CLEAN */; | ||
this._subscriptionsToActualize.forEach(function (subs) { | ||
subs._actualizeAndRecompute(); | ||
}); | ||
this._subscriptionsToActualize = []; | ||
} | ||
if (this._state !== 1 /* State.CLEAN */) { | ||
var oldSubscriber = subscriber; | ||
var shouldCheck = this._state !== 0 /* State.NOT_INITIALIZED */; | ||
var shouldCache = cacheOnUntrackedRead || oldSubscriber; | ||
subscriber = shouldCache ? this : null; | ||
this._state = 4 /* State.COMPUTING */; | ||
try { | ||
var newValue = this._fn(); | ||
this._state = shouldCache ? 1 /* State.CLEAN */ : 0 /* State.NOT_INITIALIZED */; | ||
if (shouldCheck && this._checkFn) { | ||
if (this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._notifySubscribers(3 /* State.DIRTY */); | ||
} | ||
this._value = newValue; | ||
} catch (e) { | ||
this._destroy(); | ||
throw e; | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
} | ||
}; | ||
_proto2._destroy = function _destroy() { | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._value = undefined; | ||
this._removeSubscriptions(); | ||
}; | ||
_proto2._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (this._state === 4 /* State.COMPUTING */) { | ||
throw new Error("recursive computed call"); | ||
} | ||
this._actualizeAndRecompute(); | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
return Computed; | ||
}(); | ||
function computed(fn, checkFn) { | ||
var comp = new Computed(fn, checkFn); | ||
var get = comp._getValue.bind(comp); | ||
get.$$computed = comp; | ||
get.destroy = comp._destroy.bind(comp); | ||
return get; | ||
} | ||
var Reaction = /*#__PURE__*/function () { | ||
function Reaction(fn, manager) { | ||
this._fn = fn; | ||
this._manager = manager; | ||
this._subscriptions = []; | ||
this._destructor = undefined; | ||
this._isDestroyed = false; | ||
} | ||
var _proto3 = Reaction.prototype; | ||
_proto3._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto3._notify = function _notify(state, subscription) { | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
stateActualizationQueue.push(subscription); | ||
} else { | ||
this._unsubscribeAndRemove(); | ||
reactionsQueue.push(this); | ||
} | ||
}; | ||
_proto3._subscribe = function _subscribe() { | ||
var _this4 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._addSubscriber(_this4); | ||
}); | ||
}; | ||
_proto3._unsubscribe = function _unsubscribe() { | ||
var _this5 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this5); | ||
}); | ||
}; | ||
_proto3._unsubscribeAndRemove = function _unsubscribeAndRemove() { | ||
this._unsubscribe(); | ||
this._subscriptions = []; | ||
this._destructor && this._destructor(); | ||
this._destructor = undefined; | ||
}; | ||
_proto3._runManager = function _runManager() { | ||
if (!this._isDestroyed) { | ||
if (this._manager) { | ||
this._unsubscribeAndRemove(); | ||
this._manager(); | ||
} else { | ||
this._run(); | ||
} | ||
} | ||
}; | ||
_proto3._destroy = function _destroy() { | ||
this._unsubscribeAndRemove(); | ||
this._isDestroyed = true; | ||
}; | ||
_proto3._run = function _run() { | ||
this._unsubscribeAndRemove(); | ||
var oldSubscriber = subscriber; | ||
subscriber = this; | ||
++txDepth; | ||
try { | ||
this._isDestroyed = false; | ||
this._destructor = this._fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
return Reaction; | ||
}(); | ||
function reaction(fn, manager) { | ||
var r = new Reaction(fn, manager); | ||
var destructor = r._destroy.bind(r); | ||
destructor.run = r._run.bind(r); | ||
r._run(); | ||
return destructor; | ||
} | ||
var EMPTY_ARRAY = []; | ||
function useObserver(renderFn) { | ||
var renderFnRef = useRef(renderFn); | ||
var store = useMemo(function () { | ||
var revision = {}; | ||
var subscribers = new Set(); | ||
var renderResult = null; | ||
var reactionBody = function reactionBody() { | ||
renderResult = renderFnRef.current == null ? void 0 : renderFnRef.current(); | ||
}; | ||
var r = new Reaction(reactionBody, function () { | ||
revision = {}; | ||
subscribers.forEach(function (notify) { | ||
return notify(); | ||
}); | ||
}); | ||
return { | ||
_subscribe: function _subscribe(notify) { | ||
if (subscribers.size === 0) { | ||
r._subscribe(); | ||
} | ||
subscribers.add(notify); | ||
return function () { | ||
subscribers["delete"](notify); | ||
if (subscribers.size === 0) { | ||
r._unsubscribe(); | ||
} | ||
}; | ||
}, | ||
_getRevision: function _getRevision() { | ||
return revision; | ||
}, | ||
_getRenderResult: function _getRenderResult() { | ||
return renderResult; | ||
}, | ||
_reaction: r | ||
}; | ||
}, EMPTY_ARRAY); | ||
var r = store._reaction; | ||
useSyncExternalStore(store._subscribe, store._getRevision); | ||
if (renderFn) { | ||
r._run(); | ||
return store._getRenderResult(); | ||
} else { | ||
r._unsubscribeAndRemove(); | ||
return r; | ||
} | ||
} | ||
export { action, computed, configure, observable, reaction, shallowEquals, tx, useObserver, utx }; | ||
//# sourceMappingURL=onek.module.js.map |
@@ -1,2 +0,479 @@ | ||
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],i):i((t||self).onek={},t.react)}(this,function(t,i){var n=Object.prototype.hasOwnProperty;function r(t){return t.constructor===Object||null===t.constructor}function o(t,i){if(t===i)return!0;if(Array.isArray(t)&&Array.isArray(i)){var o=t.length;if(o!==i.length)return!1;for(;o--;)if(t[o]!==i[o])return!1;return!0}if(t&&i&&"object"==typeof t&&"object"==typeof i&&r(t)&&r(i)){var s=Object.keys(t),u=s.length;if(Object.keys(i).length!==u)return!1;for(;u-- >0;){var f=s[u];if(!n.call(i,f)||t[f]!==i[f])return!1}return!0}if(t instanceof Set&&i instanceof Set){if(t.size!==i.size)return!1;var e=!0;return t.forEach(function(t){i.has(t)||(e=!1)}),e}if(t instanceof Map&&i instanceof Map){if(t.size!==i.size)return!1;var h=!0;return t.forEach(function(t,n){i.get(n)!==t&&(h=!1)}),h}return!1}var s=0,u=null,f=[],e=[],h=!1,c=[],a=function(t){Promise.resolve().then(t)},v=!0,l=null;function d(t){return function(){var i=u;u=null;try{return t.apply(this,arguments)}finally{u=i}}}function y(){!h&&(c.length||e.length||l)&&(h=!0,a(p))}function p(){try{l&&l();for(var t,i=100;c.length||e.length;){for(var n=void 0;n=e.pop();)n.t();for(;c.length&&--i;){var r=c;c=[],r.forEach(function(t){return t.i()})}if(!i)throw new Error("infinite reactions loop")}for(;t=f.pop();)t.o()}finally{h=!1,c=[]}}var w=/*#__PURE__*/function(){function t(t,i){this.u=t,this.h=new Set,this.v=i?"function"==typeof i?d(i):o:void 0}var i=t.prototype;return i.l=function(t){return!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t)},i.j=function(){var t=this;this.h.forEach(function(i){return i.j(3,t)}),!s&&y()},i.g=function(t){return void 0===t&&(t=u),t&&t.S(this),this.u},i.m=function(t,i){if(u&&u instanceof b)throw new Error("changing observable inside of computed");if(arguments.length>0){if("function"!=typeof t||i||(t=t(this.u)),this.v&&this.v(this.u,t))return;this.u=t}this.j()},t}(),b=/*#__PURE__*/function(){function t(t,i){this.O=t,this.u=void 0,this.h=new Set,this._=[],this.R=[],this.A=0,this.v=i?"function"==typeof i?d(i):o:void 0}var i=t.prototype;return i.S=function(t){t.l(this)&&this._.push(t)},i.M=function(){var t=this;this._.forEach(function(i){i.p(t)}),this._=[],this.R=[]},i.l=function(t){return 1!==this.A&&this.t(),!this.h.has(t)&&(this.h.add(t),!0)},i.p=function(t){this.h.delete(t),this.h.size||f.push(this)},i.T=function(t){var i=this;this.h.forEach(function(n){n.j(t,i)})},i.o=function(){this.h.size||0===this.A||this.q()},i.j=function(t,i){this.A>=t||(this.v?1===this.A&&this.T(2):this.T(t),this.A=t,2===t?this.R.push(i):this.M())},i.t=function(){if(2===this.A&&(this.A=1,this.R.forEach(function(t){t.t()}),this.R=[]),1!==this.A){var t=u,i=0!==this.A,n=v||t;u=n?this:null,this.A=4;try{var r=this.O();if(this.A=n?1:0,i&&this.v){if(this.v(this.u,r))return;this.T(3)}this.u=r}catch(t){throw this.q(),t}finally{u=t}}},i.q=function(){this.A=0,this.u=void 0,this.M()},i.g=function(t){if(void 0===t&&(t=u),4===this.A)throw new Error("recursive computed call");return this.t(),t&&t.S(this),this.u},t}(),j=/*#__PURE__*/function(){function t(t,i){this.O=t,this.P=i,this._=[],this.k=void 0,this.B=!1,this.C=!0}var i=t.prototype;return i.S=function(t){t.l(this)&&this._.push(t)},i.j=function(t,i){2===t?e.push(i):(this.D(),c.push(this))},i.D=function(){var t=this;this._.forEach(function(i){i.p(t)}),this._=[],this.k&&this.k(),this.k=void 0},i.i=function(){this.B||(this.P?(this.D(),this.P()):this.F())},i.q=function(){this.D(),this.B=!0},i.F=function(){this.D();var t=u;u=this,++s;try{this.B=!1,this.k=this.O()}finally{u=t,--s||y()}},t}(),g=[];t.action=function(t){return function(){var i=u;u=null,++s;try{return t.apply(this,arguments)}finally{u=i,--s||y()}}},t.computed=function(t,i){var n=new b(t,i),r=n.g.bind(n);return r.$$computed=n,r.destroy=n.q.bind(n),r},t.configure=function(t){void 0!==t.reactionRunner&&(a=t.reactionRunner),void 0!==t.cacheOnUntrackedRead&&(v=t.cacheOnUntrackedRead),void 0!==t.onTxEnd&&(l=t.onTxEnd)},t.observable=function(t,i){var n=new w(t,i),r=n.g.bind(n),o=n.m.bind(n);return r.$$observable=n,[r,o]},t.reaction=function(t,i){var n=new j(t,i),r=n.q.bind(n);return r.run=n.F.bind(n),n.F(),r},t.shallowEquals=o,t.tx=function(t){++s;try{t()}finally{--s||y()}},t.useObserver=function(t){var n=i.useRef(t),r=i.useMemo(function(){var t={},i=new Set,r=null,o=new j(function(){r=null==n.current?void 0:n.current()},function(){t={},i.forEach(function(t){return t()})});return{G:function(t){return i.add(t),function(){i.delete(t),0===i.size&&o.D()}},H:function(){return t},I:function(){return r},J:o}},g),o=r.J;return i.useSyncExternalStore(r.G,r.H),t?(o.F(),r.I()):(o.D(),o)},t.utx=function(t){var i=u;u=null,++s;try{return t()}finally{u=i,--s||y()}}}); | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) : | ||
(global = global || self, factory(global.onek = {}, global.react)); | ||
})(this, (function (exports, react) { | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function isPlainObject(obj) { | ||
return obj.constructor === Object || obj.constructor === null; | ||
} | ||
function shallowEquals(prev, next) { | ||
if (prev === next) { | ||
return true; | ||
} | ||
if (Array.isArray(prev) && Array.isArray(next)) { | ||
var idx = prev.length; | ||
if (idx !== next.length) { | ||
return false; | ||
} | ||
while (idx--) { | ||
if (prev[idx] !== next[idx]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev && next && typeof prev === "object" && typeof next === "object" && isPlainObject(prev) && isPlainObject(next)) { | ||
var prevKeys = Object.keys(prev); | ||
var index = prevKeys.length; | ||
if (Object.keys(next).length !== index) { | ||
return false; | ||
} | ||
while (index-- > 0) { | ||
var key = prevKeys[index]; | ||
if (!hasOwnProperty.call(next, key) || prev[key] !== next[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (prev instanceof Set && next instanceof Set) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var result = true; | ||
prev.forEach(function (value) { | ||
if (!next.has(value)) { | ||
result = false; | ||
} | ||
}); | ||
return result; | ||
} | ||
if (prev instanceof Map && next instanceof Map) { | ||
if (prev.size !== next.size) { | ||
return false; | ||
} | ||
var _result = true; | ||
prev.forEach(function (value, key) { | ||
if (next.get(key) !== value) { | ||
_result = false; | ||
} | ||
}); | ||
return _result; | ||
} | ||
return false; | ||
} | ||
var txDepth = 0; | ||
var subscriber = null; | ||
var subscriberChecks = []; | ||
var stateActualizationQueue = []; | ||
var reactionsScheduled = false; | ||
var reactionsQueue = []; | ||
var reactionsRunner = function reactionsRunner(runner) { | ||
Promise.resolve().then(runner); | ||
}; | ||
var cacheOnUntrackedRead = true; | ||
function configure(options) { | ||
if (options.reactionRunner !== undefined) { | ||
reactionsRunner = options.reactionRunner; | ||
} | ||
if (options.cacheOnUntrackedRead !== undefined) { | ||
cacheOnUntrackedRead = options.cacheOnUntrackedRead; | ||
} | ||
} | ||
function tx(fn) { | ||
++txDepth; | ||
try { | ||
fn(); | ||
} finally { | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function utx(fn) { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
} | ||
function untracked(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
}; | ||
} | ||
function action(fn) { | ||
return function () { | ||
var oldSubscriber = subscriber; | ||
subscriber = null; | ||
++txDepth; | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
} | ||
function endTx() { | ||
var shouldRunReactions = reactionsQueue.length || stateActualizationQueue.length; | ||
if (!reactionsScheduled && shouldRunReactions) { | ||
reactionsScheduled = true; | ||
reactionsRunner(runReactions); | ||
} | ||
} | ||
function runReactions() { | ||
try { | ||
var i = 1000; | ||
while (reactionsQueue.length || stateActualizationQueue.length) { | ||
var comp = void 0; | ||
while (comp = stateActualizationQueue.pop()) { | ||
comp._actualizeAndRecompute(); | ||
} | ||
while (reactionsQueue.length && --i) { | ||
var reactions = reactionsQueue; | ||
reactionsQueue = []; | ||
reactions.forEach(function (r) { | ||
return r._runManager(); | ||
}); | ||
} | ||
if (!i) { | ||
throw new Error("infinite reactions loop"); | ||
} | ||
} | ||
var _computed; | ||
while (_computed = subscriberChecks.pop()) { | ||
_computed._checkSubscribers(); | ||
} | ||
} finally { | ||
reactionsScheduled = false; | ||
reactionsQueue = []; | ||
} | ||
} | ||
var Observable = /*#__PURE__*/function () { | ||
function Observable(value, checkFn) { | ||
this._value = value; | ||
this._subscribers = new Set(); | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto = Observable.prototype; | ||
_proto._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
}; | ||
_proto._notify = function _notify() { | ||
var _this = this; | ||
this._subscribers.forEach(function (subs) { | ||
return subs._notify(3 /* State.DIRTY */, _this); | ||
}); | ||
!txDepth && endTx(); | ||
}; | ||
_proto._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
_proto._setValue = function _setValue(newValue, asIs) { | ||
if (subscriber && subscriber instanceof Computed) { | ||
throw new Error("changing observable inside of computed"); | ||
} | ||
if (arguments.length > 0) { | ||
if (typeof newValue === "function" && !asIs) { | ||
newValue = newValue(this._value); | ||
} | ||
if (this._checkFn && this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._value = newValue; | ||
} | ||
this._notify(); | ||
}; | ||
return Observable; | ||
}(); | ||
function observable(value, checkFn) { | ||
var obs = new Observable(value, checkFn); | ||
var get = obs._getValue.bind(obs); | ||
var set = obs._setValue.bind(obs); | ||
get.$$observable = obs; | ||
return [get, set]; | ||
} | ||
var Computed = /*#__PURE__*/function () { | ||
function Computed(fn, checkFn) { | ||
this._fn = fn; | ||
this._value = undefined; | ||
this._subscribers = new Set(); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._checkFn = checkFn ? typeof checkFn === "function" ? untracked(checkFn) : shallowEquals : undefined; | ||
} | ||
var _proto2 = Computed.prototype; | ||
_proto2._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto2._removeSubscriptions = function _removeSubscriptions() { | ||
var _this2 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this2); | ||
}); | ||
this._subscriptions = []; | ||
this._subscriptionsToActualize = []; | ||
}; | ||
_proto2._addSubscriber = function _addSubscriber(subscriber) { | ||
if (!this._subscribers.has(subscriber)) { | ||
this._subscribers.add(subscriber); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_proto2._removeSubscriber = function _removeSubscriber(subscriber) { | ||
this._subscribers["delete"](subscriber); | ||
if (!this._subscribers.size) { | ||
subscriberChecks.push(this); | ||
} | ||
}; | ||
_proto2._notifySubscribers = function _notifySubscribers(state) { | ||
var _this3 = this; | ||
this._subscribers.forEach(function (subs) { | ||
subs._notify(state, _this3); | ||
}); | ||
}; | ||
_proto2._checkSubscribers = function _checkSubscribers() { | ||
if (!this._subscribers.size && this._state !== 0 /* State.NOT_INITIALIZED */) { | ||
this._destroy(); | ||
} | ||
}; | ||
_proto2._notify = function _notify(state, subscription) { | ||
if (this._state >= state) return; | ||
if (this._checkFn) { | ||
if (this._state === 1 /* State.CLEAN */) this._notifySubscribers(2 /* State.MAYBE_DIRTY */); | ||
} else { | ||
this._notifySubscribers(state); | ||
} | ||
this._state = state; | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
this._subscriptionsToActualize.push(subscription); | ||
} else { | ||
this._removeSubscriptions(); | ||
} | ||
}; | ||
_proto2._actualizeAndRecompute = function _actualizeAndRecompute() { | ||
if (this._state === 2 /* State.MAYBE_DIRTY */) { | ||
this._state = 1 /* State.CLEAN */; | ||
this._subscriptionsToActualize.forEach(function (subs) { | ||
subs._actualizeAndRecompute(); | ||
}); | ||
this._subscriptionsToActualize = []; | ||
} | ||
if (this._state !== 1 /* State.CLEAN */) { | ||
var oldSubscriber = subscriber; | ||
var shouldCheck = this._state !== 0 /* State.NOT_INITIALIZED */; | ||
var shouldCache = cacheOnUntrackedRead || oldSubscriber; | ||
subscriber = shouldCache ? this : null; | ||
this._state = 4 /* State.COMPUTING */; | ||
try { | ||
var newValue = this._fn(); | ||
this._state = shouldCache ? 1 /* State.CLEAN */ : 0 /* State.NOT_INITIALIZED */; | ||
if (shouldCheck && this._checkFn) { | ||
if (this._checkFn(this._value, newValue)) { | ||
return; | ||
} | ||
this._notifySubscribers(3 /* State.DIRTY */); | ||
} | ||
this._value = newValue; | ||
} catch (e) { | ||
this._destroy(); | ||
throw e; | ||
} finally { | ||
subscriber = oldSubscriber; | ||
} | ||
} | ||
}; | ||
_proto2._destroy = function _destroy() { | ||
this._state = 0 /* State.NOT_INITIALIZED */; | ||
this._value = undefined; | ||
this._removeSubscriptions(); | ||
}; | ||
_proto2._getValue = function _getValue(_subscriber) { | ||
if (_subscriber === void 0) { | ||
_subscriber = subscriber; | ||
} | ||
if (this._state === 4 /* State.COMPUTING */) { | ||
throw new Error("recursive computed call"); | ||
} | ||
this._actualizeAndRecompute(); | ||
if (_subscriber) { | ||
_subscriber._addSubscription(this); | ||
} | ||
return this._value; | ||
}; | ||
return Computed; | ||
}(); | ||
function computed(fn, checkFn) { | ||
var comp = new Computed(fn, checkFn); | ||
var get = comp._getValue.bind(comp); | ||
get.$$computed = comp; | ||
get.destroy = comp._destroy.bind(comp); | ||
return get; | ||
} | ||
var Reaction = /*#__PURE__*/function () { | ||
function Reaction(fn, manager) { | ||
this._fn = fn; | ||
this._manager = manager; | ||
this._subscriptions = []; | ||
this._destructor = undefined; | ||
this._isDestroyed = false; | ||
} | ||
var _proto3 = Reaction.prototype; | ||
_proto3._addSubscription = function _addSubscription(subscription) { | ||
if (subscription._addSubscriber(this)) { | ||
this._subscriptions.push(subscription); | ||
} | ||
}; | ||
_proto3._notify = function _notify(state, subscription) { | ||
if (state === 2 /* State.MAYBE_DIRTY */) { | ||
stateActualizationQueue.push(subscription); | ||
} else { | ||
this._unsubscribeAndRemove(); | ||
reactionsQueue.push(this); | ||
} | ||
}; | ||
_proto3._subscribe = function _subscribe() { | ||
var _this4 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._addSubscriber(_this4); | ||
}); | ||
}; | ||
_proto3._unsubscribe = function _unsubscribe() { | ||
var _this5 = this; | ||
this._subscriptions.forEach(function (subs) { | ||
subs._removeSubscriber(_this5); | ||
}); | ||
}; | ||
_proto3._unsubscribeAndRemove = function _unsubscribeAndRemove() { | ||
this._unsubscribe(); | ||
this._subscriptions = []; | ||
this._destructor && this._destructor(); | ||
this._destructor = undefined; | ||
}; | ||
_proto3._runManager = function _runManager() { | ||
if (!this._isDestroyed) { | ||
if (this._manager) { | ||
this._unsubscribeAndRemove(); | ||
this._manager(); | ||
} else { | ||
this._run(); | ||
} | ||
} | ||
}; | ||
_proto3._destroy = function _destroy() { | ||
this._unsubscribeAndRemove(); | ||
this._isDestroyed = true; | ||
}; | ||
_proto3._run = function _run() { | ||
this._unsubscribeAndRemove(); | ||
var oldSubscriber = subscriber; | ||
subscriber = this; | ||
++txDepth; | ||
try { | ||
this._isDestroyed = false; | ||
this._destructor = this._fn(); | ||
} finally { | ||
subscriber = oldSubscriber; | ||
if (! --txDepth) endTx(); | ||
} | ||
}; | ||
return Reaction; | ||
}(); | ||
function reaction(fn, manager) { | ||
var r = new Reaction(fn, manager); | ||
var destructor = r._destroy.bind(r); | ||
destructor.run = r._run.bind(r); | ||
r._run(); | ||
return destructor; | ||
} | ||
var EMPTY_ARRAY = []; | ||
function useObserver(renderFn) { | ||
var renderFnRef = react.useRef(renderFn); | ||
var store = react.useMemo(function () { | ||
var revision = {}; | ||
var subscribers = new Set(); | ||
var renderResult = null; | ||
var reactionBody = function reactionBody() { | ||
renderResult = renderFnRef.current == null ? void 0 : renderFnRef.current(); | ||
}; | ||
var r = new Reaction(reactionBody, function () { | ||
revision = {}; | ||
subscribers.forEach(function (notify) { | ||
return notify(); | ||
}); | ||
}); | ||
return { | ||
_subscribe: function _subscribe(notify) { | ||
if (subscribers.size === 0) { | ||
r._subscribe(); | ||
} | ||
subscribers.add(notify); | ||
return function () { | ||
subscribers["delete"](notify); | ||
if (subscribers.size === 0) { | ||
r._unsubscribe(); | ||
} | ||
}; | ||
}, | ||
_getRevision: function _getRevision() { | ||
return revision; | ||
}, | ||
_getRenderResult: function _getRenderResult() { | ||
return renderResult; | ||
}, | ||
_reaction: r | ||
}; | ||
}, EMPTY_ARRAY); | ||
var r = store._reaction; | ||
react.useSyncExternalStore(store._subscribe, store._getRevision); | ||
if (renderFn) { | ||
r._run(); | ||
return store._getRenderResult(); | ||
} else { | ||
r._unsubscribeAndRemove(); | ||
return r; | ||
} | ||
} | ||
exports.action = action; | ||
exports.computed = computed; | ||
exports.configure = configure; | ||
exports.observable = observable; | ||
exports.reaction = reaction; | ||
exports.shallowEquals = shallowEquals; | ||
exports.tx = tx; | ||
exports.useObserver = useObserver; | ||
exports.utx = utx; | ||
})); | ||
//# sourceMappingURL=onek.umd.js.map |
{ | ||
"name": "onek", | ||
"version": "0.0.3", | ||
"description": "1KB full-featured state management inspired by MobX and Solid, batteries included ⚡️", | ||
"version": "0.0.4", | ||
"description": "1.6KB full-featured state management inspired by MobX and Solid, batteries included ⚡️", | ||
"main": "./dist/onek.js", | ||
@@ -21,3 +21,3 @@ "source": "src/index.ts", | ||
"scripts": { | ||
"build": "microbundle", | ||
"build": "microbundle --no-compress", | ||
"test": "jest" | ||
@@ -24,0 +24,0 @@ }, |
@@ -390,6 +390,16 @@ import { shallowEquals } from "./utils"; | ||
_unsubscribeAndRemove(): void { | ||
_subscribe(): void { | ||
this._subscriptions.forEach((subs) => { | ||
subs._addSubscriber(this); | ||
}); | ||
} | ||
_unsubscribe(): void { | ||
this._subscriptions.forEach((subs) => { | ||
subs._removeSubscriber(this); | ||
}); | ||
} | ||
_unsubscribeAndRemove(): void { | ||
this._unsubscribe(); | ||
this._subscriptions = []; | ||
@@ -396,0 +406,0 @@ this._destructor && this._destructor(); |
@@ -27,2 +27,6 @@ import { useMemo, useRef, useSyncExternalStore } from "react"; | ||
_subscribe(notify: () => void): () => void { | ||
if (subscribers.size === 0) { | ||
r._subscribe(); | ||
} | ||
subscribers.add(notify); | ||
@@ -34,3 +38,3 @@ | ||
if (subscribers.size === 0) { | ||
r._unsubscribeAndRemove(); | ||
r._unsubscribe(); | ||
} | ||
@@ -37,0 +41,0 @@ }; |
@@ -30,3 +30,2 @@ declare const enum State { | ||
cacheOnUntrackedRead?: boolean; | ||
onTxEnd?: () => void; | ||
}; | ||
@@ -77,6 +76,7 @@ declare function configure(options: any): void; | ||
_isDestroyed: boolean; | ||
_shouldSubscribe: boolean; | ||
constructor(fn: ReactionFn, manager?: () => void); | ||
_addSubscription(subscription: Subscription): void; | ||
_notify(state: State, subscription: Subscription): void; | ||
_subscribe(): void; | ||
_unsubscribe(): void; | ||
_unsubscribeAndRemove(): void; | ||
@@ -83,0 +83,0 @@ _runManager(): void; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
454158
2458
1
1