@vielzeug/sourcerer 26.11.4 → 26.11.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asyncSource.js +1 -2
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.js +6 -7
- package/dist/errors.js +1 -2
- package/dist/index.cjs +1 -1
- package/dist/index.js +3 -3
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.js +6 -7
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.js +11 -12
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.js +7 -8
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.js +1 -2
- package/dist/requestController.js +1 -2
- package/dist/resourceSource.cjs +1 -1
- package/dist/resourceSource.cjs.map +1 -1
- package/dist/resourceSource.js +5 -6
- package/dist/sourceStore.js +1 -2
- 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 +14 -24
- package/dist/sourcerer.js.map +1 -1
- package/package.json +8 -8
package/dist/asyncSource.js
CHANGED
|
@@ -2,7 +2,7 @@ import { SourcererDisposedError as e } from "./errors.js";
|
|
|
2
2
|
import { createRequestController as t, toError as n } from "./requestController.js";
|
|
3
3
|
import { createSourceStore as r } from "./sourceStore.js";
|
|
4
4
|
import { abortError as i } from "@vielzeug/arsenal";
|
|
5
|
-
|
|
5
|
+
// src/asyncSource.ts
|
|
6
6
|
var loadWithSignal = (e, t) => new Promise((n, r) => {
|
|
7
7
|
let abort = () => r(i(t, "The operation was aborted."));
|
|
8
8
|
if (t.aborted) {
|
|
@@ -58,7 +58,6 @@ function createAsyncSource(i) {
|
|
|
58
58
|
subscribe: a.subscribe
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
-
//#endregion
|
|
62
61
|
export { createAsyncSource };
|
|
63
62
|
|
|
64
63
|
//# sourceMappingURL=asyncSource.js.map
|
package/dist/cursorSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t)=>({...t?.nextCursor!==void 0&&{nextCursor:t.nextCursor},pageSize:e,...t?.previousCursor!==void 0&&{previousCursor:t.previousCursor},...t?.totalItems!==void 0&&{totalItems:n.totalItems(t.totalItems)}});function createCursorSource(r){if(r.after!==void 0&&r.before!==void 0)throw new t.SourcererConfigurationError(`Cursor source cannot start with both after and before`);let i={...r.after!==void 0&&{after:r.after},...r.before!==void 0&&{before:r.before},pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(i.pageSize),params:i.params}),assertLive=()=>{if(a.disposed)throw new t.SourcererDisposedError},fetch=e=>a.fetch({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:createPagination(e.pageSize,t),params:e.params})}),o={get disposalSignal(){return a.disposalSignal},dispose:a.dispose,get disposed(){return a.disposed},async next(){assertLive();let e=a.state.pagination.nextCursor;a.state.loading||e===void 0||(i={after:e,pageSize:i.pageSize,params:i.params},await fetch(i))},async previous(){assertLive();let e=a.state.pagination.previousCursor;a.state.loading||e===void 0||(i={before:e,pageSize:i.pageSize,params:i.params},await fetch(i))},reload(){return fetch(i)},async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize&&i.after===void 0&&i.before===void 0){if(a.state.loading||a.state.pagination.pageSize===t&&Object.is(a.state.params,i.params))return;await fetch(i)}else i={pageSize:t,params:i.params},await fetch(i)},async setParams(e){assertLive(),Object.is(e,i.params)?!a.state.loading&&!Object.is(e,a.state.params)&&await fetch(i):(i={pageSize:i.pageSize,params:e},await fetch(i))},get state(){return a.state},subscribe:a.subscribe,[Symbol.dispose](){o.dispose()}};return o}exports.createCursorSource=createCursorSource;
|
|
2
2
|
//# sourceMappingURL=cursorSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.cjs","names":["totalItems","SourcererConfigurationError","positiveInteger","createAsyncSource","SourcererDisposedError","
|
|
1
|
+
{"version":3,"file":"cursorSource.cjs","names":["totalItems","SourcererConfigurationError","positiveInteger","createAsyncSource","SourcererDisposedError","require_asyncSource","require_errors","require_pagination","result","config","requested","asyncSource","previous","request","source","cursor","normalized","pageSize","params"],"sources":["../src/cursorSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererConfigurationError, SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState } from './types';\n\ntype Request<TParams, TCursor> = Readonly<{\n after?: TCursor;\n before?: TCursor;\n pageSize: number;\n params: TParams;\n}>;\n\nconst createPagination = <TCursor>(\n pageSize: number,\n result?: CursorResult<unknown, TCursor>,\n): CursorPagination<TCursor> => ({\n ...(result?.nextCursor !== undefined && { nextCursor: result.nextCursor }),\n pageSize,\n ...(result?.previousCursor !== undefined && { previousCursor: result.previousCursor }),\n ...(result?.totalItems !== undefined && { totalItems: totalItems(result.totalItems) }),\n});\n\n/** Cursor sources retain committed items and cursors while replacement work is pending. */\nexport function createCursorSource<T, TParams = undefined, TCursor = string>(\n config: CursorSourceConfig<T, TParams, TCursor>,\n): CursorSource<T, TParams, TCursor> {\n if (config.after !== undefined && config.before !== undefined) {\n throw new SourcererConfigurationError('Cursor source cannot start with both after and before');\n }\n\n let requested: Request<TParams, TCursor> = {\n ...(config.after !== undefined && { after: config.after }),\n ...(config.before !== undefined && { before: config.before }),\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n const asyncSource = createAsyncSource<CursorSourceState<T, TParams, TCursor>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(requested.pageSize),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams, TCursor>): Promise<void> =>\n asyncSource.fetch({\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: createPagination(request.pageSize, result),\n params: request.params,\n }),\n });\n\n const source: CursorSource<T, TParams, TCursor> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async next() {\n assertLive();\n const cursor = asyncSource.state.pagination.nextCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { after: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n async previous() {\n assertLive();\n const cursor = asyncSource.state.pagination.previousCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { before: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\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.after === undefined && requested.before === undefined) {\n if (\n asyncSource.state.loading ||\n (asyncSource.state.pagination.pageSize === normalized &&\n Object.is(asyncSource.state.params, requested.params))\n )\n return;\n await fetch(requested);\n return;\n }\n requested = { pageSize: normalized, params: requested.params };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await fetch(requested);\n return;\n }\n requested = { pageSize: requested.pageSize, 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":"A,6FAYA,IAAM,kBACJ,EACA,KAC+B,CAC/B,GAAIQ,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYA,EAAO,UAAW,EACxE,WACA,GAAIA,GAAQ,iBAAmB,IAAA,IAAa,CAAE,eAAgBA,EAAO,cAAe,EACpF,GAAIA,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYR,EAAAA,WAAWQ,EAAO,UAAU,CAAE,CACtF,GAGA,SAAgB,mBACd,EACmC,CACnC,GAAIC,EAAO,QAAU,IAAA,IAAaA,EAAO,SAAW,IAAA,GAClD,MAAM,IAAIR,EAAAA,4BAA4B,uDAAuD,EAG/F,IAAIS,EAAuC,CACzC,GAAID,EAAO,QAAU,IAAA,IAAa,CAAE,MAAOA,EAAO,KAAM,EACxD,GAAIA,EAAO,SAAW,IAAA,IAAa,CAAE,OAAQA,EAAO,MAAO,EAC3D,SAAUP,EAAAA,gBAAgBO,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACME,EAAcR,EAAAA,kBAA0D,CAC5E,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiBO,EAAU,QAAQ,EAC/C,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAIC,EAAY,SAAU,MAAM,IAAIP,EAAAA,sBACtC,EAEM,MAAS,GACbO,EAAY,MAAM,CAChB,SAAU,EAAU,KAAW,CAAE,GAAGC,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,G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ACF,MAAM,MAAMA,CAAS,CAEvB,KACA,GAAY,CAAE,SAAUM,EAAY,OAAQN,EAAU,MAAO,EAC7D,MAAM,MAAMA,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CACtB,WAAW,EACP,OAAO,GAAGQ,EAAQR,EAAU,MAAM,EAChC,CAACC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAGO,EAAQP,EAAY,MAAM,MAAM,GAAG,MAAM,MAAMD,CAAS,GAGvG,EAAY,CAAE,SAAUA,EAAU,SAAU,QAAO,EACnD,MAAM,MAAMA,CAAS,EACvB,EAEA,IAAI,OAAQ,CACV,OAAOC,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBG,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
|
package/dist/cursorSource.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createAsyncSource as e } from "./asyncSource.js";
|
|
2
|
+
import { SourcererConfigurationError as t, SourcererDisposedError as n } from "./errors.js";
|
|
3
3
|
import { positiveInteger as r, totalItems as i } from "./pagination.js";
|
|
4
|
-
|
|
4
|
+
// src/cursorSource.ts
|
|
5
5
|
var createPagination = (e, t) => ({
|
|
6
6
|
...t?.nextCursor !== void 0 && { nextCursor: t.nextCursor },
|
|
7
7
|
pageSize: e,
|
|
@@ -9,20 +9,20 @@ var createPagination = (e, t) => ({
|
|
|
9
9
|
...t?.totalItems !== void 0 && { totalItems: i(t.totalItems) }
|
|
10
10
|
});
|
|
11
11
|
function createCursorSource(i) {
|
|
12
|
-
if (i.after !== void 0 && i.before !== void 0) throw new
|
|
12
|
+
if (i.after !== void 0 && i.before !== void 0) throw new t("Cursor source cannot start with both after and before");
|
|
13
13
|
let a = {
|
|
14
14
|
...i.after !== void 0 && { after: i.after },
|
|
15
15
|
...i.before !== void 0 && { before: i.before },
|
|
16
16
|
pageSize: r(i.pageSize ?? 20, "pageSize"),
|
|
17
17
|
params: i.params
|
|
18
|
-
}, o =
|
|
18
|
+
}, o = e({
|
|
19
19
|
error: null,
|
|
20
20
|
items: [],
|
|
21
21
|
loading: !1,
|
|
22
22
|
pagination: createPagination(a.pageSize),
|
|
23
23
|
params: a.params
|
|
24
24
|
}), assertLive = () => {
|
|
25
|
-
if (o.disposed) throw new
|
|
25
|
+
if (o.disposed) throw new n();
|
|
26
26
|
}, fetch = (e) => o.fetch({
|
|
27
27
|
failure: (e, t) => ({
|
|
28
28
|
...e,
|
|
@@ -103,7 +103,6 @@ function createCursorSource(i) {
|
|
|
103
103
|
};
|
|
104
104
|
return s;
|
|
105
105
|
}
|
|
106
|
-
//#endregion
|
|
107
106
|
export { createCursorSource };
|
|
108
107
|
|
|
109
108
|
//# sourceMappingURL=cursorSource.js.map
|
package/dist/errors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/errors.ts
|
|
2
2
|
var SourcererError = class extends Error {
|
|
3
3
|
constructor(e, t) {
|
|
4
4
|
super(e, t), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -8,7 +8,6 @@ var SourcererError = class extends Error {
|
|
|
8
8
|
super(e);
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
|
-
//#endregion
|
|
12
11
|
export { SourcererConfigurationError, SourcererDisposedError, SourcererError };
|
|
13
12
|
|
|
14
13
|
//# sourceMappingURL=errors.js.map
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./cursorSource.cjs"),t=require("./errors.cjs"),n=require("./infiniteSource.cjs"),r=require("./localSource.cjs"),i=require("./pageSource.cjs"),a=require("./resourceSource.cjs");exports.SourcererConfigurationError=t.SourcererConfigurationError,exports.SourcererDisposedError=t.SourcererDisposedError,exports.SourcererError=t.SourcererError,exports.createCursorSource=e.createCursorSource,exports.createInfiniteSource=n.createInfiniteSource,exports.createLocalSource=r.createLocalSource,exports.createPageSource=i.createPageSource,exports.createResource=a.createResource;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createCursorSource as e } from "./cursorSource.js";
|
|
2
|
+
import { SourcererConfigurationError as t, SourcererDisposedError as n, SourcererError as r } from "./errors.js";
|
|
3
3
|
import { createInfiniteSource as i } from "./infiniteSource.js";
|
|
4
4
|
import { createLocalSource as a } from "./localSource.js";
|
|
5
5
|
import { createPageSource as o } from "./pageSource.js";
|
|
6
6
|
import { createResource as s } from "./resourceSource.js";
|
|
7
|
-
export {
|
|
7
|
+
export { t as SourcererConfigurationError, n as SourcererDisposedError, r as SourcererError, e as createCursorSource, i as createInfiniteSource, a as createLocalSource, o as createPageSource, s as createResource };
|
package/dist/infiniteSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function createInfiniteSource(r){let i={pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=1,o=!1,s=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(0,i.pageSize,0),params:i.params}),assertLive=()=>{if(s.disposed)throw new t.SourcererDisposedError},fetch=(e,t,c)=>{let l=s.state.items;return s.fetch({commit:()=>{o=!0,a=t+1,i=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:n=>r.load({page:t,pageSize:e.pageSize,params:e.params,signal:n}),pending:t=>({...t,error:null,loading:!0,...c||Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>{let r=c?[...l,...t.items]:[...t.items];return{error:null,items:r,loading:!1,pagination:createPagination(r.length,e.pageSize,n.totalItems(t.totalItems)),params:e.params}}})},reload=()=>fetch(i,1,!1),c={get disposalSignal(){return s.disposalSignal},dispose:s.dispose,get disposed(){return s.disposed},async loadMore(){assertLive(),s.state.loading||(!o||!Object.is(i.params,s.state.params)?await reload():s.state.pagination.hasMore&&await fetch(i,a,!0))},reload,async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize){if(s.state.loading||s.state.pagination.pageSize===t)return;await reload()}else i={...i,pageSize:t},a=1,await reload()},async setParams(e){assertLive(),Object.is(e,i.params)?!s.state.loading&&!Object.is(e,s.state.params)&&await reload():(i={...i,params:e},a=1,await reload())},get state(){return s.state},subscribe:s.subscribe,[Symbol.dispose](){c.dispose()}};return c}exports.createInfiniteSource=createInfiniteSource;
|
|
2
2
|
//# sourceMappingURL=infiniteSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.cjs","names":["positiveInteger","createAsyncSource","SourcererDisposedError","totalItems","
|
|
1
|
+
{"version":3,"file":"infiniteSource.cjs","names":["positiveInteger","createAsyncSource","SourcererDisposedError","totalItems","require_asyncSource","require_errors","require_pagination","loadedItems","total","requested","config","nextPage","hasLoaded","asyncSource","page","request","previous","append","items","result","source","normalized","pageSize","params"],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<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 async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\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":"A,6FAOA,IAAM,kBAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAASO,EAAcC,EACvB,cACA,WACA,WAAYA,CACd,GAGA,SAAgB,qBACd,EAC4B,CAC5B,IAAIC,EAA8B,CAChC,SAAUT,EAAAA,gBAAgBU,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACIC,EAAW,EACXC,EAAY,GACVC,EAAcZ,EAAAA,kBAAmD,CACrE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiB,EAAGQ,EAAU,SAAU,CAAC,EACrD,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAII,EAAY,SAAU,MAAM,IAAIX,EAAAA,sBACtC,EAEM,OAAS,EAA2B,EAAc,IAAmC,CACzF,IAAMK,EAAcM,EAAY,MAAM,MAEtC,OAAOA,EAAY,MAAM,CACvB,WAAc,CACZ,EAAY,GACZ,EAAWC,EAAO,EAClB,EAAYC,CACd,EACA,SAAU,EAAU,KAAW,CAAE,GAAGC,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,GAC9F,KAAO,GAAWN,EAAO,KAAK,CAAE,OAAM,SAAUK,EAAQ,SAAU,OAAQA,EAAQ,OAAQ,QAAO,CAAC,EAClG,QAAU,IAAc,CACtB,GAAGC,EACH,MAAO,KACP,QAAS,GACT,GAAIC,GAAU,OAAO,GAAGF,EAAQ,OAAQC,EAAS,MAAM,EAAI,CAAC,EAAI,CAAE,cAAeD,EAAQ,MAAO,CAClG,GACA,QAAU,GAAW,CACnB,IAAMG,EAAQD,EAAS,CAAC,GAAGV,EAAa,GAAG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,EAEA,OAAOA,CACT,C"}
|
package/dist/infiniteSource.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createAsyncSource as e } from "./asyncSource.js";
|
|
2
|
+
import { SourcererDisposedError as t } from "./errors.js";
|
|
3
3
|
import { positiveInteger as n, totalItems as r } from "./pagination.js";
|
|
4
|
-
|
|
4
|
+
// src/infiniteSource.ts
|
|
5
5
|
var createPagination = (e, t, n) => ({
|
|
6
6
|
hasMore: e < n,
|
|
7
7
|
loadedItems: e,
|
|
@@ -12,14 +12,14 @@ function createInfiniteSource(i) {
|
|
|
12
12
|
let a = {
|
|
13
13
|
pageSize: n(i.pageSize ?? 20, "pageSize"),
|
|
14
14
|
params: i.params
|
|
15
|
-
}, o = 1, s = !1, c =
|
|
15
|
+
}, o = 1, s = !1, c = e({
|
|
16
16
|
error: null,
|
|
17
17
|
items: [],
|
|
18
18
|
loading: !1,
|
|
19
19
|
pagination: createPagination(0, a.pageSize, 0),
|
|
20
20
|
params: a.params
|
|
21
21
|
}), assertLive = () => {
|
|
22
|
-
if (c.disposed) throw new
|
|
22
|
+
if (c.disposed) throw new t();
|
|
23
23
|
}, fetch = (e, t, n) => {
|
|
24
24
|
let l = c.state.items;
|
|
25
25
|
return c.fetch({
|
|
@@ -64,7 +64,7 @@ function createInfiniteSource(i) {
|
|
|
64
64
|
return c.disposed;
|
|
65
65
|
},
|
|
66
66
|
async loadMore() {
|
|
67
|
-
assertLive(),
|
|
67
|
+
assertLive(), c.state.loading || (!s || !Object.is(a.params, c.state.params) ? await reload() : c.state.pagination.hasMore && await fetch(a, o, !0));
|
|
68
68
|
},
|
|
69
69
|
reload,
|
|
70
70
|
async setPageSize(e) {
|
|
@@ -94,7 +94,6 @@ function createInfiniteSource(i) {
|
|
|
94
94
|
};
|
|
95
95
|
return l;
|
|
96
96
|
}
|
|
97
|
-
//#endregion
|
|
98
97
|
export { createInfiniteSource };
|
|
99
98
|
|
|
100
99
|
//# sourceMappingURL=infiniteSource.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.js","names":["loadedItems","total","requested","positiveInteger","config","nextPage","hasLoaded","asyncSource","createAsyncSource","SourcererDisposedError","page","request","previous","append","items","result","totalItems","source","normalized","pageSize","params"],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<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 async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\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":";;;;AAOA,IAAM,oBAAoB,GAAqB,GAAkB,OAAuC;CACtG,SAASA,IAAcC;CACvB;CACA;CACA,YAAYA;AACd;AAGA,SAAgB,qBACd,GAC4B;CAC5B,IAAIC,IAA8B;EAChC,UAAUC,EAAgBC,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQA,EAAO;CACjB,GACIC,IAAW,GACXC,IAAY,IACVC,IAAcC,EAAmD;EACrE,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,iBAAiB,GAAGN,EAAU,UAAU,CAAC;EACrD,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAIK,EAAY,UAAU,MAAM,IAAIE,EAAuB;CAC7D,GAEM,SAAS,GAA2B,GAAc,MAAmC;EACzF,IAAMT,IAAcO,EAAY,MAAM;EAEtC,OAAOA,EAAY,MAAM;GACvB,cAAc;IAGZ,AAFA,IAAY,IACZ,IAAWG,IAAO,GAClB,IAAYC;GACd;GACA,UAAU,GAAU,OAAW;IAAE,GAAGC;IAAU;IAAO,SAAS;IAAO,eAAe,KAAA;GAAU;GAC9F,OAAO,MAAWR,EAAO,KAAK;IAAE;IAAM,UAAUO,EAAQ;IAAU,QAAQA,EAAQ;IAAQ;GAAO,CAAC;GAClG,UAAU,OAAc;IACtB,GAAGC;IACH,OAAO;IACP,SAAS;IACT,GAAIC,KAAU,OAAO,GAAGF,EAAQ,QAAQC,EAAS,MAAM,IAAI,CAAC,IAAI,EAAE,eAAeD,EAAQ,OAAO;GAClG;GACA,UAAU,MAAW;IACnB,IAAMG,IAAQD,IAAS,CAAC,GAAGb,GAAa,GAAGe,EAAO,KAAK,IAAI,CAAC,GAAGA,EAAO,KAAK;IAC3E,OAAO;KACL,OAAO;KACP;KACA,SAAS;KACT,YAAY,iBAAiBD,EAAM,QAAQH,EAAQ,UAAUK,EAAWD,EAAO,UAAU,CAAC;KAC1F,QAAQJ,EAAQ;IAClB;GACF;EACF,CAAC;CACH,GAEM,eAA8B,MAAMT,GAAW,GAAG,EAAK,GAEvDe,IAAqC;EACzC,IAAI,iBAAiB;GACnB,OAAOV,EAAY;EACrB;EAEA,SAASA,EAAY;EAErB,IAAI,WAAW;GACb,OAAOA,EAAY;EACrB;EAEA,MAAM,WAAW;GAEf,AADA,WAAW,
|
|
1
|
+
{"version":3,"file":"infiniteSource.js","names":["loadedItems","total","requested","positiveInteger","config","nextPage","hasLoaded","asyncSource","createAsyncSource","SourcererDisposedError","page","request","previous","append","items","result","totalItems","source","normalized","pageSize","params"],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<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 async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\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":";;;;AAOA,IAAM,oBAAoB,GAAqB,GAAkB,OAAuC;CACtG,SAASA,IAAcC;CACvB;CACA;CACA,YAAYA;AACd;AAGA,SAAgB,qBACd,GAC4B;CAC5B,IAAIC,IAA8B;EAChC,UAAUC,EAAgBC,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQA,EAAO;CACjB,GACIC,IAAW,GACXC,IAAY,IACVC,IAAcC,EAAmD;EACrE,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,iBAAiB,GAAGN,EAAU,UAAU,CAAC;EACrD,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAIK,EAAY,UAAU,MAAM,IAAIE,EAAuB;CAC7D,GAEM,SAAS,GAA2B,GAAc,MAAmC;EACzF,IAAMT,IAAcO,EAAY,MAAM;EAEtC,OAAOA,EAAY,MAAM;GACvB,cAAc;IAGZ,AAFA,IAAY,IACZ,IAAWG,IAAO,GAClB,IAAYC;GACd;GACA,UAAU,GAAU,OAAW;IAAE,GAAGC;IAA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;EAEA,MAAM,UAAU,GAAQ;GAEtB,AADA,WAAW,GACP,OAAO,GAAGE,GAAQlB,EAAU,MAAM,IAChC,CAACK,EAAY,MAAM,WAAW,CAAC,OAAO,GAAGa,GAAQb,EAAY,MAAM,MAAM,KAAG,MAAM,OAAO,KAG/F,IAAY;IAAE,GAAGL;IAAW;GAAO,GACnC,IAAW,GACX,MAAM,OAAO;EACf;EAEA,IAAI,QAAQ;GACV,OAAOK,EAAY;EACrB;EAEA,WAAWA,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAOU;AACT"}
|
package/dist/localSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs"),t=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./pagination.cjs"),n=require("./sourceStore.cjs");function createLocalSource(r,i={}){let a=r,o=1,s=t.positiveInteger(i.pageSize??20,`pageSize`),c=i.params,buildState=()=>{let e=i.filter?a.filter(e=>i.filter?.(e,c)):a,n=t.createPagePagination(o,s,e.length);if(o>n.pageCount)return o=n.pageCount,buildState();let r=(o-1)*s;return{error:null,items:e.slice(r,r+s),loading:!1,pagination:n,params:c}},l=n.createSourceStore(buildState()),assertLive=()=>{if(l.disposed)throw new e.SourcererDisposedError},samePagination=(e,t)=>Object.keys(e).every(n=>e[n]===t[n]),commit=()=>{let e=buildState(),t=l.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&&samePagination(e.pagination,t.pagination)||l.set(e)},goTo=e=>{assertLive();let n=Math.min(t.positiveInteger(e,`page`),l.value.pagination.pageCount);n!==o&&(o=n,commit())},u={get disposalSignal(){return l.disposalSignal},dispose:l.dispose,get disposed(){return l.disposed},first(){goTo(1)},goTo,last(){goTo(l.value.pagination.pageCount)},next(){l.value.pagination.hasNext?goTo(o+1):assertLive()},previous(){l.value.pagination.hasPrevious?goTo(o-1):assertLive()},setItems(e){assertLive(),e!==a&&(a=e,commit())},setPageSize(e){assertLive();let n=t.positiveInteger(e,`pageSize`);n!==s&&(o=1,s=n,commit())},setParams(e){assertLive(),Object.is(e,c)||(o=1,c=e,commit())},get state(){return l.value},subscribe:l.subscribe,[Symbol.dispose](){u.dispose()}};return u}exports.createLocalSource=createLocalSource;
|
|
2
2
|
//# sourceMappingURL=localSource.cjs.map
|
package/dist/localSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.cjs","names":["positiveInteger","createPagePagination","createSourceStore","SourcererDisposedError","require_errors","
|
|
1
|
+
{"version":3,"file":"localSource.cjs","names":["positiveInteger","createPagePagination","createSourceStore","SourcererDisposedError","require_errors","require_pagination","require_sourceStore","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,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,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,OAAO,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,27 +1,27 @@
|
|
|
1
1
|
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { createPagePagination as t, positiveInteger as n } from "./pagination.js";
|
|
3
|
+
import { createSourceStore as r } from "./sourceStore.js";
|
|
4
|
+
// src/localSource.ts
|
|
5
5
|
function createLocalSource(i, a = {}) {
|
|
6
|
-
let o = i, s = 1, c =
|
|
7
|
-
let e = a.filter ? o.filter((e) => a.filter?.(e, l)) : o,
|
|
8
|
-
if (s >
|
|
6
|
+
let o = i, s = 1, c = n(a.pageSize ?? 20, "pageSize"), l = a.params, buildState = () => {
|
|
7
|
+
let e = a.filter ? o.filter((e) => a.filter?.(e, l)) : o, n = t(s, c, e.length);
|
|
8
|
+
if (s > n.pageCount) return s = n.pageCount, buildState();
|
|
9
9
|
let r = (s - 1) * c;
|
|
10
10
|
return {
|
|
11
11
|
error: null,
|
|
12
12
|
items: e.slice(r, r + c),
|
|
13
13
|
loading: !1,
|
|
14
|
-
pagination:
|
|
14
|
+
pagination: n,
|
|
15
15
|
params: l
|
|
16
16
|
};
|
|
17
|
-
}, u =
|
|
17
|
+
}, u = r(buildState()), assertLive = () => {
|
|
18
18
|
if (u.disposed) throw new e();
|
|
19
19
|
}, samePagination = (e, t) => Object.keys(e).every((n) => e[n] === t[n]), commit = () => {
|
|
20
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
21
|
Object.is(e.params, t.params) && n && samePagination(e.pagination, t.pagination) || u.set(e);
|
|
22
22
|
}, goTo = (e) => {
|
|
23
23
|
assertLive();
|
|
24
|
-
let t = Math.min(
|
|
24
|
+
let t = Math.min(n(e, "page"), u.value.pagination.pageCount);
|
|
25
25
|
t !== s && (s = t, commit());
|
|
26
26
|
}, d = {
|
|
27
27
|
get disposalSignal() {
|
|
@@ -49,11 +49,11 @@ function createLocalSource(i, a = {}) {
|
|
|
49
49
|
},
|
|
50
50
|
setPageSize(e) {
|
|
51
51
|
assertLive();
|
|
52
|
-
let t =
|
|
52
|
+
let t = n(e, "pageSize");
|
|
53
53
|
t !== c && (s = 1, c = t, commit());
|
|
54
54
|
},
|
|
55
55
|
setParams(e) {
|
|
56
|
-
assertLive(),
|
|
56
|
+
assertLive(), Object.is(e, l) || (s = 1, l = e, commit());
|
|
57
57
|
},
|
|
58
58
|
get state() {
|
|
59
59
|
return u.value;
|
|
@@ -65,7 +65,6 @@ function createLocalSource(i, a = {}) {
|
|
|
65
65
|
};
|
|
66
66
|
return d;
|
|
67
67
|
}
|
|
68
|
-
//#endregion
|
|
69
68
|
export { createLocalSource };
|
|
70
69
|
|
|
71
70
|
//# sourceMappingURL=localSource.js.map
|
package/dist/localSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,C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AMH,IAAalB,EAAgBsB,GAAc,UAAU;GAC3D,AAAIJ,MAAenB,MACnB,IAAO,GACP,IAAWmB,GACX,OAAO;EACT;EAEA,UAAU,GAAY;GAEpB,AADA,WAAW,GACP,
|
|
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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AAS,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,OAAO,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("./
|
|
1
|
+
const e=require("./asyncSource.cjs"),t=require("./errors.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=e.createAsyncSource({error:null,items:[],loading:!1,pagination:n.createPagePagination(a.page,a.pageSize,0),params:a.params}),assertLive=()=>{if(s.disposed)throw new t.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":["positiveInteger","createAsyncSource","createPagePagination","SourcererDisposedError","totalItems","isEqual","
|
|
1
|
+
{"version":3,"file":"pageSource.cjs","names":["positiveInteger","createAsyncSource","createPagePagination","SourcererDisposedError","totalItems","isEqual","require_asyncSource","require_errors","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":"A,gIAUA,SAAgB,iBAAyC,EAA8D,CACrH,IAAIU,EAA8B,CAChC,KAAM,EACN,SAAUV,EAAAA,gBAAgBW,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACIC,EAAY,GACVC,EAAcZ,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W,KAAM,EAAG,SAAUQ,CAAW,EAC1D,MAAM,MAAMR,CAAS,EACvB,EAEA,MAAM,UAAU,EAAQ,CACtB,WAAW,EACP,OAAO,GAAGU,EAAQV,EAAU,MAAM,EAChC,CAACG,EAAY,MAAM,SAAW,CAAC,YAAYH,CAAS,GAAG,MAAM,MAAMA,CAAS,GAGlF,EAAY,CAAE,GAAGA,EAAW,KAAM,EAAG,QAAO,EAC5C,MAAM,MAAMA,CAAS,EACvB,EAEA,IAAI,OAAQ,CACV,OAAOG,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBM,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
|
package/dist/pageSource.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createAsyncSource as e } from "./asyncSource.js";
|
|
2
|
+
import { SourcererDisposedError as t } from "./errors.js";
|
|
3
3
|
import { createPagePagination as n, positiveInteger as r, totalItems as i } from "./pagination.js";
|
|
4
4
|
import { isEqual as a } from "@vielzeug/arsenal";
|
|
5
|
-
|
|
5
|
+
// src/pageSource.ts
|
|
6
6
|
function createPageSource(o) {
|
|
7
7
|
let s = {
|
|
8
8
|
page: 1,
|
|
9
9
|
pageSize: r(o.pageSize ?? 20, "pageSize"),
|
|
10
10
|
params: o.params
|
|
11
|
-
}, c = !1, l =
|
|
11
|
+
}, c = !1, l = e({
|
|
12
12
|
error: null,
|
|
13
13
|
items: [],
|
|
14
14
|
loading: !1,
|
|
15
15
|
pagination: n(s.page, s.pageSize, 0),
|
|
16
16
|
params: s.params
|
|
17
17
|
}), assertLive = () => {
|
|
18
|
-
if (l.disposed) throw new
|
|
18
|
+
if (l.disposed) throw new t();
|
|
19
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
20
|
commit: () => {
|
|
21
21
|
c = !0, s = e;
|
|
@@ -46,7 +46,7 @@ function createPageSource(o) {
|
|
|
46
46
|
}), normalizePage = (e) => c ? Math.min(r(e, "page"), l.state.pagination.pageCount) : r(e, "page"), goTo = async (e) => {
|
|
47
47
|
assertLive();
|
|
48
48
|
let t = normalizePage(e);
|
|
49
|
-
t === s.page ?
|
|
49
|
+
t === s.page ? l.state.loading || c && isCommitted(s) || await fetch(s) : (s = {
|
|
50
50
|
...s,
|
|
51
51
|
page: t
|
|
52
52
|
}, await fetch(s));
|
|
@@ -54,7 +54,7 @@ function createPageSource(o) {
|
|
|
54
54
|
async apply(e) {
|
|
55
55
|
assertLive();
|
|
56
56
|
let t = e.params === void 0 ? s.params : e.params;
|
|
57
|
-
a(t, s.params) ? e.page === void 0 ?
|
|
57
|
+
a(t, s.params) ? e.page === void 0 ? l.state.loading || c && isCommitted(s) || await fetch(s) : await goTo(e.page) : (s = {
|
|
58
58
|
...s,
|
|
59
59
|
page: e.page === void 0 ? 1 : r(e.page, "page"),
|
|
60
60
|
params: t
|
|
@@ -109,7 +109,6 @@ function createPageSource(o) {
|
|
|
109
109
|
};
|
|
110
110
|
return u;
|
|
111
111
|
}
|
|
112
|
-
//#endregion
|
|
113
112
|
export { createPageSource };
|
|
114
113
|
|
|
115
114
|
//# sourceMappingURL=pageSource.js.map
|
package/dist/pageSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":";;;;;AAUA,SAAgB,iBAAyC,GAA8D;CACrH,IAAIA,IAA8B;EAChC,MAAM;EACN,UAAUC,EAAgBC,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQA,EAAO;CACjB,GACIC,IAAY,IACVC,IAAcC,EAA+C;EACjE,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAYC,EAAqBN,EAAU,MAAMA,EAAU,UAAU,CAAC;EACtE,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAII,EAAY,UAAU,MAAM,IAAIG,EAAuB;CAC7D,GACM,eAAe,MACnBH,EAAY,MAAM,WAAW,SAASI,EAAQ,QAC9CJ,EAAY,MAAM,WAAW,aAAaI,EAAQ,YAClD,OAAO,GAAGJ,EAAY,MAAM,QAAQI,EAAQ,MAAM,GAE9C,SAAS,MACbJ,EAAY,MAAM;EAChB,cAAc;GAEZ,AADA,IAAY,IACZ,IAAYI;EACd;EACA,UAAU,GAAU,OAAW;GAAE,GAAGC;GAAU;GAAO,SAAS;GAAO,eAAe,KAAA;EAAU;EAC9F,OAAO,MAAWP,EAAO,KAAK;GAAE,GAAGM;GAAS;EAAO,CAAC;EACpD,UAAU,OAAc;GACtB,GAAGC;GACH,OAAO;GACP,SAAS;GACT,GAAI,OAAO,GAAGD,EAAQ,QAAQC,EAAS,MAAM,IAAI,CAAC,IAAI,EAAE,eAAeD,EAAQ,OAAO;EACxF;EACA,UAAU,OAAY;GACpB,OAAO;GACP,OAAO,CAAC,GAAGE,EAAO,KAAK;GACvB,SAAS;GACT,YAAYJ,EAAqBE,EAAQ,MAAMA,EAAQ,UAAUG,EAAWD,EAAO,UAAU,CAAC;GAC9F,QAAQF,EAAQ;EAClB;CACF,CAAC,GAEG,iBAAiB,MACrBL,IACI,KAAK,IAAIF,EAAgBW,GAAM,MAAM,GAAGR,EAAY,MAAM,WAAW,SAAS,IAC9EH,EAAgBW,GAAM,MAAM,GAE5B,OAAO,OAAO,MAAgC;EAClD,WAAW;EACX,IAAMC,IAAa,cAAcD,CAAI;EACrC,AAAIC,MAAeb,EAAU,
|
|
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}\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U,YAAYA,EAAU,SAAS,IACtD,CAACI,EAAY,MAAM,WAAW,CAAC,YAAYJ,CAAS,KAAG,MAAM,MAAMA,CAAS,KAGlF,IAAY;IAAE,GAAGA;IAAW,MAAM;IAAG,UAAUa;GAAW,GAC1D,MAAM,MAAMb,CAAS;EACvB;EAEA,MAAM,UAAU,GAAQ;GAEtB,AADA,WAAW,GACP,OAAO,GAAGe,GAAQf,EAAU,MAAM,IAChC,CAACI,EAAY,MAAM,WAAW,CAAC,YAAYJ,CAAS,KAAG,MAAM,MAAMA,CAAS,KAGlF,IAAY;IAAE,GAAGA;IAAW,MAAM;IAAG;GAAO,GAC5C,MAAM,MAAMA,CAAS;EACvB;EAEA,IAAI,QAAQ;GACV,OAAOI,EAAY;EACrB;EAEA,WAAWA,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAOU;AACT"}
|
package/dist/pagination.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SourcererConfigurationError as e } from "./errors.js";
|
|
2
|
-
|
|
2
|
+
// src/pagination.ts
|
|
3
3
|
function positiveInteger(t, n) {
|
|
4
4
|
if (!Number.isInteger(t) || t < 1) throw new e(`${n} must be a positive integer`);
|
|
5
5
|
return t;
|
|
@@ -19,7 +19,6 @@ function createPagePagination(e, t, n) {
|
|
|
19
19
|
totalItems: n
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
//#endregion
|
|
23
22
|
export { createPagePagination, positiveInteger, totalItems };
|
|
24
23
|
|
|
25
24
|
//# sourceMappingURL=pagination.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/requestController.ts
|
|
2
2
|
function createRequestController() {
|
|
3
3
|
let e, t = 0;
|
|
4
4
|
return {
|
|
@@ -25,7 +25,6 @@ function toError(e) {
|
|
|
25
25
|
let t = typeof e == "string" && e.length > 0 ? e : "Source request failed";
|
|
26
26
|
return Error(t, { cause: e });
|
|
27
27
|
}
|
|
28
|
-
//#endregion
|
|
29
28
|
export { createRequestController, toError };
|
|
30
29
|
|
|
31
30
|
//# sourceMappingURL=requestController.js.map
|