@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.
Files changed (66) hide show
  1. package/README.md +17 -18
  2. package/dist/asyncSource.cjs +1 -1
  3. package/dist/asyncSource.cjs.map +1 -1
  4. package/dist/asyncSource.d.ts +11 -12
  5. package/dist/asyncSource.d.ts.map +1 -1
  6. package/dist/asyncSource.js +45 -32
  7. package/dist/asyncSource.js.map +1 -1
  8. package/dist/cursorSource.cjs +1 -1
  9. package/dist/cursorSource.cjs.map +1 -1
  10. package/dist/cursorSource.d.ts +2 -2
  11. package/dist/cursorSource.d.ts.map +1 -1
  12. package/dist/cursorSource.js +94 -61
  13. package/dist/cursorSource.js.map +1 -1
  14. package/dist/errors.cjs +1 -1
  15. package/dist/errors.cjs.map +1 -1
  16. package/dist/errors.d.ts +6 -0
  17. package/dist/errors.d.ts.map +1 -1
  18. package/dist/errors.js +11 -3
  19. package/dist/errors.js.map +1 -1
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.ts +2 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +6 -6
  24. package/dist/infiniteSource.cjs +1 -1
  25. package/dist/infiniteSource.cjs.map +1 -1
  26. package/dist/infiniteSource.d.ts +2 -2
  27. package/dist/infiniteSource.d.ts.map +1 -1
  28. package/dist/infiniteSource.js +80 -52
  29. package/dist/infiniteSource.js.map +1 -1
  30. package/dist/localSource.cjs +1 -1
  31. package/dist/localSource.cjs.map +1 -1
  32. package/dist/localSource.d.ts +3 -2
  33. package/dist/localSource.d.ts.map +1 -1
  34. package/dist/localSource.js +54 -57
  35. package/dist/localSource.js.map +1 -1
  36. package/dist/pageSource.cjs +1 -1
  37. package/dist/pageSource.cjs.map +1 -1
  38. package/dist/pageSource.d.ts +2 -2
  39. package/dist/pageSource.d.ts.map +1 -1
  40. package/dist/pageSource.js +92 -60
  41. package/dist/pageSource.js.map +1 -1
  42. package/dist/pagination.cjs +1 -1
  43. package/dist/pagination.cjs.map +1 -1
  44. package/dist/pagination.d.ts +1 -2
  45. package/dist/pagination.d.ts.map +1 -1
  46. package/dist/pagination.js +9 -13
  47. package/dist/pagination.js.map +1 -1
  48. package/dist/sourceStore.cjs.map +1 -1
  49. package/dist/sourceStore.d.ts +5 -2
  50. package/dist/sourceStore.d.ts.map +1 -1
  51. package/dist/sourceStore.js.map +1 -1
  52. package/dist/sourcerer.cjs +1 -1
  53. package/dist/sourcerer.cjs.map +1 -1
  54. package/dist/sourcerer.iife.js +1 -1
  55. package/dist/sourcerer.iife.js.map +1 -1
  56. package/dist/sourcerer.js +1 -1
  57. package/dist/sourcerer.js.map +1 -1
  58. package/dist/types.d.ts +130 -117
  59. package/dist/types.d.ts.map +1 -1
  60. package/package.json +11 -7
  61. package/dist/_dev.cjs +0 -2
  62. package/dist/_dev.cjs.map +0 -1
  63. package/dist/_dev.d.ts +0 -2
  64. package/dist/_dev.d.ts.map +0 -1
  65. package/dist/_dev.js +0 -6
  66. 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.name, Object.setPrototypeOf(this, new.target.prototype);
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
@@ -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.name;\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"],"mappings":";AACA,IAAa,IAAb,cAAoC,MAAM;CACxC,YAAY,GAAiB,GAAqB;EAGhD,AAFA,MAAM,GAAS,CAAI,GACnB,KAAK,OAAO,WAAW,MACvB,OAAO,eAAe,MAAM,WAAW,SAAS;CAClD;AACF,GAGa,IAAb,cAAiD,EAAe,CAAC"}
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 { AnyPagination, CursorPagination, CursorQuery, CursorQueryPatch, CursorResult, CursorSource, CursorSourceConfig, InfiniteLoadQuery, InfinitePagination, InfiniteQuery, InfiniteQueryPatch, InfiniteSource, InfiniteSourceConfig, LoadContext, LocalQuery, LocalQueryPatch, LocalSource, LocalSourceConfig, PagePagination, PageQuery, PageQueryPatch, PageResult, PageSource, PageSourceConfig, Source, SourceSnapshot, } from './types';
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
@@ -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;AACvE,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,aAAa,EACb,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACpB,WAAW,EACX,UAAU,EACV,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,SAAS,EACT,cAAc,EACd,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,MAAM,EACN,cAAc,GACf,MAAM,SAAS,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, SourcererError as t } from "./errors.js";
2
- import { createCursorSource as n } from "./cursorSource.js";
3
- import { createInfiniteSource as r } from "./infiniteSource.js";
4
- import { createLocalSource as i } from "./localSource.js";
5
- import { createPageSource as a } from "./pageSource.js";
6
- export { e as SourcererConfigurationError, t as SourcererError, n as createCursorSource, r as createInfiniteSource, i as createLocalSource, a as createPageSource };
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 };
@@ -1,2 +1,2 @@
1
- const e=require("./_dev.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");var r=(e,t)=>({hasMore:e<t,kind:`infinite`,loaded:e,total:t}),i=(e,t={})=>({pageSize:n.positiveInteger(t.pageSize??e.pageSize,`pageSize`),search:t.search??e.search});function a(a){let o=i({pageSize:20,search:``},a.initialQuery),s=1,c=t.createAsyncSource({data:[],error:null,isFetching:!1,pagination:r(0,0),query:o}),l=(e,t,i)=>{let o=c.snapshot.data;return c.fetch({load:n=>a.load({query:{page:t,pageSize:e.pageSize,search:e.search},signal:n}),pending:t=>({...t,error:null,isFetching:!0,pagination:r(t.data.length,t.pagination.total),...i?{}:{pendingQuery:e}}),query:e,success:a=>{let c=n.totalItems(a.total),l=i?[...o,...a.data]:a.data;return s=t+1,{data:l,error:null,isFetching:!1,pagination:r(l.length,c),query:e}}})},u=()=>(s=1,l(o,1,!1)),d={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},loadMore(){let{isFetching:e,pagination:t}=c.snapshot;return e||c.snapshot.data.length>0&&!t.hasMore?Promise.resolve():l(o,s,!0)},reload:u,setQuery:async e=>{let t=i(c.snapshot.pendingQuery??c.snapshot.query,e);n.sameQuery(t,o)||(o=t,s=1,await l(t,1,!1))},get snapshot(){return c.snapshot},subscribe:c.subscribe,[Symbol.dispose](){d.dispose()}};return a.autoStart!==!1&&u().catch(()=>e.warn(`Initial load failed. Inspect source.snapshot.error.`)),d}exports.createInfiniteSource=a;
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 { warn } from './_dev';\nimport { createAsyncSource } from './asyncSource';\nimport { positiveInteger, sameQuery, totalItems } from './pagination';\nimport type {\n InfinitePagination,\n InfiniteQuery,\n InfiniteQueryPatch,\n InfiniteSource,\n InfiniteSourceConfig,\n} from './types';\n\nconst createPagination = (loaded: number, total: number): InfinitePagination => ({\n hasMore: loaded < total,\n kind: 'infinite',\n loaded,\n total,\n});\n\nconst normalizeQuery = (current: InfiniteQuery, patch: InfiniteQueryPatch = {}): InfiniteQuery => ({\n pageSize: positiveInteger(patch.pageSize ?? current.pageSize, 'pageSize'),\n search: patch.search ?? current.search,\n});\n\n/** Infinite sources preserve loaded collection state while a pending query replaces it. */\nexport function createInfiniteSource<T>(config: InfiniteSourceConfig<T>): InfiniteSource<T> {\n const initialQuery: InfiniteQuery = { pageSize: 20, search: '' };\n let requestedQuery = normalizeQuery(initialQuery, config.initialQuery);\n let nextPage = 1;\n const asyncSource = createAsyncSource<T, InfiniteQuery, InfinitePagination>({\n data: [],\n error: null,\n isFetching: false,\n pagination: createPagination(0, 0),\n query: requestedQuery,\n });\n\n const fetch = (query: InfiniteQuery, page: number, append: boolean): Promise<void> => {\n const loadedData = asyncSource.snapshot.data;\n\n return asyncSource.fetch({\n load: (signal) => config.load({ query: { page, pageSize: query.pageSize, search: query.search }, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n isFetching: true,\n pagination: createPagination(previous.data.length, previous.pagination.total),\n ...(append ? {} : { pendingQuery: query }),\n }),\n query,\n success: (result) => {\n const total = totalItems(result.total);\n const data = append ? [...loadedData, ...result.data] : result.data;\n\n nextPage = page + 1;\n\n return {\n data,\n error: null,\n isFetching: false,\n pagination: createPagination(data.length, total),\n query,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => {\n nextPage = 1;\n\n return fetch(requestedQuery, 1, false);\n };\n\n const setQuery = async (patch: InfiniteQueryPatch): Promise<void> => {\n const current = asyncSource.snapshot.pendingQuery ?? asyncSource.snapshot.query;\n const next = normalizeQuery(current, patch);\n\n if (sameQuery(next, requestedQuery)) return;\n\n requestedQuery = next;\n nextPage = 1;\n await fetch(next, 1, false);\n };\n\n const source: InfiniteSource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n loadMore() {\n const { isFetching, pagination } = asyncSource.snapshot;\n\n return isFetching || (asyncSource.snapshot.data.length > 0 && !pagination.hasMore)\n ? Promise.resolve()\n : fetch(requestedQuery, nextPage, true);\n },\n\n reload,\n setQuery,\n\n get snapshot() {\n return asyncSource.snapshot;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (config.autoStart !== false)\n void reload().catch(() => warn('Initial load failed. Inspect source.snapshot.error.'));\n\n return source;\n}\n"],"mappings":"2FAWA,IAAM,GAAoB,EAAgB,KAAuC,CAC/E,QAAS,EAAS,EAClB,KAAM,WACN,SACA,OACF,GAEM,GAAkB,EAAwB,EAA4B,CAAC,KAAsB,CACjG,SAAU,EAAA,gBAAgB,EAAM,UAAY,EAAQ,SAAU,UAAU,EACxE,OAAQ,EAAM,QAAU,EAAQ,MAClC,GAGA,SAAgB,EAAwB,EAAoD,CAE1F,IAAI,EAAiB,EAAe,CADE,SAAU,GAAI,OAAQ,EACxB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c,IAElC,EAAiB,EACjB,EAAW,EACX,MAAM,EAAM,EAAM,EAAG,EAAK,EAC5B,EAwBE,IAAI,UAAW,CACb,OAAO,EAAY,QACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAKA,OAHI,EAAO,YAAc,IACvB,EAAY,CAAC,CAAC,UAAY,EAAA,KAAK,qDAAqD,CAAC,EAEhF,CACT"}
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,MAA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MAAM,WAAW,SAAS,MAAM,EAAM,EAAW,EAAU,EAAI,CAD/E,CAEF,EAEA,SAEA,MAAM,YAAY,EAAU,CAC1B,EAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAU,UAAU,EACvD,GAAI,IAAe,EAAU,SAAU,CACrC,GAAI,EAAY,MAAM,SAAW,EAAY,MAAM,WAAW,WAAa,EAAY,OACvF,MAAM,EAAO,EACb,MACF,CACA,EAAY,CAAE,GAAG,EAAW,SAAU,CAAW,EACjD,EAAW,EACX,MAAM,EAAO,CACf,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,EAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,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"}
@@ -1,4 +1,4 @@
1
1
  import type { InfiniteSource, InfiniteSourceConfig } from './types';
2
- /** Infinite sources preserve loaded collection state while a pending query replaces it. */
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,EAIV,cAAc,EACd,oBAAoB,EACrB,MAAM,SAAS,CAAC;AAcjB,2FAA2F;AAC3F,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAgG1F"}
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"}
@@ -1,57 +1,61 @@
1
- import { warn as e } from "./_dev.js";
1
+ import { SourcererDisposedError as e } from "./errors.js";
2
2
  import { createAsyncSource as t } from "./asyncSource.js";
3
- import { positiveInteger as n, sameQuery as r, totalItems as i } from "./pagination.js";
3
+ import { positiveInteger as n, totalItems as r } from "./pagination.js";
4
4
  //#region src/infiniteSource.ts
5
- var a = (e, t) => ({
6
- hasMore: e < t,
7
- kind: "infinite",
8
- loaded: e,
9
- total: t
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 s(n) {
15
- let s = o({
16
- pageSize: 20,
17
- search: ""
18
- }, n.initialQuery), c = 1, l = t({
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
- isFetching: !1,
22
- pagination: a(0, 0),
23
- query: s
24
- }), u = (e, t, r) => {
25
- let o = l.snapshot.data;
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
- load: (r) => n.load({
28
- query: {
29
- page: t,
30
- pageSize: e.pageSize,
31
- search: e.search
32
- },
33
- signal: r
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
- isFetching: !0,
39
- pagination: a(t.data.length, t.pagination.total),
40
- ...r ? {} : { pendingQuery: e }
44
+ loading: !0,
45
+ ...n || Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
41
46
  }),
42
- query: e,
43
- success: (n) => {
44
- let s = i(n.total), l = r ? [...o, ...n.data] : n.data;
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
- isFetching: !1,
49
- pagination: a(l.length, s),
50
- query: e
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
- }, d = () => (c = 1, u(s, 1, !1)), f = {
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
- let { isFetching: e, pagination: t } = l.snapshot;
64
- return e || l.snapshot.data.length > 0 && !t.hasMore ? Promise.resolve() : u(s, c, !0);
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: d,
67
- setQuery: async (e) => {
68
- let t = o(l.snapshot.pendingQuery ?? l.snapshot.query, e);
69
- r(t, s) || (s = t, c = 1, await u(t, 1, !1));
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 snapshot() {
72
- return l.snapshot;
99
+ get state() {
100
+ return l.state;
73
101
  },
74
102
  subscribe: l.subscribe,
75
103
  [Symbol.dispose]() {
76
- f.dispose();
104
+ p.dispose();
77
105
  }
78
106
  };
79
- return n.autoStart !== !1 && d().catch(() => /* @__PURE__ */ e("Initial load failed. Inspect source.snapshot.error.")), f;
107
+ return p;
80
108
  }
81
109
  //#endregion
82
- export { s as createInfiniteSource };
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 { warn } from './_dev';\nimport { createAsyncSource } from './asyncSource';\nimport { positiveInteger, sameQuery, totalItems } from './pagination';\nimport type {\n InfinitePagination,\n InfiniteQuery,\n InfiniteQueryPatch,\n InfiniteSource,\n InfiniteSourceConfig,\n} from './types';\n\nconst createPagination = (loaded: number, total: number): InfinitePagination => ({\n hasMore: loaded < total,\n kind: 'infinite',\n loaded,\n total,\n});\n\nconst normalizeQuery = (current: InfiniteQuery, patch: InfiniteQueryPatch = {}): InfiniteQuery => ({\n pageSize: positiveInteger(patch.pageSize ?? current.pageSize, 'pageSize'),\n search: patch.search ?? current.search,\n});\n\n/** Infinite sources preserve loaded collection state while a pending query replaces it. */\nexport function createInfiniteSource<T>(config: InfiniteSourceConfig<T>): InfiniteSource<T> {\n const initialQuery: InfiniteQuery = { pageSize: 20, search: '' };\n let requestedQuery = normalizeQuery(initialQuery, config.initialQuery);\n let nextPage = 1;\n const asyncSource = createAsyncSource<T, InfiniteQuery, InfinitePagination>({\n data: [],\n error: null,\n isFetching: false,\n pagination: createPagination(0, 0),\n query: requestedQuery,\n });\n\n const fetch = (query: InfiniteQuery, page: number, append: boolean): Promise<void> => {\n const loadedData = asyncSource.snapshot.data;\n\n return asyncSource.fetch({\n load: (signal) => config.load({ query: { page, pageSize: query.pageSize, search: query.search }, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n isFetching: true,\n pagination: createPagination(previous.data.length, previous.pagination.total),\n ...(append ? {} : { pendingQuery: query }),\n }),\n query,\n success: (result) => {\n const total = totalItems(result.total);\n const data = append ? [...loadedData, ...result.data] : result.data;\n\n nextPage = page + 1;\n\n return {\n data,\n error: null,\n isFetching: false,\n pagination: createPagination(data.length, total),\n query,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => {\n nextPage = 1;\n\n return fetch(requestedQuery, 1, false);\n };\n\n const setQuery = async (patch: InfiniteQueryPatch): Promise<void> => {\n const current = asyncSource.snapshot.pendingQuery ?? asyncSource.snapshot.query;\n const next = normalizeQuery(current, patch);\n\n if (sameQuery(next, requestedQuery)) return;\n\n requestedQuery = next;\n nextPage = 1;\n await fetch(next, 1, false);\n };\n\n const source: InfiniteSource<T> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n loadMore() {\n const { isFetching, pagination } = asyncSource.snapshot;\n\n return isFetching || (asyncSource.snapshot.data.length > 0 && !pagination.hasMore)\n ? Promise.resolve()\n : fetch(requestedQuery, nextPage, true);\n },\n\n reload,\n setQuery,\n\n get snapshot() {\n return asyncSource.snapshot;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n if (config.autoStart !== false)\n void reload().catch(() => warn('Initial load failed. Inspect source.snapshot.error.'));\n\n return source;\n}\n"],"mappings":";;;;AAWA,IAAM,KAAoB,GAAgB,OAAuC;CAC/E,SAAS,IAAS;CAClB,MAAM;CACN;CACA;AACF,IAEM,KAAkB,GAAwB,IAA4B,CAAC,OAAsB;CACjG,UAAU,EAAgB,EAAM,YAAY,EAAQ,UAAU,UAAU;CACxE,QAAQ,EAAM,UAAU,EAAQ;AAClC;AAGA,SAAgB,EAAwB,GAAoD;CAE1F,IAAI,IAAiB,EAAe;EADE,UAAU;EAAI,QAAQ;CACxB,GAAc,EAAO,YAAY,GACjE,IAAW,GACT,IAAc,EAAwD;EAC1E,MAAM,CAAC;EACP,OAAO;EACP,YAAY;EACZ,YAAY,EAAiB,GAAG,CAAC;EACjC,OAAO;CACT,CAAC,GAEK,KAAS,GAAsB,GAAc,MAAmC;EACpF,IAAM,IAAa,EAAY,SAAS;EAExC,OAAO,EAAY,MAAM;GACvB,OAAO,MAAW,EAAO,KAAK;IAAE,OAAO;KAAE;KAAM,UAAU,EAAM;KAAU,QAAQ,EAAM;IAAO;IAAG;GAAO,CAAC;GACzG,UAAU,OAAc;IACtB,GAAG;IACH,OAAO;IACP,YAAY;IACZ,YAAY,EAAiB,EAAS,KAAK,QAAQ,EAAS,WAAW,KAAK;IAC5E,GAAI,IAAS,CAAC,IAAI,EAAE,cAAc,EAAM;GAC1C;GACA;GACA,UAAU,MAAW;IACnB,IAAM,IAAQ,EAAW,EAAO,KAAK,GAC/B,IAAO,IAAS,CAAC,GAAG,GAAY,GAAG,EAAO,IAAI,IAAI,EAAO;IAI/D,OAFA,IAAW,IAAO,GAEX;KACL;KACA,OAAO;KACP,YAAY;KACZ,YAAY,EAAiB,EAAK,QAAQ,CAAK;KAC/C;IACF;GACF;EACF,CAAC;CACH,GAEM,WACJ,IAAW,GAEJ,EAAM,GAAgB,GAAG,EAAK,IAcjC,IAA4B;EAChC,IAAI,iBAAiB;GACnB,OAAO,EAAY;EACrB;EAEA,SAAS,EAAY;EAErB,IAAI,WAAW;GACb,OAAO,EAAY;EACrB;EAEA,WAAW;GACT,IAAM,EAAE,eAAY,kBAAe,EAAY;GAE/C,OAAO,KAAe,EAAY,SAAS,KAAK,SAAS,KAAK,CAAC,EAAW,UACtE,QAAQ,QAAQ,IAChB,EAAM,GAAgB,GAAU,EAAI;EAC1C;EAEA;EACA,iBA/BsB,MAA6C;GAEnE,IAAM,IAAO,EADG,EAAY,SAAS,gBAAgB,EAAY,SAAS,OACrC,CAAK;GAEtC,EAAU,GAAM,CAAc,MAElC,IAAiB,GACjB,IAAW,GACX,MAAM,EAAM,GAAM,GAAG,EAAK;EAC5B;EAwBE,IAAI,WAAW;GACb,OAAO,EAAY;EACrB;EAEA,WAAW,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAKA,OAHI,EAAO,cAAc,MACvB,EAAY,CAAC,CAAC,YAAY,kBAAK,qDAAqD,CAAC,GAEhF;AACT"}
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;IACP,SAAS;IACT,GAAI,KAAU,OAAO,GAAG,EAAQ,QAAQ,EAAS,MAAM,IAAI,CAAC,IAAI,EAAE,eAAe,EAAQ,OAAO;GAClG;GACA,UAAU,MAAW;IACnB,IAAM,IAAQ,IAAS,CAAC,GAAG,GAAa,GAAG,EAAO,KAAK,IAAI,CAAC,GAAG,EAAO,K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@@ -1,2 +1,2 @@
1
- const e=require("./sourceStore.cjs"),t=require("./pagination.cjs");var n=(e,n={},r)=>{let i=n.page??e.page,a=n.pageSize??e.pageSize,o=n.search??e.search,s=n.pageSize!==void 0||n.search!==void 0,c=n.page??(s?1:i);return{page:r===void 0?t.positiveInteger(c,`page`):Math.min(t.positiveInteger(c,`page`),r),pageSize:t.positiveInteger(a,`pageSize`),search:o}};function r(r,i={}){let a=r,o=n({page:1,pageSize:20,search:``},i.initialQuery),s=()=>{let e=o.search.length>0&&i.match?a.filter(e=>i.match?.(e,o.search)):a,n=t.totalItems(e.length),r=t.createPagePagination(o.page,o.pageSize,n),s=(r.index-1)*r.size;return o={...o,page:r.index},{data:e.slice(s,s+r.size),error:null,isFetching:!1,pagination:r,query:o}},c=e.createSourceStore(s()),l=()=>{let e=s();return t.sameQuery(e.query,c.value.query)&&e.data===c.value.data&&e.pagination===c.value.pagination?!1:(c.set(e),!0)},u=e=>{let r=n(o,e,c.value.pagination.count);t.sameQuery(r,o)||(o=r,l())},d={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},page:{go(e){u({page:e})},last(){u({page:c.value.pagination.count})},next(){c.value.pagination.hasNext&&u({page:c.value.pagination.index+1})},previous(){c.value.pagination.hasPrevious&&u({page:c.value.pagination.index-1})}},setData(e){e!==a&&(a=e,l())},setQuery:u,get snapshot(){return c.value},subscribe:c.subscribe,[Symbol.dispose](){d.dispose()}};return d}exports.createLocalSource=r;
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
@@ -1 +1 @@
1
- {"version":3,"file":"localSource.cjs","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { createPagePagination, positiveInteger, sameQuery, totalItems } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type {\n LocalQuery,\n LocalQueryPatch,\n LocalSource,\n LocalSourceConfig,\n PagePagination,\n SourceSnapshot,\n} from './types';\n\nconst normalizeQuery = (current: LocalQuery, patch: LocalQueryPatch = {}, maxPage?: number): LocalQuery => {\n const page = patch.page ?? current.page;\n const pageSize = patch.pageSize ?? current.pageSize;\n const search = patch.search ?? current.search;\n const resetsPage = patch.pageSize !== undefined || patch.search !== undefined;\n\n const requestedPage = patch.page ?? (resetsPage ? 1 : page);\n\n return {\n page:\n maxPage === undefined\n ? positiveInteger(requestedPage, 'page')\n : Math.min(positiveInteger(requestedPage, 'page'), maxPage),\n pageSize: positiveInteger(pageSize, 'pageSize'),\n search,\n };\n};\n\n/** Local sources own only search and pagination; callers prepare filtering and ranking. */\nexport function createLocalSource<T>(data: readonly T[], config: LocalSourceConfig<T> = {}): LocalSource<T> {\n let allData = data;\n let query = normalizeQuery({ page: 1, pageSize: 20, search: '' }, config.initialQuery);\n\n const buildSnapshot = (): SourceSnapshot<T, LocalQuery, PagePagination> => {\n const matched =\n query.search.length > 0 && config.match ? allData.filter((item) => config.match?.(item, query.search)) : allData;\n const total = totalItems(matched.length);\n const pagination = createPagePagination(query.page, query.pageSize, total);\n const start = (pagination.index - 1) * pagination.size;\n\n query = { ...query, page: pagination.index };\n\n return {\n data: matched.slice(start, start + pagination.size),\n error: null,\n isFetching: false,\n pagination,\n query,\n };\n };\n\n const store = createSourceStore(buildSnapshot());\n\n const commit = (): boolean => {\n const next = buildSnapshot();\n\n if (\n sameQuery(next.query, store.value.query) &&\n next.data === store.value.data &&\n next.pagination === store.value.pagination\n ) {\n return false;\n }\n\n store.set(next);\n\n return true;\n };\n\n const setQuery = (patch: LocalQueryPatch): void => {\n const next = normalizeQuery(query, patch, store.value.pagination.count);\n\n if (sameQuery(next, query)) return;\n\n query = next;\n\n commit();\n };\n\n const source: LocalSource<T> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n page: {\n go(index) {\n setQuery({ page: index });\n },\n\n last() {\n setQuery({ page: store.value.pagination.count });\n },\n\n next() {\n if (store.value.pagination.hasNext) setQuery({ page: store.value.pagination.index + 1 });\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) setQuery({ page: store.value.pagination.index - 1 });\n },\n },\n\n setData(nextData) {\n if (nextData === allData) return;\n\n allData = nextData;\n\n commit();\n },\n\n setQuery,\n\n get snapshot() {\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":"mEAWA,IAAM,GAAkB,EAAqB,EAAyB,CAAC,EAAG,IAAiC,CACzG,IAAM,EAAO,EAAM,MAAQ,EAAQ,KAC7B,EAAW,E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AAK,IAEzB,EAAQ,EAER,EAAO,EACT,EAEM,EAAyB,CAC7B,IAAI,gBAAiB,CACnB,OAAO,EAAM,cACf,EAEA,QAAS,EAAM,QAEf,IAAI,UAAW,CACb,OAAO,EAAM,QACf,EAEA,KAAM,CACJ,GAAG,EAAO,CACR,EAAS,CAAE,KAAM,CAAM,CAAC,CAC1B,EAEA,MAAO,CACL,EAAS,CAAE,KAAM,EAAM,MAAM,WAAW,KAAM,CAAC,CACjD,EAEA,MAAO,CACD,EAAM,MAAM,WAAW,SAAS,EAAS,CAAE,KAAM,EAAM,MAAM,WAAW,MAAQ,CAAE,CAAC,CACzF,EAEA,UAAW,CACL,EAAM,MAAM,WAAW,aAAa,EAAS,CAAE,KAAM,EAAM,MAAM,WAAW,MAAQ,CAAE,CAAC,CAC7F,CACF,EAEA,QAAQ,EAAU,CACZ,IAAa,IAEjB,EAAU,EAEV,EAAO,EACT,EAEA,WAEA,IAAI,UAAW,CACb,OAAO,EAAM,KACf,EAEA,UAAW,EAAM,UAEjB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
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;\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tC,IAAI,gBAAiB,CACnB,OAAO,EAAM,cACf,EAEA,QAAS,EAAM,QAEf,IAAI,UAAW,CACb,OAAO,EAAM,QACf,EAEA,OAAQ,CACN,EAAK,CAAC,CACR,EAEA,OAEA,MAAO,CACL,EAAK,EAAM,MAAM,WAAW,SAAS,CACvC,EAEA,MAAO,CACD,EAAM,MAAM,WAAW,QAAS,EAAK,EAAO,CAAC,EAC5C,EAAW,CAClB,EAEA,UAAW,CACL,EAAM,MAAM,WAAW,YAAa,EAAK,EAAO,CAAC,EAChD,EAAW,CAClB,EAEA,SAAS,EAAW,CAClB,EAAW,EACP,IAAc,IAClB,EAAW,EACX,EAAO,EACT,EAEA,YAAY,EAAc,CACxB,EAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAc,UAAU,EACvD,IAAe,IACnB,EAAO,EACP,EAAW,EACX,EAAO,EACT,EAEA,UAAU,EAAY,CACpB,EAAW,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"}
@@ -1,4 +1,5 @@
1
1
  import type { LocalSource, LocalSourceConfig } from './types';
2
- /** Local sources own only search and pagination; callers prepare filtering and ranking. */
3
- export declare function createLocalSource<T>(data: readonly T[], config?: LocalSourceConfig<T>): LocalSource<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":"AAEA,OAAO,KAAK,EAGV,WAAW,EACX,iBAAiB,EAGlB,MAAM,SAAS,CAAC;AAoBjB,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,MAAM,GAAE,iBAAiB,CAAC,CAAC,CAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAqG1G"}
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"}
@@ -1,74 +1,71 @@
1
- import { createSourceStore as e } from "./sourceStore.js";
2
- import { createPagePagination as t, positiveInteger as n, sameQuery as r, totalItems as i } from "./pagination.js";
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
- var a = (e, t = {}, r) => {
5
- let i = t.page ?? e.page, a = t.pageSize ?? e.pageSize, o = t.search ?? e.search, s = t.pageSize !== void 0 || t.search !== void 0, c = t.page ?? (s ? 1 : i);
6
- return {
7
- page: r === void 0 ? n(c, "page") : Math.min(n(c, "page"), r),
8
- pageSize: n(a, "pageSize"),
9
- search: o
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
- isFetching: !1,
26
- pagination: r,
27
- query: c
12
+ items: e.slice(r, r + c),
13
+ loading: !1,
14
+ pagination: t,
15
+ params: l
28
16
  };
29
- }, u = e(l()), d = () => {
30
- let e = l();
31
- return r(e.query, u.value.query) && e.data === u.value.data && e.pagination === u.value.pagination ? !1 : (u.set(e), !0);
32
- }, f = (e) => {
33
- let t = a(c, e, u.value.pagination.count);
34
- r(t, c) || (c = t, d());
35
- }, p = {
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 u.disposalSignal;
28
+ return d.disposalSignal;
38
29
  },
39
- dispose: u.dispose,
30
+ dispose: d.dispose,
40
31
  get disposed() {
41
- return u.disposed;
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
- page: {
44
- go(e) {
45
- f({ page: e });
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
- setData(e) {
58
- e !== s && (s = e, d());
55
+ setParams(e) {
56
+ f(), !Object.is(e, l) && (s = 1, l = e, m());
59
57
  },
60
- setQuery: f,
61
- get snapshot() {
62
- return u.value;
58
+ get state() {
59
+ return d.value;
63
60
  },
64
- subscribe: u.subscribe,
61
+ subscribe: d.subscribe,
65
62
  [Symbol.dispose]() {
66
- p.dispose();
63
+ g.dispose();
67
64
  }
68
65
  };
69
- return p;
66
+ return g;
70
67
  }
71
68
  //#endregion
72
- export { o as createLocalSource };
69
+ export { i as createLocalSource };
73
70
 
74
71
  //# sourceMappingURL=localSource.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"localSource.js","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { createPagePagination, positiveInteger, sameQuery, totalItems } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type {\n LocalQuery,\n LocalQueryPatch,\n LocalSource,\n LocalSourceConfig,\n PagePagination,\n SourceSnapshot,\n} from './types';\n\nconst normalizeQuery = (current: LocalQuery, patch: LocalQueryPatch = {}, maxPage?: number): LocalQuery => {\n const page = patch.page ?? current.page;\n const pageSize = patch.pageSize ?? current.pageSize;\n const search = patch.search ?? current.search;\n const resetsPage = patch.pageSize !== undefined || patch.search !== undefined;\n\n const requestedPage = patch.page ?? (resetsPage ? 1 : page);\n\n return {\n page:\n maxPage === undefined\n ? positiveInteger(requestedPage, 'page')\n : Math.min(positiveInteger(requestedPage, 'page'), maxPage),\n pageSize: positiveInteger(pageSize, 'pageSize'),\n search,\n };\n};\n\n/** Local sources own only search and pagination; callers prepare filtering and ranking. */\nexport function createLocalSource<T>(data: readonly T[], config: LocalSourceConfig<T> = {}): LocalSource<T> {\n let allData = data;\n let query = normalizeQuery({ page: 1, pageSize: 20, search: '' }, config.initialQuery);\n\n const buildSnapshot = (): SourceSnapshot<T, LocalQuery, PagePagination> => {\n const matched =\n query.search.length > 0 && config.match ? allData.filter((item) => config.match?.(item, query.search)) : allData;\n const total = totalItems(matched.length);\n const pagination = createPagePagination(query.page, query.pageSize, total);\n const start = (pagination.index - 1) * pagination.size;\n\n query = { ...query, page: pagination.index };\n\n return {\n data: matched.slice(start, start + pagination.size),\n error: null,\n isFetching: false,\n pagination,\n query,\n };\n };\n\n const store = createSourceStore(buildSnapshot());\n\n const commit = (): boolean => {\n const next = buildSnapshot();\n\n if (\n sameQuery(next.query, store.value.query) &&\n next.data === store.value.data &&\n next.pagination === store.value.pagination\n ) {\n return false;\n }\n\n store.set(next);\n\n return true;\n };\n\n const setQuery = (patch: LocalQueryPatch): void => {\n const next = normalizeQuery(query, patch, store.value.pagination.count);\n\n if (sameQuery(next, query)) return;\n\n query = next;\n\n commit();\n };\n\n const source: LocalSource<T> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n page: {\n go(index) {\n setQuery({ page: index });\n },\n\n last() {\n setQuery({ page: store.value.pagination.count });\n },\n\n next() {\n if (store.value.pagination.hasNext) setQuery({ page: store.value.pagination.index + 1 });\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) setQuery({ page: store.value.pagination.index - 1 });\n },\n },\n\n setData(nextData) {\n if (nextData === allData) return;\n\n allData = nextData;\n\n commit();\n },\n\n setQuery,\n\n get snapshot() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.disp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,GAEM,KAAY,MAAiC;EACjD,IAAM,IAAO,EAAe,GAAO,GAAO,EAAM,MAAM,WAAW,KAAK;EAElE,EAAU,GAAM,CAAK,MAEzB,IAAQ,GAER,EAAO;CACT,GAEM,IAAyB;EAC7B,IAAI,iBAAiB;GACnB,OAAO,EAAM;EACf;EAEA,SAAS,EAAM;EAEf,IAAI,WAAW;GACb,OAAO,EAAM;EACf;EAEA,MAAM;GACJ,GAAG,GAAO;IACR,EAAS,EAAE,MAAM,EAAM,CAAC;GAC1B;GAEA,OAAO;IACL,EAAS,EAAE,MAAM,EAAM,MAAM,WAAW,MAAM,CAAC;GACjD;GAEA,OAAO;IACL,AAAI,EAAM,MAAM,WAAW,WAAS,EAAS,EAAE,MAAM,EAAM,MAAM,WAAW,QAAQ,EAAE,CAAC;GACzF;GAEA,WAAW;IACT,AAAI,EAAM,MAAM,WAAW,eAAa,EAAS,EAAE,MAAM,EAAM,MAAM,WAAW,QAAQ,EAAE,CAAC;GAC7F;EACF;EAEA,QAAQ,GAAU;GACZ,MAAa,MAEjB,IAAU,GAEV,EAAO;EACT;EAEA;EAEA,IAAI,WAAW;GACb,OAAO,EAAM;EACf;EAEA,WAAW,EAAM;EAEjB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAO;AACT"}
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;CACzB,IAAI,IAAW,GACX,IAAO,GACP,IAAW,EAAgB,EAAO,YAAY,IAAI,UAAU,GAC5D,IAAS,EAAO,QAEd,UAAiD;EACrD,IAAM,IAAW,EAAO,SAAS,EAAS,QAAQ,MAAS,EAAO,SAAS,GAAM,CAAM,CAAC,IAAI,GACtF,IAAa,EAAqB,GAAM,GAAU,EAAS,MAAM;EACvE,IAAI,IAAO,EAAW,WAEpB,OADA,IAAO,EAAW,WACX,EAAW;EAEpB,IAAM,KAAS,IAAO,KAAK;EAE3B,OAAO;GACL,OAAO;GACP,OAAO,EAAS,MAAM,GAAO,IAAQ,CAAQ;GAC7C,SAAS;GACT;GACA;EACF;CACF,GAEM,IAAQ,EAAkB,EAAW,CAAC,GACtC,UAAyB;EAC7B,IAAI,EAAM,UAAU,MAAM,IAAI,EAAuB;CACvD,GACM,KAAkB,GAAsB,MAC5C,OAAO,KAAK,CAAI,CAAC,CAAC,OAAO,MAAQ,EAAK,OAAiC,EAAM,EAA4B,GACrG,UAAqB;EACzB,IAAM,IAAO,EAAW,GAClB,IAAU,EAAM,OAChB,IACJ,EAAK,MAAM,WAAW,EAAQ,MAAM,UACpC,EAAK,MAAM,OAAO,GAAM,MAAU,OAAO,GAAG,GAAM,EAAQ,MAAM,EAAM,CAAC;EAErE,OAAO,GAAG,EAAK,QAAQ,EAAQ,MAAM,KAAK,KAAa,EAAe,EAAK,YAAY,EAAQ,UAAU,KAE7G,EAAM,IAAI,CAAI;CAChB,GACM,KAAQ,MAA2B;EACvC,EAAW;EACX,IAAM,IAAa,KAAK,IAAI,EAAgB,GAAU,MAAM,GAA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