@tanstack/react-store
Advanced tools
Comparing version 0.0.1-beta.81 to 0.0.1-beta.84
@@ -15,11 +15,36 @@ /** | ||
var withSelector = require('use-sync-external-store/shim/with-selector'); | ||
var store = require('@tanstack/store'); | ||
var withSelector = require('use-sync-external-store/shim/with-selector'); | ||
function useStore(store$1, selector = d => d, compareShallow) { | ||
const slice = withSelector.useSyncExternalStoreWithSelector(store$1.subscribe, () => store$1.state, () => store$1.state, selector, compareShallow ? store.shallow : undefined); | ||
function useStore(store, selector = d => d) { | ||
const slice = withSelector.useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, shallow); | ||
return slice; | ||
} | ||
function shallow(objA, objB) { | ||
if (Object.is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
const keysA = Object.keys(objA); | ||
if (keysA.length !== Object.keys(objB).length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
exports.shallow = shallow; | ||
exports.useStore = useStore; | ||
Object.keys(store).forEach(function (k) { | ||
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, { | ||
enumerable: true, | ||
get: function () { return store[k]; } | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -11,11 +11,29 @@ /** | ||
*/ | ||
import { shallow } from '@tanstack/store'; | ||
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'; | ||
export * from '@tanstack/store'; | ||
function useStore(store, selector = d => d, compareShallow) { | ||
const slice = useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, compareShallow ? shallow : undefined); | ||
function useStore(store, selector = d => d) { | ||
const slice = useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, shallow); | ||
return slice; | ||
} | ||
function shallow(objA, objB) { | ||
if (Object.is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
const keysA = Object.keys(objA); | ||
if (keysA.length !== Object.keys(objB).length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
export { useStore }; | ||
export { shallow, useStore }; | ||
//# sourceMappingURL=index.js.map |
@@ -14,7 +14,7 @@ { | ||
"name": "store/build/esm/index.js", | ||
"uid": "298b-92" | ||
"uid": "5b7b-10" | ||
}, | ||
{ | ||
"name": "react-store/src/index.tsx", | ||
"uid": "298b-94" | ||
"uid": "5b7b-12" | ||
} | ||
@@ -29,20 +29,20 @@ ] | ||
"nodeParts": { | ||
"298b-92": { | ||
"renderedLength": 1310, | ||
"gzipLength": 488, | ||
"5b7b-10": { | ||
"renderedLength": 1288, | ||
"gzipLength": 497, | ||
"brotliLength": 0, | ||
"mainUid": "298b-91" | ||
"mainUid": "5b7b-9" | ||
}, | ||
"298b-94": { | ||
"renderedLength": 253, | ||
"gzipLength": 171, | ||
"5b7b-12": { | ||
"renderedLength": 754, | ||
"gzipLength": 343, | ||
"brotliLength": 0, | ||
"mainUid": "298b-93" | ||
"mainUid": "5b7b-11" | ||
} | ||
}, | ||
"nodeMetas": { | ||
"298b-91": { | ||
"5b7b-9": { | ||
"id": "/packages/store/build/esm/index.js", | ||
"moduleParts": { | ||
"index.production.js": "298b-92" | ||
"index.production.js": "5b7b-10" | ||
}, | ||
@@ -52,17 +52,17 @@ "imported": [], | ||
{ | ||
"uid": "298b-93" | ||
"uid": "5b7b-11" | ||
} | ||
] | ||
}, | ||
"298b-93": { | ||
"5b7b-11": { | ||
"id": "/packages/react-store/src/index.tsx", | ||
"moduleParts": { | ||
"index.production.js": "298b-94" | ||
"index.production.js": "5b7b-12" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "298b-91" | ||
"uid": "5b7b-13" | ||
}, | ||
{ | ||
"uid": "298b-95" | ||
"uid": "5b7b-9" | ||
} | ||
@@ -73,3 +73,3 @@ ], | ||
}, | ||
"298b-95": { | ||
"5b7b-13": { | ||
"id": "use-sync-external-store/shim/with-selector", | ||
@@ -80,3 +80,3 @@ "moduleParts": {}, | ||
{ | ||
"uid": "298b-93" | ||
"uid": "5b7b-11" | ||
} | ||
@@ -83,0 +83,0 @@ ], |
@@ -12,6 +12,8 @@ /** | ||
import { AnyUpdater, Store } from '@tanstack/store'; | ||
export * from '@tanstack/store'; | ||
type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
declare function useStore<TState, TSelected = NoInfer<TState>, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: NoInfer<TState>) => TSelected, compareShallow?: boolean): TSelected; | ||
declare function useStore<TState, TSelected = NoInfer<TState>, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: NoInfer<TState>) => TSelected): TSelected; | ||
declare function shallow<T>(objA: T, objB: T): boolean; | ||
export { NoInfer, useStore }; | ||
export { NoInfer, shallow, useStore }; |
@@ -27,2 +27,45 @@ /** | ||
*/ | ||
class Store { | ||
listeners = new Set(); | ||
batching = false; | ||
queue = []; | ||
constructor(initialState, options) { | ||
this.state = initialState; | ||
this.options = options; | ||
} | ||
subscribe = listener => { | ||
this.listeners.add(listener); | ||
const unsub = this.options?.onSubscribe?.(listener, this); | ||
return () => { | ||
this.listeners.delete(listener); | ||
unsub?.(); | ||
}; | ||
}; | ||
setState = updater => { | ||
const previous = this.state; | ||
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous); | ||
if (this.state === previous) return; | ||
this.options?.onUpdate?.(this.state, previous); | ||
this.queue.push(() => { | ||
this.listeners.forEach(listener => listener(this.state, previous)); | ||
}); | ||
this.#flush(); | ||
}; | ||
#flush = () => { | ||
if (this.batching) return; | ||
this.queue.forEach(cb => cb()); | ||
this.queue = []; | ||
}; | ||
batch = cb => { | ||
this.batching = true; | ||
cb(); | ||
this.batching = false; | ||
this.#flush(); | ||
}; | ||
} | ||
function useStore(store, selector = d => d) { | ||
const slice = withSelector.useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, shallow); | ||
return slice; | ||
} | ||
function shallow(objA, objB) { | ||
@@ -35,25 +78,2 @@ if (Object.is(objA, objB)) { | ||
} | ||
// if (objA instanceof Map && objB instanceof Map) { | ||
// if (objA.size !== objB.size) return false | ||
// for (const [key, value] of objA) { | ||
// if (!Object.is(value, objB.get(key))) { | ||
// return false | ||
// } | ||
// } | ||
// return true | ||
// } | ||
// if (objA instanceof Set && objB instanceof Set) { | ||
// if (objA.size !== objB.size) return false | ||
// for (const value of objA) { | ||
// if (!objB.has(value)) { | ||
// return false | ||
// } | ||
// } | ||
// return true | ||
// } | ||
const keysA = Object.keys(objA); | ||
@@ -71,7 +91,4 @@ if (keysA.length !== Object.keys(objB).length) { | ||
function useStore(store, selector = d => d, compareShallow) { | ||
const slice = withSelector.useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, compareShallow ? shallow : undefined); | ||
return slice; | ||
} | ||
exports.Store = Store; | ||
exports.shallow = shallow; | ||
exports.useStore = useStore; | ||
@@ -78,0 +95,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("use-sync-external-store/shim/with-selector")):"function"==typeof define&&define.amd?define(["exports","use-sync-external-store/shim/with-selector"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactLoaders={},e.withSelector)}(this,(function(e,t){"use strict"; | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("use-sync-external-store/shim/with-selector")):"function"==typeof define&&define.amd?define(["exports","use-sync-external-store/shim/with-selector"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ReactLoaders={},t.withSelector)}(this,(function(t,e){"use strict"; | ||
/** | ||
@@ -22,3 +22,3 @@ * store | ||
* @license MIT | ||
*/function n(e,t){if(Object.is(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const n=Object.keys(e);if(n.length!==Object.keys(t).length)return!1;for(let o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||!Object.is(e[n[o]],t[n[o]]))return!1;return!0}e.useStore=function(e,o=(e=>e),r){return t.useSyncExternalStoreWithSelector(e.subscribe,(()=>e.state),(()=>e.state),o,r?n:void 0)},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
*/function s(t,e){if(Object.is(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;const s=Object.keys(t);if(s.length!==Object.keys(e).length)return!1;for(let i=0;i<s.length;i++)if(!Object.prototype.hasOwnProperty.call(e,s[i])||!Object.is(t[s[i]],e[s[i]]))return!1;return!0}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.state!==e&&(this.options?.onUpdate?.(this.state,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()}},t.shallow=s,t.useStore=function(t,i=(t=>t)){return e.useSyncExternalStoreWithSelector(t.subscribe,(()=>t.state),(()=>t.state),i,s)},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/react-store", | ||
"author": "Tanner Linsley", | ||
"version": "0.0.1-beta.81", | ||
"version": "0.0.1-beta.84", | ||
"license": "MIT", | ||
@@ -39,3 +39,3 @@ "repository": "tanstack/react-store", | ||
"use-sync-external-store": "^1.2.0", | ||
"@tanstack/store": "0.0.1-beta.81" | ||
"@tanstack/store": "0.0.1-beta.84" | ||
}, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
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
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
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
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
163956
351
+ Added@tanstack/store@0.0.1-beta.84(transitive)
- Removed@tanstack/store@0.0.1-beta.81(transitive)