@nanostores/query
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -45,2 +45,7 @@ import { PlatformCompat } from './platforms/type'; | ||
}; | ||
type LazyFetchValue<T = any, E = any> = { | ||
data: T; | ||
} | { | ||
error: E; | ||
}; | ||
export type FetcherStore<T = any, E = any> = MapStore<FetcherValue<T, E>> & { | ||
@@ -52,2 +57,3 @@ _: Symbol; | ||
mutate: (data?: T) => void; | ||
fetch: () => Promise<LazyFetchValue<T, E>>; | ||
}; | ||
@@ -54,0 +60,0 @@ export type FetcherStoreCreator<T = any, E = Error> = (keys: KeyInput, settings?: CommonSettings<T>) => FetcherStore<T, E>; |
@@ -146,2 +146,13 @@ import { map, onStart, onStop, atom, batched, startTask } from 'nanostores'; | ||
}; | ||
fetcherStore.fetch = async () => { | ||
let resolve; | ||
const promise = new Promise((r) => resolve = r); | ||
const unsub = fetcherStore.listen(({ error, data }) => { | ||
if (error) | ||
resolve({ error }); | ||
if (data) | ||
resolve({ data }); | ||
}); | ||
return promise.finally(unsub); | ||
}; | ||
let keysInternalUnsub, prevKey, prevKeyParts, keyUnsub, keyStore; | ||
@@ -148,0 +159,0 @@ let evtUnsubs = []; |
{ | ||
"name": "@nanostores/query", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Tiny remote data fetching library for Nano Stores", | ||
@@ -89,3 +89,3 @@ "scripts": { | ||
}, | ||
"limit": "1862 B" | ||
"limit": "1924 B" | ||
} | ||
@@ -92,0 +92,0 @@ ], |
@@ -289,2 +289,13 @@ # Nano Stores Query | ||
### Lazy fetcher | ||
Sometimes you don't want a store, you just want an async function that's gonna handle the errors and leverage the cache (perform cache lookup, save data in there upon successful execution, etc.). | ||
For that case use `fetcherStore.fetch` function. It will always resolve with the same data type as store itself (`error` and `data` only). | ||
Few gotchas: | ||
- it will execute against currently set keys (no way to customize them for the call); | ||
- it will still leverage deduplication. | ||
### Dependencies, but not in keys | ||
@@ -291,0 +302,0 @@ |
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
66153
1381
335