@tanstack/store
Advanced tools
Comparing version 0.0.1-beta.52 to 0.0.1-beta.55
@@ -15,37 +15,25 @@ /** | ||
var immer = require('immer'); | ||
immer.setAutoFreeze(false); | ||
// interface StoreOptions { | ||
// onSubscribe?: () => (() => void) | void | ||
// } | ||
class Store { | ||
listeners = new Set(); | ||
// options?: StoreOptions | ||
batching = false; | ||
queue = []; | ||
constructor(initialState | ||
// options?: StoreOptions | ||
) { | ||
constructor(initialState, options) { | ||
this.state = initialState; | ||
// this.options = options | ||
this.options = options; | ||
} | ||
subscribe = listener => { | ||
this.listeners.add(listener); | ||
// const unsub = this.options?.onSubscribe?.() | ||
const unsub = this.options?.onSubscribe?.(listener, this); | ||
return () => { | ||
this.listeners.delete(listener); | ||
// unsub?.() | ||
unsub?.(); | ||
}; | ||
}; | ||
setState = updater => { | ||
const previous = this.state; | ||
this.state = immer.produce(d => { | ||
updater(d); | ||
})(previous); | ||
this.queue.push(() => this.listeners.forEach(listener => listener(this.state, previous))); | ||
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous); | ||
this.queue.push(() => { | ||
this.listeners.forEach(listener => listener(this.state, previous)); | ||
this.options?.onUpdate?.(this.state, previous); | ||
}); | ||
this.#flush(); | ||
@@ -52,0 +40,0 @@ }; |
@@ -11,37 +11,25 @@ /** | ||
*/ | ||
import { setAutoFreeze, produce } from 'immer'; | ||
setAutoFreeze(false); | ||
// interface StoreOptions { | ||
// onSubscribe?: () => (() => void) | void | ||
// } | ||
class Store { | ||
listeners = new Set(); | ||
// options?: StoreOptions | ||
batching = false; | ||
queue = []; | ||
constructor(initialState | ||
// options?: StoreOptions | ||
) { | ||
constructor(initialState, options) { | ||
this.state = initialState; | ||
// this.options = options | ||
this.options = options; | ||
} | ||
subscribe = listener => { | ||
this.listeners.add(listener); | ||
// const unsub = this.options?.onSubscribe?.() | ||
const unsub = this.options?.onSubscribe?.(listener, this); | ||
return () => { | ||
this.listeners.delete(listener); | ||
// unsub?.() | ||
unsub?.(); | ||
}; | ||
}; | ||
setState = updater => { | ||
const previous = this.state; | ||
this.state = produce(d => { | ||
updater(d); | ||
})(previous); | ||
this.queue.push(() => this.listeners.forEach(listener => listener(this.state, previous))); | ||
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous); | ||
this.queue.push(() => { | ||
this.listeners.forEach(listener => listener(this.state, previous)); | ||
this.options?.onUpdate?.(this.state, previous); | ||
}); | ||
this.#flush(); | ||
@@ -48,0 +36,0 @@ }; |
@@ -10,8 +10,4 @@ { | ||
{ | ||
"name": "node_modules/.pnpm/immer@9.0.16/node_modules/immer/dist/immer.esm.mjs", | ||
"uid": "e407-5" | ||
}, | ||
{ | ||
"name": "packages/store/src/index.ts", | ||
"uid": "e407-7" | ||
"uid": "33c2-3" | ||
} | ||
@@ -24,38 +20,16 @@ ] | ||
"nodeParts": { | ||
"e407-5": { | ||
"renderedLength": 8203, | ||
"gzipLength": 3238, | ||
"33c2-3": { | ||
"renderedLength": 1030, | ||
"gzipLength": 375, | ||
"brotliLength": 0, | ||
"mainUid": "e407-4" | ||
}, | ||
"e407-7": { | ||
"renderedLength": 1067, | ||
"gzipLength": 410, | ||
"brotliLength": 0, | ||
"mainUid": "e407-6" | ||
"mainUid": "33c2-2" | ||
} | ||
}, | ||
"nodeMetas": { | ||
"e407-4": { | ||
"id": "/node_modules/.pnpm/immer@9.0.16/node_modules/immer/dist/immer.esm.mjs", | ||
"33c2-2": { | ||
"id": "/packages/store/src/index.ts", | ||
"moduleParts": { | ||
"index.production.js": "e407-5" | ||
"index.production.js": "33c2-3" | ||
}, | ||
"imported": [], | ||
"importedBy": [ | ||
{ | ||
"uid": "e407-6" | ||
} | ||
] | ||
}, | ||
"e407-6": { | ||
"id": "/packages/store/src/index.ts", | ||
"moduleParts": { | ||
"index.production.js": "e407-7" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "e407-4" | ||
} | ||
], | ||
"importedBy": [], | ||
@@ -62,0 +36,0 @@ "isEntry": true |
@@ -11,14 +11,22 @@ /** | ||
*/ | ||
declare class Store<TState> { | ||
type AnyUpdater = (...args: any[]) => any; | ||
type Listener<TState> = (next: TState, prev: TState) => void; | ||
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> { | ||
updateFn?: (previous: TState) => (updater: TUpdater) => TState; | ||
onSubscribe?: (listener: Listener<TState>, store: Store<TState, TUpdater>) => () => void; | ||
onUpdate?: (next: TState, prev: TState) => void; | ||
} | ||
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> { | ||
#private; | ||
listeners: Set<(next: TState, prev: TState) => void>; | ||
listeners: Set<Listener<TState>>; | ||
state: TState; | ||
options?: StoreOptions<TState, TUpdater>; | ||
batching: boolean; | ||
queue: ((...args: any[]) => void)[]; | ||
constructor(initialState: TState); | ||
subscribe: (listener: (next: TState, prev: TState) => void) => () => void; | ||
setState: (updater: (cb: TState) => void) => void; | ||
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>); | ||
subscribe: (listener: Listener<TState>) => () => void; | ||
setState: (updater: TUpdater) => void; | ||
batch: (cb: () => void) => void; | ||
} | ||
export { Store }; | ||
export { AnyUpdater, Listener, Store }; |
@@ -17,37 +17,25 @@ /** | ||
function n(n){for(var r=arguments.length,t=Array(r>1?r-1:0),e=1;e<r;e++)t[e-1]=arguments[e];{var i=Y[n],o=i?"function"==typeof i?i.apply(null,t):i:"unknown error nr: "+n;throw Error("[Immer] "+o)}}function r(n){return !!n&&!!n[Q]}function t(n){var r;return !!n&&(function(n){if(!n||"object"!=typeof n)return !1;var r=Object.getPrototypeOf(n);if(null===r)return !0;var t=Object.hasOwnProperty.call(r,"constructor")&&r.constructor;return t===Object||"function"==typeof t&&Function.toString.call(t)===Z}(n)||Array.isArray(n)||!!n[L]||!!(null===(r=n.constructor)||void 0===r?void 0:r[L])||s(n)||v(n))}function i(n,r,t){void 0===t&&(t=!1),0===o(n)?(t?Object.keys:nn)(n).forEach((function(e){t&&"symbol"==typeof e||r(e,n[e],n);})):n.forEach((function(t,e){return r(e,t,n)}));}function o(n){var r=n[Q];return r?r.i>3?r.i-4:r.i:Array.isArray(n)?1:s(n)?2:v(n)?3:0}function u(n,r){return 2===o(n)?n.has(r):Object.prototype.hasOwnProperty.call(n,r)}function a(n,r){return 2===o(n)?n.get(r):n[r]}function f(n,r,t){var e=o(n);2===e?n.set(r,t):3===e?(n.delete(r),n.add(t)):n[r]=t;}function c(n,r){return n===r?0!==n||1/n==1/r:n!=n&&r!=r}function s(n){return X&&n instanceof Map}function v(n){return q&&n instanceof Set}function p(n){return n.o||n.t}function l(n){if(Array.isArray(n))return Array.prototype.slice.call(n);var r=rn(n);delete r[Q];for(var t=nn(r),e=0;e<t.length;e++){var i=t[e],o=r[i];!1===o.writable&&(o.writable=!0,o.configurable=!0),(o.get||o.set)&&(r[i]={configurable:!0,writable:!0,enumerable:o.enumerable,value:n[i]});}return Object.create(Object.getPrototypeOf(n),r)}function d(n,e){return void 0===e&&(e=!1),y(n)||r(n)||!t(n)?n:(o(n)>1&&(n.set=n.add=n.clear=n.delete=h),Object.freeze(n),e&&i(n,(function(n,r){return d(r,!0)}),!0),n)}function h(){n(2);}function y(n){return null==n||"object"!=typeof n||Object.isFrozen(n)}function b(r){var t=tn[r];return t||n(18,r),t}function _(){return U||n(0),U}function j(n,r){r&&(b("Patches"),n.u=[],n.s=[],n.v=r);}function O(n){g(n),n.p.forEach(S),n.p=null;}function g(n){n===U&&(U=n.l);}function w(n){return U={p:[],l:U,h:n,m:!0,_:0}}function S(n){var r=n[Q];0===r.i||1===r.i?r.j():r.O=!0;}function P(r,e){e._=e.p.length;var i=e.p[0],o=void 0!==r&&r!==i;return e.h.g||b("ES5").S(e,r,o),o?(i[Q].P&&(O(e),n(4)),t(r)&&(r=M(e,r),e.l||x(e,r)),e.u&&b("Patches").M(i[Q].t,r,e.u,e.s)):r=M(e,i,[]),O(e),e.u&&e.v(e.u,e.s),r!==H?r:void 0}function M(n,r,t){if(y(r))return r;var e=r[Q];if(!e)return i(r,(function(i,o){return A(n,e,r,i,o,t)}),!0),r;if(e.A!==n)return r;if(!e.P)return x(n,e.t,!0),e.t;if(!e.I){e.I=!0,e.A._--;var o=4===e.i||5===e.i?e.o=l(e.k):e.o;i(3===e.i?new Set(o):o,(function(r,i){return A(n,e,o,r,i,t)})),x(n,o,!1),t&&n.u&&b("Patches").R(e,t,n.u,n.s);}return e.o}function A(e,i,o,a,c,s){if(c===o&&n(5),r(c)){var v=M(e,c,s&&i&&3!==i.i&&!u(i.D,a)?s.concat(a):void 0);if(f(o,a,v),!r(v))return;e.m=!1;}if(t(c)&&!y(c)){if(!e.h.F&&e._<1)return;M(e,c),i&&i.A.l||x(e,c);}}function x(n,r,t){void 0===t&&(t=!1),n.h.F&&n.m&&d(r,t);}function z(n,r){var t=n[Q];return (t?p(t):n)[r]}function I(n,r){if(r in n)for(var t=Object.getPrototypeOf(n);t;){var e=Object.getOwnPropertyDescriptor(t,r);if(e)return e;t=Object.getPrototypeOf(t);}}function k(n){n.P||(n.P=!0,n.l&&k(n.l));}function E(n){n.o||(n.o=l(n.t));}function R(n,r,t){var e=s(r)?b("MapSet").N(r,t):v(r)?b("MapSet").T(r,t):n.g?function(n,r){var t=Array.isArray(n),e={i:t?1:0,A:r?r.A:_(),P:!1,I:!1,D:{},l:r,t:n,k:null,o:null,j:null,C:!1},i=e,o=en;t&&(i=[e],o=on);var u=Proxy.revocable(i,o),a=u.revoke,f=u.proxy;return e.k=f,e.j=a,f}(r,t):b("ES5").J(r,t);return (t?t.A:_()).p.push(e),e}function D(e){return r(e)||n(22,e),function n(r){if(!t(r))return r;var e,u=r[Q],c=o(r);if(u){if(!u.P&&(u.i<4||!b("ES5").K(u)))return u.t;u.I=!0,e=F(r,c),u.I=!1;}else e=F(r,c);return i(e,(function(r,t){u&&a(u.t,r)===t||f(e,r,n(t));})),3===c?new Set(e):e}(e)}function F(n,r){switch(r){case 2:return new Map(n);case 3:return Array.from(n)}return l(n)}var G,U,W="undefined"!=typeof Symbol&&"symbol"==typeof Symbol("x"),X="undefined"!=typeof Map,q="undefined"!=typeof Set,B="undefined"!=typeof Proxy&&void 0!==Proxy.revocable&&"undefined"!=typeof Reflect,H=W?Symbol.for("immer-nothing"):((G={})["immer-nothing"]=!0,G),L=W?Symbol.for("immer-draftable"):"__$immer_draftable",Q=W?Symbol.for("immer-state"):"__$immer_state",Y={0:"Illegal state",1:"Immer drafts cannot have computed properties",2:"This object has been frozen and should not be mutated",3:function(n){return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+n},4:"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",5:"Immer forbids circular references",6:"The first or second argument to `produce` must be a function",7:"The third argument to `produce` must be a function or undefined",8:"First argument to `createDraft` must be a plain object, an array, or an immerable object",9:"First argument to `finishDraft` must be a draft returned by `createDraft`",10:"The given draft is already finalized",11:"Object.defineProperty() cannot be used on an Immer draft",12:"Object.setPrototypeOf() cannot be used on an Immer draft",13:"Immer only supports deleting array indices",14:"Immer only supports setting array indices and the 'length' property",15:function(n){return "Cannot apply patch, path doesn't resolve: "+n},16:'Sets cannot have "replace" patches.',17:function(n){return "Unsupported patch operation: "+n},18:function(n){return "The plugin for '"+n+"' has not been loaded into Immer. To enable the plugin, import and call `enable"+n+"()` when initializing your application."},20:"Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",21:function(n){return "produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '"+n+"'"},22:function(n){return "'current' expects a draft, got: "+n},23:function(n){return "'original' expects a draft, got: "+n},24:"Patching reserved attributes like __proto__, prototype and constructor is not allowed"},Z=""+Object.prototype.constructor,nn="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(n){return Object.getOwnPropertyNames(n).concat(Object.getOwnPropertySymbols(n))}:Object.getOwnPropertyNames,rn=Object.getOwnPropertyDescriptors||function(n){var r={};return nn(n).forEach((function(t){r[t]=Object.getOwnPropertyDescriptor(n,t);})),r},tn={},en={get:function(n,r){if(r===Q)return n;var e=p(n);if(!u(e,r))return function(n,r,t){var e,i=I(r,t);return i?"value"in i?i.value:null===(e=i.get)||void 0===e?void 0:e.call(n.k):void 0}(n,e,r);var i=e[r];return n.I||!t(i)?i:i===z(n.t,r)?(E(n),n.o[r]=R(n.A.h,i,n)):i},has:function(n,r){return r in p(n)},ownKeys:function(n){return Reflect.ownKeys(p(n))},set:function(n,r,t){var e=I(p(n),r);if(null==e?void 0:e.set)return e.set.call(n.k,t),!0;if(!n.P){var i=z(p(n),r),o=null==i?void 0:i[Q];if(o&&o.t===t)return n.o[r]=t,n.D[r]=!1,!0;if(c(t,i)&&(void 0!==t||u(n.t,r)))return !0;E(n),k(n);}return n.o[r]===t&&"number"!=typeof t&&(void 0!==t||r in n.o)||(n.o[r]=t,n.D[r]=!0,!0)},deleteProperty:function(n,r){return void 0!==z(n.t,r)||r in n.t?(n.D[r]=!1,E(n),k(n)):delete n.D[r],n.o&&delete n.o[r],!0},getOwnPropertyDescriptor:function(n,r){var t=p(n),e=Reflect.getOwnPropertyDescriptor(t,r);return e?{writable:!0,configurable:1!==n.i||"length"!==r,enumerable:e.enumerable,value:t[r]}:e},defineProperty:function(){n(11);},getPrototypeOf:function(n){return Object.getPrototypeOf(n.t)},setPrototypeOf:function(){n(12);}},on={};i(en,(function(n,r){on[n]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)};})),on.deleteProperty=function(r,t){return isNaN(parseInt(t))&&n(13),on.set.call(this,r,t,void 0)},on.set=function(r,t,e){return "length"!==t&&isNaN(parseInt(t))&&n(14),en.set.call(this,r[0],t,e,r[0])};var un=function(){function e(r){var e=this;this.g=B,this.F=!0,this.produce=function(r,i,o){if("function"==typeof r&&"function"!=typeof i){var u=i;i=r;var a=e;return function(n){var r=this;void 0===n&&(n=u);for(var t=arguments.length,e=Array(t>1?t-1:0),o=1;o<t;o++)e[o-1]=arguments[o];return a.produce(n,(function(n){var t;return (t=i).call.apply(t,[r,n].concat(e))}))}}var f;if("function"!=typeof i&&n(6),void 0!==o&&"function"!=typeof o&&n(7),t(r)){var c=w(e),s=R(e,r,void 0),v=!0;try{f=i(s),v=!1;}finally{v?O(c):g(c);}return "undefined"!=typeof Promise&&f instanceof Promise?f.then((function(n){return j(c,o),P(n,c)}),(function(n){throw O(c),n})):(j(c,o),P(f,c))}if(!r||"object"!=typeof r){if(void 0===(f=i(r))&&(f=r),f===H&&(f=void 0),e.F&&d(f,!0),o){var p=[],l=[];b("Patches").M(r,f,p,l),o(p,l);}return f}n(21,r);},this.produceWithPatches=function(n,r){if("function"==typeof n)return function(r){for(var t=arguments.length,i=Array(t>1?t-1:0),o=1;o<t;o++)i[o-1]=arguments[o];return e.produceWithPatches(r,(function(r){return n.apply(void 0,[r].concat(i))}))};var t,i,o=e.produce(n,r,(function(n,r){t=n,i=r;}));return "undefined"!=typeof Promise&&o instanceof Promise?o.then((function(n){return [n,t,i]})):[o,t,i]},"boolean"==typeof(null==r?void 0:r.useProxies)&&this.setUseProxies(r.useProxies),"boolean"==typeof(null==r?void 0:r.autoFreeze)&&this.setAutoFreeze(r.autoFreeze);}var i=e.prototype;return i.createDraft=function(e){t(e)||n(8),r(e)&&(e=D(e));var i=w(this),o=R(this,e,void 0);return o[Q].C=!0,g(i),o},i.finishDraft=function(r,t){var e=r&&r[Q];(e&&e.C||n(9),e.I&&n(10));var i=e.A;return j(i,t),P(void 0,i)},i.setAutoFreeze=function(n){this.F=n;},i.setUseProxies=function(r){r&&!B&&n(20),this.g=r;},i.applyPatches=function(n,t){var e;for(e=t.length-1;e>=0;e--){var i=t[e];if(0===i.path.length&&"replace"===i.op){n=i.value;break}}e>-1&&(t=t.slice(e+1));var o=b("Patches").$;return r(n)?o(n,t):this.produce(n,(function(n){return o(n,t)}))},e}(),an=new un,fn=an.produce;an.produceWithPatches.bind(an);var sn=an.setAutoFreeze.bind(an);an.setUseProxies.bind(an);an.applyPatches.bind(an);an.createDraft.bind(an);an.finishDraft.bind(an); | ||
sn(false); | ||
// interface StoreOptions { | ||
// onSubscribe?: () => (() => void) | void | ||
// } | ||
class Store { | ||
listeners = new Set(); | ||
// options?: StoreOptions | ||
batching = false; | ||
queue = []; | ||
constructor(initialState | ||
// options?: StoreOptions | ||
) { | ||
constructor(initialState, options) { | ||
this.state = initialState; | ||
// this.options = options | ||
this.options = options; | ||
} | ||
subscribe = listener => { | ||
this.listeners.add(listener); | ||
// const unsub = this.options?.onSubscribe?.() | ||
const unsub = this.options?.onSubscribe?.(listener, this); | ||
return () => { | ||
this.listeners.delete(listener); | ||
// unsub?.() | ||
unsub?.(); | ||
}; | ||
}; | ||
setState = updater => { | ||
const previous = this.state; | ||
this.state = fn(d => { | ||
updater(d); | ||
})(previous); | ||
this.queue.push(() => this.listeners.forEach(listener => listener(this.state, previous))); | ||
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous); | ||
this.queue.push(() => { | ||
this.listeners.forEach(listener => listener(this.state, previous)); | ||
this.options?.onUpdate?.(this.state, previous); | ||
}); | ||
this.#flush(); | ||
@@ -54,0 +42,0 @@ }; |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Store={})}(this,(function(t){"use strict";function e(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];throw Error("[Immer] minified error nr: "+t+(r.length?" "+r.map((function(t){return"'"+t+"'"})).join(","):"")+". Find the full error at: https://bit.ly/3cXEKWf")}function r(t){return!!t&&!!t[U]}function n(t){var e;return!!t&&(function(t){if(!t||"object"!=typeof t)return!1;var e=Object.getPrototypeOf(t);if(null===e)return!0;var r=Object.hasOwnProperty.call(e,"constructor")&&e.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===$}(t)||Array.isArray(t)||!!t[T]||!!(null===(e=t.constructor)||void 0===e?void 0:e[T])||c(t)||a(t))}function o(t,e,r){void 0===r&&(r=!1),0===i(t)?(r?Object.keys:C)(t).forEach((function(n){r&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach((function(r,n){return e(n,r,t)}))}function i(t){var e=t[U];return e?e.i>3?e.i-4:e.i:Array.isArray(t)?1:c(t)?2:a(t)?3:0}function u(t,e){return 2===i(t)?t.has(e):Object.prototype.hasOwnProperty.call(t,e)}function f(t,e,r){var n=i(t);2===n?t.set(e,r):3===n?(t.delete(e),t.add(r)):t[e]=r}function c(t){return K&&t instanceof Map}function a(t){return q&&t instanceof Set}function s(t){return t.o||t.t}function l(t){if(Array.isArray(t))return Array.prototype.slice.call(t);var e=J(t);delete e[U];for(var r=C(e),n=0;n<r.length;n++){var o=r[n],i=e[o];!1===i.writable&&(i.writable=!0,i.configurable=!0),(i.get||i.set)&&(e[o]={configurable:!0,writable:!0,enumerable:i.enumerable,value:t[o]})}return Object.create(Object.getPrototypeOf(t),e)}function p(t,e){return void 0===e&&(e=!1),y(t)||r(t)||!n(t)||(i(t)>1&&(t.set=t.add=t.clear=t.delete=h),Object.freeze(t),e&&o(t,(function(t,e){return p(e,!0)}),!0)),t}function h(){e(2)}function y(t){return null==t||"object"!=typeof t||Object.isFrozen(t)}function d(t){var r=X[t];return r||e(18,t),r}function v(){return I}function b(t,e){e&&(d("Patches"),t.u=[],t.s=[],t.v=e)}function P(t){g(t),t.p.forEach(O),t.p=null}function g(t){t===I&&(I=t.l)}function m(t){return I={p:[],l:I,h:t,m:!0,_:0}}function O(t){var e=t[U];0===e.i||1===e.i?e.j():e.O=!0}function j(t,r){r._=r.p.length;var o=r.p[0],i=void 0!==t&&t!==o;return r.h.g||d("ES5").S(r,t,i),i?(o[U].P&&(P(r),e(4)),n(t)&&(t=w(r,t),r.l||S(r,t)),r.u&&d("Patches").M(o[U].t,t,r.u,r.s)):t=w(r,o,[]),P(r),r.u&&r.v(r.u,r.s),t!==N?t:void 0}function w(t,e,r){if(y(e))return e;var n=e[U];if(!n)return o(e,(function(o,i){return A(t,n,e,o,i,r)}),!0),e;if(n.A!==t)return e;if(!n.P)return S(t,n.t,!0),n.t;if(!n.I){n.I=!0,n.A._--;var i=4===n.i||5===n.i?n.o=l(n.k):n.o;o(3===n.i?new Set(i):i,(function(e,o){return A(t,n,i,e,o,r)})),S(t,i,!1),r&&t.u&&d("Patches").R(n,r,t.u,t.s)}return n.o}function A(t,e,o,i,c,a){if(r(c)){var s=w(t,c,a&&e&&3!==e.i&&!u(e.D,i)?a.concat(i):void 0);if(f(o,i,s),!r(s))return;t.m=!1}if(n(c)&&!y(c)){if(!t.h.F&&t._<1)return;w(t,c),e&&e.A.l||S(t,c)}}function S(t,e,r){void 0===r&&(r=!1),t.h.F&&t.m&&p(e,r)}function D(t,e){var r=t[U];return(r?s(r):t)[e]}function x(t,e){if(e in t)for(var r=Object.getPrototypeOf(t);r;){var n=Object.getOwnPropertyDescriptor(r,e);if(n)return n;r=Object.getPrototypeOf(r)}}function F(t){t.P||(t.P=!0,t.l&&F(t.l))}function _(t){t.o||(t.o=l(t.t))}function E(t,e,r){var n=c(e)?d("MapSet").N(e,r):a(e)?d("MapSet").T(e,r):t.g?function(t,e){var r=Array.isArray(t),n={i:r?1:0,A:e?e.A:v(),P:!1,I:!1,D:{},l:e,t:t,k:null,o:null,j:null,C:!1},o=n,i=B;r&&(o=[n],i=G);var u=Proxy.revocable(o,i),f=u.revoke,c=u.proxy;return n.k=c,n.j=f,c}(e,r):d("ES5").J(e,r);return(r?r.A:v()).p.push(n),n}function k(t){return r(t)||e(22,t),function t(e){if(!n(e))return e;var r,u=e[U],c=i(e);if(u){if(!u.P&&(u.i<4||!d("ES5").K(u)))return u.t;u.I=!0,r=M(e,c),u.I=!1}else r=M(e,c);return o(r,(function(e,n){u&&function(t,e){return 2===i(t)?t.get(e):t[e]}(u.t,e)===n||f(r,e,t(n))})),3===c?new Set(r):r}(t)}function M(t,e){switch(e){case 2:return new Map(t);case 3:return Array.from(t)}return l(t)}var z,I,R="undefined"!=typeof Symbol&&"symbol"==typeof Symbol("x"),K="undefined"!=typeof Map,q="undefined"!=typeof Set,W="undefined"!=typeof Proxy&&void 0!==Proxy.revocable&&"undefined"!=typeof Reflect,N=R?Symbol.for("immer-nothing"):((z={})["immer-nothing"]=!0,z),T=R?Symbol.for("immer-draftable"):"__$immer_draftable",U=R?Symbol.for("immer-state"):"__$immer_state",$=""+Object.prototype.constructor,C="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(t){return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t))}:Object.getOwnPropertyNames,J=Object.getOwnPropertyDescriptors||function(t){var e={};return C(t).forEach((function(r){e[r]=Object.getOwnPropertyDescriptor(t,r)})),e},X={},B={get:function(t,e){if(e===U)return t;var r=s(t);if(!u(r,e))return function(t,e,r){var n,o=x(e,r);return o?"value"in o?o.value:null===(n=o.get)||void 0===n?void 0:n.call(t.k):void 0}(t,r,e);var o=r[e];return t.I||!n(o)?o:o===D(t.t,e)?(_(t),t.o[e]=E(t.A.h,o,t)):o},has:function(t,e){return e in s(t)},ownKeys:function(t){return Reflect.ownKeys(s(t))},set:function(t,e,r){var n=x(s(t),e);if(null==n?void 0:n.set)return n.set.call(t.k,r),!0;if(!t.P){var o=D(s(t),e),i=null==o?void 0:o[U];if(i&&i.t===r)return t.o[e]=r,t.D[e]=!1,!0;if(function(t,e){return t===e?0!==t||1/t==1/e:t!=t&&e!=e}(r,o)&&(void 0!==r||u(t.t,e)))return!0;_(t),F(t)}return t.o[e]===r&&"number"!=typeof r&&(void 0!==r||e in t.o)||(t.o[e]=r,t.D[e]=!0,!0)},deleteProperty:function(t,e){return void 0!==D(t.t,e)||e in t.t?(t.D[e]=!1,_(t),F(t)):delete t.D[e],t.o&&delete t.o[e],!0},getOwnPropertyDescriptor:function(t,e){var r=s(t),n=Reflect.getOwnPropertyDescriptor(r,e);return n?{writable:!0,configurable:1!==t.i||"length"!==e,enumerable:n.enumerable,value:r[e]}:n},defineProperty:function(){e(11)},getPrototypeOf:function(t){return Object.getPrototypeOf(t.t)},setPrototypeOf:function(){e(12)}},G={};o(B,(function(t,e){G[t]=function(){return arguments[0]=arguments[0][0],e.apply(this,arguments)}})),G.deleteProperty=function(t,e){return G.set.call(this,t,e,void 0)},G.set=function(t,e,r){return B.set.call(this,t[0],e,r,t[0])};var H=function(){function t(t){var r=this;this.g=W,this.F=!0,this.produce=function(t,o,i){if("function"==typeof t&&"function"!=typeof o){var u=o;o=t;var f=r;return function(t){var e=this;void 0===t&&(t=u);for(var r=arguments.length,n=Array(r>1?r-1:0),i=1;i<r;i++)n[i-1]=arguments[i];return f.produce(t,(function(t){var r;return(r=o).call.apply(r,[e,t].concat(n))}))}}var c;if("function"!=typeof o&&e(6),void 0!==i&&"function"!=typeof i&&e(7),n(t)){var a=m(r),s=E(r,t,void 0),l=!0;try{c=o(s),l=!1}finally{l?P(a):g(a)}return"undefined"!=typeof Promise&&c instanceof Promise?c.then((function(t){return b(a,i),j(t,a)}),(function(t){throw P(a),t})):(b(a,i),j(c,a))}if(!t||"object"!=typeof t){if(void 0===(c=o(t))&&(c=t),c===N&&(c=void 0),r.F&&p(c,!0),i){var h=[],y=[];d("Patches").M(t,c,h,y),i(h,y)}return c}e(21,t)},this.produceWithPatches=function(t,e){if("function"==typeof t)return function(e){for(var n=arguments.length,o=Array(n>1?n-1:0),i=1;i<n;i++)o[i-1]=arguments[i];return r.produceWithPatches(e,(function(e){return t.apply(void 0,[e].concat(o))}))};var n,o,i=r.produce(t,e,(function(t,e){n=t,o=e}));return"undefined"!=typeof Promise&&i instanceof Promise?i.then((function(t){return[t,n,o]})):[i,n,o]},"boolean"==typeof(null==t?void 0:t.useProxies)&&this.setUseProxies(t.useProxies),"boolean"==typeof(null==t?void 0:t.autoFreeze)&&this.setAutoFreeze(t.autoFreeze)}var o=t.prototype;return o.createDraft=function(t){n(t)||e(8),r(t)&&(t=k(t));var o=m(this),i=E(this,t,void 0);return i[U].C=!0,g(o),i},o.finishDraft=function(t,e){var r=(t&&t[U]).A;return b(r,e),j(void 0,r)},o.setAutoFreeze=function(t){this.F=t},o.setUseProxies=function(t){t&&!W&&e(20),this.g=t},o.applyPatches=function(t,e){var n;for(n=e.length-1;n>=0;n--){var o=e[n];if(0===o.path.length&&"replace"===o.op){t=o.value;break}}n>-1&&(e=e.slice(n+1));var i=d("Patches").$;return r(t)?i(t,e):this.produce(t,(function(t){return i(t,e)}))},t}(),L=new H,Q=L.produce;L.produceWithPatches.bind(L);var V=L.setAutoFreeze.bind(L);L.setUseProxies.bind(L),L.applyPatches.bind(L),L.createDraft.bind(L),L.finishDraft.bind(L),V(!1);t.Store=class{listeners=new Set;batching=!1;queue=[];constructor(t){this.state=t}subscribe=t=>(this.listeners.add(t),()=>{this.listeners.delete(t)});setState=t=>{const e=this.state;this.state=Q((e=>{t(e)}))(e),this.queue.push((()=>this.listeners.forEach((t=>t(this.state,e))))),this.#t()};#t=()=>{this.batching||(this.queue.forEach((t=>t())),this.queue=[])};batch=t=>{this.batching=!0,t(),this.batching=!1,this.#t()}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Store={})}(this,(function(t){"use strict";t.Store=class{listeners=new Set;batching=!1;queue=[];constructor(t,e){this.state=t,this.options=e}subscribe=t=>{this.listeners.add(t);const e=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),e?.()}};setState=t=>{const e=this.state;this.state=this.options?.updateFn?this.options.updateFn(e)(t):t(e),this.queue.push((()=>{this.listeners.forEach((t=>t(this.state,e))),this.options?.onUpdate?.(this.state,e)})),this.#t()};#t=()=>{this.batching||(this.queue.forEach((t=>t())),this.queue=[])};batch=t=>{this.batching=!0,t(),this.batching=!1,this.#t()}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/store", | ||
"author": "Tanner Linsley", | ||
"version": "0.0.1-beta.52", | ||
"version": "0.0.1-beta.55", | ||
"license": "MIT", | ||
@@ -31,6 +31,3 @@ "repository": "tanstack/store", | ||
], | ||
"sideEffects": false, | ||
"dependencies": { | ||
"immer": "^9.0.15" | ||
} | ||
"sideEffects": false | ||
} |
@@ -1,42 +0,51 @@ | ||
import { produce, setAutoFreeze } from 'immer' | ||
export type AnyUpdater = (...args: any[]) => any | ||
setAutoFreeze(false) | ||
export type Listener<TState> = (next: TState, prev: TState) => void | ||
// interface StoreOptions { | ||
// onSubscribe?: () => (() => void) | void | ||
// } | ||
interface StoreOptions< | ||
TState, | ||
TUpdater extends AnyUpdater = (cb: TState) => TState, | ||
> { | ||
updateFn?: (previous: TState) => (updater: TUpdater) => TState | ||
onSubscribe?: ( | ||
listener: Listener<TState>, | ||
store: Store<TState, TUpdater>, | ||
) => () => void | ||
onUpdate?: (next: TState, prev: TState) => void | ||
} | ||
export class Store<TState> { | ||
listeners = new Set<(next: TState, prev: TState) => void>() | ||
export class Store< | ||
TState, | ||
TUpdater extends AnyUpdater = (cb: TState) => TState, | ||
> { | ||
listeners = new Set<Listener<TState>>() | ||
state: TState | ||
// options?: StoreOptions | ||
options?: StoreOptions<TState, TUpdater> | ||
batching = false | ||
queue: ((...args: any[]) => void)[] = [] | ||
constructor( | ||
initialState: TState, | ||
// options?: StoreOptions | ||
) { | ||
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) { | ||
this.state = initialState | ||
// this.options = options | ||
this.options = options | ||
} | ||
subscribe = (listener: (next: TState, prev: TState) => void) => { | ||
subscribe = (listener: Listener<TState>) => { | ||
this.listeners.add(listener) | ||
// const unsub = this.options?.onSubscribe?.() | ||
const unsub = this.options?.onSubscribe?.(listener, this) | ||
return () => { | ||
this.listeners.delete(listener) | ||
// unsub?.() | ||
unsub?.() | ||
} | ||
} | ||
setState = (updater: (cb: TState) => void) => { | ||
setState = (updater: TUpdater) => { | ||
const previous = this.state | ||
this.state = produce((d) => { | ||
updater(d) | ||
})(previous) | ||
this.state = this.options?.updateFn | ||
? this.options.updateFn(previous)(updater) | ||
: (updater as any)(previous) | ||
this.queue.push(() => | ||
this.listeners.forEach((listener) => listener(this.state, previous)), | ||
) | ||
this.queue.push(() => { | ||
this.listeners.forEach((listener) => listener(this.state, previous)) | ||
this.options?.onUpdate?.(this.state, previous) | ||
}) | ||
this.#flush() | ||
@@ -43,0 +52,0 @@ } |
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
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
0
159132
302
- Removedimmer@^9.0.15
- Removedimmer@9.0.21(transitive)