@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/errors.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
//#region src/errors.ts
|
|
2
2
|
var e = class extends Error {
|
|
3
|
+
static errorName = "SourcererError";
|
|
3
4
|
constructor(e, t) {
|
|
4
|
-
super(e, t), this.name = new.target.
|
|
5
|
+
super(e, t), this.name = new.target.errorName, Object.setPrototypeOf(this, new.target.prototype);
|
|
5
6
|
}
|
|
6
|
-
}, t = class extends e {
|
|
7
|
+
}, t = class extends e {
|
|
8
|
+
static errorName = "SourcererConfigurationError";
|
|
9
|
+
}, n = class extends e {
|
|
10
|
+
static errorName = "SourcererDisposedError";
|
|
11
|
+
constructor(e = "Sourcerer source disposed") {
|
|
12
|
+
super(e);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
7
15
|
//#endregion
|
|
8
|
-
export { t as SourcererConfigurationError, e as SourcererError };
|
|
16
|
+
export { t as SourcererConfigurationError, n as SourcererDisposedError, e as SourcererError };
|
|
9
17
|
|
|
10
18
|
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.
|
|
1
|
+
{"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n protected static readonly errorName: string = 'SourcererError';\n\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = (new.target as typeof SourcererError).errorName;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {\n protected static override readonly errorName = 'SourcererConfigurationError';\n}\n\nexport class SourcererDisposedError extends SourcererError {\n protected static override readonly errorName = 'SourcererDisposedError';\n\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":";AACA,IAAa,IAAb,cAAoC,MAAM;CACxC,OAA0B,YAAoB;CAE9C,YAAY,GAAiB,GAAqB;EAGhD,AAFA,MAAM,GAAS,CAAI,GACnB,KAAK,OAAQ,WAAqC,WAClD,OAAO,eAAe,MAAM,WAAW,SAAS;CAClD;AACF,GAGa,IAAb,cAAiD,EAAe;CAC9D,OAAmC,YAAY;AACjD,GAEa,IAAb,cAA4C,EAAe;CACzD,OAAmC,YAAY;CAE/C,YAAY,IAAU,6BAA6B;EACjD,MAAM,CAAO;CACf;AACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./errors.cjs"),t=require("./cursorSource.cjs"),n=require("./infiniteSource.cjs"),r=require("./localSource.cjs"),i=require("./pageSource.cjs");exports.SourcererConfigurationError=e.SourcererConfigurationError,exports.SourcererError=e.SourcererError,exports.createCursorSource=t.createCursorSource,exports.createInfiniteSource=n.createInfiniteSource,exports.createLocalSource=r.createLocalSource,exports.createPageSource=i.createPageSource;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./errors.cjs"),t=require("./cursorSource.cjs"),n=require("./infiniteSource.cjs"),r=require("./localSource.cjs"),i=require("./pageSource.cjs");exports.SourcererConfigurationError=e.SourcererConfigurationError,exports.SourcererDisposedError=e.SourcererDisposedError,exports.SourcererError=e.SourcererError,exports.createCursorSource=t.createCursorSource,exports.createInfiniteSource=n.createInfiniteSource,exports.createLocalSource=r.createLocalSource,exports.createPageSource=i.createPageSource;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createCursorSource } from './cursorSource';
|
|
2
|
-
export { SourcererConfigurationError, SourcererError } from './errors';
|
|
2
|
+
export { SourcererConfigurationError, SourcererDisposedError, SourcererError } from './errors';
|
|
3
3
|
export { createInfiniteSource } from './infiniteSource';
|
|
4
4
|
export { createLocalSource } from './localSource';
|
|
5
5
|
export { createPageSource } from './pageSource';
|
|
6
|
-
export type {
|
|
6
|
+
export type { CursorLoadContext, CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState, InfiniteLoadContext, InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState, LocalSource, LocalSourceConfig, LocalSourceState, PageLoadContext, PagePagination, PageResult, PageSource, PageSourceConfig, PageSourceState, } from './types';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,2BAA2B,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/F,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,eAAe,GAChB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { SourcererConfigurationError as e,
|
|
2
|
-
import { createCursorSource as
|
|
3
|
-
import { createInfiniteSource as
|
|
4
|
-
import { createLocalSource as
|
|
5
|
-
import { createPageSource as
|
|
6
|
-
export { e as SourcererConfigurationError, t as
|
|
1
|
+
import { SourcererConfigurationError as e, SourcererDisposedError as t, SourcererError as n } from "./errors.js";
|
|
2
|
+
import { createCursorSource as r } from "./cursorSource.js";
|
|
3
|
+
import { createInfiniteSource as i } from "./infiniteSource.js";
|
|
4
|
+
import { createLocalSource as a } from "./localSource.js";
|
|
5
|
+
import { createPageSource as o } from "./pageSource.js";
|
|
6
|
+
export { e as SourcererConfigurationError, t as SourcererDisposedError, n as SourcererError, r as createCursorSource, i as createInfiniteSource, a as createLocalSource, o as createPageSource };
|
package/dist/infiniteSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");var r=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function i(i){let a={pageSize:n.positiveInteger(i.pageSize??20,`pageSize`),params:i.params},o=1,s=!1,c=t.createAsyncSource({error:null,items:[],loading:!1,pagination:r(0,a.pageSize,0),params:a.params}),l=()=>{if(c.disposed)throw new e.SourcererDisposedError},u=(e,t,l)=>{let u=c.state.items;return c.fetch({commit:()=>{s=!0,o=t+1,a=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:n=>i.load({page:t,pageSize:e.pageSize,params:e.params,signal:n}),pending:t=>({...t,error:null,loading:!0,...l||Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>{let i=l?[...u,...t.items]:[...t.items];return{error:null,items:i,loading:!1,pagination:r(i.length,e.pageSize,n.totalItems(t.totalItems)),params:e.params}}})},d=()=>u(a,1,!1),f={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},async loadMore(){if(l(),!c.state.loading){if(!s||!Object.is(a.params,c.state.params)){await d();return}c.state.pagination.hasMore&&await u(a,o,!0)}},reload:d,async setPageSize(e){l();let t=n.positiveInteger(e,`pageSize`);if(t===a.pageSize){if(c.state.loading||c.state.pagination.pageSize===t)return;await d();return}a={...a,pageSize:t},o=1,await d()},async setParams(e){if(l(),Object.is(e,a.params)){!c.state.loading&&!Object.is(e,c.state.params)&&await d();return}a={...a,params:e},o=1,await d()},get state(){return c.state},subscribe:c.subscribe,[Symbol.dispose](){f.dispose()}};return f}exports.createInfiniteSource=i;
|
|
2
2
|
//# sourceMappingURL=infiniteSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.cjs","names":[],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"infiniteSource.cjs","names":[],"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":"6FAOA,IAAM,GAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAAS,EAAc,EACvB,cACA,WACA,WAAY,CACd,GAGA,SAAgB,EACd,EAC4B,CAC5B,IAAI,EAA8B,CAChC,SAAU,EAAA,gBAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,EACI,EAAW,EACX,EAAY,GACV,EAAc,EAAA,kBAAmD,CACrE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,EAAiB,EAAG,EAAU,SAAU,CAAC,EACrD,OAAQ,EAAU,MACpB,CAAC,EACK,MAAyB,CAC7B,GAAI,EAAY,SAAU,MAAM,IAAI,EAAA,sBACtC,EAEM,GAAS,EAA2B,EAAc,IAAmC,CACzF,IAAM,EAAc,EAAY,MAAM,MAEtC,OAAO,EAAY,MAAM,CACvB,WAAc,CACZ,EAAY,GACZ,EAAW,EAAO,EAClB,EAAY,CACd,EACA,SAAU,EAAU,KAAW,CAAE,GAAG,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,GAC9F,KAAO,GAAW,EAAO,KAAK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EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAG,EAAQ,EAAY,MAAM,MAAM,GAAG,MAAM,EAAO,EAC7F,MACF,CACA,EAAY,CAAE,GAAG,EAAW,QAAO,EACnC,EAAW,EACX,MAAM,EAAO,CACf,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
|
package/dist/infiniteSource.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { InfiniteSource, InfiniteSourceConfig } from './types';
|
|
2
|
-
/** Infinite sources
|
|
3
|
-
export declare function createInfiniteSource<T>(config: InfiniteSourceConfig<T>): InfiniteSource<T>;
|
|
2
|
+
/** Infinite sources retain committed items while replacement work is pending. */
|
|
3
|
+
export declare function createInfiniteSource<T, TParams = undefined>(config: InfiniteSourceConfig<T, TParams>): InfiniteSource<T, TParams>;
|
|
4
4
|
//# sourceMappingURL=infiniteSource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.d.ts","sourceRoot":"","sources":["../src/infiniteSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"infiniteSource.d.ts","sourceRoot":"","sources":["../src/infiniteSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAsB,cAAc,EAAE,oBAAoB,EAAuB,MAAM,SAAS,CAAC;AAW7G,iFAAiF;AACjF,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,EACzD,MAAM,EAAE,oBAAoB,CAAC,CAAC,EAAE,OAAO,CAAC,GACvC,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,CA6G5B"}
|
package/dist/infiniteSource.js
CHANGED
|
@@ -1,57 +1,61 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
2
|
import { createAsyncSource as t } from "./asyncSource.js";
|
|
3
|
-
import { positiveInteger as n,
|
|
3
|
+
import { positiveInteger as n, totalItems as r } from "./pagination.js";
|
|
4
4
|
//#region src/infiniteSource.ts
|
|
5
|
-
var
|
|
6
|
-
hasMore: e <
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}), o = (e, t = {}) => ({
|
|
11
|
-
pageSize: n(t.pageSize ?? e.pageSize, "pageSize"),
|
|
12
|
-
search: t.search ?? e.search
|
|
5
|
+
var i = (e, t, n) => ({
|
|
6
|
+
hasMore: e < n,
|
|
7
|
+
loadedItems: e,
|
|
8
|
+
pageSize: t,
|
|
9
|
+
totalItems: n
|
|
13
10
|
});
|
|
14
|
-
function
|
|
15
|
-
let
|
|
16
|
-
pageSize: 20,
|
|
17
|
-
|
|
18
|
-
},
|
|
19
|
-
data: [],
|
|
11
|
+
function a(a) {
|
|
12
|
+
let o = {
|
|
13
|
+
pageSize: n(a.pageSize ?? 20, "pageSize"),
|
|
14
|
+
params: a.params
|
|
15
|
+
}, s = 1, c = !1, l = t({
|
|
20
16
|
error: null,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
items: [],
|
|
18
|
+
loading: !1,
|
|
19
|
+
pagination: i(0, o.pageSize, 0),
|
|
20
|
+
params: o.params
|
|
21
|
+
}), u = () => {
|
|
22
|
+
if (l.disposed) throw new e();
|
|
23
|
+
}, d = (e, t, n) => {
|
|
24
|
+
let u = l.state.items;
|
|
26
25
|
return l.fetch({
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
commit: () => {
|
|
27
|
+
c = !0, s = t + 1, o = e;
|
|
28
|
+
},
|
|
29
|
+
failure: (e, t) => ({
|
|
30
|
+
...e,
|
|
31
|
+
error: t,
|
|
32
|
+
loading: !1,
|
|
33
|
+
pendingParams: void 0
|
|
34
|
+
}),
|
|
35
|
+
load: (n) => a.load({
|
|
36
|
+
page: t,
|
|
37
|
+
pageSize: e.pageSize,
|
|
38
|
+
params: e.params,
|
|
39
|
+
signal: n
|
|
34
40
|
}),
|
|
35
41
|
pending: (t) => ({
|
|
36
42
|
...t,
|
|
37
43
|
error: null,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
...r ? {} : { pendingQuery: e }
|
|
44
|
+
loading: !0,
|
|
45
|
+
...n || Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
41
46
|
}),
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return c = t + 1, {
|
|
46
|
-
data: l,
|
|
47
|
+
success: (t) => {
|
|
48
|
+
let a = n ? [...u, ...t.items] : [...t.items];
|
|
49
|
+
return {
|
|
47
50
|
error: null,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
items: a,
|
|
52
|
+
loading: !1,
|
|
53
|
+
pagination: i(a.length, e.pageSize, r(t.totalItems)),
|
|
54
|
+
params: e.params
|
|
51
55
|
};
|
|
52
56
|
}
|
|
53
57
|
});
|
|
54
|
-
},
|
|
58
|
+
}, f = () => d(o, 1, !1), p = {
|
|
55
59
|
get disposalSignal() {
|
|
56
60
|
return l.disposalSignal;
|
|
57
61
|
},
|
|
@@ -59,26 +63,50 @@ function s(n) {
|
|
|
59
63
|
get disposed() {
|
|
60
64
|
return l.disposed;
|
|
61
65
|
},
|
|
62
|
-
loadMore() {
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
async loadMore() {
|
|
67
|
+
if (u(), !l.state.loading) {
|
|
68
|
+
if (!c || !Object.is(o.params, l.state.params)) {
|
|
69
|
+
await f();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
l.state.pagination.hasMore && await d(o, s, !0);
|
|
73
|
+
}
|
|
65
74
|
},
|
|
66
|
-
reload:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
reload: f,
|
|
76
|
+
async setPageSize(e) {
|
|
77
|
+
u();
|
|
78
|
+
let t = n(e, "pageSize");
|
|
79
|
+
if (t === o.pageSize) {
|
|
80
|
+
if (l.state.loading || l.state.pagination.pageSize === t) return;
|
|
81
|
+
await f();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
o = {
|
|
85
|
+
...o,
|
|
86
|
+
pageSize: t
|
|
87
|
+
}, s = 1, await f();
|
|
88
|
+
},
|
|
89
|
+
async setParams(e) {
|
|
90
|
+
if (u(), Object.is(e, o.params)) {
|
|
91
|
+
!l.state.loading && !Object.is(e, l.state.params) && await f();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
o = {
|
|
95
|
+
...o,
|
|
96
|
+
params: e
|
|
97
|
+
}, s = 1, await f();
|
|
70
98
|
},
|
|
71
|
-
get
|
|
72
|
-
return l.
|
|
99
|
+
get state() {
|
|
100
|
+
return l.state;
|
|
73
101
|
},
|
|
74
102
|
subscribe: l.subscribe,
|
|
75
103
|
[Symbol.dispose]() {
|
|
76
|
-
|
|
104
|
+
p.dispose();
|
|
77
105
|
}
|
|
78
106
|
};
|
|
79
|
-
return
|
|
107
|
+
return p;
|
|
80
108
|
}
|
|
81
109
|
//#endregion
|
|
82
|
-
export {
|
|
110
|
+
export { a as createInfiniteSource };
|
|
83
111
|
|
|
84
112
|
//# sourceMappingURL=infiniteSource.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.js","names":[],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"infiniteSource.js","names":[],"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,KAAoB,GAAqB,GAAkB,OAAuC;CACtG,SAAS,IAAc;CACvB;CACA;CACA,YAAY;AACd;AAGA,SAAgB,EACd,GAC4B;CAC5B,IAAI,IAA8B;EAChC,UAAU,EAAgB,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQ,EAAO;CACjB,GACI,IAAW,GACX,IAAY,IACV,IAAc,EAAmD;EACrE,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,EAAiB,GAAG,EAAU,UAAU,CAAC;EACrD,QAAQ,EAAU;CACpB,CAAC,GACK,UAAyB;EAC7B,IAAI,EAAY,UAAU,MAAM,IAAI,EAAuB;CAC7D,GAEM,KAAS,GAA2B,GAAc,MAAmC;EACzF,IAAM,IAAc,EAAY,MAAM;EAEtC,OAAO,EAAY,MAAM;GACvB,cAAc;IAGZ,AAFA,IAAY,IACZ,IAAW,IAAO,GAClB,IAAY;GACd;GACA,UAAU,GAAU,OAAW;IAAE,GAAG;IAAU;IAAO,SAAS;IAAO,eAAe,KAAA;GAAU;GAC9F,OAAO,MAAW,EAAO,KAAK;IAAE;IAAM,UAAU,EAAQ;IAAU,QAAQ,EAAQ;IAAQ;GAAO,CAAC;GAClG,UAAU,OAAc;IACtB,GAAG;IACH,OAAO;IAC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;GACF;GAGA,AAFA,IAAY;IAAE,GAAG;IAAW;GAAO,GACnC,IAAW,GACX,MAAM,EAAO;EACf;EAEA,IAAI,QAAQ;GACV,OAAO,EAAY;EACrB;EAEA,WAAW,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAO;AACT"}
|
package/dist/localSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./sourceStore.cjs"),n=require("./pagination.cjs");function r(r,i={}){let a=r,o=1,s=n.positiveInteger(i.pageSize??20,`pageSize`),c=i.params,l=()=>{let e=i.filter?a.filter(e=>i.filter?.(e,c)):a,t=n.createPagePagination(o,s,e.length);if(o>t.pageCount)return o=t.pageCount,l();let r=(o-1)*s;return{error:null,items:e.slice(r,r+s),loading:!1,pagination:t,params:c}},u=t.createSourceStore(l()),d=()=>{if(u.disposed)throw new e.SourcererDisposedError},f=(e,t)=>Object.keys(e).every(n=>e[n]===t[n]),p=()=>{let e=l(),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&&f(e.pagination,t.pagination)||u.set(e)},m=e=>{d();let t=Math.min(n.positiveInteger(e,`page`),u.value.pagination.pageCount);t!==o&&(o=t,p())},h={get disposalSignal(){return u.disposalSignal},dispose:u.dispose,get disposed(){return u.disposed},first(){m(1)},goTo:m,last(){m(u.value.pagination.pageCount)},next(){u.value.pagination.hasNext?m(o+1):d()},previous(){u.value.pagination.hasPrevious?m(o-1):d()},setItems(e){d(),e!==a&&(a=e,p())},setPageSize(e){d();let t=n.positiveInteger(e,`pageSize`);t!==s&&(o=1,s=t,p())},setParams(e){d(),!Object.is(e,c)&&(o=1,c=e,p())},get state(){return u.value},subscribe:u.subscribe,[Symbol.dispose](){h.dispose()}};return h}exports.createLocalSource=r;
|
|
2
2
|
//# sourceMappingURL=localSource.cjs.map
|
package/dist/localSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.cjs","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { createPagePagination, positiveInteger
|
|
1
|
+
{"version":3,"file":"localSource.cjs","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAWA,SAAgB,EACd,EACA,EAAwC,CAAC,EAChB,CACzB,IAAI,EAAW,EACX,EAAO,EACP,EAAW,EAAA,gBAAgB,EAAO,UAAY,GAAI,UAAU,EAC5D,EAAS,EAAO,OAEd,MAAiD,CACrD,IAAM,EAAW,EAAO,OAAS,EAAS,OAAQ,GAAS,EAAO,SAAS,EAAM,CAAM,CAAC,EAAI,EACtF,EAAa,EAAA,qBAAqB,EAAM,EAAU,EAAS,MAAM,EACvE,GAAI,EAAO,EAAW,UAEpB,MADA,GAAO,EAAW,UACX,EAAW,EAEpB,IAAM,GAAS,EAAO,GAAK,EAE3B,MAAO,CACL,MAAO,KACP,MAAO,EAAS,MAAM,EAAO,EAAQ,CAAQ,EAC7C,QAAS,GACT,aACA,QACF,CACF,EAE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AAW,EACP,QAAO,GAAG,EAAY,CAAM,IAChC,EAAO,EACP,EAAS,EACT,EAAO,EACT,EAEA,IAAI,OAAQ,CACV,OAAO,EAAM,KACf,EAEA,UAAW,EAAM,UAEjB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
|
package/dist/localSource.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { LocalSource, LocalSourceConfig } from './types';
|
|
2
|
-
/** Local sources
|
|
3
|
-
export declare function createLocalSource<T>(
|
|
2
|
+
/** Local sources filter and paginate an in-memory collection synchronously. */
|
|
3
|
+
export declare function createLocalSource<T>(items: readonly T[]): LocalSource<T>;
|
|
4
|
+
export declare function createLocalSource<T, TParams = undefined>(items: readonly T[], config: LocalSourceConfig<T, TParams>): LocalSource<T, TParams>;
|
|
4
5
|
//# sourceMappingURL=localSource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.d.ts","sourceRoot":"","sources":["../src/localSource.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"localSource.d.ts","sourceRoot":"","sources":["../src/localSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAoC,MAAM,SAAS,CAAC;AAEhG,+EAA+E;AAC/E,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;AAC1E,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,EACtD,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,MAAM,EAAE,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,GACpC,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC"}
|
package/dist/localSource.js
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
|
+
import { createSourceStore as t } from "./sourceStore.js";
|
|
3
|
+
import { createPagePagination as n, positiveInteger as r } from "./pagination.js";
|
|
3
4
|
//#region src/localSource.ts
|
|
4
|
-
|
|
5
|
-
let
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
function o(n, o = {}) {
|
|
13
|
-
let s = n, c = a({
|
|
14
|
-
page: 1,
|
|
15
|
-
pageSize: 20,
|
|
16
|
-
search: ""
|
|
17
|
-
}, o.initialQuery), l = () => {
|
|
18
|
-
let e = c.search.length > 0 && o.match ? s.filter((e) => o.match?.(e, c.search)) : s, n = i(e.length), r = t(c.page, c.pageSize, n), a = (r.index - 1) * r.size;
|
|
19
|
-
return c = {
|
|
20
|
-
...c,
|
|
21
|
-
page: r.index
|
|
22
|
-
}, {
|
|
23
|
-
data: e.slice(a, a + r.size),
|
|
5
|
+
function i(i, a = {}) {
|
|
6
|
+
let o = i, s = 1, c = r(a.pageSize ?? 20, "pageSize"), l = a.params, u = () => {
|
|
7
|
+
let e = a.filter ? o.filter((e) => a.filter?.(e, l)) : o, t = n(s, c, e.length);
|
|
8
|
+
if (s > t.pageCount) return s = t.pageCount, u();
|
|
9
|
+
let r = (s - 1) * c;
|
|
10
|
+
return {
|
|
24
11
|
error: null,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
items: e.slice(r, r + c),
|
|
13
|
+
loading: !1,
|
|
14
|
+
pagination: t,
|
|
15
|
+
params: l
|
|
28
16
|
};
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
17
|
+
}, d = t(u()), f = () => {
|
|
18
|
+
if (d.disposed) throw new e();
|
|
19
|
+
}, p = (e, t) => Object.keys(e).every((n) => e[n] === t[n]), m = () => {
|
|
20
|
+
let e = u(), t = d.value, n = e.items.length === t.items.length && e.items.every((e, n) => Object.is(e, t.items[n]));
|
|
21
|
+
Object.is(e.params, t.params) && n && p(e.pagination, t.pagination) || d.set(e);
|
|
22
|
+
}, h = (e) => {
|
|
23
|
+
f();
|
|
24
|
+
let t = Math.min(r(e, "page"), d.value.pagination.pageCount);
|
|
25
|
+
t !== s && (s = t, m());
|
|
26
|
+
}, g = {
|
|
36
27
|
get disposalSignal() {
|
|
37
|
-
return
|
|
28
|
+
return d.disposalSignal;
|
|
38
29
|
},
|
|
39
|
-
dispose:
|
|
30
|
+
dispose: d.dispose,
|
|
40
31
|
get disposed() {
|
|
41
|
-
return
|
|
32
|
+
return d.disposed;
|
|
33
|
+
},
|
|
34
|
+
first() {
|
|
35
|
+
h(1);
|
|
36
|
+
},
|
|
37
|
+
goTo: h,
|
|
38
|
+
last() {
|
|
39
|
+
h(d.value.pagination.pageCount);
|
|
40
|
+
},
|
|
41
|
+
next() {
|
|
42
|
+
d.value.pagination.hasNext ? h(s + 1) : f();
|
|
43
|
+
},
|
|
44
|
+
previous() {
|
|
45
|
+
d.value.pagination.hasPrevious ? h(s - 1) : f();
|
|
46
|
+
},
|
|
47
|
+
setItems(e) {
|
|
48
|
+
f(), e !== o && (o = e, m());
|
|
42
49
|
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
last() {
|
|
48
|
-
f({ page: u.value.pagination.count });
|
|
49
|
-
},
|
|
50
|
-
next() {
|
|
51
|
-
u.value.pagination.hasNext && f({ page: u.value.pagination.index + 1 });
|
|
52
|
-
},
|
|
53
|
-
previous() {
|
|
54
|
-
u.value.pagination.hasPrevious && f({ page: u.value.pagination.index - 1 });
|
|
55
|
-
}
|
|
50
|
+
setPageSize(e) {
|
|
51
|
+
f();
|
|
52
|
+
let t = r(e, "pageSize");
|
|
53
|
+
t !== c && (s = 1, c = t, m());
|
|
56
54
|
},
|
|
57
|
-
|
|
58
|
-
e
|
|
55
|
+
setParams(e) {
|
|
56
|
+
f(), !Object.is(e, l) && (s = 1, l = e, m());
|
|
59
57
|
},
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return u.value;
|
|
58
|
+
get state() {
|
|
59
|
+
return d.value;
|
|
63
60
|
},
|
|
64
|
-
subscribe:
|
|
61
|
+
subscribe: d.subscribe,
|
|
65
62
|
[Symbol.dispose]() {
|
|
66
|
-
|
|
63
|
+
g.dispose();
|
|
67
64
|
}
|
|
68
65
|
};
|
|
69
|
-
return
|
|
66
|
+
return g;
|
|
70
67
|
}
|
|
71
68
|
//#endregion
|
|
72
|
-
export {
|
|
69
|
+
export { i as createLocalSource };
|
|
73
70
|
|
|
74
71
|
//# sourceMappingURL=localSource.js.map
|
package/dist/localSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.js","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { createPagePagination, positiveInteger
|
|
1
|
+
{"version":3,"file":"localSource.js","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":";;;;AAWA,SAAgB,EACd,GACA,IAAwC,CAAC,GAChB;C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