@nanostores/query
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -16,3 +16,3 @@ import { map, onStart, onStop, atom } from "nanostores"; | ||
subscribe("online", () => events.emit(RECONNECT)); | ||
const _refetchOnInterval = /* @__PURE__ */ new Map(), _lastFetch = /* @__PURE__ */ new Map(), _runningFetches = /* @__PURE__ */ new Set(); | ||
const _refetchOnInterval = /* @__PURE__ */ new Map(), _lastFetch = /* @__PURE__ */ new Map(), _runningFetches = /* @__PURE__ */ new Map(); | ||
let rewrittenSettings = {}; | ||
@@ -29,2 +29,9 @@ const runFetcher = async ([key, keyParts], store, settings, force) => { | ||
}; | ||
const setAsLoading = () => { | ||
set({ | ||
...store.value, | ||
...loading, | ||
promise: _runningFetches.get(key) | ||
}); | ||
}; | ||
const { dedupeTime = 4e3, fetcher } = { | ||
@@ -35,6 +42,11 @@ ...settings, | ||
const now = getNow(); | ||
if (_runningFetches.has(key)) { | ||
if (!store.value.loading) | ||
setAsLoading(); | ||
return; | ||
} | ||
if (!force) { | ||
const cached = cache.get(key); | ||
if (store.value.data !== cached) | ||
set(cached ? { data: cached, ...notLoading } : { ...loading }); | ||
if (cached && store.value.data !== cached) | ||
set({ data: cached, ...notLoading }); | ||
const last = _lastFetch.get(key); | ||
@@ -45,10 +57,7 @@ if (last && last + dedupeTime > now) { | ||
} | ||
if (_runningFetches.has(key)) { | ||
return; | ||
} | ||
_lastFetch.set(key, now); | ||
_runningFetches.add(key); | ||
try { | ||
const promise = fetcher(...keyParts); | ||
set({ data: store.value.data, ...loading, promise }); | ||
_lastFetch.set(key, now); | ||
_runningFetches.set(key, promise); | ||
setAsLoading(); | ||
const res = await promise; | ||
@@ -89,3 +98,2 @@ cache.set(key, res); | ||
}; | ||
fetcherStore.get = () => fetcherStore.value; | ||
let keysInternalUnsub, prevKey, prevKeyParts, keyUnsub, keyStore; | ||
@@ -92,0 +100,0 @@ let evtUnsubs = []; |
{ | ||
"name": "@nanostores/query", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"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
33471
658