cross-state 0.55.0 → 0.55.1
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/dist/index.cjs +39 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -6
- package/dist/index.d.ts +18 -6
- package/dist/index.js +39 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -9,43 +9,59 @@ const require_persist = require('./persist-CGLe7YUX.cjs');
|
|
|
9
9
|
//#region src/core/pagedCache.ts
|
|
10
10
|
var PagedCache = class PagedCache extends require_scope.Cache {
|
|
11
11
|
constructor(definition, args, options = {}) {
|
|
12
|
-
super(async (helpers) =>
|
|
13
|
-
const { fetchPage } = definition;
|
|
14
|
-
const page = await fetchPage({
|
|
15
|
-
...helpers,
|
|
16
|
-
pages: [],
|
|
17
|
-
prevPage: null
|
|
18
|
-
});
|
|
19
|
-
return page === null ? [] : [page];
|
|
20
|
-
}, args, options, void 0);
|
|
12
|
+
super(async (helpers) => loadPage(definition, helpers, []), args, options, void 0);
|
|
21
13
|
this.definition = definition;
|
|
22
14
|
require_store.autobind(PagedCache);
|
|
23
15
|
}
|
|
24
|
-
async fetchNextPage() {
|
|
25
|
-
const { fetchPage } = this.definition;
|
|
16
|
+
async fetchNextPage({ ignoreErrors } = {}) {
|
|
26
17
|
const { status, isStale, isUpdating, value } = this.state.get();
|
|
27
|
-
if (status
|
|
18
|
+
if (status === "error") {
|
|
19
|
+
if (ignoreErrors) return;
|
|
20
|
+
throw new Error("Cannot fetch next page while cache is in error state");
|
|
21
|
+
}
|
|
22
|
+
if (isUpdating) {
|
|
23
|
+
if (ignoreErrors) return;
|
|
24
|
+
throw new Error("Cannot fetch next page while another page is being fetched");
|
|
25
|
+
}
|
|
26
|
+
if (status === "pending" || isStale) {
|
|
27
|
+
await this.get().catch(() => {});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
28
30
|
this.stalePromise = this.calculatedValue?.value;
|
|
29
31
|
const ac = new AbortController();
|
|
30
|
-
const
|
|
32
|
+
const promise = loadPage(this.definition, {
|
|
31
33
|
use() {
|
|
32
34
|
throw new Error("Not implemented");
|
|
33
35
|
},
|
|
34
36
|
connect() {
|
|
35
37
|
throw new Error("Not implemented");
|
|
36
38
|
},
|
|
37
|
-
signal: ac.signal
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return pagePromise;
|
|
39
|
+
signal: ac.signal
|
|
40
|
+
}, value.pages);
|
|
41
|
+
this.updateValue(promise);
|
|
42
|
+
try {
|
|
43
|
+
await promise;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (ignoreErrors) return;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
47
48
|
}
|
|
48
49
|
};
|
|
50
|
+
async function loadPage({ fetchPage, hasMorePages, getPageCount }, helpers, oldPages) {
|
|
51
|
+
const page = await fetchPage({
|
|
52
|
+
...helpers,
|
|
53
|
+
pages: oldPages,
|
|
54
|
+
prevPage: oldPages.length > 0 ? oldPages[oldPages.length - 1] : null
|
|
55
|
+
});
|
|
56
|
+
const pages = page === null ? oldPages : oldPages.concat(page);
|
|
57
|
+
const pageCount = getPageCount?.(pages) ?? null;
|
|
58
|
+
const hasMore = page === null ? false : hasMorePages ? hasMorePages(pages) : pageCount !== null ? pages.length < pageCount : true;
|
|
59
|
+
return {
|
|
60
|
+
pages,
|
|
61
|
+
hasMore,
|
|
62
|
+
pageCount
|
|
63
|
+
};
|
|
64
|
+
}
|
|
49
65
|
function createPaged(definition, options) {
|
|
50
66
|
return require_scope.internalCreate((args, options$1) => {
|
|
51
67
|
if (definition instanceof Function) definition = definition(...args);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["Cache","definition: PagedCacheDefinition<T>","internalCreate","options","createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> }","defaultCacheOptions"],"sources":["../src/core/pagedCache.ts","../src/lib/updateHelpers.ts"],"sourcesContent":["import {\n Cache,\n defaultCacheOptions,\n internalCreate,\n type CacheOptions,\n type CreateCacheResult,\n} from '@core/cache';\nimport type { CalculationActions } from '@core/commonTypes';\nimport { autobind } from '@lib/autobind';\n\nexport interface PageCacheFunctionProps<T> extends CalculationActions<Promise<T
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Cache","definition: PagedCacheDefinition<T>","internalCreate","options","createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> }","defaultCacheOptions"],"sources":["../src/core/pagedCache.ts","../src/lib/updateHelpers.ts"],"sourcesContent":["import {\n Cache,\n defaultCacheOptions,\n internalCreate,\n type CacheOptions,\n type CreateCacheResult,\n} from '@core/cache';\nimport type { CalculationActions } from '@core/commonTypes';\nimport { autobind } from '@lib/autobind';\n\nexport interface PageCacheFunctionProps<T> extends CalculationActions<Promise<PagedState<T>>> {\n pages: T[];\n prevPage: T | null;\n}\n\nexport interface PageCacheFunction<T> {\n (props: PageCacheFunctionProps<T>): Promise<T | null>;\n}\n\nexport interface PagedCacheDefinition<T> {\n fetchPage: (props: PageCacheFunctionProps<T>) => Promise<T | null>;\n getPageCount?: (pages: T[]) => number | null;\n hasMorePages?: (pages: T[]) => boolean;\n}\n\nexport interface PagedCacheDefinitionFunction<T, Args extends any[]> {\n (...args: Args): PagedCacheDefinition<T>;\n}\n\nexport interface PagedState<T> {\n pages: T[];\n hasMore: boolean;\n pageCount: number | null;\n}\n\nexport interface FetchNextPageOptions {\n ignoreErrors?: boolean;\n}\n\nexport class PagedCache<T, Args extends any[] = []> extends Cache<PagedState<T>, Args> {\n constructor(\n public readonly definition: PagedCacheDefinition<T>,\n args: Args,\n options: CacheOptions<PagedState<T>, Args> = {},\n ) {\n super(async (helpers) => loadPage(definition, helpers, []), args, options, undefined);\n autobind(PagedCache);\n }\n\n async fetchNextPage({ ignoreErrors }: FetchNextPageOptions = {}): Promise<void> {\n const { status, isStale, isUpdating, value } = this.state.get();\n\n if (status === 'error') {\n if (ignoreErrors) return;\n throw new Error('Cannot fetch next page while cache is in error state');\n }\n\n if (isUpdating) {\n if (ignoreErrors) return;\n throw new Error('Cannot fetch next page while another page is being fetched');\n }\n\n if (status === 'pending' || isStale) {\n await this.get().catch(() => {});\n return;\n }\n\n this.stalePromise = this.calculatedValue?.value;\n\n const ac = new AbortController();\n const promise = loadPage(\n this.definition,\n {\n use() {\n throw new Error('Not implemented');\n },\n connect() {\n throw new Error('Not implemented');\n },\n signal: ac.signal,\n },\n value.pages,\n );\n\n this.updateValue(promise);\n\n try {\n await promise;\n } catch (error) {\n if (ignoreErrors) return;\n throw error;\n }\n }\n}\n\nasync function loadPage<T>(\n { fetchPage, hasMorePages, getPageCount }: PagedCacheDefinition<T>,\n helpers: CalculationActions<Promise<PagedState<T>>>,\n oldPages: T[],\n) {\n const page = await fetchPage({\n ...helpers,\n pages: oldPages,\n prevPage: oldPages.length > 0 ? oldPages[oldPages.length - 1]! : null,\n });\n\n const pages = page === null ? oldPages : oldPages.concat(page);\n const pageCount = getPageCount?.(pages) ?? null;\n const hasMore =\n page === null\n ? false\n : hasMorePages\n ? hasMorePages(pages)\n : pageCount !== null\n ? pages.length < pageCount\n : true;\n\n return { pages, hasMore, pageCount };\n}\n\nfunction createPaged<T, Args extends any[] = []>(\n definition: PagedCacheDefinitionFunction<T, Args>,\n options?: CacheOptions<PagedState<T>, Args>,\n): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>>;\n\nfunction createPaged<T>(\n definition: PagedCacheDefinition<T>,\n options?: CacheOptions<PagedState<T>, []>,\n): CreateCacheResult<PagedState<T>, [], PagedCache<T, []>>;\n\nfunction createPaged<T, Args extends any[] = []>(\n definition: PagedCacheDefinitionFunction<T, Args> | PagedCacheDefinition<T>,\n options?: CacheOptions<PagedState<T>, Args>,\n): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>> {\n return internalCreate<PagedState<T>, Args, PagedCache<T, Args>>((args, options) => {\n if (definition instanceof Function) {\n definition = definition(...args);\n }\n return new PagedCache(definition, args, options);\n }, options);\n}\n\nexport const createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> } =\n /* @__PURE__ */ Object.assign(createPaged, {\n defaultOptions: defaultCacheOptions,\n });\n","export function findOrDefault<T>(\n array: T[],\n predicate: (item: T) => boolean,\n defaultValue: T | (() => T),\n): T {\n const index = array.findIndex(predicate);\n\n if (index >= 0) {\n return array[index]!;\n }\n\n const value = defaultValue instanceof Function ? defaultValue() : defaultValue;\n array.push(value);\n return value;\n}\n"],"mappings":";;;;;;;;;AAuCA,IAAa,aAAb,MAAa,mBAA+CA,oBAA2B;CACrF,YACE,AAAgBC,YAChB,MACA,UAA6C,IAC7C;AACA,QAAM,OAAO,YAAY,SAAS,YAAY,SAAS,KAAK,MAAM,SAAS;EAJ3D;AAKhB,yBAAS;;CAGX,MAAM,cAAc,EAAE,iBAAuC,IAAmB;EAC9E,MAAM,EAAE,QAAQ,SAAS,YAAY,UAAU,KAAK,MAAM;AAE1D,MAAI,WAAW,SAAS;AACtB,OAAI,aAAc;AAClB,SAAM,IAAI,MAAM;;AAGlB,MAAI,YAAY;AACd,OAAI,aAAc;AAClB,SAAM,IAAI,MAAM;;AAGlB,MAAI,WAAW,aAAa,SAAS;AACnC,SAAM,KAAK,MAAM,YAAY;AAC7B;;AAGF,OAAK,eAAe,KAAK,iBAAiB;EAE1C,MAAM,KAAK,IAAI;EACf,MAAM,UAAU,SACd,KAAK,YACL;GACE,MAAM;AACJ,UAAM,IAAI,MAAM;;GAElB,UAAU;AACR,UAAM,IAAI,MAAM;;GAElB,QAAQ,GAAG;KAEb,MAAM;AAGR,OAAK,YAAY;AAEjB,MAAI;AACF,SAAM;WACC,OAAO;AACd,OAAI,aAAc;AAClB,SAAM;;;;AAKZ,eAAe,SACb,EAAE,WAAW,cAAc,gBAC3B,SACA,UACA;CACA,MAAM,OAAO,MAAM,UAAU;EAC3B,GAAG;EACH,OAAO;EACP,UAAU,SAAS,SAAS,IAAI,SAAS,SAAS,SAAS,KAAM;;CAGnE,MAAM,QAAQ,SAAS,OAAO,WAAW,SAAS,OAAO;CACzD,MAAM,YAAY,eAAe,UAAU;CAC3C,MAAM,UACJ,SAAS,OACL,QACA,eACE,aAAa,SACb,cAAc,OACZ,MAAM,SAAS,YACf;AAEV,QAAO;EAAE;EAAO;EAAS;;;AAa3B,SAAS,YACP,YACA,SAC6D;AAC7D,QAAOC,8BAA0D,MAAM,cAAY;AACjF,MAAI,sBAAsB,SACxB,cAAa,WAAW,GAAG;AAE7B,SAAO,IAAI,WAAW,YAAY,MAAMC;IACvC;;AAGL,MAAaC,mBACK,uBAAO,OAAO,aAAa,EACzC,gBAAgBC;;;;AChJpB,SAAgB,cACd,OACA,WACA,cACG;CACH,MAAM,QAAQ,MAAM,UAAU;AAE9B,KAAI,SAAS,EACX,QAAO,MAAM;CAGf,MAAM,QAAQ,wBAAwB,WAAW,iBAAiB;AAClE,OAAM,KAAK;AACX,QAAO"}
|
package/dist/index.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ import { Patch, diff } from "./diff-BQ8bB3Wk.cjs";
|
|
|
4
4
|
import { Persist, PersistOptions, PersistStorage, PersistStorageBase, PersistStorageWithKeys, PersistStorageWithLength, PersistStorageWithListItems, persist } from "./persist-D7MAsyyW.cjs";
|
|
5
5
|
|
|
6
6
|
//#region src/core/pagedCache.d.ts
|
|
7
|
-
interface PageCacheFunctionProps<T> extends CalculationActions<Promise<T
|
|
7
|
+
interface PageCacheFunctionProps<T> extends CalculationActions<Promise<PagedState<T>>> {
|
|
8
8
|
pages: T[];
|
|
9
9
|
prevPage: T | null;
|
|
10
10
|
}
|
|
@@ -13,17 +13,29 @@ interface PageCacheFunction<T> {
|
|
|
13
13
|
}
|
|
14
14
|
interface PagedCacheDefinition<T> {
|
|
15
15
|
fetchPage: (props: PageCacheFunctionProps<T>) => Promise<T | null>;
|
|
16
|
+
getPageCount?: (pages: T[]) => number | null;
|
|
17
|
+
hasMorePages?: (pages: T[]) => boolean;
|
|
16
18
|
}
|
|
17
19
|
interface PagedCacheDefinitionFunction<T, Args extends any[]> {
|
|
18
20
|
(...args: Args): PagedCacheDefinition<T>;
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
interface PagedState<T> {
|
|
23
|
+
pages: T[];
|
|
24
|
+
hasMore: boolean;
|
|
25
|
+
pageCount: number | null;
|
|
26
|
+
}
|
|
27
|
+
interface FetchNextPageOptions {
|
|
28
|
+
ignoreErrors?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare class PagedCache<T, Args extends any[] = []> extends Cache<PagedState<T>, Args> {
|
|
21
31
|
readonly definition: PagedCacheDefinition<T>;
|
|
22
|
-
constructor(definition: PagedCacheDefinition<T>, args: Args, options?: CacheOptions<T
|
|
23
|
-
fetchNextPage(
|
|
32
|
+
constructor(definition: PagedCacheDefinition<T>, args: Args, options?: CacheOptions<PagedState<T>, Args>);
|
|
33
|
+
fetchNextPage({
|
|
34
|
+
ignoreErrors
|
|
35
|
+
}?: FetchNextPageOptions): Promise<void>;
|
|
24
36
|
}
|
|
25
|
-
declare function createPaged<T, Args extends any[] = []>(definition: PagedCacheDefinitionFunction<T, Args>, options?: CacheOptions<T
|
|
26
|
-
declare function createPaged<T>(definition: PagedCacheDefinition<T>, options?: CacheOptions<T
|
|
37
|
+
declare function createPaged<T, Args extends any[] = []>(definition: PagedCacheDefinitionFunction<T, Args>, options?: CacheOptions<PagedState<T>, Args>): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>>;
|
|
38
|
+
declare function createPaged<T>(definition: PagedCacheDefinition<T>, options?: CacheOptions<PagedState<T>, []>): CreateCacheResult<PagedState<T>, [], PagedCache<T, []>>;
|
|
27
39
|
declare const createPagedCache: typeof createPaged & {
|
|
28
40
|
defaultOptions: CacheOptions<any, any>;
|
|
29
41
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Patch, diff } from "./diff-gZezL04N.js";
|
|
|
4
4
|
import { Persist, PersistOptions, PersistStorage, PersistStorageBase, PersistStorageWithKeys, PersistStorageWithLength, PersistStorageWithListItems, persist } from "./persist-CPjpg6D0.js";
|
|
5
5
|
|
|
6
6
|
//#region src/core/pagedCache.d.ts
|
|
7
|
-
interface PageCacheFunctionProps<T> extends CalculationActions<Promise<T
|
|
7
|
+
interface PageCacheFunctionProps<T> extends CalculationActions<Promise<PagedState<T>>> {
|
|
8
8
|
pages: T[];
|
|
9
9
|
prevPage: T | null;
|
|
10
10
|
}
|
|
@@ -13,17 +13,29 @@ interface PageCacheFunction<T> {
|
|
|
13
13
|
}
|
|
14
14
|
interface PagedCacheDefinition<T> {
|
|
15
15
|
fetchPage: (props: PageCacheFunctionProps<T>) => Promise<T | null>;
|
|
16
|
+
getPageCount?: (pages: T[]) => number | null;
|
|
17
|
+
hasMorePages?: (pages: T[]) => boolean;
|
|
16
18
|
}
|
|
17
19
|
interface PagedCacheDefinitionFunction<T, Args extends any[]> {
|
|
18
20
|
(...args: Args): PagedCacheDefinition<T>;
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
interface PagedState<T> {
|
|
23
|
+
pages: T[];
|
|
24
|
+
hasMore: boolean;
|
|
25
|
+
pageCount: number | null;
|
|
26
|
+
}
|
|
27
|
+
interface FetchNextPageOptions {
|
|
28
|
+
ignoreErrors?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare class PagedCache<T, Args extends any[] = []> extends Cache<PagedState<T>, Args> {
|
|
21
31
|
readonly definition: PagedCacheDefinition<T>;
|
|
22
|
-
constructor(definition: PagedCacheDefinition<T>, args: Args, options?: CacheOptions<T
|
|
23
|
-
fetchNextPage(
|
|
32
|
+
constructor(definition: PagedCacheDefinition<T>, args: Args, options?: CacheOptions<PagedState<T>, Args>);
|
|
33
|
+
fetchNextPage({
|
|
34
|
+
ignoreErrors
|
|
35
|
+
}?: FetchNextPageOptions): Promise<void>;
|
|
24
36
|
}
|
|
25
|
-
declare function createPaged<T, Args extends any[] = []>(definition: PagedCacheDefinitionFunction<T, Args>, options?: CacheOptions<T
|
|
26
|
-
declare function createPaged<T>(definition: PagedCacheDefinition<T>, options?: CacheOptions<T
|
|
37
|
+
declare function createPaged<T, Args extends any[] = []>(definition: PagedCacheDefinitionFunction<T, Args>, options?: CacheOptions<PagedState<T>, Args>): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>>;
|
|
38
|
+
declare function createPaged<T>(definition: PagedCacheDefinition<T>, options?: CacheOptions<PagedState<T>, []>): CreateCacheResult<PagedState<T>, [], PagedCache<T, []>>;
|
|
27
39
|
declare const createPagedCache: typeof createPaged & {
|
|
28
40
|
defaultOptions: CacheOptions<any, any>;
|
|
29
41
|
};
|
package/dist/index.js
CHANGED
|
@@ -9,43 +9,59 @@ import { persist } from "./persist-Btgg3qGU.js";
|
|
|
9
9
|
//#region src/core/pagedCache.ts
|
|
10
10
|
var PagedCache = class PagedCache extends Cache {
|
|
11
11
|
constructor(definition, args, options = {}) {
|
|
12
|
-
super(async (helpers) =>
|
|
13
|
-
const { fetchPage } = definition;
|
|
14
|
-
const page = await fetchPage({
|
|
15
|
-
...helpers,
|
|
16
|
-
pages: [],
|
|
17
|
-
prevPage: null
|
|
18
|
-
});
|
|
19
|
-
return page === null ? [] : [page];
|
|
20
|
-
}, args, options, void 0);
|
|
12
|
+
super(async (helpers) => loadPage(definition, helpers, []), args, options, void 0);
|
|
21
13
|
this.definition = definition;
|
|
22
14
|
autobind(PagedCache);
|
|
23
15
|
}
|
|
24
|
-
async fetchNextPage() {
|
|
25
|
-
const { fetchPage } = this.definition;
|
|
16
|
+
async fetchNextPage({ ignoreErrors } = {}) {
|
|
26
17
|
const { status, isStale, isUpdating, value } = this.state.get();
|
|
27
|
-
if (status
|
|
18
|
+
if (status === "error") {
|
|
19
|
+
if (ignoreErrors) return;
|
|
20
|
+
throw new Error("Cannot fetch next page while cache is in error state");
|
|
21
|
+
}
|
|
22
|
+
if (isUpdating) {
|
|
23
|
+
if (ignoreErrors) return;
|
|
24
|
+
throw new Error("Cannot fetch next page while another page is being fetched");
|
|
25
|
+
}
|
|
26
|
+
if (status === "pending" || isStale) {
|
|
27
|
+
await this.get().catch(() => {});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
28
30
|
this.stalePromise = this.calculatedValue?.value;
|
|
29
31
|
const ac = new AbortController();
|
|
30
|
-
const
|
|
32
|
+
const promise = loadPage(this.definition, {
|
|
31
33
|
use() {
|
|
32
34
|
throw new Error("Not implemented");
|
|
33
35
|
},
|
|
34
36
|
connect() {
|
|
35
37
|
throw new Error("Not implemented");
|
|
36
38
|
},
|
|
37
|
-
signal: ac.signal
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return pagePromise;
|
|
39
|
+
signal: ac.signal
|
|
40
|
+
}, value.pages);
|
|
41
|
+
this.updateValue(promise);
|
|
42
|
+
try {
|
|
43
|
+
await promise;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (ignoreErrors) return;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
47
48
|
}
|
|
48
49
|
};
|
|
50
|
+
async function loadPage({ fetchPage, hasMorePages, getPageCount }, helpers, oldPages) {
|
|
51
|
+
const page = await fetchPage({
|
|
52
|
+
...helpers,
|
|
53
|
+
pages: oldPages,
|
|
54
|
+
prevPage: oldPages.length > 0 ? oldPages[oldPages.length - 1] : null
|
|
55
|
+
});
|
|
56
|
+
const pages = page === null ? oldPages : oldPages.concat(page);
|
|
57
|
+
const pageCount = getPageCount?.(pages) ?? null;
|
|
58
|
+
const hasMore = page === null ? false : hasMorePages ? hasMorePages(pages) : pageCount !== null ? pages.length < pageCount : true;
|
|
59
|
+
return {
|
|
60
|
+
pages,
|
|
61
|
+
hasMore,
|
|
62
|
+
pageCount
|
|
63
|
+
};
|
|
64
|
+
}
|
|
49
65
|
function createPaged(definition, options) {
|
|
50
66
|
return internalCreate((args, options$1) => {
|
|
51
67
|
if (definition instanceof Function) definition = definition(...args);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["definition: PagedCacheDefinition<T>","options","createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> }"],"sources":["../src/core/pagedCache.ts","../src/lib/updateHelpers.ts"],"sourcesContent":["import {\n Cache,\n defaultCacheOptions,\n internalCreate,\n type CacheOptions,\n type CreateCacheResult,\n} from '@core/cache';\nimport type { CalculationActions } from '@core/commonTypes';\nimport { autobind } from '@lib/autobind';\n\nexport interface PageCacheFunctionProps<T> extends CalculationActions<Promise<T
|
|
1
|
+
{"version":3,"file":"index.js","names":["definition: PagedCacheDefinition<T>","options","createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> }"],"sources":["../src/core/pagedCache.ts","../src/lib/updateHelpers.ts"],"sourcesContent":["import {\n Cache,\n defaultCacheOptions,\n internalCreate,\n type CacheOptions,\n type CreateCacheResult,\n} from '@core/cache';\nimport type { CalculationActions } from '@core/commonTypes';\nimport { autobind } from '@lib/autobind';\n\nexport interface PageCacheFunctionProps<T> extends CalculationActions<Promise<PagedState<T>>> {\n pages: T[];\n prevPage: T | null;\n}\n\nexport interface PageCacheFunction<T> {\n (props: PageCacheFunctionProps<T>): Promise<T | null>;\n}\n\nexport interface PagedCacheDefinition<T> {\n fetchPage: (props: PageCacheFunctionProps<T>) => Promise<T | null>;\n getPageCount?: (pages: T[]) => number | null;\n hasMorePages?: (pages: T[]) => boolean;\n}\n\nexport interface PagedCacheDefinitionFunction<T, Args extends any[]> {\n (...args: Args): PagedCacheDefinition<T>;\n}\n\nexport interface PagedState<T> {\n pages: T[];\n hasMore: boolean;\n pageCount: number | null;\n}\n\nexport interface FetchNextPageOptions {\n ignoreErrors?: boolean;\n}\n\nexport class PagedCache<T, Args extends any[] = []> extends Cache<PagedState<T>, Args> {\n constructor(\n public readonly definition: PagedCacheDefinition<T>,\n args: Args,\n options: CacheOptions<PagedState<T>, Args> = {},\n ) {\n super(async (helpers) => loadPage(definition, helpers, []), args, options, undefined);\n autobind(PagedCache);\n }\n\n async fetchNextPage({ ignoreErrors }: FetchNextPageOptions = {}): Promise<void> {\n const { status, isStale, isUpdating, value } = this.state.get();\n\n if (status === 'error') {\n if (ignoreErrors) return;\n throw new Error('Cannot fetch next page while cache is in error state');\n }\n\n if (isUpdating) {\n if (ignoreErrors) return;\n throw new Error('Cannot fetch next page while another page is being fetched');\n }\n\n if (status === 'pending' || isStale) {\n await this.get().catch(() => {});\n return;\n }\n\n this.stalePromise = this.calculatedValue?.value;\n\n const ac = new AbortController();\n const promise = loadPage(\n this.definition,\n {\n use() {\n throw new Error('Not implemented');\n },\n connect() {\n throw new Error('Not implemented');\n },\n signal: ac.signal,\n },\n value.pages,\n );\n\n this.updateValue(promise);\n\n try {\n await promise;\n } catch (error) {\n if (ignoreErrors) return;\n throw error;\n }\n }\n}\n\nasync function loadPage<T>(\n { fetchPage, hasMorePages, getPageCount }: PagedCacheDefinition<T>,\n helpers: CalculationActions<Promise<PagedState<T>>>,\n oldPages: T[],\n) {\n const page = await fetchPage({\n ...helpers,\n pages: oldPages,\n prevPage: oldPages.length > 0 ? oldPages[oldPages.length - 1]! : null,\n });\n\n const pages = page === null ? oldPages : oldPages.concat(page);\n const pageCount = getPageCount?.(pages) ?? null;\n const hasMore =\n page === null\n ? false\n : hasMorePages\n ? hasMorePages(pages)\n : pageCount !== null\n ? pages.length < pageCount\n : true;\n\n return { pages, hasMore, pageCount };\n}\n\nfunction createPaged<T, Args extends any[] = []>(\n definition: PagedCacheDefinitionFunction<T, Args>,\n options?: CacheOptions<PagedState<T>, Args>,\n): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>>;\n\nfunction createPaged<T>(\n definition: PagedCacheDefinition<T>,\n options?: CacheOptions<PagedState<T>, []>,\n): CreateCacheResult<PagedState<T>, [], PagedCache<T, []>>;\n\nfunction createPaged<T, Args extends any[] = []>(\n definition: PagedCacheDefinitionFunction<T, Args> | PagedCacheDefinition<T>,\n options?: CacheOptions<PagedState<T>, Args>,\n): CreateCacheResult<PagedState<T>, Args, PagedCache<T, Args>> {\n return internalCreate<PagedState<T>, Args, PagedCache<T, Args>>((args, options) => {\n if (definition instanceof Function) {\n definition = definition(...args);\n }\n return new PagedCache(definition, args, options);\n }, options);\n}\n\nexport const createPagedCache: typeof createPaged & { defaultOptions: CacheOptions<any, any> } =\n /* @__PURE__ */ Object.assign(createPaged, {\n defaultOptions: defaultCacheOptions,\n });\n","export function findOrDefault<T>(\n array: T[],\n predicate: (item: T) => boolean,\n defaultValue: T | (() => T),\n): T {\n const index = array.findIndex(predicate);\n\n if (index >= 0) {\n return array[index]!;\n }\n\n const value = defaultValue instanceof Function ? defaultValue() : defaultValue;\n array.push(value);\n return value;\n}\n"],"mappings":";;;;;;;;;AAuCA,IAAa,aAAb,MAAa,mBAA+C,MAA2B;CACrF,YACE,AAAgBA,YAChB,MACA,UAA6C,IAC7C;AACA,QAAM,OAAO,YAAY,SAAS,YAAY,SAAS,KAAK,MAAM,SAAS;EAJ3D;AAKhB,WAAS;;CAGX,MAAM,cAAc,EAAE,iBAAuC,IAAmB;EAC9E,MAAM,EAAE,QAAQ,SAAS,YAAY,UAAU,KAAK,MAAM;AAE1D,MAAI,WAAW,SAAS;AACtB,OAAI,aAAc;AAClB,SAAM,IAAI,MAAM;;AAGlB,MAAI,YAAY;AACd,OAAI,aAAc;AAClB,SAAM,IAAI,MAAM;;AAGlB,MAAI,WAAW,aAAa,SAAS;AACnC,SAAM,KAAK,MAAM,YAAY;AAC7B;;AAGF,OAAK,eAAe,KAAK,iBAAiB;EAE1C,MAAM,KAAK,IAAI;EACf,MAAM,UAAU,SACd,KAAK,YACL;GACE,MAAM;AACJ,UAAM,IAAI,MAAM;;GAElB,UAAU;AACR,UAAM,IAAI,MAAM;;GAElB,QAAQ,GAAG;KAEb,MAAM;AAGR,OAAK,YAAY;AAEjB,MAAI;AACF,SAAM;WACC,OAAO;AACd,OAAI,aAAc;AAClB,SAAM;;;;AAKZ,eAAe,SACb,EAAE,WAAW,cAAc,gBAC3B,SACA,UACA;CACA,MAAM,OAAO,MAAM,UAAU;EAC3B,GAAG;EACH,OAAO;EACP,UAAU,SAAS,SAAS,IAAI,SAAS,SAAS,SAAS,KAAM;;CAGnE,MAAM,QAAQ,SAAS,OAAO,WAAW,SAAS,OAAO;CACzD,MAAM,YAAY,eAAe,UAAU;CAC3C,MAAM,UACJ,SAAS,OACL,QACA,eACE,aAAa,SACb,cAAc,OACZ,MAAM,SAAS,YACf;AAEV,QAAO;EAAE;EAAO;EAAS;;;AAa3B,SAAS,YACP,YACA,SAC6D;AAC7D,QAAO,gBAA0D,MAAM,cAAY;AACjF,MAAI,sBAAsB,SACxB,cAAa,WAAW,GAAG;AAE7B,SAAO,IAAI,WAAW,YAAY,MAAMC;IACvC;;AAGL,MAAaC,mBACK,uBAAO,OAAO,aAAa,EACzC,gBAAgB;;;;AChJpB,SAAgB,cACd,OACA,WACA,cACG;CACH,MAAM,QAAQ,MAAM,UAAU;AAE9B,KAAI,SAAS,EACX,QAAO,MAAM;CAGf,MAAM,QAAQ,wBAAwB,WAAW,iBAAiB;AAClE,OAAM,KAAK;AACX,QAAO"}
|