@vielzeug/sourcerer 3.0.0 → 26.10.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/dist/asyncSource.cjs +1 -1
- package/dist/asyncSource.cjs.map +1 -1
- package/dist/asyncSource.js +1 -63
- package/dist/asyncSource.js.map +1 -1
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.js +1 -114
- 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 +0 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -17
- package/dist/errors.js.map +1 -1
- package/dist/index.js +1 -6
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.js +1 -111
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.js +1 -70
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.js +1 -116
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.cjs.map +1 -1
- package/dist/pagination.js +1 -24
- package/dist/pagination.js.map +1 -1
- package/dist/requestController.cjs +1 -1
- package/dist/requestController.cjs.map +1 -1
- package/dist/requestController.js +1 -30
- package/dist/requestController.js.map +1 -1
- package/dist/sourceStore.cjs +1 -1
- package/dist/sourceStore.cjs.map +1 -1
- package/dist/sourceStore.js +1 -37
- 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/package.json +6 -6
|
@@ -1 +1 @@
|
|
|
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":"
|
|
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":"4KAOA,IAAM,kBAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAAS,EAAc,EACvB,cACA,WACA,WAAY,CACd,GAGA,SAAgB,qBACd,EAC4B,CAC5B,IAAI,EAA8B,CAChC,SAAU,EAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,EACI,EAAW,EACX,EAAY,GACV,EAAc,EAAmD,CACrE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiB,EAAG,EAAU,SAAU,CAAC,EACrD,OAAQ,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAI,EAAY,SAAU,MAAM,IAAI,CACtC,EAEM,OAAS,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,CAAE,OAAM,SAAU,EAAQ,SAAU,OAAQ,EAAQ,OAAQ,QAAO,CAAC,EAClG,QAAU,IAAc,CACtB,GAAG,EACH,MAAO,KACP,QAAS,GACT,GAAI,GAAU,OAAO,GAAG,EAAQ,OAAQ,EAAS,MAAM,EAAI,CAAC,EAAI,CAAE,cAAe,EAAQ,MAAO,CAClG,GACA,QAAU,GAAW,CACnB,IAAM,EAAQ,EAAS,CAAC,GAAG,EAAa,GAAG,EAAO,KAAK,EAAI,CAAC,GAAG,EAAO,KAAK,EAC3E,MAAO,CACL,MAAO,KACP,QACA,QAAS,GACT,WAAY,iBAAiB,EAAM,OAAQ,EAAQ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.cjs");function
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./sourceStore.cjs"),n=require("./pagination.cjs");function createLocalSource(r,i={}){let a=r,o=1,s=n.positiveInteger(i.pageSize??20,`pageSize`),c=i.params,buildState=()=>{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,buildState();let r=(o-1)*s;return{error:null,items:e.slice(r,r+s),loading:!1,pagination:t,params:c}},l=t.createSourceStore(buildState()),assertLive=()=>{if(l.disposed)throw new e.SourcererDisposedError},samePagination=(e,t)=>Object.keys(e).every(n=>e[n]===t[n]),commit=()=>{let e=buildState(),t=l.value,n=e.items.length===t.items.length&&e.items.every((e,n)=>Object.is(e,t.items[n]));Object.is(e.params,t.params)&&n&&samePagination(e.pagination,t.pagination)||l.set(e)},goTo=e=>{assertLive();let t=Math.min(n.positiveInteger(e,`page`),l.value.pagination.pageCount);t!==o&&(o=t,commit())},u={get disposalSignal(){return l.disposalSignal},dispose:l.dispose,get disposed(){return l.disposed},first(){goTo(1)},goTo,last(){goTo(l.value.pagination.pageCount)},next(){l.value.pagination.hasNext?goTo(o+1):assertLive()},previous(){l.value.pagination.hasPrevious?goTo(o-1):assertLive()},setItems(e){assertLive(),e!==a&&(a=e,commit())},setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);t!==s&&(o=1,s=t,commit())},setParams(e){assertLive(),!Object.is(e,c)&&(o=1,c=e,commit())},get state(){return l.value},subscribe:l.subscribe,[Symbol.dispose](){u.dispose()}};return u}exports.createLocalSource=createLocalSource;
|
|
2
2
|
//# sourceMappingURL=localSource.cjs.map
|
package/dist/localSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.cjs","names":[],"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,
|
|
1
|
+
{"version":3,"file":"localSource.cjs","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAWA,SAAgB,kBACd,EACA,EAAwC,CAAC,EAChB,CACzB,IAAI,EAAW,EACX,EAAO,EACP,EAAW,EAAA,gBAAgB,EAAO,UAAY,GAAI,UAAU,EAC5D,EAAS,EAAO,OAEd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,QAAS,KAAK,EAAO,CAAC,EAC5C,WAAW,CAClB,EAEA,UAAW,CACL,EAAM,MAAM,WAAW,YAAa,KAAK,EAAO,CAAC,EAChD,WAAW,CAClB,EAEA,SAAS,EAAW,CAClB,WAAW,EACP,IAAc,IAClB,EAAW,EACX,OAAO,EACT,EAEA,YAAY,EAAc,CACxB,WAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAc,UAAU,EACvD,IAAe,IACnB,EAAO,EACP,EAAW,EACX,OAAO,EACT,EAEA,UAAU,EAAY,CACpB,WAAW,EACP,QAAO,GAAG,EAAY,CAAM,IAChC,EAAO,EACP,EAAS,EACT,OAAO,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.js
CHANGED
|
@@ -1,71 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createSourceStore as t } from "./sourceStore.js";
|
|
3
|
-
import { createPagePagination as n, positiveInteger as r } from "./pagination.js";
|
|
4
|
-
//#region src/localSource.ts
|
|
5
|
-
function 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 {
|
|
11
|
-
error: null,
|
|
12
|
-
items: e.slice(r, r + c),
|
|
13
|
-
loading: !1,
|
|
14
|
-
pagination: t,
|
|
15
|
-
params: l
|
|
16
|
-
};
|
|
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 = {
|
|
27
|
-
get disposalSignal() {
|
|
28
|
-
return d.disposalSignal;
|
|
29
|
-
},
|
|
30
|
-
dispose: d.dispose,
|
|
31
|
-
get 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());
|
|
49
|
-
},
|
|
50
|
-
setPageSize(e) {
|
|
51
|
-
f();
|
|
52
|
-
let t = r(e, "pageSize");
|
|
53
|
-
t !== c && (s = 1, c = t, m());
|
|
54
|
-
},
|
|
55
|
-
setParams(e) {
|
|
56
|
-
f(), !Object.is(e, l) && (s = 1, l = e, m());
|
|
57
|
-
},
|
|
58
|
-
get state() {
|
|
59
|
-
return d.value;
|
|
60
|
-
},
|
|
61
|
-
subscribe: d.subscribe,
|
|
62
|
-
[Symbol.dispose]() {
|
|
63
|
-
g.dispose();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
return g;
|
|
67
|
-
}
|
|
68
|
-
//#endregion
|
|
69
|
-
export { i as createLocalSource };
|
|
70
|
-
|
|
1
|
+
import{SourcererDisposedError as e}from"./errors.js";import{createSourceStore as t}from"./sourceStore.js";import{createPagePagination as n,positiveInteger as r}from"./pagination.js";function createLocalSource(i,a={}){let o=i,s=1,c=r(a.pageSize??20,`pageSize`),l=a.params,buildState=()=>{let e=a.filter?o.filter(e=>a.filter?.(e,l)):o,t=n(s,c,e.length);if(s>t.pageCount)return s=t.pageCount,buildState();let r=(s-1)*c;return{error:null,items:e.slice(r,r+c),loading:!1,pagination:t,params:l}},u=t(buildState()),assertLive=()=>{if(u.disposed)throw new e},samePagination=(e,t)=>Object.keys(e).every(n=>e[n]===t[n]),commit=()=>{let e=buildState(),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&&samePagination(e.pagination,t.pagination)||u.set(e)},goTo=e=>{assertLive();let t=Math.min(r(e,`page`),u.value.pagination.pageCount);t!==s&&(s=t,commit())},d={get disposalSignal(){return u.disposalSignal},dispose:u.dispose,get disposed(){return u.disposed},first(){goTo(1)},goTo,last(){goTo(u.value.pagination.pageCount)},next(){u.value.pagination.hasNext?goTo(s+1):assertLive()},previous(){u.value.pagination.hasPrevious?goTo(s-1):assertLive()},setItems(e){assertLive(),e!==o&&(o=e,commit())},setPageSize(e){assertLive();let t=r(e,`pageSize`);t!==c&&(s=1,c=t,commit())},setParams(e){assertLive(),!Object.is(e,l)&&(s=1,l=e,commit())},get state(){return u.value},subscribe:u.subscribe,[Symbol.dispose](){d.dispose()}};return d}export{createLocalSource};
|
|
71
2
|
//# sourceMappingURL=localSource.js.map
|
package/dist/localSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSource.js","names":[],"sources":["../src/localSource.ts"],"sourcesContent":["import { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger } from './pagination';\nimport { createSourceStore } from './sourceStore';\nimport type { LocalSource, LocalSourceConfig, LocalSourceState, PagePagination } from './types';\n\n/** Local sources filter and paginate an in-memory collection synchronously. */\nexport function createLocalSource<T>(items: readonly T[]): LocalSource<T>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams>;\nexport function createLocalSource<T, TParams = undefined>(\n items: readonly T[],\n config: LocalSourceConfig<T, TParams> = {} as LocalSourceConfig<T, TParams>,\n): LocalSource<T, TParams> {\n let allItems = items;\n let page = 1;\n let pageSize = positiveInteger(config.pageSize ?? 20, 'pageSize');\n let params = config.params as TParams;\n\n const buildState = (): LocalSourceState<T, TParams> => {\n const filtered = config.filter ? allItems.filter((item) => config.filter?.(item, params)) : allItems;\n const pagination = createPagePagination(page, pageSize, filtered.length);\n if (page > pagination.pageCount) {\n page = pagination.pageCount;\n return buildState();\n }\n const start = (page - 1) * pageSize;\n\n return {\n error: null,\n items: filtered.slice(start, start + pageSize),\n loading: false,\n pagination,\n params,\n };\n };\n\n const store = createSourceStore(buildState());\n const assertLive = (): void => {\n if (store.disposed) throw new SourcererDisposedError();\n };\n const samePagination = (left: PagePagination, right: PagePagination): boolean =>\n Object.keys(left).every((key) => left[key as keyof PagePagination] === right[key as keyof PagePagination]);\n const commit = (): void => {\n const next = buildState();\n const current = store.value;\n const sameItems =\n next.items.length === current.items.length &&\n next.items.every((item, index) => Object.is(item, current.items[index]));\n\n if (Object.is(next.params, current.params) && sameItems && samePagination(next.pagination, current.pagination))\n return;\n store.set(next);\n };\n const goTo = (nextPage: number): void => {\n assertLive();\n const normalized = Math.min(positiveInteger(nextPage, 'page'), store.value.pagination.pageCount);\n if (normalized === page) return;\n page = normalized;\n commit();\n };\n\n const source: LocalSource<T, TParams> = {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose: store.dispose,\n\n get disposed() {\n return store.disposed;\n },\n\n first() {\n goTo(1);\n },\n\n goTo,\n\n last() {\n goTo(store.value.pagination.pageCount);\n },\n\n next() {\n if (store.value.pagination.hasNext) goTo(page + 1);\n else assertLive();\n },\n\n previous() {\n if (store.value.pagination.hasPrevious) goTo(page - 1);\n else assertLive();\n },\n\n setItems(nextItems) {\n assertLive();\n if (nextItems === allItems) return;\n allItems = nextItems;\n commit();\n },\n\n setPageSize(nextPageSize) {\n assertLive();\n const normalized = positiveInteger(nextPageSize, 'pageSize');\n if (normalized === pageSize) return;\n page = 1;\n pageSize = normalized;\n commit();\n },\n\n setParams(nextParams) {\n assertLive();\n if (Object.is(nextParams, params)) return;\n page = 1;\n params = nextParams;\n commit();\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"localSource.js","names":[],"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":"sLAWA,SAAgB,kBACd,EACA,EAAwC,CAAC,EAChB,CACzB,IAAI,EAAW,EACX,EAAO,EACP,EAAW,EAAgB,EAAO,UAAY,GAAI,UAAU,EAC5D,EAAS,EAAO,OAEd,eAAiD,CACrD,IAAM,EAAW,EAAO,OAAS,EAAS,OAAQ,GAAS,EAAO,SAAS,EAAM,CAAM,CAAC,EAAI,EACtF,EAAa,EAAqB,EAAM,EAAU,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P,IAAc,IAClB,EAAW,EACX,OAAO,EACT,EAEA,YAAY,EAAc,CACxB,WAAW,EACX,IAAM,EAAa,EAAgB,EAAc,UAAU,EACvD,IAAe,IACnB,EAAO,EACP,EAAW,EACX,OAAO,EACT,EAEA,UAAU,EAAY,CACpB,WAAW,EACP,QAAO,GAAG,EAAY,CAAM,IAChC,EAAO,EACP,EAAS,EACT,OAAO,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/pageSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");function
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");function createPageSource(r){let i={page:1,pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=!1,o=t.createAsyncSource({error:null,items:[],loading:!1,pagination:n.createPagePagination(i.page,i.pageSize,0),params:i.params}),assertLive=()=>{if(o.disposed)throw new e.SourcererDisposedError},isCommitted=e=>o.state.pagination.page===e.page&&o.state.pagination.pageSize===e.pageSize&&Object.is(o.state.params,e.params),fetch=e=>o.fetch({commit:()=>{a=!0,i=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>r.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:n.createPagePagination(e.page,e.pageSize,n.totalItems(t.totalItems)),params:e.params})}),goTo=async e=>{assertLive();let t=a?Math.min(n.positiveInteger(e,`page`),o.state.pagination.pageCount):n.positiveInteger(e,`page`);if(t===i.page){!o.state.loading&&(!a||!isCommitted(i))&&await fetch(i);return}i={...i,page:t},await fetch(i)},s={get disposalSignal(){return o.disposalSignal},dispose:o.dispose,get disposed(){return o.disposed},first(){return goTo(1)},goTo,last(){return goTo(o.state.pagination.pageCount)},async next(){assertLive(),(!a||i.page<o.state.pagination.pageCount)&&await goTo(i.page+1)},async previous(){assertLive(),i.page>1&&await goTo(i.page-1)},reload(){return fetch(i)},async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize&&i.page===1){!o.state.loading&&!isCommitted(i)&&await fetch(i);return}i={...i,page:1,pageSize:t},await fetch(i)},async setParams(e){if(assertLive(),Object.is(e,i.params)){!o.state.loading&&!isCommitted(i)&&await fetch(i);return}i={...i,page:1,params:e},await fetch(i)},get state(){return o.state},subscribe:o.subscribe,[Symbol.dispose](){s.dispose()}};return s}exports.createPageSource=createPageSource;
|
|
2
2
|
//# sourceMappingURL=pageSource.cjs.map
|
package/dist/pageSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pageSource.cjs","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAQA,SAAgB,
|
|
1
|
+
{"version":3,"file":"pageSource.cjs","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAQA,SAAgB,iBAAyC,EAA8D,CACrH,IAAI,EAA8B,CAChC,KAAM,EACN,SAAU,EAAA,gBAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,EACI,EAAY,GACV,EAAc,EAAA,kBAA+C,CACjE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,EAAA,qBAAqB,EAA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AEA,QAAS,EAAY,QAErB,IAAI,UAAW,CACb,OAAO,EAAY,QACrB,EAEA,OAAQ,CACN,OAAO,KAAK,CAAC,CACf,EAEA,KAEA,MAAO,CACL,OAAO,KAAK,EAAY,MAAM,WAAW,SAAS,CACpD,EAEA,MAAM,MAAO,CACX,WAAW,GACP,CAAC,GAAa,EAAU,KAAO,EAAY,MAAM,WAAW,YAAW,MAAM,KAAK,EAAU,KAAO,CAAC,CAC1G,EAEA,MAAM,UAAW,CACf,WAAW,EACP,EAAU,KAAO,GAAG,MAAM,KAAK,EAAU,KAAO,CAAC,CACvD,EAEA,QAAS,CACP,OAAO,MAAM,CAAS,CACxB,EAEA,MAAM,YAAY,EAAU,CAC1B,WAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAU,UAAU,EACvD,GAAI,IAAe,EAAU,UAAY,EAAU,OAAS,EAAG,CACzD,CAAC,EAAY,MAAM,SAAW,CAAC,YAAY,CAAS,GAAG,MAAM,MAAM,CAAS,EAChF,MACF,CACA,EAAY,CAAE,GAAG,EAAW,KAAM,EAAG,SAAU,CAAW,EAC1D,MAAM,MAAM,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,YAAY,CAAS,GAAG,MAAM,MAAM,CAAS,EAChF,MACF,CACA,EAAY,CAAE,GAAG,EAAW,KAAM,EAAG,QAAO,EAC5C,MAAM,MAAM,CAAS,CACvB,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
|
package/dist/pageSource.js
CHANGED
|
@@ -1,117 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createAsyncSource as t } from "./asyncSource.js";
|
|
3
|
-
import { createPagePagination as n, positiveInteger as r, totalItems as i } from "./pagination.js";
|
|
4
|
-
//#region src/pageSource.ts
|
|
5
|
-
function a(a) {
|
|
6
|
-
let o = {
|
|
7
|
-
page: 1,
|
|
8
|
-
pageSize: r(a.pageSize ?? 20, "pageSize"),
|
|
9
|
-
params: a.params
|
|
10
|
-
}, s = !1, c = t({
|
|
11
|
-
error: null,
|
|
12
|
-
items: [],
|
|
13
|
-
loading: !1,
|
|
14
|
-
pagination: n(o.page, o.pageSize, 0),
|
|
15
|
-
params: o.params
|
|
16
|
-
}), l = () => {
|
|
17
|
-
if (c.disposed) throw new e();
|
|
18
|
-
}, u = (e) => c.state.pagination.page === e.page && c.state.pagination.pageSize === e.pageSize && Object.is(c.state.params, e.params), d = (e) => c.fetch({
|
|
19
|
-
commit: () => {
|
|
20
|
-
s = !0, o = e;
|
|
21
|
-
},
|
|
22
|
-
failure: (e, t) => ({
|
|
23
|
-
...e,
|
|
24
|
-
error: t,
|
|
25
|
-
loading: !1,
|
|
26
|
-
pendingParams: void 0
|
|
27
|
-
}),
|
|
28
|
-
load: (t) => a.load({
|
|
29
|
-
...e,
|
|
30
|
-
signal: t
|
|
31
|
-
}),
|
|
32
|
-
pending: (t) => ({
|
|
33
|
-
...t,
|
|
34
|
-
error: null,
|
|
35
|
-
loading: !0,
|
|
36
|
-
...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
37
|
-
}),
|
|
38
|
-
success: (t) => ({
|
|
39
|
-
error: null,
|
|
40
|
-
items: [...t.items],
|
|
41
|
-
loading: !1,
|
|
42
|
-
pagination: n(e.page, e.pageSize, i(t.totalItems)),
|
|
43
|
-
params: e.params
|
|
44
|
-
})
|
|
45
|
-
}), f = async (e) => {
|
|
46
|
-
l();
|
|
47
|
-
let t = s ? Math.min(r(e, "page"), c.state.pagination.pageCount) : r(e, "page");
|
|
48
|
-
if (t === o.page) {
|
|
49
|
-
!c.state.loading && (!s || !u(o)) && await d(o);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
o = {
|
|
53
|
-
...o,
|
|
54
|
-
page: t
|
|
55
|
-
}, await d(o);
|
|
56
|
-
}, p = {
|
|
57
|
-
get disposalSignal() {
|
|
58
|
-
return c.disposalSignal;
|
|
59
|
-
},
|
|
60
|
-
dispose: c.dispose,
|
|
61
|
-
get disposed() {
|
|
62
|
-
return c.disposed;
|
|
63
|
-
},
|
|
64
|
-
first() {
|
|
65
|
-
return f(1);
|
|
66
|
-
},
|
|
67
|
-
goTo: f,
|
|
68
|
-
last() {
|
|
69
|
-
return f(c.state.pagination.pageCount);
|
|
70
|
-
},
|
|
71
|
-
async next() {
|
|
72
|
-
l(), (!s || o.page < c.state.pagination.pageCount) && await f(o.page + 1);
|
|
73
|
-
},
|
|
74
|
-
async previous() {
|
|
75
|
-
l(), o.page > 1 && await f(o.page - 1);
|
|
76
|
-
},
|
|
77
|
-
reload() {
|
|
78
|
-
return d(o);
|
|
79
|
-
},
|
|
80
|
-
async setPageSize(e) {
|
|
81
|
-
l();
|
|
82
|
-
let t = r(e, "pageSize");
|
|
83
|
-
if (t === o.pageSize && o.page === 1) {
|
|
84
|
-
!c.state.loading && !u(o) && await d(o);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
o = {
|
|
88
|
-
...o,
|
|
89
|
-
page: 1,
|
|
90
|
-
pageSize: t
|
|
91
|
-
}, await d(o);
|
|
92
|
-
},
|
|
93
|
-
async setParams(e) {
|
|
94
|
-
if (l(), Object.is(e, o.params)) {
|
|
95
|
-
!c.state.loading && !u(o) && await d(o);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
o = {
|
|
99
|
-
...o,
|
|
100
|
-
page: 1,
|
|
101
|
-
params: e
|
|
102
|
-
}, await d(o);
|
|
103
|
-
},
|
|
104
|
-
get state() {
|
|
105
|
-
return c.state;
|
|
106
|
-
},
|
|
107
|
-
subscribe: c.subscribe,
|
|
108
|
-
[Symbol.dispose]() {
|
|
109
|
-
p.dispose();
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
return p;
|
|
113
|
-
}
|
|
114
|
-
//#endregion
|
|
115
|
-
export { a as createPageSource };
|
|
116
|
-
|
|
1
|
+
import{SourcererDisposedError as e}from"./errors.js";import{createAsyncSource as t}from"./asyncSource.js";import{createPagePagination as n,positiveInteger as r,totalItems as i}from"./pagination.js";function createPageSource(a){let o={page:1,pageSize:r(a.pageSize??20,`pageSize`),params:a.params},s=!1,c=t({error:null,items:[],loading:!1,pagination:n(o.page,o.pageSize,0),params:o.params}),assertLive=()=>{if(c.disposed)throw new e},isCommitted=e=>c.state.pagination.page===e.page&&c.state.pagination.pageSize===e.pageSize&&Object.is(c.state.params,e.params),fetch=e=>c.fetch({commit:()=>{s=!0,o=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>a.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:n(e.page,e.pageSize,i(t.totalItems)),params:e.params})}),goTo=async e=>{assertLive();let t=s?Math.min(r(e,`page`),c.state.pagination.pageCount):r(e,`page`);if(t===o.page){!c.state.loading&&(!s||!isCommitted(o))&&await fetch(o);return}o={...o,page:t},await fetch(o)},l={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},first(){return goTo(1)},goTo,last(){return goTo(c.state.pagination.pageCount)},async next(){assertLive(),(!s||o.page<c.state.pagination.pageCount)&&await goTo(o.page+1)},async previous(){assertLive(),o.page>1&&await goTo(o.page-1)},reload(){return fetch(o)},async setPageSize(e){assertLive();let t=r(e,`pageSize`);if(t===o.pageSize&&o.page===1){!c.state.loading&&!isCommitted(o)&&await fetch(o);return}o={...o,page:1,pageSize:t},await fetch(o)},async setParams(e){if(assertLive(),Object.is(e,o.params)){!c.state.loading&&!isCommitted(o)&&await fetch(o);return}o={...o,page:1,params:e},await fetch(o)},get state(){return c.state},subscribe:c.subscribe,[Symbol.dispose](){l.dispose()}};return l}export{createPageSource};
|
|
117
2
|
//# sourceMappingURL=pageSource.js.map
|
package/dist/pageSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pageSource.js","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"pageSource.js","names":[],"sources":["../src/pageSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { createPagePagination, positiveInteger, totalItems } from './pagination';\nimport type { PageSource, PageSourceConfig, PageSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ page: number; pageSize: number; params: TParams }>;\n\n/** Page sources retain committed items while replacement work is pending. */\nexport function createPageSource<T, TParams = undefined>(config: PageSourceConfig<T, TParams>): PageSource<T, TParams> {\n let requested: Request<TParams> = {\n page: 1,\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let hasLoaded = false;\n const asyncSource = createAsyncSource<PageSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagePagination(requested.page, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const isCommitted = (request: Request<TParams>): boolean =>\n asyncSource.state.pagination.page === request.page &&\n asyncSource.state.pagination.pageSize === request.pageSize &&\n Object.is(asyncSource.state.params, request.params);\n\n const fetch = (request: Request<TParams>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagePagination(request.page, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n }),\n });\n\n const goTo = async (page: number): Promise<void> => {\n assertLive();\n const normalized = hasLoaded\n ? Math.min(positiveInteger(page, 'page'), asyncSource.state.pagination.pageCount)\n : positiveInteger(page, 'page');\n if (normalized === requested.page) {\n if (!asyncSource.state.loading && (!hasLoaded || !isCommitted(requested))) await fetch(requested);\n return;\n }\n requested = { ...requested, page: normalized };\n await fetch(requested);\n };\n\n const source: PageSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n first() {\n return goTo(1);\n },\n\n goTo,\n\n last() {\n return goTo(asyncSource.state.pagination.pageCount);\n },\n\n async next() {\n assertLive();\n if (!hasLoaded || requested.page < asyncSource.state.pagination.pageCount) await goTo(requested.page + 1);\n },\n\n async previous() {\n assertLive();\n if (requested.page > 1) await goTo(requested.page - 1);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.page === 1) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, pageSize: normalized };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !isCommitted(requested)) await fetch(requested);\n return;\n }\n requested = { ...requested, page: 1, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"sMAQA,SAAgB,iBAAyC,EAA8D,CACrH,IAAI,EAA8B,CAChC,KAAM,EACN,SAAU,EAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,EACI,EAAY,GACV,EAAc,EAA+C,CACjE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,EAAqB,EAAU,KAAM,EAAU,SAAU,CAAC,EACtE,OAAQ,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAI,EAAY,SAAU,MAAM,IAAI,CACtC,EACM,YAAe,GACnB,EAAY,MAAM,WAAW,OAAS,EAAQ,MAC9C,EAAY,MAAM,WAAW,WAAa,EAAQ,UAClD,OAAO,GAAG,EAAY,MAAM,OAAQ,EAAQ,MAAM,EAE9C,MAAS,GACb,EAAY,MAAM,CAChB,WAAc,CACZ,EAAY,GACZ,EAAY,CACd,EACA,SAAU,EAAU,KAAW,CAAE,GAAG,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,GAC9F,KAAO,GAAW,EAAO,KAAK,CAAE,GAAG,EAAS,QAAO,CAAC,EACpD,QAAU,IAAc,CACtB,GAAG,EACH,MA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,CACzD,CAAC,EAAY,MAAM,SAAW,CAAC,YAAY,CAAS,GAAG,MAAM,MAAM,CAAS,EAChF,MACF,CACA,EAAY,CAAE,GAAG,EAAW,KAAM,EAAG,SAAU,CAAW,EAC1D,MAAM,MAAM,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,YAAY,CAAS,GAAG,MAAM,MAAM,CAAS,EAChF,MACF,CACA,EAAY,CAAE,GAAG,EAAW,KAAM,EAAG,QAAO,EAC5C,MAAM,MAAM,CAAS,CACvB,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
|
package/dist/pagination.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs");function
|
|
1
|
+
const e=require("./errors.cjs");function positiveInteger(t,n){if(!Number.isInteger(t)||t<1)throw new e.SourcererConfigurationError(`${n} must be a positive integer`);return t}function totalItems(t){if(!Number.isInteger(t)||t<0)throw new e.SourcererConfigurationError(`totalItems must be a non-negative integer`);return t}function createPagePagination(e,t,n){let r=Math.max(1,Math.ceil(n/t));return{hasNext:e<r,hasPrevious:e>1,page:e,pageCount:r,pageSize:t,totalItems:n}}exports.createPagePagination=createPagePagination,exports.positiveInteger=positiveInteger,exports.totalItems=totalItems;
|
|
2
2
|
//# sourceMappingURL=pagination.cjs.map
|
package/dist/pagination.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.cjs","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"gCAGA,SAAgB,
|
|
1
|
+
{"version":3,"file":"pagination.cjs","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"gCAGA,SAAgB,gBAAgB,EAAe,EAAsB,CACnE,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAAA,4BAA4B,GAAG,EAAK,4BAA4B,EAE5E,OAAO,CACT,CAEA,SAAgB,WAAW,EAAuB,CAChD,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAAA,4BAA4B,2CAA2C,EAEnF,OAAO,CACT,CAEA,SAAgB,qBAAqB,EAAc,EAAkB,EAA+B,CAClG,IAAM,EAAY,KAAK,IAAI,EAAG,KAAK,KAAK,EAAQ,CAAQ,CAAC,EAEzD,MAAO,CACL,QAAS,EAAO,EAChB,YAAa,EAAO,EACpB,OACA,YACA,WACA,WAAY,CACd,CACF"}
|
package/dist/pagination.js
CHANGED
|
@@ -1,25 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
//#region src/pagination.ts
|
|
3
|
-
function t(t, n) {
|
|
4
|
-
if (!Number.isInteger(t) || t < 1) throw new e(`${n} must be a positive integer`);
|
|
5
|
-
return t;
|
|
6
|
-
}
|
|
7
|
-
function n(t) {
|
|
8
|
-
if (!Number.isInteger(t) || t < 0) throw new e("totalItems must be a non-negative integer");
|
|
9
|
-
return t;
|
|
10
|
-
}
|
|
11
|
-
function r(e, t, n) {
|
|
12
|
-
let r = Math.max(1, Math.ceil(n / t));
|
|
13
|
-
return {
|
|
14
|
-
hasNext: e < r,
|
|
15
|
-
hasPrevious: e > 1,
|
|
16
|
-
page: e,
|
|
17
|
-
pageCount: r,
|
|
18
|
-
pageSize: t,
|
|
19
|
-
totalItems: n
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
export { r as createPagePagination, t as positiveInteger, n as totalItems };
|
|
24
|
-
|
|
1
|
+
import{SourcererConfigurationError as e}from"./errors.js";function positiveInteger(t,n){if(!Number.isInteger(t)||t<1)throw new e(`${n} must be a positive integer`);return t}function totalItems(t){if(!Number.isInteger(t)||t<0)throw new e(`totalItems must be a non-negative integer`);return t}function createPagePagination(e,t,n){let r=Math.max(1,Math.ceil(n/t));return{hasNext:e<r,hasPrevious:e>1,page:e,pageCount:r,pageSize:t,totalItems:n}}export{createPagePagination,positiveInteger,totalItems};
|
|
25
2
|
//# sourceMappingURL=pagination.js.map
|
package/dist/pagination.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.js","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"pagination.js","names":[],"sources":["../src/pagination.ts"],"sourcesContent":["import { SourcererConfigurationError } from './errors';\nimport type { PagePagination } from './types';\n\nexport function positiveInteger(value: number, name: string): number {\n if (!Number.isInteger(value) || value < 1)\n throw new SourcererConfigurationError(`${name} must be a positive integer`);\n\n return value;\n}\n\nexport function totalItems(value: number): number {\n if (!Number.isInteger(value) || value < 0)\n throw new SourcererConfigurationError('totalItems must be a non-negative integer');\n\n return value;\n}\n\nexport function createPagePagination(page: number, pageSize: number, total: number): PagePagination {\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n return {\n hasNext: page < pageCount,\n hasPrevious: page > 1,\n page,\n pageCount,\n pageSize,\n totalItems: total,\n };\n}\n"],"mappings":"0DAGA,SAAgB,gBAAgB,EAAe,EAAsB,CACnE,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAA4B,GAAG,EAAK,4BAA4B,EAE5E,OAAO,CACT,CAEA,SAAgB,WAAW,EAAuB,CAChD,GAAI,CAAC,OAAO,UAAU,CAAK,GAAK,EAAQ,EACtC,MAAM,IAAI,EAA4B,2CAA2C,EAEnF,OAAO,CACT,CAEA,SAAgB,qBAAqB,EAAc,EAAkB,EAA+B,CAClG,IAAM,EAAY,KAAK,IAAI,EAAG,KAAK,KAAK,EAAQ,CAAQ,CAAC,EAEzD,MAAO,CACL,QAAS,EAAO,EAChB,YAAa,EAAO,EACpB,OACA,YACA,WACA,WAAY,CACd,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function createRequestController(){let e,t=0;return{begin(){e?.abort();let n=new AbortController,r=++t;return e=n,{finish(){r===t&&(e=void 0)},isCurrent(){return r===t},signal:n.signal}},dispose(){e?.abort(),e=void 0,t++}}}function toError(e){if(e instanceof Error)return e;let t=typeof e==`string`&&e.length>0?e:`Source request failed`;return Error(t,{cause:e})}exports.createRequestController=createRequestController,exports.toError=toError;
|
|
2
2
|
//# sourceMappingURL=requestController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestController.cjs","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,
|
|
1
|
+
{"version":3,"file":"requestController.cjs","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,yBAA6C,CAC3D,IAAI,EACA,EAAW,EAEf,MAAO,CACL,OAAQ,CACN,GAAQ,MAAM,EAEd,IAAM,EAAa,IAAI,gBACjB,EAAkB,EAAE,EAI1B,MAFA,GAAS,EAEF,CACL,QAAS,CACH,IAAoB,IAAU,EAAS,IAAA,GAC7C,EAEA,WAAY,CACV,OAAO,IAAoB,CAC7B,EAEA,OAAQ,EAAW,MACrB,CACF,EAEA,SAAU,CACR,GAAQ,MAAM,EACd,EAAS,IAAA,GACT,GACF,CACF,CACF,CAEA,SAAgB,QAAQ,EAAwB,CAC9C,GAAI,aAAkB,MAAO,OAAO,EAEpC,IAAM,EAAU,OAAO,GAAW,UAAY,EAAO,OAAS,EAAI,EAAS,wBAE3E,OAAW,MAAM,EAAS,CAAE,MAAO,CAAO,CAAC,CAC7C"}
|
|
@@ -1,31 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
function e() {
|
|
3
|
-
let e, t = 0;
|
|
4
|
-
return {
|
|
5
|
-
begin() {
|
|
6
|
-
e?.abort();
|
|
7
|
-
let n = new AbortController(), r = ++t;
|
|
8
|
-
return e = n, {
|
|
9
|
-
finish() {
|
|
10
|
-
r === t && (e = void 0);
|
|
11
|
-
},
|
|
12
|
-
isCurrent() {
|
|
13
|
-
return r === t;
|
|
14
|
-
},
|
|
15
|
-
signal: n.signal
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
dispose() {
|
|
19
|
-
e?.abort(), e = void 0, t++;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function t(e) {
|
|
24
|
-
if (e instanceof Error) return e;
|
|
25
|
-
let t = typeof e == "string" && e.length > 0 ? e : "Source request failed";
|
|
26
|
-
return Error(t, { cause: e });
|
|
27
|
-
}
|
|
28
|
-
//#endregion
|
|
29
|
-
export { e as createRequestController, t as toError };
|
|
30
|
-
|
|
1
|
+
function createRequestController(){let e,t=0;return{begin(){e?.abort();let n=new AbortController,r=++t;return e=n,{finish(){r===t&&(e=void 0)},isCurrent(){return r===t},signal:n.signal}},dispose(){e?.abort(),e=void 0,t++}}}function toError(e){if(e instanceof Error)return e;let t=typeof e==`string`&&e.length>0?e:`Source request failed`;return Error(t,{cause:e})}export{createRequestController,toError};
|
|
31
2
|
//# sourceMappingURL=requestController.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestController.js","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"requestController.js","names":[],"sources":["../src/requestController.ts"],"sourcesContent":["export type RequestHandle = Readonly<{\n finish(): void;\n isCurrent(): boolean;\n signal: AbortSignal;\n}>;\n\nexport type RequestController = Readonly<{\n begin(): RequestHandle;\n dispose(): void;\n}>;\n\n/** A source owns one active request: newer query state always supersedes older work. */\nexport function createRequestController(): RequestController {\n let active: AbortController | undefined;\n let sequence = 0;\n\n return {\n begin() {\n active?.abort();\n\n const controller = new AbortController();\n const requestSequence = ++sequence;\n\n active = controller;\n\n return {\n finish() {\n if (requestSequence === sequence) active = undefined;\n },\n\n isCurrent() {\n return requestSequence === sequence;\n },\n\n signal: controller.signal,\n };\n },\n\n dispose() {\n active?.abort();\n active = undefined;\n sequence++;\n },\n };\n}\n\nexport function toError(reason: unknown): Error {\n if (reason instanceof Error) return reason;\n\n const message = typeof reason === 'string' && reason.length > 0 ? reason : 'Source request failed';\n\n return new Error(message, { cause: reason });\n}\n"],"mappings":"AAYA,SAAgB,yBAA6C,CAC3D,IAAI,EACA,EAAW,EAEf,MAAO,CACL,OAAQ,CACN,GAAQ,MAAM,EAEd,IAAM,EAAa,IAAI,gBACjB,EAAkB,EAAE,EAI1B,MAFA,GAAS,EAEF,CACL,QAAS,CACH,IAAoB,IAAU,EAAS,IAAA,GAC7C,EAEA,WAAY,CACV,OAAO,IAAoB,CAC7B,EAEA,OAAQ,EAAW,MACrB,CACF,EAEA,SAAU,CACR,GAAQ,MAAM,EACd,EAAS,IAAA,GACT,GACF,CACF,CACF,CAEA,SAAgB,QAAQ,EAAwB,CAC9C,GAAI,aAAkB,MAAO,OAAO,EAEpC,IAAM,EAAU,OAAO,GAAW,UAAY,EAAO,OAAS,EAAI,EAAS,wBAE3E,OAAW,MAAM,EAAS,CAAE,MAAO,CAAO,CAAC,CAC7C"}
|
package/dist/sourceStore.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function createSourceStore(e){let t=new AbortController,n=new Set,r=!1,i=e,dispose=()=>{r||(r=!0,n.clear(),t.abort())};return{get disposalSignal(){return t.signal},dispose,get disposed(){return r},set(e){if(!r){i=e;for(let e of n)try{e(i)}catch(e){queueMicrotask(()=>{throw e})}}},subscribe(e){return r?()=>{}:(n.add(e),()=>n.delete(e))},[Symbol.dispose]:dispose,get value(){return i}}}exports.createSourceStore=createSourceStore;
|
|
2
2
|
//# sourceMappingURL=sourceStore.cjs.map
|
package/dist/sourceStore.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.cjs","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"AAWA,SAAgB,
|
|
1
|
+
{"version":3,"file":"sourceStore.cjs","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"AAWA,SAAgB,kBAAqB,EAAiC,CACpE,IAAM,EAAa,IAAI,gBACjB,EAAY,IAAI,IAClB,EAAW,GACX,EAAQ,EAEN,YAAsB,CACtB,IAEJ,EAAW,GACX,EAAU,MAAM,EAChB,EAAW,MAAM,EACnB,EAEA,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAO,EAAW,MACpB,EAEA,QAEA,IAAI,UAAW,CACb,OAAO,CACT,EAEA,IAAI,EAAW,CACT,MAEJ,GAAQ,EAER,IAAK,IAAM,KAAY,EACrB,GAAI,CACF,EAAS,CAAK,CAChB,OAAS,EAAO,CACd,mBAAqB,CACnB,MAAM,CACR,CAAC,CACH,CATM,CAWV,EAEA,UAAU,EAAU,CAKlB,OAJI,MAAuB,CAAC,GAE5B,EAAU,IAAI,CAAQ,MAET,EAAU,OAAO,CAAQ,EACxC,GAEC,OAAO,SAAU,QAElB,IAAI,OAAQ,CACV,OAAO,CACT,CACF,CACF"}
|
package/dist/sourceStore.js
CHANGED
|
@@ -1,38 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
function e(e) {
|
|
3
|
-
let t = new AbortController(), n = /* @__PURE__ */ new Set(), r = !1, i = e, a = () => {
|
|
4
|
-
r || (r = !0, n.clear(), t.abort());
|
|
5
|
-
};
|
|
6
|
-
return {
|
|
7
|
-
get disposalSignal() {
|
|
8
|
-
return t.signal;
|
|
9
|
-
},
|
|
10
|
-
dispose: a,
|
|
11
|
-
get disposed() {
|
|
12
|
-
return r;
|
|
13
|
-
},
|
|
14
|
-
set(e) {
|
|
15
|
-
if (!r) {
|
|
16
|
-
i = e;
|
|
17
|
-
for (let e of n) try {
|
|
18
|
-
e(i);
|
|
19
|
-
} catch (e) {
|
|
20
|
-
queueMicrotask(() => {
|
|
21
|
-
throw e;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
subscribe(e) {
|
|
27
|
-
return r ? () => {} : (n.add(e), () => n.delete(e));
|
|
28
|
-
},
|
|
29
|
-
[Symbol.dispose]: a,
|
|
30
|
-
get value() {
|
|
31
|
-
return i;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
//#endregion
|
|
36
|
-
export { e as createSourceStore };
|
|
37
|
-
|
|
1
|
+
function createSourceStore(e){let t=new AbortController,n=new Set,r=!1,i=e,dispose=()=>{r||(r=!0,n.clear(),t.abort())};return{get disposalSignal(){return t.signal},dispose,get disposed(){return r},set(e){if(!r){i=e;for(let e of n)try{e(i)}catch(e){queueMicrotask(()=>{throw e})}}},subscribe(e){return r?()=>{}:(n.add(e),()=>n.delete(e))},[Symbol.dispose]:dispose,get value(){return i}}}export{createSourceStore};
|
|
38
2
|
//# sourceMappingURL=sourceStore.js.map
|
package/dist/sourceStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sourceStore.js","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"sourceStore.js","names":[],"sources":["../src/sourceStore.ts"],"sourcesContent":["type SourceStore<T> = {\n [Symbol.dispose](): void;\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n set(value: T): void;\n subscribe(listener: (value: T) => void): () => void;\n readonly value: T;\n};\n\n/** Store commits source state before notifying; observer failures cannot rewrite committed state. */\nexport function createSourceStore<T>(initialValue: T): SourceStore<T> {\n const controller = new AbortController();\n const listeners = new Set<(value: T) => void>();\n let disposed = false;\n let value = initialValue;\n\n const dispose = (): void => {\n if (disposed) return;\n\n disposed = true;\n listeners.clear();\n controller.abort();\n };\n\n return {\n get disposalSignal() {\n return controller.signal;\n },\n\n dispose,\n\n get disposed() {\n return disposed;\n },\n\n set(nextValue) {\n if (disposed) return;\n\n value = nextValue;\n\n for (const listener of listeners) {\n try {\n listener(value);\n } catch (error) {\n queueMicrotask(() => {\n throw error;\n });\n }\n }\n },\n\n subscribe(listener) {\n if (disposed) return () => {};\n\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n\n [Symbol.dispose]: dispose,\n\n get value() {\n return value;\n },\n };\n}\n"],"mappings":"AAWA,SAAgB,kBAAqB,EAAiC,CACpE,IAAM,EAAa,IAAI,gBACjB,EAAY,IAAI,IAClB,EAAW,GACX,EAAQ,EAEN,YAAsB,CACtB,IAEJ,EAAW,GACX,EAAU,MAAM,EAChB,EAAW,MAAM,EACnB,EAEA,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAO,EAAW,MACpB,EAEA,QAEA,IAAI,UAAW,CACb,OAAO,CACT,EAEA,IAAI,EAAW,CACT,MAEJ,GAAQ,EAER,IAAK,IAAM,KAAY,EACrB,GAAI,CACF,EAAS,CAAK,CAChB,OAAS,EAAO,CACd,mBAAqB,CACnB,MAAM,CACR,CAAC,CACH,CATM,CAWV,EAEA,UAAU,EAAU,CAKlB,OAJI,MAAuB,CAAC,GAE5B,EAAU,IAAI,CAAQ,MAET,EAAU,OAAO,CAAQ,EACxC,GAEC,OAAO,SAAU,QAElB,IAAI,OAAQ,CACV,OAAO,CACT,CACF,CACF"}
|