@vielzeug/sourcerer 2.3.0 → 3.0.0
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/README.md +17 -18
- package/dist/asyncSource.cjs +1 -1
- package/dist/asyncSource.cjs.map +1 -1
- package/dist/asyncSource.d.ts +11 -12
- package/dist/asyncSource.d.ts.map +1 -1
- package/dist/asyncSource.js +45 -32
- package/dist/asyncSource.js.map +1 -1
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.d.ts +2 -2
- package/dist/cursorSource.d.ts.map +1 -1
- package/dist/cursorSource.js +94 -61
- package/dist/cursorSource.js.map +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +11 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -6
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.d.ts +2 -2
- package/dist/infiniteSource.d.ts.map +1 -1
- package/dist/infiniteSource.js +80 -52
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.d.ts +3 -2
- package/dist/localSource.d.ts.map +1 -1
- package/dist/localSource.js +54 -57
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.d.ts +2 -2
- package/dist/pageSource.d.ts.map +1 -1
- package/dist/pageSource.js +92 -60
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.cjs.map +1 -1
- package/dist/pagination.d.ts +1 -2
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +9 -13
- package/dist/pagination.js.map +1 -1
- package/dist/sourceStore.cjs.map +1 -1
- package/dist/sourceStore.d.ts +5 -2
- package/dist/sourceStore.d.ts.map +1 -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 +1 -1
- package/dist/sourcerer.js.map +1 -1
- package/dist/types.d.ts +130 -117
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -7
- package/dist/_dev.cjs +0 -2
- package/dist/_dev.cjs.map +0 -1
- package/dist/_dev.d.ts +0 -2
- package/dist/_dev.d.ts.map +0 -1
- package/dist/_dev.js +0 -6
- package/dist/_dev.js.map +0 -1
package/dist/pageSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");function r(r){let i={page:1,pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=!1,o=t.createAsyncSource({error:null,items:[],loading:!1,pagination:n.createPagePagination(i.page,i.pageSize,0),params:i.params}),s=()=>{if(o.disposed)throw new e.SourcererDisposedError},c=e=>o.state.pagination.page===e.page&&o.state.pagination.pageSize===e.pageSize&&Object.is(o.state.params,e.params),l=e=>o.fetch({commit:()=>{a=!0,i=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>r.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})}),u=async e=>{s();let t=a?Math.min(n.positiveInteger(e,`page`),o.state.pagination.pageCount):n.positiveInteger(e,`page`);if(t===i.page){!o.state.loading&&(!a||!c(i))&&await l(i);return}i={...i,page:t},await l(i)},d={get disposalSignal(){return o.disposalSignal},dispose:o.dispose,get disposed(){return o.disposed},first(){return u(1)},goTo:u,last(){return u(o.state.pagination.pageCount)},async next(){s(),(!a||i.page<o.state.pagination.pageCount)&&await u(i.page+1)},async previous(){s(),i.page>1&&await u(i.page-1)},reload(){return l(i)},async setPageSize(e){s();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize&&i.page===1){!o.state.loading&&!c(i)&&await l(i);return}i={...i,page:1,pageSize:t},await l(i)},async setParams(e){if(s(),Object.is(e,i.params)){!o.state.loading&&!c(i)&&await l(i);return}i={...i,page:1,params:e},await l(i)},get state(){return o.state},subscribe:o.subscribe,[Symbol.dispose](){d.dispose()}};return d}exports.createPageSource=r;
|
|
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 {
|
|
1
|
+
{"version":3,"file":"pageSource.cjs","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { 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 goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, '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 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.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { PageSource, PageSourceConfig } from './types';
|
|
2
|
-
/** Page sources retain
|
|
3
|
-
export declare function createPageSource<T,
|
|
2
|
+
/** Page sources retain committed items while replacement work is pending. */
|
|
3
|
+
export declare function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams>;
|
|
4
4
|
//# sourceMappingURL=pageSource.d.ts.map
|
package/dist/pageSource.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pageSource.d.ts","sourceRoot":"","sources":["../src/pageSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"pageSource.d.ts","sourceRoot":"","sources":["../src/pageSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAmB,MAAM,SAAS,CAAC;AAI7E,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,EAAE,MAAM,EAAE,gBAAgB,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CA8HrH"}
|
package/dist/pageSource.js
CHANGED
|
@@ -1,51 +1,59 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
2
|
import { createAsyncSource as t } from "./asyncSource.js";
|
|
3
|
-
import { createPagePagination as n, positiveInteger as r,
|
|
3
|
+
import { createPagePagination as n, positiveInteger as r, totalItems as i } from "./pagination.js";
|
|
4
4
|
//#region src/pageSource.ts
|
|
5
|
-
|
|
6
|
-
let
|
|
7
|
-
return {
|
|
8
|
-
..."filter" in t ? t.filter !== void 0 && { filter: t.filter } : e.filter !== void 0 && { filter: e.filter },
|
|
9
|
-
..."sort" in t ? t.sort !== void 0 && { sort: t.sort } : e.sort !== void 0 && { sort: e.sort },
|
|
10
|
-
page: n === void 0 ? r(c, "page") : Math.min(r(c, "page"), n),
|
|
11
|
-
pageSize: r(a, "pageSize"),
|
|
12
|
-
search: o
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
function s(r) {
|
|
16
|
-
let s = o({
|
|
5
|
+
function a(a) {
|
|
6
|
+
let o = {
|
|
17
7
|
page: 1,
|
|
18
|
-
pageSize: 20,
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
data: [],
|
|
8
|
+
pageSize: r(a.pageSize ?? 20, "pageSize"),
|
|
9
|
+
params: a.params
|
|
10
|
+
}, s = !1, c = t({
|
|
22
11
|
error: null,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
items: [],
|
|
13
|
+
loading: !1,
|
|
14
|
+
pagination: n(o.page, o.pageSize, 0),
|
|
15
|
+
params: o.params
|
|
16
|
+
}), l = () => {
|
|
17
|
+
if (c.disposed) throw new e();
|
|
18
|
+
}, u = (e) => c.state.pagination.page === e.page && c.state.pagination.pageSize === e.pageSize && Object.is(c.state.params, e.params), d = (e) => c.fetch({
|
|
19
|
+
commit: () => {
|
|
20
|
+
s = !0, o = e;
|
|
21
|
+
},
|
|
22
|
+
failure: (e, t) => ({
|
|
23
|
+
...e,
|
|
24
|
+
error: t,
|
|
25
|
+
loading: !1,
|
|
26
|
+
pendingParams: void 0
|
|
27
|
+
}),
|
|
28
|
+
load: (t) => a.load({
|
|
29
|
+
...e,
|
|
29
30
|
signal: t
|
|
30
31
|
}),
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
pending: (t) => ({
|
|
33
|
+
...t,
|
|
34
|
+
error: null,
|
|
35
|
+
loading: !0,
|
|
36
|
+
...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
37
|
+
}),
|
|
38
|
+
success: (t) => ({
|
|
39
|
+
error: null,
|
|
40
|
+
items: [...t.items],
|
|
41
|
+
loading: !1,
|
|
42
|
+
pagination: n(e.page, e.pageSize, i(t.totalItems)),
|
|
43
|
+
params: e.params
|
|
44
|
+
})
|
|
45
|
+
}), f = async (e) => {
|
|
46
|
+
l();
|
|
47
|
+
let t = s ? Math.min(r(e, "page"), c.state.pagination.pageCount) : r(e, "page");
|
|
48
|
+
if (t === o.page) {
|
|
49
|
+
!c.state.loading && (!s || !u(o)) && await d(o);
|
|
50
|
+
return;
|
|
44
51
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
o = {
|
|
53
|
+
...o,
|
|
54
|
+
page: t
|
|
55
|
+
}, await d(o);
|
|
56
|
+
}, p = {
|
|
49
57
|
get disposalSignal() {
|
|
50
58
|
return c.disposalSignal;
|
|
51
59
|
},
|
|
@@ -53,33 +61,57 @@ function s(r) {
|
|
|
53
61
|
get disposed() {
|
|
54
62
|
return c.disposed;
|
|
55
63
|
},
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
first() {
|
|
65
|
+
return f(1);
|
|
66
|
+
},
|
|
67
|
+
goTo: f,
|
|
68
|
+
last() {
|
|
69
|
+
return f(c.state.pagination.pageCount);
|
|
70
|
+
},
|
|
71
|
+
async next() {
|
|
72
|
+
l(), (!s || o.page < c.state.pagination.pageCount) && await f(o.page + 1);
|
|
73
|
+
},
|
|
74
|
+
async previous() {
|
|
75
|
+
l(), o.page > 1 && await f(o.page - 1);
|
|
76
|
+
},
|
|
77
|
+
reload() {
|
|
78
|
+
return d(o);
|
|
79
|
+
},
|
|
80
|
+
async setPageSize(e) {
|
|
81
|
+
l();
|
|
82
|
+
let t = r(e, "pageSize");
|
|
83
|
+
if (t === o.pageSize && o.page === 1) {
|
|
84
|
+
!c.state.loading && !u(o) && await d(o);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
o = {
|
|
88
|
+
...o,
|
|
89
|
+
page: 1,
|
|
90
|
+
pageSize: t
|
|
91
|
+
}, await d(o);
|
|
92
|
+
},
|
|
93
|
+
async setParams(e) {
|
|
94
|
+
if (l(), Object.is(e, o.params)) {
|
|
95
|
+
!c.state.loading && !u(o) && await d(o);
|
|
96
|
+
return;
|
|
68
97
|
}
|
|
98
|
+
o = {
|
|
99
|
+
...o,
|
|
100
|
+
page: 1,
|
|
101
|
+
params: e
|
|
102
|
+
}, await d(o);
|
|
69
103
|
},
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
get snapshot() {
|
|
73
|
-
return c.snapshot;
|
|
104
|
+
get state() {
|
|
105
|
+
return c.state;
|
|
74
106
|
},
|
|
75
107
|
subscribe: c.subscribe,
|
|
76
108
|
[Symbol.dispose]() {
|
|
77
|
-
|
|
109
|
+
p.dispose();
|
|
78
110
|
}
|
|
79
111
|
};
|
|
80
|
-
return
|
|
112
|
+
return p;
|
|
81
113
|
}
|
|
82
114
|
//#endregion
|
|
83
|
-
export {
|
|
115
|
+
export { a as createPageSource };
|
|
84
116
|
|
|
85
117
|
//# 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 {
|
|
1
|
+
{"version":3,"file":"pageSource.js","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { 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 goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, '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 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
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs");function t(t,n){if(!Number.isInteger(t)||t<1)throw new e.SourcererConfigurationError(`${n} must be a positive integer`);return t}function n(t){if(!Number.isInteger(t)||t<0)throw new e.SourcererConfigurationError(`
|
|
1
|
+
const e=require("./errors.cjs");function t(t,n){if(!Number.isInteger(t)||t<1)throw new e.SourcererConfigurationError(`${n} must be a positive integer`);return t}function n(t){if(!Number.isInteger(t)||t<0)throw new e.SourcererConfigurationError(`totalItems must be a non-negative integer`);return t}function r(e,t,n){let r=Math.max(1,Math.ceil(n/t));return{hasNext:e<r,hasPrevious:e>1,page:e,pageCount:r,pageSize:t,totalItems:n}}exports.createPagePagination=r,exports.positiveInteger=t,exports.totalItems=n;
|
|
2
2
|
//# sourceMappingURL=pagination.cjs.map
|
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('
|
|
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,EAAgB,EAAe,EAAsB,CACnE,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAAA,4BAA4B,GAAG,EAAK,4BAA4B,EAE5E,OAAO,CACT,CAEA,SAAgB,EAAW,EAAuB,CAChD,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAAA,4BAA4B,2CAA2C,EAEnF,OAAO,CACT,CAEA,SAAgB,EAAqB,EAAc,EAAkB,EAA+B,CAClG,IAAM,EAAY,KAAK,IAAI,EAAG,KAAK,KAAK,EAAQ,CAAQ,CAAC,EAEzD,MAAO,CACL,QAAS,EAAO,EAChB,YAAa,EAAO,EACpB,OACA,YACA,WACA,WAAY,CACd,CACF"}
|
package/dist/pagination.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { PagePagination } from './types';
|
|
2
2
|
export declare function positiveInteger(value: number, name: string): number;
|
|
3
3
|
export declare function totalItems(value: number): number;
|
|
4
|
-
export declare function createPagePagination(
|
|
5
|
-
export declare function sameQuery<T extends Record<string, unknown>>(left: T, right: T): boolean;
|
|
4
|
+
export declare function createPagePagination(page: number, pageSize: number, total: number): PagePagination;
|
|
6
5
|
//# sourceMappingURL=pagination.d.ts.map
|
package/dist/pagination.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKnE;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKhD;AAED,wBAAgB,oBAAoB,CAAC,
|
|
1
|
+
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKnE;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKhD;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAWlG"}
|
package/dist/pagination.js
CHANGED
|
@@ -5,25 +5,21 @@ function t(t, n) {
|
|
|
5
5
|
return t;
|
|
6
6
|
}
|
|
7
7
|
function n(t) {
|
|
8
|
-
if (!Number.isInteger(t) || t < 0) throw new e("
|
|
8
|
+
if (!Number.isInteger(t) || t < 0) throw new e("totalItems must be a non-negative integer");
|
|
9
9
|
return t;
|
|
10
10
|
}
|
|
11
11
|
function r(e, t, n) {
|
|
12
|
-
let r = Math.max(1, Math.ceil(n / t))
|
|
12
|
+
let r = Math.max(1, Math.ceil(n / t));
|
|
13
13
|
return {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
total: n
|
|
14
|
+
hasNext: e < r,
|
|
15
|
+
hasPrevious: e > 1,
|
|
16
|
+
page: e,
|
|
17
|
+
pageCount: r,
|
|
18
|
+
pageSize: t,
|
|
19
|
+
totalItems: n
|
|
21
20
|
};
|
|
22
21
|
}
|
|
23
|
-
function i(e, t) {
|
|
24
|
-
return [.../* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])].every((n) => Object.is(e[n], t[n]));
|
|
25
|
-
}
|
|
26
22
|
//#endregion
|
|
27
|
-
export { r as createPagePagination, t as positiveInteger,
|
|
23
|
+
export { r as createPagePagination, t as positiveInteger, n as totalItems };
|
|
28
24
|
|
|
29
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('
|
|
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":";;AAGA,SAAgB,EAAgB,GAAe,GAAsB;CACnE,IAAI,CAAC,OAAO,UAAU,CAAK,KAAK,IAAQ,GACtC,MAAM,IAAI,EAA4B,GAAG,EAAK,4BAA4B;CAE5E,OAAO;AACT;AAEA,SAAgB,EAAW,GAAuB;CAChD,IAAI,CAAC,OAAO,UAAU,CAAK,KAAK,IAAQ,GACtC,MAAM,IAAI,EAA4B,2CAA2C;CAEnF,OAAO;AACT;AAEA,SAAgB,EAAqB,GAAc,GAAkB,GAA+B;CAClG,IAAM,IAAY,KAAK,IAAI,GAAG,KAAK,KAAK,IAAQ,CAAQ,CAAC;CAEzD,OAAO;EACL,SAAS,IAAO;EAChB,aAAa,IAAO;EACpB;EACA;EACA;EACA,YAAY;CACd;AACF"}
|
package/dist/sourceStore.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.cjs","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["
|
|
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,EAAqB,EAAiC,CACpE,IAAM,EAAa,IAAI,gBACjB,EAAY,IAAI,IAClB,EAAW,GACX,EAAQ,EAEN,MAAsB,CACtB,IAEJ,EAAW,GACX,EAAU,MAAM,EAChB,EAAW,MAAM,EACnB,EAEA,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAO,EAAW,MACpB,EAEA,UAEA,IAAI,UAAW,CACb,OAAO,CACT,EAEA,IAAI,EAAW,CACT,MAEJ,GAAQ,EAER,IAAK,IAAM,KAAY,EACrB,GAAI,CACF,EAAS,CAAK,CAChB,OAAS,EAAO,CACd,mBAAqB,CACnB,MAAM,CACR,CAAC,CACH,CATM,CAWV,EAEA,UAAU,EAAU,CAKlB,OAJI,MAAuB,CAAC,GAE5B,EAAU,IAAI,CAAQ,MAET,EAAU,OAAO,CAAQ,EACxC,GAEC,OAAO,SAAU,EAElB,IAAI,OAAQ,CACV,OAAO,CACT,CACF,CACF"}
|
package/dist/sourceStore.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type SourceStore<T> = {
|
|
2
|
+
[Symbol.dispose](): void;
|
|
3
|
+
readonly disposalSignal: AbortSignal;
|
|
4
|
+
dispose(): void;
|
|
5
|
+
readonly disposed: boolean;
|
|
3
6
|
set(value: T): void;
|
|
4
7
|
subscribe(listener: (value: T) => void): () => void;
|
|
5
8
|
readonly value: T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.d.ts","sourceRoot":"","sources":["../src/sourceStore.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"sourceStore.d.ts","sourceRoot":"","sources":["../src/sourceStore.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,CAAC,CAAC,IAAI;IACpB,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;IACzB,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC;IACrC,OAAO,IAAI,IAAI,CAAC;IAChB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IACpB,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACpD,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACnB,CAAC;AAEF,qGAAqG;AACrG,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAuDpE"}
|
package/dist/sourceStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.js","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"sourceStore.js","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,EAAqB,GAAiC;CACpE,IAAM,IAAa,IAAI,gBAAgB,GACjC,oBAAY,IAAI,IAAwB,GAC1C,IAAW,IACX,IAAQ,GAEN,UAAsB;EACtB,MAEJ,IAAW,IACX,EAAU,MAAM,GAChB,EAAW,MAAM;CACnB;CAEA,OAAO;EACL,IAAI,iBAAiB;GACnB,OAAO,EAAW;EACpB;EAEA;EAEA,IAAI,WAAW;GACb,OAAO;EACT;EAEA,IAAI,GAAW;GACT,QAEJ;QAAQ;IAER,KAAK,IAAM,KAAY,GACrB,IAAI;KACF,EAAS,CAAK;IAChB,SAAS,GAAO;KACd,qBAAqB;MACnB,MAAM;KACR,CAAC;IACH;GATM;EAWV;EAEA,UAAU,GAAU;GAKlB,OAJI,UAAuB,CAAC,KAE5B,EAAU,IAAI,CAAQ,SAET,EAAU,OAAO,CAAQ;EACxC;GAEC,OAAO,UAAU;EAElB,IAAI,QAAQ;GACV,OAAO;EACT;CACF;AACF"}
|
package/dist/sourcerer.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@vielzeug/arsenal");var t=class extends Error{static errorName=`SourcererError`;constructor(e,t){super(e,t),this.name=new.target.errorName,Object.setPrototypeOf(this,new.target.prototype)}},n=class extends t{static errorName=`SourcererConfigurationError`},r=class extends t{static errorName=`SourcererDisposedError`;constructor(e=`Sourcerer source disposed`){super(e)}};function i(){let e,t=0;return{begin(){e?.abort();let n=new AbortController,r=++t;return e=n,{finish(){r===t&&(e=void 0)},isCurrent(){return r===t},signal:n.signal}},dispose(){e?.abort(),e=void 0,t++}}}function a(e){if(e instanceof Error)return e;let t=typeof e==`string`&&e.length>0?e:`Source request failed`;return Error(t,{cause:e})}function o(e){let t=new AbortController,n=new Set,r=!1,i=e,a=()=>{r||(r=!0,n.clear(),t.abort())};return{get disposalSignal(){return t.signal},dispose:a,get disposed(){return r},set(e){if(!r){i=e;for(let e of n)try{e(i)}catch(e){queueMicrotask(()=>{throw e})}}},subscribe(e){return r?()=>{}:(n.add(e),()=>n.delete(e))},[Symbol.dispose]:a,get value(){return i}}}var s=(t,n)=>new Promise((r,i)=>{let a=()=>i((0,e.abortError)(n,`The operation was aborted.`));if(n.aborted){a();return}n.addEventListener(`abort`,a,{once:!0});let o;try{o=t(n)}catch(e){n.removeEventListener(`abort`,a),i(e);return}o.then(e=>{n.removeEventListener(`abort`,a),r(e)},e=>{n.removeEventListener(`abort`,a),i(e)})});function c(e){let t=o(e),n=i();return{get disposalSignal(){return t.disposalSignal},dispose(){n.dispose(),t.dispose()},get disposed(){return t.disposed},async fetch(e){if(t.disposed)throw new r;let i=n.begin(),o=t.value;t.set(e.pending(o));try{let n=await s(e.load,i.signal);if(!i.isCurrent()||t.disposed)return;let r=e.success(n);e.commit?.(r,n),t.set(r)}catch(n){if(!i.isCurrent()||t.disposed)return;let r=a(n);throw t.set(e.failure(o,r)),r}finally{i.finish()}},get state(){return t.value},subscribe:t.subscribe}}function l(e,t){if(!Number.isInteger(e)||e<1)throw new n(`${t} must be a positive integer`);return e}function u(e){if(!Number.isInteger(e)||e<0)throw new n(`totalItems must be a non-negative integer`);return e}function d(e,t,n){let r=Math.max(1,Math.ceil(n/t));return{hasNext:e<r,hasPrevious:e>1,page:e,pageCount:r,pageSize:t,totalItems:n}}var f=(e,t)=>({...t?.nextCursor!==void 0&&{nextCursor:t.nextCursor},pageSize:e,...t?.previousCursor!==void 0&&{previousCursor:t.previousCursor},...t?.totalItems!==void 0&&{totalItems:u(t.totalItems)}});function p(e){if(e.after!==void 0&&e.before!==void 0)throw new n(`Cursor source cannot start with both after and before`);let t={...e.after!==void 0&&{after:e.after},...e.before!==void 0&&{before:e.before},pageSize:l(e.pageSize??20,`pageSize`),params:e.params},i=c({error:null,items:[],loading:!1,pagination:f(t.pageSize),params:t.params}),a=()=>{if(i.disposed)throw new r},o=t=>i.fetch({failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:n=>e.load({...t,signal:n}),pending:e=>({...e,error:null,loading:!0,...Object.is(t.params,e.params)?{}:{pendingParams:t.params}}),success:e=>({error:null,items:[...e.items],loading:!1,pagination:f(t.pageSize,e),params:t.params})}),s={get disposalSignal(){return i.disposalSignal},dispose:i.dispose,get disposed(){return i.disposed},async next(){a();let e=i.state.pagination.nextCursor;i.state.loading||e===void 0||(t={after:e,pageSize:t.pageSize,params:t.params},await o(t))},async previous(){a();let e=i.state.pagination.previousCursor;i.state.loading||e===void 0||(t={before:e,pageSize:t.pageSize,params:t.params},await o(t))},reload(){return o(t)},async setPageSize(e){a();let n=l(e,`pageSize`);if(n===t.pageSize&&t.after===void 0&&t.before===void 0){if(i.state.loading||i.state.pagination.pageSize===n&&Object.is(i.state.params,t.params))return;await o(t);return}t={pageSize:n,params:t.params},await o(t)},async setParams(e){if(a(),Object.is(e,t.params)){!i.state.loading&&!Object.is(e,i.state.params)&&await o(t);return}t={pageSize:t.pageSize,params:e},await o(t)},get state(){return i.state},subscribe:i.subscribe,[Symbol.dispose](){s.dispose()}};return s}var m=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function h(e){let t={pageSize:l(e.pageSize??20,`pageSize`),params:e.params},n=1,i=!1,a=c({error:null,items:[],loading:!1,pagination:m(0,t.pageSize,0),params:t.params}),o=()=>{if(a.disposed)throw new r},s=(r,o,s)=>{let c=a.state.items;return a.fetch({commit:()=>{i=!0,n=o+1,t=r},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>e.load({page:o,pageSize:r.pageSize,params:r.params,signal:t}),pending:e=>({...e,error:null,loading:!0,...s||Object.is(r.params,e.params)?{}:{pendingParams:r.params}}),success:e=>{let t=s?[...c,...e.items]:[...e.items];return{error:null,items:t,loading:!1,pagination:m(t.length,r.pageSize,u(e.totalItems)),params:r.params}}})},d=()=>s(t,1,!1),f={get disposalSignal(){return a.disposalSignal},dispose:a.dispose,get disposed(){return a.disposed},async loadMore(){if(o(),!a.state.loading){if(!i||!Object.is(t.params,a.state.params)){await d();return}a.state.pagination.hasMore&&await s(t,n,!0)}},reload:d,async setPageSize(e){o();let r=l(e,`pageSize`);if(r===t.pageSize){if(a.state.loading||a.state.pagination.pageSize===r)return;await d();return}t={...t,pageSize:r},n=1,await d()},async setParams(e){if(o(),Object.is(e,t.params)){!a.state.loading&&!Object.is(e,a.state.params)&&await d();return}t={...t,params:e},n=1,await d()},get state(){return a.state},subscribe:a.subscribe,[Symbol.dispose](){f.dispose()}};return f}function g(e,t={}){let n=e,i=1,a=l(t.pageSize??20,`pageSize`),s=t.params,c=()=>{let e=t.filter?n.filter(e=>t.filter?.(e,s)):n,r=d(i,a,e.length);if(i>r.pageCount)return i=r.pageCount,c();let o=(i-1)*a;return{error:null,items:e.slice(o,o+a),loading:!1,pagination:r,params:s}},u=o(c()),f=()=>{if(u.disposed)throw new r},p=(e,t)=>Object.keys(e).every(n=>e[n]===t[n]),m=()=>{let e=c(),t=u.value,n=e.items.length===t.items.length&&e.items.every((e,n)=>Object.is(e,t.items[n]));Object.is(e.params,t.params)&&n&&p(e.pagination,t.pagination)||u.set(e)},h=e=>{f();let t=Math.min(l(e,`page`),u.value.pagination.pageCount);t!==i&&(i=t,m())},g={get disposalSignal(){return u.disposalSignal},dispose:u.dispose,get disposed(){return u.disposed},first(){h(1)},goTo:h,last(){h(u.value.pagination.pageCount)},next(){u.value.pagination.hasNext?h(i+1):f()},previous(){u.value.pagination.hasPrevious?h(i-1):f()},setItems(e){f(),e!==n&&(n=e,m())},setPageSize(e){f();let t=l(e,`pageSize`);t!==a&&(i=1,a=t,m())},setParams(e){f(),!Object.is(e,s)&&(i=1,s=e,m())},get state(){return u.value},subscribe:u.subscribe,[Symbol.dispose](){g.dispose()}};return g}function _(e){let t={page:1,pageSize:l(e.pageSize??20,`pageSize`),params:e.params},n=!1,i=c({error:null,items:[],loading:!1,pagination:d(t.page,t.pageSize,0),params:t.params}),a=()=>{if(i.disposed)throw new r},o=e=>i.state.pagination.page===e.page&&i.state.pagination.pageSize===e.pageSize&&Object.is(i.state.params,e.params),s=r=>i.fetch({commit:()=>{n=!0,t=r},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>e.load({...r,signal:t}),pending:e=>({...e,error:null,loading:!0,...Object.is(r.params,e.params)?{}:{pendingParams:r.params}}),success:e=>({error:null,items:[...e.items],loading:!1,pagination:d(r.page,r.pageSize,u(e.totalItems)),params:r.params})}),f=async e=>{a();let r=n?Math.min(l(e,`page`),i.state.pagination.pageCount):l(e,`page`);if(r===t.page){!i.state.loading&&(!n||!o(t))&&await s(t);return}t={...t,page:r},await s(t)},p={get disposalSignal(){return i.disposalSignal},dispose:i.dispose,get disposed(){return i.disposed},first(){return f(1)},goTo:f,last(){return f(i.state.pagination.pageCount)},async next(){a(),(!n||t.page<i.state.pagination.pageCount)&&await f(t.page+1)},async previous(){a(),t.page>1&&await f(t.page-1)},reload(){return s(t)},async setPageSize(e){a();let n=l(e,`pageSize`);if(n===t.pageSize&&t.page===1){!i.state.loading&&!o(t)&&await s(t);return}t={...t,page:1,pageSize:n},await s(t)},async setParams(e){if(a(),Object.is(e,t.params)){!i.state.loading&&!o(t)&&await s(t);return}t={...t,page:1,params:e},await s(t)},get state(){return i.state},subscribe:i.subscribe,[Symbol.dispose](){p.dispose()}};return p}exports.SourcererConfigurationError=n,exports.SourcererDisposedError=r,exports.SourcererError=t,exports.createCursorSource=p,exports.createInfiniteSource=h,exports.createLocalSource=g,exports.createPageSource=_;
|
|
2
2
|
//# sourceMappingURL=sourcerer.cjs.map
|