@vielzeug/sourcerer 26.10.15 → 26.10.17
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/LICENSE +21 -0
- package/dist/asyncSource.cjs.map +1 -1
- package/dist/asyncSource.js +63 -1
- package/dist/asyncSource.js.map +1 -1
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.js +108 -1
- package/dist/cursorSource.js.map +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.js +13 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.js +7 -1
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.js +99 -1
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.js +70 -1
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.js +114 -1
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.cjs.map +1 -1
- package/dist/pagination.js +24 -1
- package/dist/pagination.js.map +1 -1
- package/dist/requestController.cjs.map +1 -1
- package/dist/requestController.js +30 -1
- package/dist/requestController.js.map +1 -1
- package/dist/resourceSource.cjs.map +1 -1
- package/dist/resourceSource.js +50 -1
- package/dist/resourceSource.js.map +1 -1
- package/dist/sourceStore.cjs.map +1 -1
- package/dist/sourceStore.js +37 -1
- package/dist/sourceStore.js.map +1 -1
- package/dist/sourcerer.cjs +1 -1
- package/dist/sourcerer.cjs.map +1 -1
- package/dist/sourcerer.iife.js +1 -1
- package/dist/sourcerer.iife.js.map +1 -1
- package/dist/sourcerer.js +571 -1
- package/dist/sourcerer.js.map +1 -1
- package/package.json +6 -5
package/dist/localSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.cjs","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAWA,SAAgB,kBACd,EACA,EAAwC,CAAC,EAChB,CACzB,
|
|
1
|
+
{"version":3,"file":"localSource.cjs","names":["positiveInteger","createPagePagination","createSourceStore","SourcererDisposedError","require_errors","require_sourceStore","require_pagination","allItems","items","page","pageSize","config","params","filtered","item","pagination","start","store","left","key","right","next","current","sameItems","index","normalized","nextPage","source","nextItems","nextPageSize","nextParams"],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"A,6FAWA,SAAgB,kBACd,EACA,EAAwC,CAAC,EAChB,CACzB,IAAIO,EAAWC,EACXC,EAAO,EACPC,EAAWV,EAAAA,gBAAgBW,EAAO,UAAY,GAAI,UAAU,EAC5DC,EAASD,EAAO,OAEd,eAAiD,CACrD,IAAME,EAAWF,EAAO,OAASJ,EAAS,OAAQ,GAASI,EAAO,SAASG,EAAMF,CAAM,CAAC,EAAIL,EACtFQ,EAAad,EAAAA,qBAAqBQ,EAAMC,EAAUG,EAAS,MAAM,EACvE,GAAIJ,EAAOM,EAAW,UAEpB,MADA,GAAOA,EAAW,UACX,WAAW,EAEpB,IAAMC,GAASP,EAAO,GAAKC,EAE3B,MAAO,CACL,MAAO,KACP,MAAOG,EAAS,MAAMG,EAAOA,EAAQN,CAAQ,EAC7C,QAAS,GACT,aACA,QACF,CACF,EAEMO,EAAQf,EAAAA,kBAAkB,WAAW,CAAC,EACtC,eAAyB,CAC7B,GAAIe,EAAM,SAAU,MAAM,IAAId,EAAAA,sBAChC,EACM,gBAAkB,EAAsB,IAC5C,OAAO,KAAKe,CAAI,CAAC,CAAC,MAAO,GAAQA,EAAKC,KAAiCC,EAAMD,EAA4B,EACrG,WAAqB,CACzB,IAAME,EAAO,WAAW,EAClBC,EAAUL,EAAM,MAChBM,EACJF,EAAK,MAAM,SAAWC,EAAQ,MAAM,QACpCD,EAAK,MAAM,OAAO,EAAM,IAAU,OAAO,GAAGP,EAAMQ,EAAQ,MAAME,EAAM,CAAC,EAErE,OAAO,GAAGH,EAAK,OAAQC,EAAQ,MAAM,GAAKC,GAAa,eAAeF,EAAK,WAAYC,EAAQ,UAAU,GAE7GL,EAAM,IAAII,CAAI,CAChB,EACM,KAAQ,GAA2B,CACvC,WAAW,EACX,IAAMI,EAAa,KAAK,IAAIzB,EAAAA,gBAAgB0B,EAAU,MAAM,EAAGT,EAAM,MAAM,WAAW,SAAS,EAC3FQ,IAAehB,IACnB,EAAOgB,EACP,OAAO,EACT,EAEME,EAAkC,CACtC,IAAI,gBAAiB,CACnB,OAAOV,EAAM,cACf,EAEA,QAASA,EAAM,QAEf,IAAI,UAAW,CACb,OAAOA,EAAM,QACf,EAEA,OAAQ,CACN,KAAK,CAAC,CACR,EAEA,KAEA,MAAO,CACL,KAAKA,EAAM,MAAM,WAAW,SAAS,CACvC,EAEA,MAAO,CACDA,EAAM,MAAM,WAAW,QAAS,KAAKR,EAAO,CAAC,EAC5C,WAAW,CAClB,EAEA,UAAW,CACLQ,EAAM,MAAM,WAAW,YAAa,KAAKR,EAAO,CAAC,EAChD,WAAW,CAClB,EAEA,SAAS,EAAW,CAClB,WAAW,EACPmB,IAAcrB,IAClB,EAAWqB,EACX,OAAO,EACT,EAEA,YAAY,EAAc,CACxB,WAAW,EACX,IAAMH,EAAazB,EAAAA,gBAAgB6B,EAAc,UAAU,EACvDJ,IAAef,IACnB,EAAO,EACP,EAAWe,EACX,OAAO,EACT,EAEA,UAAU,EAAY,CACpB,WAAW,EACP,QAAO,GAAGK,EAAYlB,CAAM,IAChC,EAAO,EACP,EAASkB,EACT,OAAO,EACT,EAEA,IAAI,OAAQ,CACV,OAAOb,EAAM,KACf,EAEA,UAAWA,EAAM,UAEjB,CAAC,OAAO,UAAW,CACjBU,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
|
package/dist/localSource.js
CHANGED
|
@@ -1,2 +1,71 @@
|
|
|
1
|
-
import{SourcererDisposedError as e}
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
|
+
import { createSourceStore as t } from "./sourceStore.js";
|
|
3
|
+
import { createPagePagination as n, positiveInteger as r } from "./pagination.js";
|
|
4
|
+
//#region src/localSource.ts
|
|
5
|
+
function createLocalSource(i, a = {}) {
|
|
6
|
+
let o = i, s = 1, c = r(a.pageSize ?? 20, "pageSize"), l = a.params, buildState = () => {
|
|
7
|
+
let e = a.filter ? o.filter((e) => a.filter?.(e, l)) : o, t = n(s, c, e.length);
|
|
8
|
+
if (s > t.pageCount) return s = t.pageCount, buildState();
|
|
9
|
+
let r = (s - 1) * c;
|
|
10
|
+
return {
|
|
11
|
+
error: null,
|
|
12
|
+
items: e.slice(r, r + c),
|
|
13
|
+
loading: !1,
|
|
14
|
+
pagination: t,
|
|
15
|
+
params: l
|
|
16
|
+
};
|
|
17
|
+
}, u = t(buildState()), assertLive = () => {
|
|
18
|
+
if (u.disposed) throw new e();
|
|
19
|
+
}, samePagination = (e, t) => Object.keys(e).every((n) => e[n] === t[n]), commit = () => {
|
|
20
|
+
let e = buildState(), t = u.value, n = e.items.length === t.items.length && e.items.every((e, n) => Object.is(e, t.items[n]));
|
|
21
|
+
Object.is(e.params, t.params) && n && samePagination(e.pagination, t.pagination) || u.set(e);
|
|
22
|
+
}, goTo = (e) => {
|
|
23
|
+
assertLive();
|
|
24
|
+
let t = Math.min(r(e, "page"), u.value.pagination.pageCount);
|
|
25
|
+
t !== s && (s = t, commit());
|
|
26
|
+
}, d = {
|
|
27
|
+
get disposalSignal() {
|
|
28
|
+
return u.disposalSignal;
|
|
29
|
+
},
|
|
30
|
+
dispose: u.dispose,
|
|
31
|
+
get disposed() {
|
|
32
|
+
return u.disposed;
|
|
33
|
+
},
|
|
34
|
+
first() {
|
|
35
|
+
goTo(1);
|
|
36
|
+
},
|
|
37
|
+
goTo,
|
|
38
|
+
last() {
|
|
39
|
+
goTo(u.value.pagination.pageCount);
|
|
40
|
+
},
|
|
41
|
+
next() {
|
|
42
|
+
u.value.pagination.hasNext ? goTo(s + 1) : assertLive();
|
|
43
|
+
},
|
|
44
|
+
previous() {
|
|
45
|
+
u.value.pagination.hasPrevious ? goTo(s - 1) : assertLive();
|
|
46
|
+
},
|
|
47
|
+
setItems(e) {
|
|
48
|
+
assertLive(), e !== o && (o = e, commit());
|
|
49
|
+
},
|
|
50
|
+
setPageSize(e) {
|
|
51
|
+
assertLive();
|
|
52
|
+
let t = r(e, "pageSize");
|
|
53
|
+
t !== c && (s = 1, c = t, commit());
|
|
54
|
+
},
|
|
55
|
+
setParams(e) {
|
|
56
|
+
assertLive(), !Object.is(e, l) && (s = 1, l = e, commit());
|
|
57
|
+
},
|
|
58
|
+
get state() {
|
|
59
|
+
return u.value;
|
|
60
|
+
},
|
|
61
|
+
subscribe: u.subscribe,
|
|
62
|
+
[Symbol.dispose]() {
|
|
63
|
+
d.dispose();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return d;
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
export { createLocalSource };
|
|
70
|
+
|
|
2
71
|
//# sourceMappingURL=localSource.js.map
|
package/dist/localSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.js","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"localSource.js","names":["allItems","items","page","pageSize","positiveInteger","config","params","filtered","item","pagination","createPagePagination","start","store","createSourceStore","SourcererDisposedError","left","key","right","next","current","sameItems","index","normalized","nextPage","source","nextItems","nextPageSize","nextParams"],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":";;;;AAWA,SAAgB,kBACd,GACA,IAAwC,CAAC,GAChB;CACzB,IAAIA,IAAWC,GACXC,IAAO,GACPC,IAAWC,EAAgBC,EAAO,YAAY,IAAI,UAAU,GAC5DC,IAASD,EAAO,QAEd,mBAAiD;EACrD,IAAME,IAAWF,EAAO,SAASL,EAAS,QAAQ,MAASK,EAAO,SAASG,GAAMF,CAAM,CAAC,IAAIN,GACtFS,IAAaC,EAAqBR,GAAMC,GAAUI,EAAS,MAAM;EACvE,IAAIL,IAAOO,EAAW,WAEpB,OADA,IAAOA,EAAW,WACX,WAAW;EAEpB,IAAME,KAAST,IAAO,KAAKC;EAE3B,OAAO;GACL,OAAO;GACP,OAAOI,EAAS,MAAMI,GAAOA,IAAQR,CAAQ;GAC7C,SAAS;GACT;GACA;EACF;CACF,GAEMS,IAAQC,EAAkB,WAAW,CAAC,GACtC,mBAAyB;EAC7B,IAAID,EAAM,UAAU,MAAM,IAAIE,EAAuB;CACvD,GACM,kBAAkB,GAAsB,MAC5C,OAAO,KAAKC,CAAI,CAAC,CAAC,OAAO,MAAQA,EAAKC,OAAiCC,EAAMD,EAA4B,GACrG,eAAqB;EACzB,IAAME,IAAO,WAAW,GAClBC,IAAUP,EAAM,OAChBQ,IACJF,EAAK,MAAM,WAAWC,EAAQ,MAAM,UACpCD,EAAK,MAAM,OAAO,GAAM,MAAU,OAAO,GAAGV,GAAMW,EAAQ,MAAME,EAAM,CAAC;EAEzE,AAAI,OAAO,GAAGH,EAAK,QAAQC,EAAQ,MAAM,KAAKC,KAAa,eAAeF,EAAK,YAAYC,EAAQ,UAAU,KAE7GP,EAAM,IAAIM,CAAI;CAChB,GACM,QAAQ,MAA2B;EACvC,WAAW;EACX,IAAMI,IAAa,KAAK,IAAIlB,EAAgBmB,GAAU,MAAM,GAAGX,EAAM,MAAM,WAAW,SAAS;EAC/F,AAAIU,MAAepB,MACnB,IAAOoB,GACP,OAAO;CACT,GAEME,IAAkC;EACtC,IAAI,iBAAiB;GACnB,OAAOZ,EAAM;EACf;EAEA,SAASA,EAAM;EAEf,IAAI,WAAW;GACb,OAAOA,EAAM;EACf;EAEA,QAAQ;GACN,KAAK,CAAC;EACR;EAEA;EAEA,OAAO;GACL,KAAKA,EAAM,MAAM,WAAW,SAAS;EACvC;EAEA,OAAO;GACL,AAAIA,EAAM,MAAM,WAAW,UAAS,KAAKV,IAAO,CAAC,IAC5C,WAAW;EAClB;EAEA,WAAW;GACT,AAAIU,EAAM,MAAM,WAAW,cAAa,KAAKV,IAAO,CAAC,IAChD,WAAW;EAClB;EAEA,SAAS,GAAW;GAElB,AADA,WAAW,GACPuB,MAAczB,MAClB,IAAWyB,GACX,OAAO;EACT;EAEA,YAAY,GAAc;GACxB,WAAW;GACX,IAAMH,IAAalB,EAAgBsB,GAAc,UAAU;GAC3D,AAAIJ,MAAenB,MACnB,IAAO,GACP,IAAWmB,GACX,OAAO;EACT;EAEA,UAAU,GAAY;GAEpB,AADA,WAAW,GACP,QAAO,GAAGK,GAAYrB,CAAM,MAChC,IAAO,GACP,IAASqB,GACT,OAAO;EACT;EAEA,IAAI,QAAQ;GACV,OAAOf,EAAM;EACf;EAEA,WAAWA,EAAM;EAEjB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAOY;AACT"}
|
package/dist/pageSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");let r=require("@vielzeug/arsenal");function createPageSource(i){let a={page:1,pageSize:n.positiveInteger(i.pageSize??20,`pageSize`),params:i.params},o=!1,s=t.createAsyncSource({error:null,items:[],loading:!1,pagination:n.createPagePagination(a.page,a.pageSize,0),params:a.params}),assertLive=()=>{if(s.disposed)throw new e.SourcererDisposedError},isCommitted=e=>s.state.pagination.page===e.page&&s.state.pagination.pageSize===e.pageSize&&Object.is(s.state.params,e.params),fetch=e=>s.fetch({commit:()=>{o=!0,a=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>i.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:n.createPagePagination(e.page,e.pageSize,n.totalItems(t.totalItems)),params:e.params})}),normalizePage=e=>o?Math.min(n.positiveInteger(e,`page`),s.state.pagination.pageCount):n.positiveInteger(e,`page`),goTo=async e=>{assertLive();let t=normalizePage(e);
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");let r=require("@vielzeug/arsenal");function createPageSource(i){let a={page:1,pageSize:n.positiveInteger(i.pageSize??20,`pageSize`),params:i.params},o=!1,s=t.createAsyncSource({error:null,items:[],loading:!1,pagination:n.createPagePagination(a.page,a.pageSize,0),params:a.params}),assertLive=()=>{if(s.disposed)throw new e.SourcererDisposedError},isCommitted=e=>s.state.pagination.page===e.page&&s.state.pagination.pageSize===e.pageSize&&Object.is(s.state.params,e.params),fetch=e=>s.fetch({commit:()=>{o=!0,a=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>i.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:n.createPagePagination(e.page,e.pageSize,n.totalItems(t.totalItems)),params:e.params})}),normalizePage=e=>o?Math.min(n.positiveInteger(e,`page`),s.state.pagination.pageCount):n.positiveInteger(e,`page`),goTo=async e=>{assertLive();let t=normalizePage(e);t===a.page?!s.state.loading&&(!o||!isCommitted(a))&&await fetch(a):(a={...a,page:t},await fetch(a))},c={async apply(e){assertLive();let t=e.params===void 0?a.params:e.params;(0,r.isEqual)(t,a.params)?e.page===void 0?!s.state.loading&&(!o||!isCommitted(a))&&await fetch(a):await goTo(e.page):(a={...a,page:e.page===void 0?1:n.positiveInteger(e.page,`page`),params:t},await fetch(a))},get disposalSignal(){return s.disposalSignal},dispose:s.dispose,get disposed(){return s.disposed},first(){return goTo(1)},goTo,last(){return goTo(s.state.pagination.pageCount)},async next(){assertLive(),(!o||a.page<s.state.pagination.pageCount)&&await goTo(a.page+1)},async previous(){assertLive(),a.page>1&&await goTo(a.page-1)},reload(){return fetch(a)},async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);t===a.pageSize&&a.page===1?!s.state.loading&&!isCommitted(a)&&await fetch(a):(a={...a,page:1,pageSize:t},await fetch(a))},async setParams(e){assertLive(),Object.is(e,a.params)?!s.state.loading&&!isCommitted(a)&&await fetch(a):(a={...a,page:1,params:e},await fetch(a))},get state(){return s.state},subscribe:s.subscribe,[Symbol.dispose](){c.dispose()}};return c}exports.createPageSource=createPageSource;
|
|
2
2
|
//# sourceMappingURL=pageSource.cjs.map
|
package/dist/pageSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pageSource.cjs","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const normalizePage = (page: number): number =>\n hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = normalizePage(page);\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n async apply(update) {\n assertLive();\n // An absent `params` means \"not part of this update\", which is how a source whose\n // TParams genuinely allows undefined keeps its params: use setParams for that.\n const params = update.params === undefined ? requested.params : update.params;\n\n if (!isEqual(params, requested.params)) {\n // The committed page count belongs to the previous params, so clamping here\n // would clamp to a stale bound. The requested page is validated and sent as\n // given; the response recomputes pagination from real totals.\n requested = {\n ...requested,\n page: update.page === undefined ? 1 : positiveInteger(update.page, 'page'),\n params,\n };\n await fetch(requested);\n return;\n }\n\n if (update.page !== undefined) {\n await goTo(update.page);\n return;\n }\n\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n },\n\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"gIAUA,SAAgB,iBAAyC,EAA8D,CACrH,
|
|
1
|
+
{"version":3,"file":"pageSource.cjs","names":["positiveInteger","createAsyncSource","createPagePagination","SourcererDisposedError","totalItems","isEqual","require_errors","require_asyncSource","require_pagination","_vielzeug_arsenal","requested","config","hasLoaded","asyncSource","request","previous","result","page","normalized","source","params","update","pageSize"],"sources":["../src/pageSource.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const normalizePage = (page: number): number =>\n hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = normalizePage(page);\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n async apply(update) {\n assertLive();\n // An absent `params` means \"not part of this update\", which is how a source whose\n // TParams genuinely allows undefined keeps its params: use setParams for that.\n const params = update.params === undefined ? requested.params : update.params;\n\n if (!isEqual(params, requested.params)) {\n // The committed page count belongs to the previous params, so clamping here\n // would clamp to a stale bound. The requested page is validated and sent as\n // given; the response recomputes pagination from real totals.\n requested = {\n ...requested,\n page: update.page === undefined ? 1 : positiveInteger(update.page, 'page'),\n params,\n };\n await fetch(requested);\n return;\n }\n\n if (update.page !== undefined) {\n await goTo(update.page);\n return;\n }\n\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n },\n\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"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"}
|
package/dist/pageSource.js
CHANGED
|
@@ -1,2 +1,115 @@
|
|
|
1
|
-
import{SourcererDisposedError as e}
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
|
+
import { createAsyncSource as t } from "./asyncSource.js";
|
|
3
|
+
import { createPagePagination as n, positiveInteger as r, totalItems as i } from "./pagination.js";
|
|
4
|
+
import { isEqual as a } from "@vielzeug/arsenal";
|
|
5
|
+
//#region src/pageSource.ts
|
|
6
|
+
function createPageSource(o) {
|
|
7
|
+
let s = {
|
|
8
|
+
page: 1,
|
|
9
|
+
pageSize: r(o.pageSize ?? 20, "pageSize"),
|
|
10
|
+
params: o.params
|
|
11
|
+
}, c = !1, l = t({
|
|
12
|
+
error: null,
|
|
13
|
+
items: [],
|
|
14
|
+
loading: !1,
|
|
15
|
+
pagination: n(s.page, s.pageSize, 0),
|
|
16
|
+
params: s.params
|
|
17
|
+
}), assertLive = () => {
|
|
18
|
+
if (l.disposed) throw new e();
|
|
19
|
+
}, isCommitted = (e) => l.state.pagination.page === e.page && l.state.pagination.pageSize === e.pageSize && Object.is(l.state.params, e.params), fetch = (e) => l.fetch({
|
|
20
|
+
commit: () => {
|
|
21
|
+
c = !0, s = e;
|
|
22
|
+
},
|
|
23
|
+
failure: (e, t) => ({
|
|
24
|
+
...e,
|
|
25
|
+
error: t,
|
|
26
|
+
loading: !1,
|
|
27
|
+
pendingParams: void 0
|
|
28
|
+
}),
|
|
29
|
+
load: (t) => o.load({
|
|
30
|
+
...e,
|
|
31
|
+
signal: t
|
|
32
|
+
}),
|
|
33
|
+
pending: (t) => ({
|
|
34
|
+
...t,
|
|
35
|
+
error: null,
|
|
36
|
+
loading: !0,
|
|
37
|
+
...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
38
|
+
}),
|
|
39
|
+
success: (t) => ({
|
|
40
|
+
error: null,
|
|
41
|
+
items: [...t.items],
|
|
42
|
+
loading: !1,
|
|
43
|
+
pagination: n(e.page, e.pageSize, i(t.totalItems)),
|
|
44
|
+
params: e.params
|
|
45
|
+
})
|
|
46
|
+
}), normalizePage = (e) => c ? Math.min(r(e, "page"), l.state.pagination.pageCount) : r(e, "page"), goTo = async (e) => {
|
|
47
|
+
assertLive();
|
|
48
|
+
let t = normalizePage(e);
|
|
49
|
+
t === s.page ? !l.state.loading && (!c || !isCommitted(s)) && await fetch(s) : (s = {
|
|
50
|
+
...s,
|
|
51
|
+
page: t
|
|
52
|
+
}, await fetch(s));
|
|
53
|
+
}, u = {
|
|
54
|
+
async apply(e) {
|
|
55
|
+
assertLive();
|
|
56
|
+
let t = e.params === void 0 ? s.params : e.params;
|
|
57
|
+
a(t, s.params) ? e.page === void 0 ? !l.state.loading && (!c || !isCommitted(s)) && await fetch(s) : await goTo(e.page) : (s = {
|
|
58
|
+
...s,
|
|
59
|
+
page: e.page === void 0 ? 1 : r(e.page, "page"),
|
|
60
|
+
params: t
|
|
61
|
+
}, await fetch(s));
|
|
62
|
+
},
|
|
63
|
+
get disposalSignal() {
|
|
64
|
+
return l.disposalSignal;
|
|
65
|
+
},
|
|
66
|
+
dispose: l.dispose,
|
|
67
|
+
get disposed() {
|
|
68
|
+
return l.disposed;
|
|
69
|
+
},
|
|
70
|
+
first() {
|
|
71
|
+
return goTo(1);
|
|
72
|
+
},
|
|
73
|
+
goTo,
|
|
74
|
+
last() {
|
|
75
|
+
return goTo(l.state.pagination.pageCount);
|
|
76
|
+
},
|
|
77
|
+
async next() {
|
|
78
|
+
assertLive(), (!c || s.page < l.state.pagination.pageCount) && await goTo(s.page + 1);
|
|
79
|
+
},
|
|
80
|
+
async previous() {
|
|
81
|
+
assertLive(), s.page > 1 && await goTo(s.page - 1);
|
|
82
|
+
},
|
|
83
|
+
reload() {
|
|
84
|
+
return fetch(s);
|
|
85
|
+
},
|
|
86
|
+
async setPageSize(e) {
|
|
87
|
+
assertLive();
|
|
88
|
+
let t = r(e, "pageSize");
|
|
89
|
+
t === s.pageSize && s.page === 1 ? !l.state.loading && !isCommitted(s) && await fetch(s) : (s = {
|
|
90
|
+
...s,
|
|
91
|
+
page: 1,
|
|
92
|
+
pageSize: t
|
|
93
|
+
}, await fetch(s));
|
|
94
|
+
},
|
|
95
|
+
async setParams(e) {
|
|
96
|
+
assertLive(), Object.is(e, s.params) ? !l.state.loading && !isCommitted(s) && await fetch(s) : (s = {
|
|
97
|
+
...s,
|
|
98
|
+
page: 1,
|
|
99
|
+
params: e
|
|
100
|
+
}, await fetch(s));
|
|
101
|
+
},
|
|
102
|
+
get state() {
|
|
103
|
+
return l.state;
|
|
104
|
+
},
|
|
105
|
+
subscribe: l.subscribe,
|
|
106
|
+
[Symbol.dispose]() {
|
|
107
|
+
u.dispose();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
return u;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
export { createPageSource };
|
|
114
|
+
|
|
2
115
|
//# sourceMappingURL=pageSource.js.map
|
package/dist/pageSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pageSource.js","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const normalizePage = (page: number): number =>\n hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = normalizePage(page);\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n async apply(update) {\n assertLive();\n // An absent `params` means \"not part of this update\", which is how a source whose\n // TParams genuinely allows undefined keeps its params: use setParams for that.\n const params = update.params === undefined ? requested.params : update.params;\n\n if (!isEqual(params, requested.params)) {\n // The committed page count belongs to the previous params, so clamping here\n // would clamp to a stale bound. The requested page is validated and sent as\n // given; the response recomputes pagination from real totals.\n requested = {\n ...requested,\n page: update.page === undefined ? 1 : positiveInteger(update.page, 'page'),\n params,\n };\n await fetch(requested);\n return;\n }\n\n if (update.page !== undefined) {\n await goTo(update.page);\n return;\n }\n\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n },\n\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"pageSource.js","names":["requested","positiveInteger","config","hasLoaded","asyncSource","createAsyncSource","createPagePagination","SourcererDisposedError","request","previous","result","totalItems","page","normalized","source","params","update","isEqual","pageSize"],"sources":["../src/pageSource.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const normalizePage = (page: number): number =>\n hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = normalizePage(page);\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n async apply(update) {\n assertLive();\n // An absent `params` means \"not part of this update\", which is how a source whose\n // TParams genuinely allows undefined keeps its params: use setParams for that.\n const params = update.params === undefined ? requested.params : update.params;\n\n if (!isEqual(params, requested.params)) {\n // The committed page count belongs to the previous params, so clamping here\n // would clamp to a stale bound. The requested page is validated and sent as\n // given; the response recomputes pagination from real totals.\n requested = {\n ...requested,\n page: update.page === undefined ? 1 : positiveInteger(update.page, 'page'),\n params,\n };\n await fetch(requested);\n return;\n }\n\n if (update.page !== undefined) {\n await goTo(update.page);\n return;\n }\n\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n },\n\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"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"}
|
package/dist/pagination.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.cjs","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"gCAGA,SAAgB,gBAAgB,EAAe,EAAsB,CACnE,GAAI,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"pagination.cjs","names":["SourcererConfigurationError","require_errors","value","name","pageCount","total","pageSize","page"],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"A,gCAGA,SAAgB,gBAAgB,EAAe,EAAsB,CACnE,GAAI,CAAC,OAAO,UAAUE,CAAK,GAAKA,EAAQ,EACtC,MAAM,IAAIF,EAAAA,4BAA4B,GAAGG,EAAK,4BAA4B,EAE5E,OAAOD,CACT,CAEA,SAAgB,WAAW,EAAuB,CAChD,GAAI,CAAC,OAAO,UAAUA,CAAK,GAAKA,EAAQ,EACtC,MAAM,IAAIF,EAAAA,4BAA4B,2CAA2C,EAEnF,OAAOE,CACT,CAEA,SAAgB,qBAAqB,EAAc,EAAkB,EAA+B,CAClG,IAAME,EAAY,KAAK,IAAI,EAAG,KAAK,KAAKC,EAAQC,CAAQ,CAAC,EAEzD,MAAO,CACL,QAASC,EAAOH,EAChB,YAAaG,EAAO,EACpB,OACA,YACA,WACA,WAAYF,CACd,CACF,C"}
|
package/dist/pagination.js
CHANGED
|
@@ -1,2 +1,25 @@
|
|
|
1
|
-
import{SourcererConfigurationError as e}from"./errors.js";
|
|
1
|
+
import { SourcererConfigurationError as e } from "./errors.js";
|
|
2
|
+
//#region src/pagination.ts
|
|
3
|
+
function positiveInteger(t, n) {
|
|
4
|
+
if (!Number.isInteger(t) || t < 1) throw new e(`${n} must be a positive integer`);
|
|
5
|
+
return t;
|
|
6
|
+
}
|
|
7
|
+
function totalItems(t) {
|
|
8
|
+
if (!Number.isInteger(t) || t < 0) throw new e("totalItems must be a non-negative integer");
|
|
9
|
+
return t;
|
|
10
|
+
}
|
|
11
|
+
function createPagePagination(e, t, n) {
|
|
12
|
+
let r = Math.max(1, Math.ceil(n / t));
|
|
13
|
+
return {
|
|
14
|
+
hasNext: e < r,
|
|
15
|
+
hasPrevious: e > 1,
|
|
16
|
+
page: e,
|
|
17
|
+
pageCount: r,
|
|
18
|
+
pageSize: t,
|
|
19
|
+
totalItems: n
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createPagePagination, positiveInteger, totalItems };
|
|
24
|
+
|
|
2
25
|
//# sourceMappingURL=pagination.js.map
|
package/dist/pagination.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.js","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"pagination.js","names":["value","SourcererConfigurationError","name","pageCount","total","pageSize","page"],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":";;AAGA,SAAgB,gBAAgB,GAAe,GAAsB;CACnE,IAAI,CAAC,OAAO,UAAUA,CAAK,KAAKA,IAAQ,GACtC,MAAM,IAAIC,EAA4B,GAAGC,EAAK,4BAA4B;CAE5E,OAAOF;AACT;AAEA,SAAgB,WAAW,GAAuB;CAChD,IAAI,CAAC,OAAO,UAAUA,CAAK,KAAKA,IAAQ,GACtC,MAAM,IAAIC,EAA4B,2CAA2C;CAEnF,OAAOD;AACT;AAEA,SAAgB,qBAAqB,GAAc,GAAkB,GAA+B;CAClG,IAAMG,IAAY,KAAK,IAAI,GAAG,KAAK,KAAKC,IAAQC,CAAQ,CAAC;CAEzD,OAAO;EACL,SAASC,IAAOH;EAChB,aAAaG,IAAO;EACpB;EACA;EACA;EACA,YAAYF;CACd;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestController.cjs","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,yBAA6C,CAC3D,
|
|
1
|
+
{"version":3,"file":"requestController.cjs","names":["active","sequence","controller","requestSequence","reason","message"],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,yBAA6C,CAC3D,IAAIA,EACAC,EAAW,EAEf,MAAO,CACL,OAAQ,CACND,GAAQ,MAAM,EAEd,IAAME,EAAa,IAAI,gBACjBC,EAAkB,EAAEF,EAI1B,MAFA,GAASC,EAEF,CACL,QAAS,CACHC,IAAoBF,IAAU,EAAS,IAAA,GAC7C,EAEA,WAAY,CACV,OAAOE,IAAoBF,CAC7B,EAEA,OAAQC,EAAW,MACrB,CACF,EAEA,SAAU,CACRF,GAAQ,MAAM,EACd,EAAS,IAAA,GACT,GACF,CACF,CACF,CAEA,SAAgB,QAAQ,EAAwB,CAC9C,GAAII,aAAkB,MAAO,OAAOA,EAEpC,IAAMC,EAAU,OAAOD,GAAW,UAAYA,EAAO,OAAS,EAAIA,EAAS,wBAE3E,OAAW,MAAMC,EAAS,CAAE,MAAOD,CAAO,CAAC,CAC7C,C"}
|
|
@@ -1,2 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/requestController.ts
|
|
2
|
+
function createRequestController() {
|
|
3
|
+
let e, t = 0;
|
|
4
|
+
return {
|
|
5
|
+
begin() {
|
|
6
|
+
e?.abort();
|
|
7
|
+
let n = new AbortController(), r = ++t;
|
|
8
|
+
return e = n, {
|
|
9
|
+
finish() {
|
|
10
|
+
r === t && (e = void 0);
|
|
11
|
+
},
|
|
12
|
+
isCurrent() {
|
|
13
|
+
return r === t;
|
|
14
|
+
},
|
|
15
|
+
signal: n.signal
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
dispose() {
|
|
19
|
+
e?.abort(), e = void 0, t++;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function toError(e) {
|
|
24
|
+
if (e instanceof Error) return e;
|
|
25
|
+
let t = typeof e == "string" && e.length > 0 ? e : "Source request failed";
|
|
26
|
+
return Error(t, { cause: e });
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { createRequestController, toError };
|
|
30
|
+
|
|
2
31
|
//# sourceMappingURL=requestController.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestController.js","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,
|
|
1
|
+
{"version":3,"file":"requestController.js","names":["active","sequence","controller","requestSequence","reason","message"],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":";AAYA,SAAgB,0BAA6C;CAC3D,IAAIA,GACAC,IAAW;CAEf,OAAO;EACL,QAAQ;GACN,GAAQ,MAAM;GAEd,IAAMC,IAAa,IAAI,gBAAgB,GACjCC,IAAkB,EAAEF;GAI1B,OAFA,IAASC,GAEF;IACL,SAAS;KACP,AAAIC,MAAoBF,MAAU,IAAS,KAAA;IAC7C;IAEA,YAAY;KACV,OAAOE,MAAoBF;IAC7B;IAEA,QAAQC,EAAW;GACrB;EACF;EAEA,UAAU;GAGR,AAFAF,GAAQ,MAAM,GACd,IAAS,KAAA,GACT;EACF;CACF;AACF;AAEA,SAAgB,QAAQ,GAAwB;CAC9C,IAAII,aAAkB,OAAO,OAAOA;CAEpC,IAAMC,IAAU,OAAOD,KAAW,YAAYA,EAAO,SAAS,IAAIA,IAAS;CAE3E,OAAW,MAAMC,GAAS,EAAE,OAAOD,EAAO,CAAC;AAC7C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resourceSource.cjs","names":[],"sources":["../src/resourceSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport type { Resource, ResourceConfig, ResourceState } from './types';\n\nconst initialResourceState = <T>(start: boolean): ResourceState<T> => ({\n data: undefined,\n error: null,\n loading: start,\n});\n\n/**\n * Fetch one value, reactively. `load` receives a signal that aborts on disposal and on\n * the next `reload`, so a superseded response can never commit over a newer one.\n *\n * Unlike the paginated sources, a resource starts loading during construction — it\n * models a value that must exist rather than a window into a collection. Pass\n * `start: false` to trigger the first `reload()` yourself. A failed initial load is\n * reported through `state.error` rather than as an unhandled rejection; later `reload()`\n * calls reject so callers can await them directly.\n */\nexport function createResource<T>(config: ResourceConfig<T>): Resource<T> {\n const start = config.start ?? true;\n const asyncSource = createAsyncSource<ResourceState<T>>(initialResourceState<T>(start));\n\n const fetchResource = (): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false }),\n load: (signal) => config.load({ signal }),\n pending: (previous) => ({ ...previous, error: null, loading: true }),\n success: (data) => ({ data, error: null, loading: false }),\n });\n\n const source: Resource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n reload() {\n if (asyncSource.disposed) return Promise.reject(new SourcererDisposedError());\n\n return fetchResource();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (start) {\n // The initial load has no caller to reject to: failures surface via state.error.\n void fetchResource().catch(() => undefined);\n }\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"resourceSource.cjs","names":["createAsyncSource","SourcererDisposedError","require_errors","require_asyncSource","start","config","asyncSource","previous","source"],"sources":["../src/resourceSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport type { Resource, ResourceConfig, ResourceState } from './types';\n\nconst initialResourceState = <T>(start: boolean): ResourceState<T> => ({\n data: undefined,\n error: null,\n loading: start,\n});\n\n/**\n * Fetch one value, reactively. `load` receives a signal that aborts on disposal and on\n * the next `reload`, so a superseded response can never commit over a newer one.\n *\n * Unlike the paginated sources, a resource starts loading during construction — it\n * models a value that must exist rather than a window into a collection. Pass\n * `start: false` to trigger the first `reload()` yourself. A failed initial load is\n * reported through `state.error` rather than as an unhandled rejection; later `reload()`\n * calls reject so callers can await them directly.\n */\nexport function createResource<T>(config: ResourceConfig<T>): Resource<T> {\n const start = config.start ?? true;\n const asyncSource = createAsyncSource<ResourceState<T>>(initialResourceState<T>(start));\n\n const fetchResource = (): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false }),\n load: (signal) => config.load({ signal }),\n pending: (previous) => ({ ...previous, error: null, loading: true }),\n success: (data) => ({ data, error: null, loading: false }),\n });\n\n const source: Resource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n reload() {\n if (asyncSource.disposed) return Promise.reject(new SourcererDisposedError());\n\n return fetchResource();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (start) {\n // The initial load has no caller to reject to: failures surface via state.error.\n void fetchResource().catch(() => undefined);\n }\n\n return source;\n}\n"],"mappings":"A,+DAIA,IAAM,qBAA2B,IAAsC,CACrE,KAAM,IAAA,GACN,MAAO,KACP,QAASI,CACX,GAYA,SAAgB,eAAkB,EAAwC,CACxE,IAAMA,EAAQC,EAAO,OAAS,GACxBC,EAAcN,EAAAA,kBAAoC,qBAAwBI,CAAK,CAAC,EAEhF,kBACJE,EAAY,MAAM,CAChB,SAAU,EAAU,KAAW,CAAE,GAAGC,EAAU,QAAO,QAAS,EAAM,GACpE,KAAO,GAAWF,EAAO,KAAK,CAAE,QAAO,CAAC,EACxC,QAAU,IAAc,CAAE,GAAGE,EAAU,MAAO,KAAM,QAAS,EAAK,GAClE,QAAU,IAAU,CAAE,OAAM,MAAO,KAAM,QAAS,EAAM,EAC1D,CAAC,EAEGC,EAAsB,CAC1B,IAAI,gBAAiB,CACnB,OAAOF,EAAY,cACrB,EAEA,QAASA,EAAY,QAErB,IAAI,UAAW,CACb,OAAOA,EAAY,QACrB,EAEA,QAAS,CAGP,OAFIA,EAAY,SAAiB,QAAQ,OAAO,IAAIL,EAAAA,sBAAwB,EAErE,cAAc,CACvB,EAEA,IAAI,OAAQ,CACV,OAAOK,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBE,EAAO,QAAQ,CACjB,CACF,EAOA,OALIJ,GAEF,cAAmB,CAAC,CAAC,UAAY,IAAA,EAAS,EAGrCI,CACT,C"}
|
package/dist/resourceSource.js
CHANGED
|
@@ -1,2 +1,51 @@
|
|
|
1
|
-
import{SourcererDisposedError as e}from"./errors.js";
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
|
+
import { createAsyncSource as t } from "./asyncSource.js";
|
|
3
|
+
//#region src/resourceSource.ts
|
|
4
|
+
var initialResourceState = (e) => ({
|
|
5
|
+
data: void 0,
|
|
6
|
+
error: null,
|
|
7
|
+
loading: e
|
|
8
|
+
});
|
|
9
|
+
function createResource(n) {
|
|
10
|
+
let r = n.start ?? !0, i = t(initialResourceState(r)), fetchResource = () => i.fetch({
|
|
11
|
+
failure: (e, t) => ({
|
|
12
|
+
...e,
|
|
13
|
+
error: t,
|
|
14
|
+
loading: !1
|
|
15
|
+
}),
|
|
16
|
+
load: (e) => n.load({ signal: e }),
|
|
17
|
+
pending: (e) => ({
|
|
18
|
+
...e,
|
|
19
|
+
error: null,
|
|
20
|
+
loading: !0
|
|
21
|
+
}),
|
|
22
|
+
success: (e) => ({
|
|
23
|
+
data: e,
|
|
24
|
+
error: null,
|
|
25
|
+
loading: !1
|
|
26
|
+
})
|
|
27
|
+
}), a = {
|
|
28
|
+
get disposalSignal() {
|
|
29
|
+
return i.disposalSignal;
|
|
30
|
+
},
|
|
31
|
+
dispose: i.dispose,
|
|
32
|
+
get disposed() {
|
|
33
|
+
return i.disposed;
|
|
34
|
+
},
|
|
35
|
+
reload() {
|
|
36
|
+
return i.disposed ? Promise.reject(new e()) : fetchResource();
|
|
37
|
+
},
|
|
38
|
+
get state() {
|
|
39
|
+
return i.state;
|
|
40
|
+
},
|
|
41
|
+
subscribe: i.subscribe,
|
|
42
|
+
[Symbol.dispose]() {
|
|
43
|
+
a.dispose();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return r && fetchResource().catch(() => void 0), a;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { createResource };
|
|
50
|
+
|
|
2
51
|
//# sourceMappingURL=resourceSource.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resourceSource.js","names":[],"sources":["../src/resourceSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport type { Resource, ResourceConfig, ResourceState } from './types';\n\nconst initialResourceState = <T>(start: boolean): ResourceState<T> => ({\n data: undefined,\n error: null,\n loading: start,\n});\n\n/**\n * Fetch one value, reactively. `load` receives a signal that aborts on disposal and on\n * the next `reload`, so a superseded response can never commit over a newer one.\n *\n * Unlike the paginated sources, a resource starts loading during construction — it\n * models a value that must exist rather than a window into a collection. Pass\n * `start: false` to trigger the first `reload()` yourself. A failed initial load is\n * reported through `state.error` rather than as an unhandled rejection; later `reload()`\n * calls reject so callers can await them directly.\n */\nexport function createResource<T>(config: ResourceConfig<T>): Resource<T> {\n const start = config.start ?? true;\n const asyncSource = createAsyncSource<ResourceState<T>>(initialResourceState<T>(start));\n\n const fetchResource = (): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false }),\n load: (signal) => config.load({ signal }),\n pending: (previous) => ({ ...previous, error: null, loading: true }),\n success: (data) => ({ data, error: null, loading: false }),\n });\n\n const source: Resource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n reload() {\n if (asyncSource.disposed) return Promise.reject(new SourcererDisposedError());\n\n return fetchResource();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (start) {\n // The initial load has no caller to reject to: failures surface via state.error.\n void fetchResource().catch(() => undefined);\n }\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"resourceSource.js","names":["start","config","asyncSource","createAsyncSource","previous","source","SourcererDisposedError"],"sources":["../src/resourceSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport type { Resource, ResourceConfig, ResourceState } from './types';\n\nconst initialResourceState = <T>(start: boolean): ResourceState<T> => ({\n data: undefined,\n error: null,\n loading: start,\n});\n\n/**\n * Fetch one value, reactively. `load` receives a signal that aborts on disposal and on\n * the next `reload`, so a superseded response can never commit over a newer one.\n *\n * Unlike the paginated sources, a resource starts loading during construction — it\n * models a value that must exist rather than a window into a collection. Pass\n * `start: false` to trigger the first `reload()` yourself. A failed initial load is\n * reported through `state.error` rather than as an unhandled rejection; later `reload()`\n * calls reject so callers can await them directly.\n */\nexport function createResource<T>(config: ResourceConfig<T>): Resource<T> {\n const start = config.start ?? true;\n const asyncSource = createAsyncSource<ResourceState<T>>(initialResourceState<T>(start));\n\n const fetchResource = (): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false }),\n load: (signal) => config.load({ signal }),\n pending: (previous) => ({ ...previous, error: null, loading: true }),\n success: (data) => ({ data, error: null, loading: false }),\n });\n\n const source: Resource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n reload() {\n if (asyncSource.disposed) return Promise.reject(new SourcererDisposedError());\n\n return fetchResource();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (start) {\n // The initial load has no caller to reject to: failures surface via state.error.\n void fetchResource().catch(() => undefined);\n }\n\n return source;\n}\n"],"mappings":";;;AAIA,IAAM,wBAA2B,OAAsC;CACrE,MAAM,KAAA;CACN,OAAO;CACP,SAASA;AACX;AAYA,SAAgB,eAAkB,GAAwC;CACxE,IAAMA,IAAQC,EAAO,SAAS,IACxBC,IAAcC,EAAoC,qBAAwBH,CAAK,CAAC,GAEhF,sBACJE,EAAY,MAAM;EAChB,UAAU,GAAU,OAAW;GAAE,GAAGE;GAAU;GAAO,SAAS;EAAM;EACpE,OAAO,MAAWH,EAAO,KAAK,EAAE,UAAO,CAAC;EACxC,UAAU,OAAc;GAAE,GAAGG;GAAU,OAAO;GAAM,SAAS;EAAK;EAClE,UAAU,OAAU;GAAE;GAAM,OAAO;GAAM,SAAS;EAAM;CAC1D,CAAC,GAEGC,IAAsB;EAC1B,IAAI,iBAAiB;GACnB,OAAOH,EAAY;EACrB;EAEA,SAASA,EAAY;EAErB,IAAI,WAAW;GACb,OAAOA,EAAY;EACrB;EAEA,SAAS;GAGP,OAFIA,EAAY,WAAiB,QAAQ,OAAO,IAAII,EAAuB,CAAC,IAErE,cAAc;EACvB;EAEA,IAAI,QAAQ;GACV,OAAOJ,EAAY;EACrB;EAEA,WAAWA,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAOA,OALIF,KAEF,cAAmB,CAAC,CAAC,YAAY,KAAA,CAAS,GAGrCK;AACT"}
|
package/dist/sourceStore.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.cjs","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"AAWA,SAAgB,kBAAqB,EAAiC,CACpE,
|
|
1
|
+
{"version":3,"file":"sourceStore.cjs","names":["controller","listeners","disposed","value","initialValue","nextValue","listener","error"],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"AAWA,SAAgB,kBAAqB,EAAiC,CACpE,IAAMA,EAAa,IAAI,gBACjBC,EAAY,IAAI,IAClBC,EAAW,GACXC,EAAQC,EAEN,YAAsB,CACtBF,IAEJ,EAAW,GACXD,EAAU,MAAM,EAChBD,EAAW,MAAM,EACnB,EAEA,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAOA,EAAW,MACpB,EAEA,QAEA,IAAI,UAAW,CACb,OAAOE,CACT,EAEA,IAAI,EAAW,CACb,GAAI,GAEJ,GAAQG,EAER,IAAK,IAAMC,KAAYL,EACrB,GAAI,CACFK,EAASH,CAAK,CAChB,OAASI,EAAO,CACd,mBAAqB,CACnB,MAAMA,CACR,CAAC,CACH,CATM,CAWV,EAEA,UAAU,EAAU,CAKlB,OAJIL,MAAuB,CAAC,GAE5BD,EAAU,IAAIK,CAAQ,MAETL,EAAU,OAAOK,CAAQ,EACxC,GAEC,OAAO,SAAU,QAElB,IAAI,OAAQ,CACV,OAAOH,CACT,CACF,CACF,C"}
|