@nanostores/query
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -34,4 +34,5 @@ import { MapStore, ReadableAtom } from "nanostores"; | ||
}) => Promise<Result>; | ||
declare type MutateCb<Data> = Data extends void ? () => Promise<unknown> : (data: Data) => Promise<unknown>; | ||
export declare type MutatorStore<Data = void, Result = unknown, E = Error> = MapStore<{ | ||
mutate: (data: Data) => Promise<Result>; | ||
mutate: MutateCb<Data>; | ||
data?: Result; | ||
@@ -38,0 +39,0 @@ loading?: boolean; |
@@ -168,38 +168,40 @@ import { map, onStart, onStop, atom } from "nanostores"; | ||
function createMutatorStore(mutator) { | ||
const mutate = async (data) => { | ||
var _a, _b; | ||
const newMutator = (_a = rewrittenSettings.fetcher) != null ? _a : mutator; | ||
const keysToInvalidate = []; | ||
try { | ||
store.set({ | ||
error: void 0, | ||
loading: true, | ||
data: void 0, | ||
mutate: store.get().mutate | ||
}); | ||
const result = await newMutator({ | ||
data, | ||
invalidate: (key) => { | ||
keysToInvalidate.push(key); | ||
}, | ||
getCacheUpdater: (key, shouldInvalidate = true) => [ | ||
(newVal) => { | ||
mutateCache(key, newVal); | ||
if (shouldInvalidate) { | ||
keysToInvalidate.push(key); | ||
} | ||
}, | ||
cache.get(key) | ||
] | ||
}); | ||
store.setKey("data", result); | ||
return result; | ||
} catch (error) { | ||
(_b = globalSettings == null ? void 0 : globalSettings.onError) == null ? void 0 : _b.call(globalSettings, error); | ||
store.setKey("error", error); | ||
} finally { | ||
store.setKey("loading", false); | ||
invalidateKeys(keysToInvalidate); | ||
} | ||
}; | ||
const store = map({ | ||
mutate: async (data) => { | ||
var _a, _b; | ||
const newMutator = (_a = rewrittenSettings.fetcher) != null ? _a : mutator; | ||
const keysToInvalidate = []; | ||
try { | ||
store.set({ | ||
error: void 0, | ||
loading: true, | ||
data: void 0, | ||
mutate: store.get().mutate | ||
}); | ||
const result = await newMutator({ | ||
data, | ||
invalidate: (key) => { | ||
keysToInvalidate.push(key); | ||
}, | ||
getCacheUpdater: (key, shouldInvalidate = true) => [ | ||
(newVal) => { | ||
mutateCache(key, newVal); | ||
if (shouldInvalidate) { | ||
keysToInvalidate.push(key); | ||
} | ||
}, | ||
cache.get(key) | ||
] | ||
}); | ||
store.setKey("data", result); | ||
} catch (error) { | ||
(_b = globalSettings == null ? void 0 : globalSettings.onError) == null ? void 0 : _b.call(globalSettings, error); | ||
store.setKey("error", error); | ||
} finally { | ||
store.setKey("loading", false); | ||
invalidateKeys(keysToInvalidate); | ||
} | ||
}, | ||
mutate, | ||
loading: false | ||
@@ -206,0 +208,0 @@ }); |
{ | ||
"name": "@nanostores/query", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Tiny remote data fetching library for Nano Stores", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
28058
598