https://github.com/marianmeres/store
Basic store. Svelte store contract compatible.
https://github.com/marianmeres/store
subcribe svelte-store
Last synced: 5 months ago
JSON representation
Basic store. Svelte store contract compatible.
- Host: GitHub
- URL: https://github.com/marianmeres/store
- Owner: marianmeres
- License: mit
- Created: 2022-12-14T17:59:22.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-10T14:13:41.000Z (6 months ago)
- Last Synced: 2024-12-10T14:18:04.863Z (6 months ago)
- Topics: subcribe, svelte-store
- Language: TypeScript
- Homepage:
- Size: 286 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @marianmeres/store
Basic store (arbitrary data with the ability to subscribe to changes) utility.
[Svelte store contract](https://svelte.dev/docs#component-format-script-4-prefix-stores-with-$-to-access-their-values-store-contract) compatible.## Install
```shell
$ npm i @marianmeres/store
```## Usage
```typescript
const store = createStore('foo');// always able to `get` current value
assert('foo' === store.get());// from now on, console.log changes
let unsub = store.subscribe(console.log); // log: foostore.set('bar'); // logs: bar
store.update((old) => old + 'baz'); // logs: barbazunsub(); // stop console.log changes for `store`
// derived example
const store2 = createStore(123);
const derived = createDerivedStore([store, store2], ([a, b]) => [a, b].join());// derived store value is intially `undefined` until subscription exists
assert(derived.get() === undefined);unsub = derived.subscribe(console.log); // logs: barbaz,123
store2.set(456); // logs: barbaz,456
unsub();
store.set(789); // no log// derived async example (the deriveFn accepts second 'set' argument)
const derivedAsync = createDerivedStore([store, store2], ([a, b], set) => {
setTimeout(() => { set([a, b].join()) }, 1000)
});
```