@tanstack/query-async-storage-persister 5.0.0-alpha.61 → 5.0.0-alpha.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/asyncThrottle.legacy.cjs +52 -0
- package/build/lib/asyncThrottle.legacy.cjs.map +1 -0
- package/build/lib/asyncThrottle.legacy.js +50 -0
- package/build/lib/asyncThrottle.legacy.js.map +1 -0
- package/build/lib/index.legacy.cjs +62 -0
- package/build/lib/index.legacy.cjs.map +1 -0
- package/build/lib/index.legacy.js +60 -0
- package/build/lib/index.legacy.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const noop = () => {
|
|
4
|
+
/* do nothing */
|
|
5
|
+
};
|
|
6
|
+
function asyncThrottle(func, {
|
|
7
|
+
interval = 1000,
|
|
8
|
+
onError = noop
|
|
9
|
+
} = {}) {
|
|
10
|
+
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
11
|
+
let running = false;
|
|
12
|
+
let lastTime = 0;
|
|
13
|
+
let timeout;
|
|
14
|
+
let currentArgs = null;
|
|
15
|
+
const execFunc = async () => {
|
|
16
|
+
if (currentArgs) {
|
|
17
|
+
const args = currentArgs;
|
|
18
|
+
currentArgs = null;
|
|
19
|
+
try {
|
|
20
|
+
running = true;
|
|
21
|
+
await func(...args);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
onError(error);
|
|
24
|
+
} finally {
|
|
25
|
+
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
26
|
+
running = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const delayFunc = async () => {
|
|
31
|
+
clearTimeout(timeout);
|
|
32
|
+
timeout = setTimeout(() => {
|
|
33
|
+
if (running) {
|
|
34
|
+
delayFunc(); // Will come here when 'func' execution time is greater than the interval.
|
|
35
|
+
} else {
|
|
36
|
+
execFunc();
|
|
37
|
+
}
|
|
38
|
+
}, interval);
|
|
39
|
+
};
|
|
40
|
+
return (...args) => {
|
|
41
|
+
currentArgs = args;
|
|
42
|
+
const tooSoon = Date.now() - lastTime < interval;
|
|
43
|
+
if (running || tooSoon) {
|
|
44
|
+
delayFunc();
|
|
45
|
+
} else {
|
|
46
|
+
execFunc();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.asyncThrottle = asyncThrottle;
|
|
52
|
+
//# sourceMappingURL=asyncThrottle.legacy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asyncThrottle.legacy.cjs","sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"names":["noop","asyncThrottle","func","interval","onError","Error","running","lastTime","timeout","currentArgs","execFunc","args","error","Date","now","delayFunc","clearTimeout","setTime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AACxBF,IAAAA,WAAW,GAAGE,IAAI,CAAA;IAElB,MAAMO,OAAO,GAAGL,IAAI,CAACC,GAAG,EAAE,GAAGP,QAAQ,GAAGJ,QAAQ,CAAA;IAChD,IAAIG,OAAO,IAAIY,OAAO,EAAE;AACtBH,MAAAA,SAAS,EAAE,CAAA;AACb,KAAC,MAAM;AACLL,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAA;GACD,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const noop = () => {
|
|
2
|
+
/* do nothing */
|
|
3
|
+
};
|
|
4
|
+
function asyncThrottle(func, {
|
|
5
|
+
interval = 1000,
|
|
6
|
+
onError = noop
|
|
7
|
+
} = {}) {
|
|
8
|
+
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
9
|
+
let running = false;
|
|
10
|
+
let lastTime = 0;
|
|
11
|
+
let timeout;
|
|
12
|
+
let currentArgs = null;
|
|
13
|
+
const execFunc = async () => {
|
|
14
|
+
if (currentArgs) {
|
|
15
|
+
const args = currentArgs;
|
|
16
|
+
currentArgs = null;
|
|
17
|
+
try {
|
|
18
|
+
running = true;
|
|
19
|
+
await func(...args);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
onError(error);
|
|
22
|
+
} finally {
|
|
23
|
+
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
24
|
+
running = false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const delayFunc = async () => {
|
|
29
|
+
clearTimeout(timeout);
|
|
30
|
+
timeout = setTimeout(() => {
|
|
31
|
+
if (running) {
|
|
32
|
+
delayFunc(); // Will come here when 'func' execution time is greater than the interval.
|
|
33
|
+
} else {
|
|
34
|
+
execFunc();
|
|
35
|
+
}
|
|
36
|
+
}, interval);
|
|
37
|
+
};
|
|
38
|
+
return (...args) => {
|
|
39
|
+
currentArgs = args;
|
|
40
|
+
const tooSoon = Date.now() - lastTime < interval;
|
|
41
|
+
if (running || tooSoon) {
|
|
42
|
+
delayFunc();
|
|
43
|
+
} else {
|
|
44
|
+
execFunc();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { asyncThrottle };
|
|
50
|
+
//# sourceMappingURL=asyncThrottle.legacy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asyncThrottle.legacy.js","sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"names":["noop","asyncThrottle","func","interval","onError","Error","running","lastTime","timeout","currentArgs","execFunc","args","error","Date","now","delayFunc","clearTimeout","setTimeout","tooSoon"],"mappings":"AAKA,MAAMA,IAAI,GAAGA,MAAM;AACjB;AAAA,CACD,CAAA;AAEM,SAASC,aAAaA,CAC3BC,IAAsC,EACtC;AAAEC,EAAAA,QAAQ,GAAG,IAAI;AAAEC,EAAAA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E;AACtBH,MAAAA,SAAS,EAAE,CAAA;AACb,KAAC,MAAM;AACLL,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAA;GACD,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var asyncThrottle = require('./asyncThrottle.legacy.cjs');
|
|
4
|
+
|
|
5
|
+
const createAsyncStoragePersister = ({
|
|
6
|
+
storage,
|
|
7
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
8
|
+
throttleTime = 1000,
|
|
9
|
+
serialize = JSON.stringify,
|
|
10
|
+
deserialize = JSON.parse,
|
|
11
|
+
retry
|
|
12
|
+
}) => {
|
|
13
|
+
if (storage) {
|
|
14
|
+
const trySave = async persistedClient => {
|
|
15
|
+
try {
|
|
16
|
+
await storage.setItem(key, serialize(persistedClient));
|
|
17
|
+
return;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return error;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
persistClient: asyncThrottle.asyncThrottle(async persistedClient => {
|
|
24
|
+
let client = persistedClient;
|
|
25
|
+
let error = await trySave(client);
|
|
26
|
+
let errorCount = 0;
|
|
27
|
+
while (error && client) {
|
|
28
|
+
errorCount++;
|
|
29
|
+
client = await (retry == null ? void 0 : retry({
|
|
30
|
+
persistedClient: client,
|
|
31
|
+
error,
|
|
32
|
+
errorCount
|
|
33
|
+
}));
|
|
34
|
+
if (client) {
|
|
35
|
+
error = await trySave(client);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}, {
|
|
39
|
+
interval: throttleTime
|
|
40
|
+
}),
|
|
41
|
+
restoreClient: async () => {
|
|
42
|
+
const cacheString = await storage.getItem(key);
|
|
43
|
+
if (!cacheString) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
return deserialize(cacheString);
|
|
47
|
+
},
|
|
48
|
+
removeClient: () => storage.removeItem(key)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
persistClient: noop,
|
|
53
|
+
restoreClient: () => Promise.resolve(undefined),
|
|
54
|
+
removeClient: noop
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
59
|
+
function noop() {}
|
|
60
|
+
|
|
61
|
+
exports.createAsyncStoragePersister = createAsyncStoragePersister;
|
|
62
|
+
//# sourceMappingURL=index.legacy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.legacy.cjs","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport type AsyncPersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => Promisable<PersistedClient | undefined>\n\ninterface CreateAsyncStoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`. Note that window.localStorage can be\n * `null` in Android WebViews depending on how they are configured.\n */\n storage: AsyncStorage | undefined | null\n /** The key to use when storing the cache */\n key?: string\n /** To avoid spamming,\n * pass a time in ms to throttle saving the cache to disk */\n throttleTime?: number\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (client: PersistedClient) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedClient\n\n retry?: AsyncPersistRetryer\n}\n\nexport const createAsyncStoragePersister = ({\n storage,\n key = `REACT_QUERY_OFFLINE_CACHE`,\n throttleTime = 1000,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n retry,\n}: CreateAsyncStoragePersisterOptions): Persister => {\n if (storage) {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\n return\n } catch (error) {\n return error as Error\n }\n }\n\n return {\n persistClient: asyncThrottle(\n async (persistedClient) => {\n let client: PersistedClient | undefined = persistedClient\n let error = await trySave(client)\n let errorCount = 0\n while (error && client) {\n errorCount++\n client = await retry?.({\n persistedClient: client,\n error,\n errorCount,\n })\n\n if (client) {\n error = await trySave(client)\n }\n }\n },\n { interval: throttleTime },\n ),\n restoreClient: async () => {\n const cacheString = await storage.getItem(key)\n\n if (!cacheString) {\n return\n }\n\n return deserialize(cacheString)\n },\n removeClient: () => storage.removeItem(key),\n }\n }\n\n return {\n persistClient: noop,\n restoreClient: () => Promise.resolve(undefined),\n removeClient: noop,\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction noop() {}\n"],"names":["createAsyncStoragePersister","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","persistedClient","setItem","error","persistClient","asyncThrottle","client","errorCount","interval","restoreClient","cacheString","getItem","removeClient","removeItem","noop","Promise","resolve","undefined"],"mappings":";;;;AA4CO,MAAMA,2BAA2B,GAAGA,CAAC;EAC1CC,OAAO;AACPC,EAAAA,GAAG,GAAI,CAA0B,yBAAA,CAAA;AACjCC,EAAAA,YAAY,GAAG,IAAI;EACnBC,SAAS,GAAGC,IAAI,CAACC,SAAS;EAC1BC,WAAW,GAAGF,IAAI,CAACG,KAAK;AACxBC,EAAAA,KAAAA;AACkC,CAAC,KAAgB;AACnD,EAAA,IAAIR,OAAO,EAAE;AACX,IAAA,MAAMS,OAAO,GAAG,MACdC,eAAgC,IACD;MAC/B,IAAI;QACF,MAAMV,OAAO,CAACW,OAAO,CAACV,GAAG,EAAEE,SAAS,CAACO,eAAe,CAAC,CAAC,CAAA;AACtD,QAAA,OAAA;OACD,CAAC,OAAOE,KAAK,EAAE;AACd,QAAA,OAAOA,KAAK,CAAA;AACd,OAAA;KACD,CAAA;IAED,O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legacy.js';
|
|
2
|
+
|
|
3
|
+
const createAsyncStoragePersister = ({
|
|
4
|
+
storage,
|
|
5
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
6
|
+
throttleTime = 1000,
|
|
7
|
+
serialize = JSON.stringify,
|
|
8
|
+
deserialize = JSON.parse,
|
|
9
|
+
retry
|
|
10
|
+
}) => {
|
|
11
|
+
if (storage) {
|
|
12
|
+
const trySave = async persistedClient => {
|
|
13
|
+
try {
|
|
14
|
+
await storage.setItem(key, serialize(persistedClient));
|
|
15
|
+
return;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return error;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
persistClient: asyncThrottle(async persistedClient => {
|
|
22
|
+
let client = persistedClient;
|
|
23
|
+
let error = await trySave(client);
|
|
24
|
+
let errorCount = 0;
|
|
25
|
+
while (error && client) {
|
|
26
|
+
errorCount++;
|
|
27
|
+
client = await (retry == null ? void 0 : retry({
|
|
28
|
+
persistedClient: client,
|
|
29
|
+
error,
|
|
30
|
+
errorCount
|
|
31
|
+
}));
|
|
32
|
+
if (client) {
|
|
33
|
+
error = await trySave(client);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}, {
|
|
37
|
+
interval: throttleTime
|
|
38
|
+
}),
|
|
39
|
+
restoreClient: async () => {
|
|
40
|
+
const cacheString = await storage.getItem(key);
|
|
41
|
+
if (!cacheString) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
return deserialize(cacheString);
|
|
45
|
+
},
|
|
46
|
+
removeClient: () => storage.removeItem(key)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
persistClient: noop,
|
|
51
|
+
restoreClient: () => Promise.resolve(undefined),
|
|
52
|
+
removeClient: noop
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
57
|
+
function noop() {}
|
|
58
|
+
|
|
59
|
+
export { createAsyncStoragePersister };
|
|
60
|
+
//# sourceMappingURL=index.legacy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.legacy.js","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport type AsyncPersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => Promisable<PersistedClient | undefined>\n\ninterface CreateAsyncStoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`. Note that window.localStorage can be\n * `null` in Android WebViews depending on how they are configured.\n */\n storage: AsyncStorage | undefined | null\n /** The key to use when storing the cache */\n key?: string\n /** To avoid spamming,\n * pass a time in ms to throttle saving the cache to disk */\n throttleTime?: number\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (client: PersistedClient) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedClient\n\n retry?: AsyncPersistRetryer\n}\n\nexport const createAsyncStoragePersister = ({\n storage,\n key = `REACT_QUERY_OFFLINE_CACHE`,\n throttleTime = 1000,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n retry,\n}: CreateAsyncStoragePersisterOptions): Persister => {\n if (storage) {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\n return\n } catch (error) {\n return error as Error\n }\n }\n\n return {\n persistClient: asyncThrottle(\n async (persistedClient) => {\n let client: PersistedClient | undefined = persistedClient\n let error = await trySave(client)\n let errorCount = 0\n while (error && client) {\n errorCount++\n client = await retry?.({\n persistedClient: client,\n error,\n errorCount,\n })\n\n if (client) {\n error = await trySave(client)\n }\n }\n },\n { interval: throttleTime },\n ),\n restoreClient: async () => {\n const cacheString = await storage.getItem(key)\n\n if (!cacheString) {\n return\n }\n\n return deserialize(cacheString)\n },\n removeClient: () => storage.removeItem(key),\n }\n }\n\n return {\n persistClient: noop,\n restoreClient: () => Promise.resolve(undefined),\n removeClient: noop,\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction noop() {}\n"],"names":["createAsyncStoragePersister","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","persistedClient","setItem","error","persistClient","asyncThrottle","client","errorCount","interval","restoreClient","cacheString","getItem","removeClient","removeItem","noop","Promise","resolve","undefined"],"mappings":";;AA4CO,MAAMA,2BAA2B,GAAGA,CAAC;EAC1CC,OAAO;AACPC,EAAAA,GAAG,GAAI,CAA0B,yBAAA,CAAA;AACjCC,EAAAA,YAAY,GAAG,IAAI;EACnBC,SAAS,GAAGC,IAAI,CAACC,SAAS;EAC1BC,WAAW,GAAGF,IAAI,CAACG,KAAK;AACxBC,EAAAA,KAAAA;AACkC,CAAC,KAAgB;AACnD,EAAA,IAAIR,OAAO,EAAE;AACX,IAAA,MAAMS,OAAO,GAAG,MACdC,eAAgC,IACD;MAC/B,IAAI;QACF,MAAMV,OAAO,CAACW,OAAO,CAACV,GAAG,EAAEE,SAAS,CAACO,eAAe,CAAC,CAAC,CAAA;AACtD,QAAA,OAAA;OACD,CAAC,OAAOE,KAAK,EAAE;AACd,QAAA,OAAOA,KAAK,CAAA;AACd,OAAA;KACD,CAAA;IAED,OAAO;AACLC,MAAAA,aAAa,EAAEC,aAAa,CAC1B,MAAOJ,eAAe,IAAK;QACzB,IAAIK,MAAmC,GAAGL,eAAe,CAAA;AACzD,QAAA,IAAIE,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;QACjC,IAAIC,UAAU,GAAG,CAAC,CAAA;QAClB,OAAOJ,KAAK,IAAIG,MAAM,EAAE;AACtBC,UAAAA,UAAU,EAAE,CAAA;AACZD,UAAAA,MAAM,GAAG,OAAMP,KAAK,IAAA,IAAA,GAAA,KAAA,CAAA,GAALA,KAAK,CAAG;AACrBE,YAAAA,eAAe,EAAEK,MAAM;YACvBH,KAAK;AACLI,YAAAA,UAAAA;AACF,WAAC,CAAC,CAAA,CAAA;AAEF,UAAA,IAAID,MAAM,EAAE;AACVH,YAAAA,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;AAC/B,WAAA;AACF,SAAA;AACF,OAAC,EACD;AAAEE,QAAAA,QAAQ,EAAEf,YAAAA;AAAa,OAC3B,CAAC;MACDgB,aAAa,EAAE,YAAY;QACzB,MAAMC,WAAW,GAAG,MAAMnB,OAAO,CAACoB,OAAO,CAACnB,GAAG,CAAC,CAAA;QAE9C,IAAI,CAACkB,WAAW,EAAE;AAChB,UAAA,OAAA;AACF,SAAA;QAEA,OAAOb,WAAW,CAACa,WAAW,CAAC,CAAA;OAChC;AACDE,MAAAA,YAAY,EAAEA,MAAMrB,OAAO,CAACsB,UAAU,CAACrB,GAAG,CAAA;KAC3C,CAAA;AACH,GAAA;EAEA,OAAO;AACLY,IAAAA,aAAa,EAAEU,IAAI;IACnBL,aAAa,EAAEA,MAAMM,OAAO,CAACC,OAAO,CAACC,SAAS,CAAC;AAC/CL,IAAAA,YAAY,EAAEE,IAAAA;GACf,CAAA;AACH,EAAC;;AAED;AACA,SAASA,IAAIA,GAAG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-async-storage-persister",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.63",
|
|
4
4
|
"description": "A persister for asynchronous storages, to be used with TanStack/Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
14
|
"types": "build/lib/index.d.ts",
|
|
15
|
-
"main": "build/lib/index.cjs",
|
|
16
|
-
"module": "build/lib/index.js",
|
|
15
|
+
"main": "build/lib/index.legacy.cjs",
|
|
16
|
+
"module": "build/lib/index.legacy.js",
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
19
|
"types": "./build/lib/index.d.ts",
|