nanostores
Advanced tools
Comparing version 0.9.3 to 0.9.4
@@ -11,3 +11,3 @@ import type { WritableStore } from '../map/index.js' | ||
/** | ||
* Action is a function which change the store. | ||
* Action is a function which changes the store. | ||
* | ||
@@ -17,7 +17,7 @@ * This wrap allows DevTools to see the name of action, which changes the store. | ||
* ```js | ||
* export const increase = action($counter, 'increase', (store, value = 1) => { | ||
* if (validateMax(store.get() + value)) { | ||
* store.set(store.get() + value) | ||
* export const increase = action($counter, 'increase', ($store, value = 1) => { | ||
* if (validateMax($store.get() + value)) { | ||
* $store.set($store.get() + value) | ||
* } | ||
* return store.get() | ||
* return $store.get() | ||
* }) | ||
@@ -36,3 +36,3 @@ * | ||
SomeStore extends WritableStore, | ||
Callback extends (store: SomeStore, ...args: any[]) => any | ||
Callback extends ($store: SomeStore, ...args: any[]) => any | ||
>(store: SomeStore, actionName: string, cb: Callback): OmitFirstArg<Callback> |
@@ -8,24 +8,24 @@ import { startTask } from '../task/index.js' | ||
export let doAction = (store, actionName, cb, args) => { | ||
export let doAction = ($store, actionName, cb, args) => { | ||
let id = ++uid | ||
let tracker = { ...store } | ||
let tracker = { ...$store } | ||
tracker.set = (...setArgs) => { | ||
store[lastAction] = actionName | ||
store[actionId] = id | ||
store.set(...setArgs) | ||
delete store[lastAction] | ||
delete store[actionId] | ||
$store[lastAction] = actionName | ||
$store[actionId] = id | ||
$store.set(...setArgs) | ||
delete $store[lastAction] | ||
delete $store[actionId] | ||
} | ||
if (store.setKey) { | ||
if ($store.setKey) { | ||
tracker.setKey = (...setArgs) => { | ||
store[lastAction] = actionName | ||
store[actionId] = id | ||
store.setKey(...setArgs) | ||
delete store[lastAction] | ||
delete store[actionId] | ||
$store[lastAction] = actionName | ||
$store[actionId] = id | ||
$store.setKey(...setArgs) | ||
delete $store[lastAction] | ||
delete $store[actionId] | ||
} | ||
} | ||
let onError, onEnd | ||
if (store.action) { | ||
;[onError, onEnd] = store.action(id, actionName, args) | ||
if ($store.action) { | ||
;[onError, onEnd] = $store.action(id, actionName, args) | ||
} | ||
@@ -50,4 +50,4 @@ let result = cb(tracker, ...args) | ||
export let action = | ||
(store, actionName, cb) => | ||
($store, actionName, cb) => | ||
(...args) => | ||
doAction(store, actionName, cb, args) | ||
doAction($store, actionName, cb, args) |
@@ -7,8 +7,8 @@ import { clean } from '../clean-stores/index.js' | ||
let listeners = [] | ||
let store = { | ||
let $atom = { | ||
get() { | ||
if (!store.lc) { | ||
store.listen(() => {})() | ||
if (!$atom.lc) { | ||
$atom.listen(() => {})() | ||
} | ||
return store.value | ||
return $atom.value | ||
}, | ||
@@ -18,3 +18,3 @@ l: level || 0, | ||
listen(listener, listenerLevel) { | ||
store.lc = listeners.push(listener, listenerLevel || store.l) / 2 | ||
$atom.lc = listeners.push(listener, listenerLevel || $atom.l) / 2 | ||
@@ -25,4 +25,3 @@ return () => { | ||
listeners.splice(index, 2) | ||
store.lc-- | ||
if (!store.lc) store.off() | ||
if (!--$atom.lc) $atom.off() | ||
} | ||
@@ -36,5 +35,5 @@ } | ||
listeners[i], | ||
store.value, | ||
listeners[i + 1], | ||
$atom.value, | ||
changedKey, | ||
listeners[i + 1] | ||
) | ||
@@ -45,19 +44,16 @@ } | ||
for (let i = 0; i < listenerQueue.length; i += 4) { | ||
let skip = false | ||
for (let j = i + 7; j < listenerQueue.length; j += 4) { | ||
if (listenerQueue[j] < listenerQueue[i + 3]) { | ||
skip = true | ||
break | ||
let skip | ||
for (let j = i + 1; !skip && (j += 4) < listenerQueue.length;) { | ||
if (listenerQueue[j] < listenerQueue[i + 1]) { | ||
skip = listenerQueue.push( | ||
listenerQueue[i], | ||
listenerQueue[i + 1], | ||
listenerQueue[i + 2], | ||
listenerQueue[i + 3] | ||
) | ||
} | ||
} | ||
if (skip) { | ||
listenerQueue.push( | ||
listenerQueue[i], | ||
listenerQueue[i + 1], | ||
listenerQueue[i + 2], | ||
listenerQueue[i + 3] | ||
) | ||
} else { | ||
listenerQueue[i](listenerQueue[i + 1], listenerQueue[i + 2]) | ||
if (!skip) { | ||
listenerQueue[i](listenerQueue[i + 2], listenerQueue[i + 3]) | ||
} | ||
@@ -71,10 +67,10 @@ } | ||
set(data) { | ||
if (store.value !== data) { | ||
store.value = data | ||
store.notify() | ||
if ($atom.value !== data) { | ||
$atom.value = data | ||
$atom.notify() | ||
} | ||
}, | ||
subscribe(cb, listenerLevel) { | ||
let unbind = store.listen(cb, listenerLevel) | ||
cb(store.value) | ||
subscribe(listener, listenerLevel) { | ||
let unbind = $atom.listen(listener, listenerLevel) | ||
listener($atom.value) | ||
return unbind | ||
@@ -86,10 +82,10 @@ }, | ||
if (process.env.NODE_ENV !== 'production') { | ||
store[clean] = () => { | ||
$atom[clean] = () => { | ||
listeners = [] | ||
store.lc = 0 | ||
store.off() | ||
$atom.lc = 0 | ||
$atom.off() | ||
} | ||
} | ||
return store | ||
return $atom | ||
} |
@@ -12,8 +12,8 @@ import { cleanTasks } from '../task/index.js' | ||
cleanTasks() | ||
for (let store of stores) { | ||
if (store) { | ||
if (store.mocked) delete store.mocked | ||
if (store[clean]) store[clean]() | ||
for (let $store of stores) { | ||
if ($store) { | ||
if ($store.mocked) delete $store.mocked | ||
if ($store[clean]) $store[clean]() | ||
} | ||
} | ||
} |
@@ -9,3 +9,3 @@ import { atom } from '../atom/index.js' | ||
let run = () => { | ||
let args = stores.map(store => store.get()) | ||
let args = stores.map($store => $store.get()) | ||
if ( | ||
@@ -16,9 +16,9 @@ diamondArgs === undefined || | ||
diamondArgs = args | ||
derived.set(cb(...args)) | ||
$computed.set(cb(...args)) | ||
} | ||
} | ||
let derived = atom(undefined, Math.max(...stores.map(s => s.l)) + 1) | ||
let $computed = atom(undefined, Math.max(...stores.map(s => s.l)) + 1) | ||
onMount(derived, () => { | ||
let unbinds = stores.map(store => store.listen(run, derived.l)) | ||
onMount($computed, () => { | ||
let unbinds = stores.map($store => $store.listen(run, $computed.l)) | ||
run() | ||
@@ -30,3 +30,3 @@ return () => { | ||
return derived | ||
return $computed | ||
} |
@@ -7,10 +7,10 @@ import { atom } from '../atom/index.js' | ||
export function deepMap(initial = {}) { | ||
let store = atom(initial) | ||
store.setKey = (key, value) => { | ||
if (getPath(store.value, key) !== value) { | ||
store.value = setPath(store.value, key, value) | ||
store.notify(key) | ||
let $deepMap = atom(initial) | ||
$deepMap.setKey = (key, value) => { | ||
if (getPath($deepMap.value, key) !== value) { | ||
$deepMap.value = {...setPath($deepMap.value, key, value)} | ||
$deepMap.notify(key) | ||
} | ||
} | ||
return store | ||
return $deepMap | ||
} |
@@ -14,4 +14,4 @@ import type { MapCreator, Store } from '../map/index.js' | ||
* | ||
* @param store The store. | ||
* @param $store The store. | ||
*/ | ||
export function keepMount(store: MapCreator | Store): void | ||
export function keepMount($store: MapCreator | Store): void |
@@ -1,3 +0,3 @@ | ||
export let keepMount = store => { | ||
store.listen(() => {}) | ||
export let keepMount = $store => { | ||
$store.listen(() => {}) | ||
} |
@@ -52,3 +52,3 @@ import type { MapStore, Store, StoreValue } from '../map/index.js' | ||
* | ||
* @param store The store to add listener. | ||
* @param $store The store to add listener. | ||
* @param listener Event callback. | ||
@@ -58,3 +58,3 @@ * @returns A function to remove listener. | ||
export function onSet<Shared = never, SomeStore extends Store = Store>( | ||
store: SomeStore, | ||
$store: SomeStore, | ||
listener: ( | ||
@@ -75,3 +75,3 @@ payload: SomeStore extends MapStore | ||
* | ||
* @param store The store to add listener. | ||
* @param $store The store to add listener. | ||
* @param listener Event callback. | ||
@@ -81,3 +81,3 @@ * @returns A function to remove listener. | ||
export function onNotify<Shared = never, SomeStore extends Store = Store>( | ||
store: SomeStore, | ||
$store: SomeStore, | ||
listener: ( | ||
@@ -98,3 +98,3 @@ payload: SomeStore extends MapStore | ||
* | ||
* @param store The store to add listener. | ||
* @param $store The store to add listener. | ||
* @param listener Event callback. | ||
@@ -104,3 +104,3 @@ * @returns A function to remove listener. | ||
export function onStart<Shared = never>( | ||
store: Store, | ||
$store: Store, | ||
listener: (payload: { shared: Shared }) => void | ||
@@ -117,3 +117,3 @@ ): () => void | ||
* | ||
* @param store The store to add listener. | ||
* @param $store The store to add listener. | ||
* @param listener Event callback. | ||
@@ -123,3 +123,3 @@ * @returns A function to remove listener. | ||
export function onStop<Shared = never>( | ||
store: Store, | ||
$store: Store, | ||
listener: (payload: { shared: Shared }) => void | ||
@@ -151,3 +151,3 @@ ): () => void | ||
* | ||
* @param store Store to listen. | ||
* @param $store Store to listen. | ||
* @param initialize Store constructor. Returns store destructor. | ||
@@ -157,3 +157,3 @@ * @return A function to remove constructor and destructor from store. | ||
export function onMount<Shared = never>( | ||
store: Store, | ||
$store: Store, | ||
initialize: (payload: { shared: Shared }) => (() => void) | void | ||
@@ -185,3 +185,3 @@ ): () => void | ||
* | ||
* @param store The store to add listener. | ||
* @param $store The store to add listener. | ||
* @param listener Event callback. | ||
@@ -191,3 +191,3 @@ * @returns A function to remove listener. | ||
export function onAction<Shared = never>( | ||
store: Store, | ||
$store: Store, | ||
listener: (payload: { | ||
@@ -194,0 +194,0 @@ actionName: string |
@@ -37,10 +37,10 @@ import { clean } from '../clean-stores/index.js' | ||
export let onStart = (store, listener) => | ||
on(store, listener, START, runListeners => { | ||
let originListen = store.listen | ||
store.listen = arg => { | ||
if (!store.lc && !store.starting) { | ||
store.starting = true | ||
export let onStart = ($store, listener) => | ||
on($store, listener, START, runListeners => { | ||
let originListen = $store.listen | ||
$store.listen = arg => { | ||
if (!$store.lc && !$store.starting) { | ||
$store.starting = true | ||
runListeners() | ||
delete store.starting | ||
delete $store.starting | ||
} | ||
@@ -50,10 +50,10 @@ return originListen(arg) | ||
return () => { | ||
store.listen = originListen | ||
$store.listen = originListen | ||
} | ||
}) | ||
export let onStop = (store, listener) => | ||
on(store, listener, STOP, runListeners => { | ||
let originOff = store.off | ||
store.off = () => { | ||
export let onStop = ($store, listener) => | ||
on($store, listener, STOP, runListeners => { | ||
let originOff = $store.off | ||
$store.off = () => { | ||
runListeners() | ||
@@ -63,12 +63,12 @@ originOff() | ||
return () => { | ||
store.off = originOff | ||
$store.off = originOff | ||
} | ||
}) | ||
export let onSet = (store, listener) => | ||
on(store, listener, SET, runListeners => { | ||
let originSet = store.set | ||
let originSetKey = store.setKey | ||
if (store.setKey) { | ||
store.setKey = (changed, changedValue) => { | ||
export let onSet = ($store, listener) => | ||
on($store, listener, SET, runListeners => { | ||
let originSet = $store.set | ||
let originSetKey = $store.setKey | ||
if ($store.setKey) { | ||
$store.setKey = (changed, changedValue) => { | ||
let isAborted | ||
@@ -82,3 +82,3 @@ let abort = () => { | ||
changed, | ||
newValue: { ...store.value, [changed]: changedValue } | ||
newValue: { ...$store.value, [changed]: changedValue } | ||
}) | ||
@@ -88,3 +88,3 @@ if (!isAborted) return originSetKey(changed, changedValue) | ||
} | ||
store.set = newValue => { | ||
$store.set = newValue => { | ||
let isAborted | ||
@@ -99,11 +99,11 @@ let abort = () => { | ||
return () => { | ||
store.set = originSet | ||
store.setKey = originSetKey | ||
$store.set = originSet | ||
$store.setKey = originSetKey | ||
} | ||
}) | ||
export let onNotify = (store, listener) => | ||
on(store, listener, NOTIFY, runListeners => { | ||
let originNotify = store.notify | ||
store.notify = changed => { | ||
export let onNotify = ($store, listener) => | ||
on($store, listener, NOTIFY, runListeners => { | ||
let originNotify = $store.notify | ||
$store.notify = changed => { | ||
let isAborted | ||
@@ -118,3 +118,3 @@ let abort = () => { | ||
return () => { | ||
store.notify = originNotify | ||
$store.notify = originNotify | ||
} | ||
@@ -125,12 +125,12 @@ }) | ||
export let onMount = (store, initialize) => { | ||
export let onMount = ($store, initialize) => { | ||
let listener = payload => { | ||
let destroy = initialize(payload) | ||
if (destroy) store.events[UNMOUNT].push(destroy) | ||
if (destroy) $store.events[UNMOUNT].push(destroy) | ||
} | ||
return on(store, listener, MOUNT, runListeners => { | ||
let originListen = store.listen | ||
store.listen = (...args) => { | ||
if (!store.lc && !store.active) { | ||
store.active = true | ||
return on($store, listener, MOUNT, runListeners => { | ||
let originListen = $store.listen | ||
$store.listen = (...args) => { | ||
if (!$store.lc && !$store.active) { | ||
$store.active = true | ||
runListeners() | ||
@@ -141,11 +141,11 @@ } | ||
let originOff = store.off | ||
store.events[UNMOUNT] = [] | ||
store.off = () => { | ||
let originOff = $store.off | ||
$store.events[UNMOUNT] = [] | ||
$store.off = () => { | ||
originOff() | ||
setTimeout(() => { | ||
if (store.active && !store.lc) { | ||
store.active = false | ||
for (let destroy of store.events[UNMOUNT]) destroy() | ||
store.events[UNMOUNT] = [] | ||
if ($store.active && !$store.lc) { | ||
$store.active = false | ||
for (let destroy of $store.events[UNMOUNT]) destroy() | ||
$store.events[UNMOUNT] = [] | ||
} | ||
@@ -156,7 +156,7 @@ }, STORE_UNMOUNT_DELAY) | ||
if (process.env.NODE_ENV !== 'production') { | ||
let originClean = store[clean] | ||
store[clean] = () => { | ||
for (let destroy of store.events[UNMOUNT]) destroy() | ||
store.events[UNMOUNT] = [] | ||
store.active = false | ||
let originClean = $store[clean] | ||
$store[clean] = () => { | ||
for (let destroy of $store.events[UNMOUNT]) destroy() | ||
$store.events[UNMOUNT] = [] | ||
$store.active = false | ||
originClean() | ||
@@ -167,4 +167,4 @@ } | ||
return () => { | ||
store.listen = originListen | ||
store.off = originOff | ||
$store.listen = originListen | ||
$store.off = originOff | ||
} | ||
@@ -175,8 +175,8 @@ }) | ||
export let onAction = (store, listener) => | ||
on(store, listener, ACTION, runListeners => { | ||
export let onAction = ($store, listener) => | ||
on($store, listener, ACTION, runListeners => { | ||
let errorListeners = {} | ||
let endListeners = {} | ||
let originAction = store.action | ||
store.action = (id, actionName, args) => { | ||
let originAction = $store.action | ||
$store.action = (id, actionName, args) => { | ||
runListeners({ | ||
@@ -209,4 +209,4 @@ actionName, | ||
return () => { | ||
store.action = originAction | ||
$store.action = originAction | ||
} | ||
}) |
@@ -14,3 +14,3 @@ import type { StoreValue } from '../map/index.js' | ||
* | ||
* @param store The store to listen. | ||
* @param $store The store to listen. | ||
* @param keys The keys to listen. | ||
@@ -22,3 +22,3 @@ * @param listener Standard listener. | ||
>( | ||
store: SomeStore, | ||
$store: SomeStore, | ||
keys: SomeStore extends { setKey: (key: infer Key, value: never) => unknown } | ||
@@ -25,0 +25,0 @@ ? readonly Key[] |
@@ -1,4 +0,4 @@ | ||
export function listenKeys(store, keys, listener) { | ||
export function listenKeys($store, keys, listener) { | ||
let keysSet = new Set([...keys, undefined]) | ||
return store.listen((value, changed) => { | ||
return $store.listen((value, changed) => { | ||
if (keysSet.has(changed)) { | ||
@@ -5,0 +5,0 @@ listener(value, changed) |
import { atom } from '../atom/index.js' | ||
export let map = (value = {}) => { | ||
let store = atom(value) | ||
let $map = atom(value) | ||
store.setKey = function (key, newValue) { | ||
$map.setKey = function (key, newValue) { | ||
if (typeof newValue === 'undefined') { | ||
if (key in store.value) { | ||
store.value = { ...store.value } | ||
delete store.value[key] | ||
store.notify(key) | ||
if (key in $map.value) { | ||
$map.value = { ...$map.value } | ||
delete $map.value[key] | ||
$map.notify(key) | ||
} | ||
} else if (store.value[key] !== newValue) { | ||
store.value = { | ||
...store.value, | ||
} else if ($map.value[key] !== newValue) { | ||
$map.value = { | ||
...$map.value, | ||
[key]: newValue | ||
} | ||
store.notify(key) | ||
$map.notify(key) | ||
} | ||
} | ||
return store | ||
return $map | ||
} |
{ | ||
"name": "nanostores", | ||
"version": "0.9.3", | ||
"description": "A tiny (313 bytes) state manager for React/Preact/Vue/Svelte with many atomic tree-shakable stores", | ||
"version": "0.9.4", | ||
"description": "A tiny (297 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 313 and 1030 bytes (minified and gzipped). | ||
* **Small.** Between 297 and 1013 bytes (minified and gzipped). | ||
Zero dependencies. It uses [Size Limit] to control size. | ||
@@ -54,7 +54,8 @@ * **Fast.** With small atomic and derived stores, you do not need to call | ||
<a href="https://evilmartians.com/?utm_source=nanostores"> | ||
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" | ||
alt="Sponsored by Evil Martians" width="236" height="54"> | ||
</a> | ||
--- | ||
<img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="" width="22" height="16" /> Made in <b><a href="https://evilmartians.com/devtools?utm_source=nanostores&utm_campaign=devtools-button&utm_medium=github">Evil Martians</a></b>, product consulting for <b>developer tools</b>. | ||
--- | ||
[Size Limit]: https://github.com/ai/size-limit | ||
@@ -61,0 +62,0 @@ |
@@ -28,3 +28,3 @@ /** | ||
* | ||
* function saveUser () { | ||
* async function saveUser () { | ||
* await task(async () => { | ||
@@ -31,0 +31,0 @@ * await api.submit('/user', user.get()) |
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
39305
64
1309