nanostores
Advanced tools
Comparing version 0.5.10 to 0.5.11
@@ -97,2 +97,3 @@ import { lastAction } from '../action/index.js' | ||
export declare let notifyStart:number | ||
/** | ||
@@ -99,0 +100,0 @@ * Create store with atomic value. It could be a string or an object, which you |
import { clean } from '../clean-stores/index.js' | ||
let listenerQueue = [] | ||
export let notifyId = 0 | ||
export let atom = initialValue => { | ||
@@ -21,5 +25,13 @@ let currentListeners | ||
currentListeners = nextListeners | ||
for (let listener of currentListeners) { | ||
listener(store.value, changedKey) | ||
let runListenerQueue = !listenerQueue.length | ||
for (let i = 0; i < currentListeners.length; i++) { | ||
listenerQueue.push(currentListeners[i], store.value, changedKey) | ||
} | ||
if (runListenerQueue) { | ||
notifyId++ | ||
for (let i = 0; i < listenerQueue.length; i += 3) { | ||
listenerQueue[i](listenerQueue[i + 1], listenerQueue[i + 2]) | ||
} | ||
listenerQueue.length = 0 | ||
} | ||
}, | ||
@@ -48,3 +60,4 @@ listen(listener) { | ||
}, | ||
off() {} | ||
off() {} /* It will be called on last listener unsubscribing. | ||
We will redefine it in onMount and onStop. */ | ||
} | ||
@@ -51,0 +64,0 @@ |
import { onMount } from '../lifecycle/index.js' | ||
import { atom } from '../atom/index.js' | ||
import { atom, notifyId } from '../atom/index.js' | ||
const collectWritable = deps => [ | ||
...new Set( | ||
deps.reduce( | ||
(acc, dep) => (dep.deps ? [...acc, ...dep.deps] : [...acc, dep]), | ||
[] | ||
) | ||
) | ||
] | ||
export let computed = (stores, cb) => { | ||
if (!Array.isArray(stores)) stores = [stores] | ||
let deps = collectWritable(stores) | ||
let run = () => cb(...stores.map(store => store.get())) | ||
let diamondNotifyId | ||
let run = () => { | ||
if (diamondNotifyId !== notifyId) { | ||
diamondNotifyId = notifyId | ||
derived.set(cb(...stores.map(store => store.get()))) | ||
} | ||
} | ||
let derived = atom() | ||
onMount(derived, () => { | ||
derived.set(run()) | ||
let unbinds = deps.map(store => | ||
store.listen(() => { | ||
derived.set(run()) | ||
}) | ||
let unbinds = stores.map(store => | ||
store.listen(run, cb) | ||
) | ||
run() | ||
return () => { | ||
@@ -32,5 +26,3 @@ for (let unbind of unbinds) unbind() | ||
derived.deps = deps | ||
return derived | ||
} |
{ | ||
"name": "nanostores", | ||
"version": "0.5.10", | ||
"description": "A tiny (199 bytes) state manager for React/Preact/Vue/Svelte with many atomic tree-shakable stores", | ||
"version": "0.5.11", | ||
"description": "A tiny (258 bytes) state manager for React/Preact/Vue/Svelte with many atomic tree-shakable stores", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "store", |
@@ -10,3 +10,3 @@ # Nano Stores | ||
* **Small.** Between 199 and 923 bytes (minified and gzipped). | ||
* **Small.** Between 258 and 938 bytes (minified and gzipped). | ||
Zero dependencies. It uses [Size Limit] to control size. | ||
@@ -13,0 +13,0 @@ * **Fast.** With small atomic and derived stores, you do not need to call |
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
37572
1288