@vielzeug/sourcerer 26.11.12 → 26.11.14
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.d.ts +2 -1
- package/dist/asyncSource.d.ts.map +1 -1
- package/dist/asyncSource.js +17 -37
- package/dist/asyncSource.js.map +1 -1
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.d.ts.map +1 -1
- package/dist/cursorSource.js +48 -46
- package/dist/cursorSource.js.map +1 -1
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.d.ts.map +1 -1
- package/dist/infiniteSource.js +57 -61
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.d.ts.map +1 -1
- package/dist/localSource.js +31 -30
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.d.ts.map +1 -1
- package/dist/pageSource.js +38 -37
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.cjs.map +1 -1
- package/dist/pagination.d.ts +2 -0
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +5 -1
- package/dist/pagination.js.map +1 -1
- package/dist/resourceSource.cjs +1 -1
- package/dist/resourceSource.cjs.map +1 -1
- package/dist/resourceSource.d.ts.map +1 -1
- package/dist/resourceSource.js +12 -15
- package/dist/resourceSource.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 +173 -192
- package/dist/sourcerer.js.map +1 -1
- package/package.json +2 -2
package/dist/asyncSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs"),t=require("./requestController.cjs"),n=require("./sourceStore.cjs");let r=require("@vielzeug/arsenal");
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./requestController.cjs"),n=require("./sourceStore.cjs");let r=require("@vielzeug/arsenal");function createAsyncSource(i){let a=n.createSourceStore(i),o=t.createRequestController();return{get disposalSignal(){return a.disposalSignal},dispose(){o.dispose(),a.dispose()},get disposed(){return a.disposed},async fetch(n){if(a.disposed)throw new e.SourcererDisposedError;let i=o.begin(),s=a.value;a.set(n.pending(s));try{let e=await(0,r.abortable)(n.load(i.signal),i.signal);if(!i.isCurrent()||a.disposed)return;let t=n.success(e);n.commit?.(),a.set(t)}catch(e){if(!i.isCurrent()||a.disposed)return;let r=t.toError(e);throw a.set(n.failure(s,r)),r}finally{i.finish()}},get state(){return a.value},subscribe:a.subscribe}}exports.createAsyncSource=createAsyncSource;
|
|
2
2
|
//# sourceMappingURL=asyncSource.cjs.map
|
package/dist/asyncSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.cjs","names":["
|
|
1
|
+
{"version":3,"file":"asyncSource.cjs","names":["createSourceStore","createRequestController","SourcererDisposedError","abortable","toError","require_errors","require_requestController","require_sourceStore","_vielzeug_arsenal","store","initial","requests","request","previous","options","result","state","reason","error"],"sources":["../src/asyncSource.ts"],"sourcesContent":["import { abortable } from '@vielzeug/arsenal';\n\nimport { SourcererDisposedError } from './errors';\nimport { createRequestController, toError } from './requestController';\nimport { createSourceStore } from './sourceStore';\n\ntype FetchOptions<TState, TResult> = {\n load(signal: AbortSignal): Promise<TResult>;\n success(result: TResult): TState;\n /** Runs right before the successful state commits; only for the current request. */\n commit?(): void;\n pending(previous: TState): TState;\n failure(previous: TState, error: Error): TState;\n};\n\ntype AsyncSource<TState> = Readonly<{\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n fetch<TResult>(options: FetchOptions<TState, TResult>): Promise<void>;\n readonly state: TState;\n subscribe(listener: (state: TState) => void): () => void;\n}>;\n\n/** Only the current request may commit; superseded work settles without changing source state. */\nexport function createAsyncSource<TState>(initial: TState): AsyncSource<TState> {\n const store = createSourceStore(initial);\n const requests = createRequestController();\n\n return {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose() {\n requests.dispose();\n store.dispose();\n },\n\n get disposed() {\n return store.disposed;\n },\n\n async fetch(options) {\n if (store.disposed) throw new SourcererDisposedError();\n\n const request = requests.begin();\n const previous = store.value;\n store.set(options.pending(previous));\n\n try {\n const result = await abortable(options.load(request.signal), request.signal);\n if (!request.isCurrent() || store.disposed) return;\n\n const state = options.success(result);\n options.commit?.();\n store.set(state);\n } catch (reason: unknown) {\n if (!request.isCurrent() || store.disposed) return;\n\n const error = toError(reason);\n store.set(options.failure(previous, error));\n throw error;\n } finally {\n request.finish();\n }\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n };\n}\n"],"mappings":"A,uIAyBA,SAAgB,kBAA0B,EAAsC,CAC9E,IAAMS,EAAQT,EAAAA,kBAAkBU,CAAO,EACjCC,EAAWV,EAAAA,wBAAwB,EAEzC,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAOQ,EAAM,cACf,EAEA,SAAU,CACRE,EAAS,QAAQ,EACjBF,EAAM,QAAQ,CAChB,EAEA,IAAI,UAAW,CACb,OAAOA,EAAM,QACf,EAEA,MAAM,MAAM,EAAS,CACnB,GAAIA,EAAM,SAAU,MAAM,IAAIP,EAAAA,uBAE9B,IAAMU,EAAUD,EAAS,MAAM,EACzBE,EAAWJ,EAAM,MACvBA,EAAM,IAAIK,EAAQ,QAAQD,CAAQ,CAAC,EAEnC,GAAI,CACF,IAAME,EAAS,MAAA,EAAMZ,EAAAA,UAAAA,CAAUW,EAAQ,KAAKF,EAAQ,MAAM,EAAGA,EAAQ,MAAM,EAC3E,GAAI,CAACA,EAAQ,UAAU,GAAKH,EAAM,SAAU,OAE5C,IAAMO,EAAQF,EAAQ,QAAQC,CAAM,EACpCD,EAAQ,SAAS,EACjBL,EAAM,IAAIO,CAAK,CACjB,OAASC,EAAiB,CACxB,GAAI,CAACL,EAAQ,UAAU,GAAKH,EAAM,SAAU,OAE5C,IAAMS,EAAQd,EAAAA,QAAQa,CAAM,EAE5B,MADAR,EAAM,IAAIK,EAAQ,QAAQD,EAAUK,CAAK,CAAC,EACpCA,CACR,QAAU,CACRN,EAAQ,OAAO,CACjB,CACF,EAEA,IAAI,OAAQ,CACV,OAAOH,EAAM,KACf,EAEA,UAAWA,EAAM,SACnB,CACF,C"}
|
package/dist/asyncSource.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
type FetchOptions<TState, TResult> = {
|
|
2
2
|
load(signal: AbortSignal): Promise<TResult>;
|
|
3
3
|
success(result: TResult): TState;
|
|
4
|
-
|
|
4
|
+
/** Runs right before the successful state commits; only for the current request. */
|
|
5
|
+
commit?(): void;
|
|
5
6
|
pending(previous: TState): TState;
|
|
6
7
|
failure(previous: TState, error: Error): TState;
|
|
7
8
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.d.ts","sourceRoot":"","sources":["../src/asyncSource.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"asyncSource.d.ts","sourceRoot":"","sources":["../src/asyncSource.ts"],"names":[],"mappings":"AAMA,KAAK,YAAY,CAAC,MAAM,EAAE,OAAO,IAAI;IACnC,IAAI,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5C,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC;IACjC,oFAAoF;IACpF,MAAM,CAAC,IAAI,IAAI,CAAC;IAChB,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IAClC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,CAAC;CACjD,CAAC;AAEF,KAAK,WAAW,CAAC,MAAM,IAAI,QAAQ,CAAC;IAClC,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC;IACrC,OAAO,IAAI,IAAI,CAAC;IAChB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CAC1D,CAAC,CAAC;AAEH,kGAAkG;AAClG,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAiD9E"}
|
package/dist/asyncSource.js
CHANGED
|
@@ -1,61 +1,41 @@
|
|
|
1
1
|
import { SourcererDisposedError as e } from "./errors.js";
|
|
2
2
|
import { createRequestController as t, toError as n } from "./requestController.js";
|
|
3
3
|
import { createSourceStore as r } from "./sourceStore.js";
|
|
4
|
-
import {
|
|
4
|
+
import { abortable as i } from "@vielzeug/arsenal";
|
|
5
5
|
// src/asyncSource.ts
|
|
6
|
-
|
|
7
|
-
let
|
|
8
|
-
if (t.aborted) {
|
|
9
|
-
abort();
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
t.addEventListener("abort", abort, { once: !0 });
|
|
13
|
-
let a;
|
|
14
|
-
try {
|
|
15
|
-
a = e(t);
|
|
16
|
-
} catch (e) {
|
|
17
|
-
t.removeEventListener("abort", abort), r(e);
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
a.then((e) => {
|
|
21
|
-
t.removeEventListener("abort", abort), n(e);
|
|
22
|
-
}, (e) => {
|
|
23
|
-
t.removeEventListener("abort", abort), r(e);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
function createAsyncSource(i) {
|
|
27
|
-
let a = r(i), o = t();
|
|
6
|
+
function createAsyncSource(a) {
|
|
7
|
+
let o = r(a), s = t();
|
|
28
8
|
return {
|
|
29
9
|
get disposalSignal() {
|
|
30
|
-
return
|
|
10
|
+
return o.disposalSignal;
|
|
31
11
|
},
|
|
32
12
|
dispose() {
|
|
33
|
-
|
|
13
|
+
s.dispose(), o.dispose();
|
|
34
14
|
},
|
|
35
15
|
get disposed() {
|
|
36
|
-
return
|
|
16
|
+
return o.disposed;
|
|
37
17
|
},
|
|
38
18
|
async fetch(t) {
|
|
39
|
-
if (
|
|
40
|
-
let r =
|
|
41
|
-
|
|
19
|
+
if (o.disposed) throw new e();
|
|
20
|
+
let r = s.begin(), a = o.value;
|
|
21
|
+
o.set(t.pending(a));
|
|
42
22
|
try {
|
|
43
|
-
let e = await
|
|
44
|
-
if (!r.isCurrent() ||
|
|
23
|
+
let e = await i(t.load(r.signal), r.signal);
|
|
24
|
+
if (!r.isCurrent() || o.disposed) return;
|
|
45
25
|
let n = t.success(e);
|
|
46
|
-
t.commit?.(
|
|
26
|
+
t.commit?.(), o.set(n);
|
|
47
27
|
} catch (e) {
|
|
48
|
-
if (!r.isCurrent() ||
|
|
49
|
-
let
|
|
50
|
-
throw
|
|
28
|
+
if (!r.isCurrent() || o.disposed) return;
|
|
29
|
+
let i = n(e);
|
|
30
|
+
throw o.set(t.failure(a, i)), i;
|
|
51
31
|
} finally {
|
|
52
32
|
r.finish();
|
|
53
33
|
}
|
|
54
34
|
},
|
|
55
35
|
get state() {
|
|
56
|
-
return
|
|
36
|
+
return o.value;
|
|
57
37
|
},
|
|
58
|
-
subscribe:
|
|
38
|
+
subscribe: o.subscribe
|
|
59
39
|
};
|
|
60
40
|
}
|
|
61
41
|
export { createAsyncSource };
|
package/dist/asyncSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.js","names":["
|
|
1
|
+
{"version":3,"file":"asyncSource.js","names":["store","createSourceStore","initial","requests","createRequestController","SourcererDisposedError","request","previous","options","result","abortable","state","reason","error","toError"],"sources":["../src/asyncSource.ts"],"sourcesContent":["import { abortable } from '@vielzeug/arsenal';\n\nimport { SourcererDisposedError } from './errors';\nimport { createRequestController, toError } from './requestController';\nimport { createSourceStore } from './sourceStore';\n\ntype FetchOptions<TState, TResult> = {\n load(signal: AbortSignal): Promise<TResult>;\n success(result: TResult): TState;\n /** Runs right before the successful state commits; only for the current request. */\n commit?(): void;\n pending(previous: TState): TState;\n failure(previous: TState, error: Error): TState;\n};\n\ntype AsyncSource<TState> = Readonly<{\n readonly disposalSignal: AbortSignal;\n dispose(): void;\n readonly disposed: boolean;\n fetch<TResult>(options: FetchOptions<TState, TResult>): Promise<void>;\n readonly state: TState;\n subscribe(listener: (state: TState) => void): () => void;\n}>;\n\n/** Only the current request may commit; superseded work settles without changing source state. */\nexport function createAsyncSource<TState>(initial: TState): AsyncSource<TState> {\n const store = createSourceStore(initial);\n const requests = createRequestController();\n\n return {\n get disposalSignal() {\n return store.disposalSignal;\n },\n\n dispose() {\n requests.dispose();\n store.dispose();\n },\n\n get disposed() {\n return store.disposed;\n },\n\n async fetch(options) {\n if (store.disposed) throw new SourcererDisposedError();\n\n const request = requests.begin();\n const previous = store.value;\n store.set(options.pending(previous));\n\n try {\n const result = await abortable(options.load(request.signal), request.signal);\n if (!request.isCurrent() || store.disposed) return;\n\n const state = options.success(result);\n options.commit?.();\n store.set(state);\n } catch (reason: unknown) {\n if (!request.isCurrent() || store.disposed) return;\n\n const error = toError(reason);\n store.set(options.failure(previous, error));\n throw error;\n } finally {\n request.finish();\n }\n },\n\n get state() {\n return store.value;\n },\n\n subscribe: store.subscribe,\n };\n}\n"],"mappings":";;;;;AAyBA,SAAgB,kBAA0B,GAAsC;CAC9E,IAAMA,IAAQC,EAAkBC,CAAO,GACjCC,IAAWC,EAAwB;CAEzC,OAAO;EACL,IAAI,iBAAiB;GACnB,OAAOJ,EAAM;EACf;EAEA,UAAU;GAER,AADAG,EAAS,QAAQ,GACjBH,EAAM,QAAQ;EAChB;EAEA,IAAI,WAAW;GACb,OAAOA,EAAM;EACf;EAEA,MAAM,MAAM,GAAS;GACnB,IAAIA,EAAM,UAAU,MAAM,IAAIK,EAAuB;GAErD,IAAMC,IAAUH,EAAS,MAAM,GACzBI,IAAWP,EAAM;GACvB,EAAM,IAAIQ,EAAQ,QAAQD,CAAQ,CAAC;GAEnC,IAAI;IACF,IAAME,IAAS,MAAMC,EAAUF,EAAQ,KAAKF,EAAQ,MAAM,GAAGA,EAAQ,MAAM;IAC3E,IAAI,CAACA,EAAQ,UAAU,KAAKN,EAAM,UAAU;IAE5C,IAAMW,IAAQH,EAAQ,QAAQC,CAAM;IAEpC,AADAD,EAAQ,SAAS,GACjBR,EAAM,IAAIW,CAAK;GACjB,SAASC,GAAiB;IACxB,IAAI,CAACN,EAAQ,UAAU,KAAKN,EAAM,UAAU;IAE5C,IAAMa,IAAQC,EAAQF,CAAM;IAE5B,MADAZ,EAAM,IAAIQ,EAAQ,QAAQD,GAAUM,CAAK,CAAC,GACpCA;GACR,UAAU;IACR,EAAQ,OAAO;GACjB;EACF;EAEA,IAAI,QAAQ;GACV,OAAOb,EAAM;EACf;EAEA,WAAWA,EAAM;CACnB;AACF"}
|
package/dist/cursorSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t)=>({...t?.nextCursor!==void 0&&{nextCursor:t.nextCursor},pageSize:e,...t?.previousCursor!==void 0&&{previousCursor:t.previousCursor},...t?.totalItems!==void 0&&{totalItems:n.totalItems(t.totalItems)}});function createCursorSource(r){if(r.after!==void 0&&r.before!==void 0)throw new t.SourcererConfigurationError(`Cursor source cannot start with both after and before`);let i={...r.after!==void 0&&{after:r.after},...r.before!==void 0&&{before:r.before},pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(
|
|
1
|
+
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t)=>({...t?.nextCursor!==void 0&&{nextCursor:t.nextCursor},pageSize:e,...t?.previousCursor!==void 0&&{previousCursor:t.previousCursor},...t?.totalItems!==void 0&&{totalItems:n.totalItems(t.totalItems)}});function createCursorSource(r){if(r.after!==void 0&&r.before!==void 0)throw new t.SourcererConfigurationError(`Cursor source cannot start with both after and before`);let i={...r.after!==void 0&&{after:r.after},...r.before!==void 0&&{before:r.before},pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=i,o=i,s=!1,c=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(a.pageSize),params:a.params}),assertLive=()=>{if(c.disposed)throw new t.SourcererDisposedError},ensureLoaded=async()=>{!c.state.loading&&!s&&await fetch(a)},fetch=e=>c.fetch({commit:()=>{s=!0,o=e,a=e},failure:(e,t)=>(a=o,{...e,error:t,loading:!1,pendingParams:void 0}),load:t=>r.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...n.sameParams(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:createPagination(e.pageSize,t),params:e.params})}),l={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},async next(){assertLive();let e=c.state.pagination.nextCursor;c.state.loading||e===void 0||(a={after:e,pageSize:a.pageSize,params:a.params},await fetch(a))},async previous(){assertLive();let e=c.state.pagination.previousCursor;c.state.loading||e===void 0||(a={before:e,pageSize:a.pageSize,params:a.params},await fetch(a))},reload(){return fetch(a)},async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`),r=a.after===void 0&&a.before===void 0;t===a.pageSize&&r?await ensureLoaded():(a={pageSize:t,params:a.params},await fetch(a))},async setParams(e){assertLive(),n.sameParams(e,a.params)?await ensureLoaded():(a={pageSize:a.pageSize,params:e},await fetch(a))},get state(){return c.state},subscribe:c.subscribe,[Symbol.dispose](){l.dispose()}};return l}exports.createCursorSource=createCursorSource;
|
|
2
2
|
//# sourceMappingURL=cursorSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.cjs","names":["totalItems","SourcererConfigurationError","positiveInteger","createAsyncSource","SourcererDisposedError","require_asyncSource","require_errors","require_pagination","result","config","requested","
|
|
1
|
+
{"version":3,"file":"cursorSource.cjs","names":["totalItems","SourcererConfigurationError","positiveInteger","createAsyncSource","SourcererDisposedError","sameParams","require_asyncSource","require_errors","require_pagination","result","config","initial","requested","committed","hasLoaded","asyncSource","request","previous","source","cursor","normalized","pageSize","onFirstPage","params"],"sources":["../src/cursorSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererConfigurationError, SourcererDisposedError } from './errors';\nimport { positiveInteger, sameParams, totalItems } from './pagination';\nimport type { CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState } from './types';\n\ntype Request<TParams, TCursor> = Readonly<{\n after?: TCursor;\n before?: TCursor;\n pageSize: number;\n params: TParams;\n}>;\n\nconst createPagination = <TCursor>(\n pageSize: number,\n result?: CursorResult<unknown, TCursor>,\n): CursorPagination<TCursor> => ({\n ...(result?.nextCursor !== undefined && { nextCursor: result.nextCursor }),\n pageSize,\n ...(result?.previousCursor !== undefined && { previousCursor: result.previousCursor }),\n ...(result?.totalItems !== undefined && { totalItems: totalItems(result.totalItems) }),\n});\n\n/** Cursor sources retain committed items and cursors while replacement work is pending. */\nexport function createCursorSource<T, TParams = undefined, TCursor = string>(\n config: CursorSourceConfig<T, TParams, TCursor>,\n): CursorSource<T, TParams, TCursor> {\n if (config.after !== undefined && config.before !== undefined) {\n throw new SourcererConfigurationError('Cursor source cannot start with both after and before');\n }\n\n const initial: Request<TParams, TCursor> = {\n ...(config.after !== undefined && { after: config.after }),\n ...(config.before !== undefined && { before: config.before }),\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let requested: Request<TParams, TCursor> = initial;\n let committed: Request<TParams, TCursor> = initial;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<CursorSourceState<T, TParams, TCursor>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(requested.pageSize),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const ensureLoaded = async (): Promise<void> => {\n // While idle, `requested` always matches the committed request, so the only\n // missing work is the first load (which a failure rolls back to).\n if (!asyncSource.state.loading && !hasLoaded) await fetch(requested);\n };\n\n const fetch = (request: Request<TParams, TCursor>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n committed = request;\n requested = request;\n },\n // Only the current request reaches `failure` (superseded work settles silently),\n // so the desired request rolls back and never mixes new params with stale cursors.\n failure: (previous, error) => {\n requested = committed;\n return { ...previous, error, loading: false, pendingParams: undefined };\n },\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(sameParams(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagination(request.pageSize, result),\n params: request.params,\n }),\n });\n\n const source: CursorSource<T, TParams, TCursor> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async next() {\n assertLive();\n const cursor = asyncSource.state.pagination.nextCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { after: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n async previous() {\n assertLive();\n const cursor = asyncSource.state.pagination.previousCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { before: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n const onFirstPage = requested.after === undefined && requested.before === undefined;\n if (normalized === requested.pageSize && onFirstPage) {\n await ensureLoaded();\n return;\n }\n requested = { pageSize: normalized, params: requested.params };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (sameParams(params, requested.params)) {\n await ensureLoaded();\n return;\n }\n requested = { pageSize: requested.pageSize, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"A,6FAYA,IAAM,kBACJ,EACA,KAC+B,CAC/B,GAAIS,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYA,EAAO,UAAW,EACxE,WACA,GAAIA,GAAQ,iBAAmB,IAAA,IAAa,CAAE,eAAgBA,EAAO,cAAe,EACpF,GAAIA,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYT,EAAAA,WAAWS,EAAO,UAAU,CAAE,CACtF,GAGA,SAAgB,mBACd,EACmC,CACnC,GAAIC,EAAO,QAAU,IAAA,IAAaA,EAAO,SAAW,IAAA,GAClD,MAAM,IAAIT,EAAAA,4BAA4B,uDAAuD,EAG/F,IAAMU,EAAqC,CACzC,GAAID,EAAO,QAAU,IAAA,IAAa,CAAE,MAAOA,EAAO,KAAM,EACxD,GAAIA,EAAO,SAAW,IAAA,IAAa,CAAE,OAAQA,EAAO,MAAO,EAC3D,SAAUR,EAAAA,gBAAgBQ,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACIE,EAAuCD,EACvCE,EAAuCF,EACvCG,EAAY,GACVC,EAAcZ,EAAAA,kBAA0D,CAC5E,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiBS,EAAU,QAAQ,EAC/C,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GA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CACP,OAAO,MAAMA,CAAS,CACxB,EAEA,MAAM,YAAY,EAAU,CAC1B,WAAW,EACX,IAAMQ,EAAalB,EAAAA,gBAAgBmB,EAAU,UAAU,EACjDC,EAAcV,EAAU,QAAU,IAAA,IAAaA,EAAU,SAAW,IAAA,GACtEQ,IAAeR,EAAU,UAAYU,EACvC,MAAM,aAAa,GAGrB,EAAY,CAAE,SAAUF,EAAY,OAAQR,EAAU,MAAO,EAC7D,MAAM,MAAMA,CAAS,EACvB,EAEA,MAAM,UAAU,EAAQ,CACtB,WAAW,EACPP,EAAAA,WAAWkB,EAAQX,EAAU,MAAM,EACrC,MAAM,aAAa,GAGrB,EAAY,CAAE,SAAUA,EAAU,SAAU,QAAO,EACnD,MAAM,MAAMA,CAAS,EACvB,EAEA,IAAI,OAAQ,CACV,OAAOG,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBG,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.d.ts","sourceRoot":"","sources":["../src/cursorSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAkC,YAAY,EAAE,kBAAkB,EAAqB,MAAM,SAAS,CAAC;AAmBnH,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,EAAE,OAAO,GAAG,MAAM,EACzE,MAAM,EAAE,kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,GAC9C,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"cursorSource.d.ts","sourceRoot":"","sources":["../src/cursorSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAkC,YAAY,EAAE,kBAAkB,EAAqB,MAAM,SAAS,CAAC;AAmBnH,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,EAAE,OAAO,GAAG,MAAM,EACzE,MAAM,EAAE,kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,GAC9C,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CA4HnC"}
|
package/dist/cursorSource.js
CHANGED
|
@@ -1,36 +1,41 @@
|
|
|
1
1
|
import { createAsyncSource as e } from "./asyncSource.js";
|
|
2
2
|
import { SourcererConfigurationError as t, SourcererDisposedError as n } from "./errors.js";
|
|
3
|
-
import { positiveInteger as r,
|
|
3
|
+
import { positiveInteger as r, sameParams as i, totalItems as a } from "./pagination.js";
|
|
4
4
|
// src/cursorSource.ts
|
|
5
5
|
var createPagination = (e, t) => ({
|
|
6
6
|
...t?.nextCursor !== void 0 && { nextCursor: t.nextCursor },
|
|
7
7
|
pageSize: e,
|
|
8
8
|
...t?.previousCursor !== void 0 && { previousCursor: t.previousCursor },
|
|
9
|
-
...t?.totalItems !== void 0 && { totalItems:
|
|
9
|
+
...t?.totalItems !== void 0 && { totalItems: a(t.totalItems) }
|
|
10
10
|
});
|
|
11
|
-
function createCursorSource(
|
|
12
|
-
if (
|
|
13
|
-
let
|
|
14
|
-
...
|
|
15
|
-
...
|
|
16
|
-
pageSize: r(
|
|
17
|
-
params:
|
|
18
|
-
}, o = e({
|
|
11
|
+
function createCursorSource(a) {
|
|
12
|
+
if (a.after !== void 0 && a.before !== void 0) throw new t("Cursor source cannot start with both after and before");
|
|
13
|
+
let o = {
|
|
14
|
+
...a.after !== void 0 && { after: a.after },
|
|
15
|
+
...a.before !== void 0 && { before: a.before },
|
|
16
|
+
pageSize: r(a.pageSize ?? 20, "pageSize"),
|
|
17
|
+
params: a.params
|
|
18
|
+
}, s = o, c = o, l = !1, u = e({
|
|
19
19
|
error: null,
|
|
20
20
|
items: [],
|
|
21
21
|
loading: !1,
|
|
22
|
-
pagination: createPagination(
|
|
23
|
-
params:
|
|
22
|
+
pagination: createPagination(s.pageSize),
|
|
23
|
+
params: s.params
|
|
24
24
|
}), assertLive = () => {
|
|
25
|
-
if (
|
|
26
|
-
},
|
|
27
|
-
|
|
25
|
+
if (u.disposed) throw new n();
|
|
26
|
+
}, ensureLoaded = async () => {
|
|
27
|
+
!u.state.loading && !l && await fetch(s);
|
|
28
|
+
}, fetch = (e) => u.fetch({
|
|
29
|
+
commit: () => {
|
|
30
|
+
l = !0, c = e, s = e;
|
|
31
|
+
},
|
|
32
|
+
failure: (e, t) => (s = c, {
|
|
28
33
|
...e,
|
|
29
34
|
error: t,
|
|
30
35
|
loading: !1,
|
|
31
36
|
pendingParams: void 0
|
|
32
37
|
}),
|
|
33
|
-
load: (t) =>
|
|
38
|
+
load: (t) => a.load({
|
|
34
39
|
...e,
|
|
35
40
|
signal: t
|
|
36
41
|
}),
|
|
@@ -38,7 +43,7 @@ function createCursorSource(i) {
|
|
|
38
43
|
...t,
|
|
39
44
|
error: null,
|
|
40
45
|
loading: !0,
|
|
41
|
-
...
|
|
46
|
+
...i(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
42
47
|
}),
|
|
43
48
|
success: (t) => ({
|
|
44
49
|
error: null,
|
|
@@ -47,61 +52,58 @@ function createCursorSource(i) {
|
|
|
47
52
|
pagination: createPagination(e.pageSize, t),
|
|
48
53
|
params: e.params
|
|
49
54
|
})
|
|
50
|
-
}),
|
|
55
|
+
}), d = {
|
|
51
56
|
get disposalSignal() {
|
|
52
|
-
return
|
|
57
|
+
return u.disposalSignal;
|
|
53
58
|
},
|
|
54
|
-
dispose:
|
|
59
|
+
dispose: u.dispose,
|
|
55
60
|
get disposed() {
|
|
56
|
-
return
|
|
61
|
+
return u.disposed;
|
|
57
62
|
},
|
|
58
63
|
async next() {
|
|
59
64
|
assertLive();
|
|
60
|
-
let e =
|
|
61
|
-
|
|
65
|
+
let e = u.state.pagination.nextCursor;
|
|
66
|
+
u.state.loading || e === void 0 || (s = {
|
|
62
67
|
after: e,
|
|
63
|
-
pageSize:
|
|
64
|
-
params:
|
|
65
|
-
}, await fetch(
|
|
68
|
+
pageSize: s.pageSize,
|
|
69
|
+
params: s.params
|
|
70
|
+
}, await fetch(s));
|
|
66
71
|
},
|
|
67
72
|
async previous() {
|
|
68
73
|
assertLive();
|
|
69
|
-
let e =
|
|
70
|
-
|
|
74
|
+
let e = u.state.pagination.previousCursor;
|
|
75
|
+
u.state.loading || e === void 0 || (s = {
|
|
71
76
|
before: e,
|
|
72
|
-
pageSize:
|
|
73
|
-
params:
|
|
74
|
-
}, await fetch(
|
|
77
|
+
pageSize: s.pageSize,
|
|
78
|
+
params: s.params
|
|
79
|
+
}, await fetch(s));
|
|
75
80
|
},
|
|
76
81
|
reload() {
|
|
77
|
-
return fetch(
|
|
82
|
+
return fetch(s);
|
|
78
83
|
},
|
|
79
84
|
async setPageSize(e) {
|
|
80
85
|
assertLive();
|
|
81
|
-
let t = r(e, "pageSize");
|
|
82
|
-
|
|
83
|
-
if (o.state.loading || o.state.pagination.pageSize === t && Object.is(o.state.params, a.params)) return;
|
|
84
|
-
await fetch(a);
|
|
85
|
-
} else a = {
|
|
86
|
+
let t = r(e, "pageSize"), n = s.after === void 0 && s.before === void 0;
|
|
87
|
+
t === s.pageSize && n ? await ensureLoaded() : (s = {
|
|
86
88
|
pageSize: t,
|
|
87
|
-
params:
|
|
88
|
-
}, await fetch(
|
|
89
|
+
params: s.params
|
|
90
|
+
}, await fetch(s));
|
|
89
91
|
},
|
|
90
92
|
async setParams(e) {
|
|
91
|
-
assertLive(),
|
|
92
|
-
pageSize:
|
|
93
|
+
assertLive(), i(e, s.params) ? await ensureLoaded() : (s = {
|
|
94
|
+
pageSize: s.pageSize,
|
|
93
95
|
params: e
|
|
94
|
-
}, await fetch(
|
|
96
|
+
}, await fetch(s));
|
|
95
97
|
},
|
|
96
98
|
get state() {
|
|
97
|
-
return
|
|
99
|
+
return u.state;
|
|
98
100
|
},
|
|
99
|
-
subscribe:
|
|
101
|
+
subscribe: u.subscribe,
|
|
100
102
|
[Symbol.dispose]() {
|
|
101
|
-
|
|
103
|
+
d.dispose();
|
|
102
104
|
}
|
|
103
105
|
};
|
|
104
|
-
return
|
|
106
|
+
return d;
|
|
105
107
|
}
|
|
106
108
|
export { createCursorSource };
|
|
107
109
|
|
package/dist/cursorSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.js","names":["result","totalItems","config","SourcererConfigurationError","
|
|
1
|
+
{"version":3,"file":"cursorSource.js","names":["result","totalItems","config","SourcererConfigurationError","initial","positiveInteger","requested","committed","hasLoaded","asyncSource","createAsyncSource","SourcererDisposedError","request","previous","sameParams","source","cursor","normalized","pageSize","onFirstPage","params"],"sources":["../src/cursorSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererConfigurationError, SourcererDisposedError } from './errors';\nimport { positiveInteger, sameParams, totalItems } from './pagination';\nimport type { CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState } from './types';\n\ntype Request<TParams, TCursor> = Readonly<{\n after?: TCursor;\n before?: TCursor;\n pageSize: number;\n params: TParams;\n}>;\n\nconst createPagination = <TCursor>(\n pageSize: number,\n result?: CursorResult<unknown, TCursor>,\n): CursorPagination<TCursor> => ({\n ...(result?.nextCursor !== undefined && { nextCursor: result.nextCursor }),\n pageSize,\n ...(result?.previousCursor !== undefined && { previousCursor: result.previousCursor }),\n ...(result?.totalItems !== undefined && { totalItems: totalItems(result.totalItems) }),\n});\n\n/** Cursor sources retain committed items and cursors while replacement work is pending. */\nexport function createCursorSource<T, TParams = undefined, TCursor = string>(\n config: CursorSourceConfig<T, TParams, TCursor>,\n): CursorSource<T, TParams, TCursor> {\n if (config.after !== undefined && config.before !== undefined) {\n throw new SourcererConfigurationError('Cursor source cannot start with both after and before');\n }\n\n const initial: Request<TParams, TCursor> = {\n ...(config.after !== undefined && { after: config.after }),\n ...(config.before !== undefined && { before: config.before }),\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let requested: Request<TParams, TCursor> = initial;\n let committed: Request<TParams, TCursor> = initial;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<CursorSourceState<T, TParams, TCursor>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(requested.pageSize),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n const ensureLoaded = async (): Promise<void> => {\n // While idle, `requested` always matches the committed request, so the only\n // missing work is the first load (which a failure rolls back to).\n if (!asyncSource.state.loading && !hasLoaded) await fetch(requested);\n };\n\n const fetch = (request: Request<TParams, TCursor>): Promise<void> =>\n asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n committed = request;\n requested = request;\n },\n // Only the current request reaches `failure` (superseded work settles silently),\n // so the desired request rolls back and never mixes new params with stale cursors.\n failure: (previous, error) => {\n requested = committed;\n return { ...previous, error, loading: false, pendingParams: undefined };\n },\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(sameParams(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagination(request.pageSize, result),\n params: request.params,\n }),\n });\n\n const source: CursorSource<T, TParams, TCursor> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async next() {\n assertLive();\n const cursor = asyncSource.state.pagination.nextCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { after: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n async previous() {\n assertLive();\n const cursor = asyncSource.state.pagination.previousCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { before: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n const onFirstPage = requested.after === undefined && requested.before === undefined;\n if (normalized === requested.pageSize && onFirstPage) {\n await ensureLoaded();\n return;\n }\n requested = { pageSize: normalized, params: requested.params };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (sameParams(params, requested.params)) {\n await ensureLoaded();\n return;\n }\n requested = { pageSize: requested.pageSize, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":";;;;AAYA,IAAM,oBACJ,GACA,OAC+B;CAC/B,GAAIA,GAAQ,eAAe,KAAA,KAAa,EAAE,YAAYA,EAAO,WAAW;CACxE;CACA,GAAIA,GAAQ,mBAAmB,KAAA,KAAa,EAAE,gBAAgBA,EAAO,eAAe;CACpF,GAAIA,GAAQ,eAAe,KAAA,KAAa,EAAE,YAAYC,EAAWD,EAAO,UAAU,EAAE;AACtF;AAGA,SAAgB,mBACd,GACmC;CACnC,IAAIE,EAAO,UAAU,KAAA,KAAaA,EAAO,WAAW,KAAA,GAClD,MAAM,IAAIC,EAA4B,uDAAuD;CAG/F,IAAMC,IAAqC;EACzC,GAAIF,EAAO,UAAU,KAAA,KAAa,EAAE,OAAOA,EAAO,MAAM;EACxD,GAAIA,EAAO,WAAW,KAAA,KAAa,EAAE,QAAQA,EAAO,OAAO;EAC3D,UAAUG,EAAgBH,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQA,EAAO;CACjB,GACII,IAAuCF,GACvCG,IAAuCH,GACvCI,IAAY,IACVC,IAAcC,EAA0D;EAC5E,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,iBAAiBJ,EAAU,QAAQ;EAC/C,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAIG,EAAY,UAAU,MAAM,IAAIE,EAAuB;CAC7D,GACM,eAAe,YAA2B;EAG9C,AAAI,CAACF,EAAY,M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,IAAMW,IAAaZ,EAAgBa,GAAU,UAAU,GACjDC,IAAcb,EAAU,UAAU,KAAA,KAAaA,EAAU,WAAW,KAAA;GAC1E,AAAIW,MAAeX,EAAU,YAAYa,IACvC,MAAM,aAAa,KAGrB,IAAY;IAAE,UAAUF;IAAY,QAAQX,EAAU;GAAO,GAC7D,MAAM,MAAMA,CAAS;EACvB;EAEA,MAAM,UAAU,GAAQ;GAEtB,AADA,WAAW,GACPQ,EAAWM,GAAQd,EAAU,MAAM,IACrC,MAAM,aAAa,KAGrB,IAAY;IAAE,UAAUA,EAAU;IAAU;GAAO,GACnD,MAAM,MAAMA,CAAS;EACvB;EAEA,IAAI,QAAQ;GACV,OAAOG,EAAY;EACrB;EAEA,WAAWA,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAOM;AACT"}
|
package/dist/infiniteSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function createInfiniteSource(r){let i={pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=1,
|
|
1
|
+
const e=require("./asyncSource.cjs"),t=require("./errors.cjs"),n=require("./pagination.cjs");var createPagination=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function createInfiniteSource(r){let i={pageSize:n.positiveInteger(r.pageSize??20,`pageSize`),params:r.params},a=i,o=i,s=1,c=1,l=!1,u=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(0,a.pageSize,0),params:a.params}),assertLive=()=>{if(u.disposed)throw new t.SourcererDisposedError},fetch=(e,t,i)=>u.fetch({commit:()=>{l=!0,o=e,c=t+1,a=e,s=t+1},failure:(e,t)=>(a=o,s=c,{...e,error:t,loading:!1,pendingParams:void 0}),load:n=>r.load({page:t,pageSize:e.pageSize,params:e.params,signal:n}),pending:t=>({...t,error:null,loading:!0,...i||n.sameParams(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>{let r=i?[...u.state.items,...t.items]:[...t.items];return{error:null,items:r,loading:!1,pagination:createPagination(r.length,e.pageSize,n.totalItems(t.totalItems)),params:e.params}}}),reload=()=>fetch(a,1,!1),ensureLoaded=async()=>{!u.state.loading&&!l&&await reload()},d={get disposalSignal(){return u.disposalSignal},dispose:u.dispose,get disposed(){return u.disposed},async loadMore(){assertLive(),u.state.loading||(l?u.state.pagination.hasMore&&await fetch(a,s,!0):await reload())},reload,async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);t===a.pageSize?await ensureLoaded():(a={...a,pageSize:t},await reload())},async setParams(e){assertLive(),n.sameParams(e,a.params)?await ensureLoaded():(a={...a,params:e},await reload())},get state(){return u.state},subscribe:u.subscribe,[Symbol.dispose](){d.dispose()}};return d}exports.createInfiniteSource=createInfiniteSource;
|
|
2
2
|
//# sourceMappingURL=infiniteSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteSource.cjs","names":["positiveInteger","createAsyncSource","SourcererDisposedError","totalItems","require_asyncSource","require_errors","require_pagination","loadedItems","total","
|
|
1
|
+
{"version":3,"file":"infiniteSource.cjs","names":["positiveInteger","createAsyncSource","SourcererDisposedError","sameParams","totalItems","require_asyncSource","require_errors","require_pagination","loadedItems","total","initial","config","requested","committed","nextPage","committedNextPage","hasLoaded","asyncSource","request","page","previous","append","items","result","source","normalized","pageSize","params"],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, sameParams, 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 const initial: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let requested: Request<TParams> = initial;\n let committed: Request<TParams> = initial;\n let nextPage = 1;\n let committedNextPage = 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 asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n committed = request;\n committedNextPage = page + 1;\n requested = request;\n nextPage = page + 1;\n },\n // Only the current request reaches `failure` (superseded work settles silently),\n // so the desired request rolls back to what the published feed still shows.\n failure: (previous, error) => {\n requested = committed;\n nextPage = committedNextPage;\n return { ...previous, error, loading: false, pendingParams: undefined };\n },\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 || sameParams(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...asyncSource.state.items, ...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 const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const ensureLoaded = async (): Promise<void> => {\n // While idle, `requested` always matches the committed feed, so the only\n // missing work is the first load (which a failure rolls back to).\n if (!asyncSource.state.loading && !hasLoaded) await reload();\n };\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) {\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 await ensureLoaded();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (sameParams(params, requested.params)) {\n await ensureLoaded();\n return;\n }\n requested = { ...requested, params };\n await reload();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"A,6FAOA,IAAM,kBAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAASQ,EAAcC,EACvB,cACA,WACA,WAAYA,CACd,GAGA,SAAgB,qBACd,EAC4B,CAC5B,IAAMC,EAA4B,CAChC,SAAUV,EAAAA,gBAAgBW,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACIC,EAA8BF,EAC9BG,EAA8BH,EAC9BI,EAAW,EACXC,EAAoB,EACpBC,EAAY,GACVC,EAAchB,EAAAA,kBAAmD,CACrE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiB,EAAGW,EAAU,SAAU,CAAC,EACrD,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAIK,EAAY,SAAU,MAAM,IAAIf,EAAAA,sBACtC,EAEM,OAAS,EAA2B,EAAc,IACtDe,EAAY,MAAM,CAChB,WAAc,CACZ,EAAY,GACZ,EAAYC,EACZ,EAAoBC,EAAO,EAC3B,EAAYD,EACZ,EAAWC,EAAO,CACpB,EAGA,SAAU,EAAU,KAClB,EAAYN,EACZ,EAAWE,EACJ,CAAE,G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,EAAAA,WAAWwB,EAAQf,EAAU,MAAM,EACrC,MAAM,aAAa,GAGrB,EAAY,CAAE,GAAGA,EAAW,QAAO,EACnC,MAAM,OAAO,EACf,EAEA,IAAI,OAAQ,CACV,OAAOK,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBO,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,CAwH5B"}
|