@vielzeug/sourcerer 26.11.11 → 26.11.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/asyncSource.cjs +1 -1
  2. package/dist/asyncSource.cjs.map +1 -1
  3. package/dist/asyncSource.d.ts +2 -1
  4. package/dist/asyncSource.d.ts.map +1 -1
  5. package/dist/asyncSource.js +17 -37
  6. package/dist/asyncSource.js.map +1 -1
  7. package/dist/cursorSource.cjs +1 -1
  8. package/dist/cursorSource.cjs.map +1 -1
  9. package/dist/cursorSource.d.ts.map +1 -1
  10. package/dist/cursorSource.js +48 -46
  11. package/dist/cursorSource.js.map +1 -1
  12. package/dist/infiniteSource.cjs +1 -1
  13. package/dist/infiniteSource.cjs.map +1 -1
  14. package/dist/infiniteSource.d.ts.map +1 -1
  15. package/dist/infiniteSource.js +57 -61
  16. package/dist/infiniteSource.js.map +1 -1
  17. package/dist/localSource.cjs +1 -1
  18. package/dist/localSource.cjs.map +1 -1
  19. package/dist/localSource.d.ts.map +1 -1
  20. package/dist/localSource.js +31 -30
  21. package/dist/localSource.js.map +1 -1
  22. package/dist/pageSource.cjs +1 -1
  23. package/dist/pageSource.cjs.map +1 -1
  24. package/dist/pageSource.d.ts.map +1 -1
  25. package/dist/pageSource.js +38 -37
  26. package/dist/pageSource.js.map +1 -1
  27. package/dist/pagination.cjs +1 -1
  28. package/dist/pagination.cjs.map +1 -1
  29. package/dist/pagination.d.ts +2 -0
  30. package/dist/pagination.d.ts.map +1 -1
  31. package/dist/pagination.js +5 -1
  32. package/dist/pagination.js.map +1 -1
  33. package/dist/resourceSource.cjs +1 -1
  34. package/dist/resourceSource.cjs.map +1 -1
  35. package/dist/resourceSource.d.ts.map +1 -1
  36. package/dist/resourceSource.js +12 -15
  37. package/dist/resourceSource.js.map +1 -1
  38. package/dist/sourcerer.cjs +1 -1
  39. package/dist/sourcerer.cjs.map +1 -1
  40. package/dist/sourcerer.iife.js +1 -1
  41. package/dist/sourcerer.iife.js.map +1 -1
  42. package/dist/sourcerer.js +173 -192
  43. package/dist/sourcerer.js.map +1 -1
  44. package/package.json +2 -2
@@ -1,2 +1,2 @@
1
- const e=require("./errors.cjs"),t=require("./requestController.cjs"),n=require("./sourceStore.cjs");let r=require("@vielzeug/arsenal");var loadWithSignal=(e,t)=>new Promise((n,i)=>{let abort=()=>i((0,r.abortError)(t,`The operation was aborted.`));if(t.aborted){abort();return}t.addEventListener(`abort`,abort,{once:!0});let a;try{a=e(t)}catch(e){t.removeEventListener(`abort`,abort),i(e);return}a.then(e=>{t.removeEventListener(`abort`,abort),n(e)},e=>{t.removeEventListener(`abort`,abort),i(e)})});function createAsyncSource(r){let i=n.createSourceStore(r),a=t.createRequestController();return{get disposalSignal(){return i.disposalSignal},dispose(){a.dispose(),i.dispose()},get disposed(){return i.disposed},async fetch(n){if(i.disposed)throw new e.SourcererDisposedError;let r=a.begin(),o=i.value;i.set(n.pending(o));try{let e=await loadWithSignal(n.load,r.signal);if(!r.isCurrent()||i.disposed)return;let t=n.success(e);n.commit?.(t,e),i.set(t)}catch(e){if(!r.isCurrent()||i.disposed)return;let a=t.toError(e);throw i.set(n.failure(o,a)),a}finally{r.finish()}},get state(){return i.value},subscribe:i.subscribe}}exports.createAsyncSource=createAsyncSource;
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
@@ -1 +1 @@
1
- {"version":3,"file":"asyncSource.cjs","names":["abortError","createSourceStore","createRequestController","SourcererDisposedError","toError","require_errors","require_requestController","require_sourceStore","_vielzeug_arsenal","reject","signal","result","load","error","resolve","value","store","initial","requests","request","previous","options","state","reason"],"sources":["../src/asyncSource.ts"],"sourcesContent":["import { abortError } from '@vielzeug/arsenal';\n\nimport { SourcererDisposedError } from './errors';\nimport { createRequestController, toError } from './requestController';\nimport { createSourceStore } from './sourceStore';\n\nconst loadWithSignal = <T>(load: (signal: AbortSignal) => Promise<T>, signal: AbortSignal): Promise<T> =>\n new Promise((resolve, reject) => {\n const abort = (): void => reject(abortError(signal, 'The operation was aborted.'));\n if (signal.aborted) {\n abort();\n return;\n }\n\n signal.addEventListener('abort', abort, { once: true });\n let result: Promise<T>;\n try {\n result = load(signal);\n } catch (error) {\n signal.removeEventListener('abort', abort);\n reject(error);\n return;\n }\n\n result.then(\n (value) => {\n signal.removeEventListener('abort', abort);\n resolve(value);\n },\n (error: unknown) => {\n signal.removeEventListener('abort', abort);\n reject(error);\n },\n );\n });\n\ntype FetchOptions<TState, TResult> = {\n load(signal: AbortSignal): Promise<TResult>;\n success(result: TResult): TState;\n commit?(state: TState, result: TResult): 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 loadWithSignal(options.load, request.signal);\n if (!request.isCurrent() || store.disposed) return;\n\n const state = options.success(result);\n options.commit?.(state, result);\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,uIAMA,IAAM,gBAAqB,EAA2C,IACpE,IAAI,SAAS,EAAS,IAAW,CAC/B,IAAM,UAAoBS,GAAA,EAAOT,EAAAA,WAAAA,CAAWU,EAAQ,4BAA4B,CAAC,EACjF,GAAIA,EAAO,QAAS,CAClB,MAAM,EACN,MACF,CAEAA,EAAO,iBAAiB,QAAS,MAAO,CAAE,KAAM,EAAK,CAAC,EACtD,IAAIC,EACJ,GAAI,CACF,EAASC,EAAKF,CAAM,CACtB,OAASG,EAAO,CACdH,EAAO,oBAAoB,QAAS,KAAK,EACzCD,EAAOI,CAAK,EACZ,MACF,CAEAF,EAAO,KACJ,GAAU,CACTD,EAAO,oBAAoB,QAAS,KAAK,EACzCI,EAAQC,CAAK,CACf,EACC,GAAmB,CAClBL,EAAO,oBAAoB,QAAS,KAAK,EACzCD,EAAOI,CAAK,CACd,CACF,CACF,CAAC,EAoBH,SAAgB,kBAA0B,EAAsC,CAC9E,IAAMG,EAAQf,EAAAA,kBAAkBgB,CAAO,EACjCC,EAAWhB,EAAAA,wBAAwB,EAEzC,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAOc,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,IAAIb,EAAAA,uBAE9B,IAAMgB,EAAUD,EAAS,MAAM,EACzBE,EAAWJ,EAAM,MACvBA,EAAM,IAAIK,EAAQ,QAAQD,CAAQ,CAAC,EAEnC,GAAI,CACF,IAAMT,EAAS,MAAM,eAAeU,EAAQ,KAAMF,EAAQ,MAAM,EAChE,GAAI,CAACA,EAAQ,UAAU,GAAKH,EAAM,SAAU,OAE5C,IAAMM,EAAQD,EAAQ,QAAQV,CAAM,EACpCU,EAAQ,SAASC,EAAOX,CAAM,EAC9BK,EAAM,IAAIM,CAAK,CACjB,OAASC,EAAiB,CACxB,GAAI,CAACJ,EAAQ,UAAU,GAAKH,EAAM,SAAU,OAE5C,IAAMH,EAAQT,EAAAA,QAAQmB,CAAM,EAE5B,MADAP,EAAM,IAAIK,EAAQ,QAAQD,EAAUP,CAAK,CAAC,EACpCA,CACR,QAAU,CACRM,EAAQ,OAAO,CACjB,CACF,EAEA,IAAI,OAAQ,CACV,OAAOH,EAAM,KACf,EAEA,UAAWA,EAAM,SACnB,CACF,C"}
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"}
@@ -1,7 +1,8 @@
1
1
  type FetchOptions<TState, TResult> = {
2
2
  load(signal: AbortSignal): Promise<TResult>;
3
3
  success(result: TResult): TState;
4
- commit?(state: TState, result: TResult): void;
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":"AAoCA,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,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,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;IAC3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IAAI,CAAC;CAC1D,CAAC,CAAC;AAEH,kGAAkG;AAClG,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAiD9E"}
@@ -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 { abortError as i } from "@vielzeug/arsenal";
4
+ import { abortable as i } from "@vielzeug/arsenal";
5
5
  // src/asyncSource.ts
6
- var loadWithSignal = (e, t) => new Promise((n, r) => {
7
- let abort = () => r(i(t, "The operation was aborted."));
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 a.disposalSignal;
10
+ return o.disposalSignal;
31
11
  },
32
12
  dispose() {
33
- o.dispose(), a.dispose();
13
+ s.dispose(), o.dispose();
34
14
  },
35
15
  get disposed() {
36
- return a.disposed;
16
+ return o.disposed;
37
17
  },
38
18
  async fetch(t) {
39
- if (a.disposed) throw new e();
40
- let r = o.begin(), i = a.value;
41
- a.set(t.pending(i));
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 loadWithSignal(t.load, r.signal);
44
- if (!r.isCurrent() || a.disposed) return;
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?.(n, e), a.set(n);
26
+ t.commit?.(), o.set(n);
47
27
  } catch (e) {
48
- if (!r.isCurrent() || a.disposed) return;
49
- let o = n(e);
50
- throw a.set(t.failure(i, o)), o;
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 a.value;
36
+ return o.value;
57
37
  },
58
- subscribe: a.subscribe
38
+ subscribe: o.subscribe
59
39
  };
60
40
  }
61
41
  export { createAsyncSource };
@@ -1 +1 @@
1
- {"version":3,"file":"asyncSource.js","names":["reject","abortError","signal","result","load","error","resolve","value","store","createSourceStore","initial","requests","createRequestController","SourcererDisposedError","request","previous","options","state","reason","toError"],"sources":["../src/asyncSource.ts"],"sourcesContent":["import { abortError } from '@vielzeug/arsenal';\n\nimport { SourcererDisposedError } from './errors';\nimport { createRequestController, toError } from './requestController';\nimport { createSourceStore } from './sourceStore';\n\nconst loadWithSignal = <T>(load: (signal: AbortSignal) => Promise<T>, signal: AbortSignal): Promise<T> =>\n new Promise((resolve, reject) => {\n const abort = (): void => reject(abortError(signal, 'The operation was aborted.'));\n if (signal.aborted) {\n abort();\n return;\n }\n\n signal.addEventListener('abort', abort, { once: true });\n let result: Promise<T>;\n try {\n result = load(signal);\n } catch (error) {\n signal.removeEventListener('abort', abort);\n reject(error);\n return;\n }\n\n result.then(\n (value) => {\n signal.removeEventListener('abort', abort);\n resolve(value);\n },\n (error: unknown) => {\n signal.removeEventListener('abort', abort);\n reject(error);\n },\n );\n });\n\ntype FetchOptions<TState, TResult> = {\n load(signal: AbortSignal): Promise<TResult>;\n success(result: TResult): TState;\n commit?(state: TState, result: TResult): 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 loadWithSignal(options.load, request.signal);\n if (!request.isCurrent() || store.disposed) return;\n\n const state = options.success(result);\n options.commit?.(state, result);\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":";;;;;AAMA,IAAM,kBAAqB,GAA2C,MACpE,IAAI,SAAS,GAAS,MAAW;CAC/B,IAAM,cAAoBA,EAAOC,EAAWC,GAAQ,4BAA4B,CAAC;CACjF,IAAIA,EAAO,SAAS;EAClB,MAAM;EACN;CACF;CAEA,EAAO,iBAAiB,SAAS,OAAO,EAAE,MAAM,GAAK,CAAC;CACtD,IAAIC;CACJ,IAAI;EACF,IAASC,EAAKF,CAAM;CACtB,SAASG,GAAO;EAEd,AADAH,EAAO,oBAAoB,SAAS,KAAK,GACzCF,EAAOK,CAAK;EACZ;CACF;CAEA,EAAO,MACJ,MAAU;EAET,AADAH,EAAO,oBAAoB,SAAS,KAAK,GACzCI,EAAQC,CAAK;CACf,IACC,MAAmB;EAElB,AADAL,EAAO,oBAAoB,SAAS,KAAK,GACzCF,EAAOK,CAAK;CACd,CACF;AACF,CAAC;AAoBH,SAAgB,kBAA0B,GAAsC;CAC9E,IAAMG,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,IAAMZ,IAAS,MAAM,eAAea,EAAQ,MAAMF,EAAQ,MAAM;IAChE,IAAI,CAACA,EAAQ,UAAU,KAAKN,EAAM,UAAU;IAE5C,IAAMS,IAAQD,EAAQ,QAAQb,CAAM;IAEpC,AADAa,EAAQ,SAASC,GAAOd,CAAM,GAC9BK,EAAM,IAAIS,CAAK;GACjB,SAASC,GAAiB;IACxB,IAAI,CAACJ,EAAQ,UAAU,KAAKN,EAAM,UAAU;IAE5C,IAAMH,IAAQc,EAAQD,CAAM;IAE5B,MADAV,EAAM,IAAIQ,EAAQ,QAAQD,GAAUV,CAAK,CAAC,GACpCA;GACR,UAAU;IACR,EAAQ,OAAO;GACjB;EACF;EAEA,IAAI,QAAQ;GACV,OAAOG,EAAM;EACf;EAEA,WAAWA,EAAM;CACnB;AACF"}
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"}
@@ -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(i.pageSize),params:i.params}),assertLive=()=>{if(a.disposed)throw new t.SourcererDisposedError},fetch=e=>a.fetch({failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>r.load({...e,signal:t}),pending:t=>({...t,error:null,loading:!0,...Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>({error:null,items:[...t.items],loading:!1,pagination:createPagination(e.pageSize,t),params:e.params})}),o={get disposalSignal(){return a.disposalSignal},dispose:a.dispose,get disposed(){return a.disposed},async next(){assertLive();let e=a.state.pagination.nextCursor;a.state.loading||e===void 0||(i={after:e,pageSize:i.pageSize,params:i.params},await fetch(i))},async previous(){assertLive();let e=a.state.pagination.previousCursor;a.state.loading||e===void 0||(i={before:e,pageSize:i.pageSize,params:i.params},await fetch(i))},reload(){return fetch(i)},async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize&&i.after===void 0&&i.before===void 0){if(a.state.loading||a.state.pagination.pageSize===t&&Object.is(a.state.params,i.params))return;await fetch(i)}else i={pageSize:t,params:i.params},await fetch(i)},async setParams(e){assertLive(),Object.is(e,i.params)?!a.state.loading&&!Object.is(e,a.state.params)&&await fetch(i):(i={pageSize:i.pageSize,params:e},await fetch(i))},get state(){return a.state},subscribe:a.subscribe,[Symbol.dispose](){o.dispose()}};return o}exports.createCursorSource=createCursorSource;
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","asyncSource","previous","request","source","cursor","normalized","pageSize","params"],"sources":["../src/cursorSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererConfigurationError, SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState } from './types';\n\ntype Request<TParams, TCursor> = Readonly<{\n after?: TCursor;\n before?: TCursor;\n pageSize: number;\n params: TParams;\n}>;\n\nconst createPagination = <TCursor>(\n pageSize: number,\n result?: CursorResult<unknown, TCursor>,\n): CursorPagination<TCursor> => ({\n ...(result?.nextCursor !== undefined && { nextCursor: result.nextCursor }),\n pageSize,\n ...(result?.previousCursor !== undefined && { previousCursor: result.previousCursor }),\n ...(result?.totalItems !== undefined && { totalItems: totalItems(result.totalItems) }),\n});\n\n/** Cursor sources retain committed items and cursors while replacement work is pending. */\nexport function createCursorSource<T, TParams = undefined, TCursor = string>(\n config: CursorSourceConfig<T, TParams, TCursor>,\n): CursorSource<T, TParams, TCursor> {\n if (config.after !== undefined && config.before !== undefined) {\n throw new SourcererConfigurationError('Cursor source cannot start with both after and before');\n }\n\n let requested: Request<TParams, TCursor> = {\n ...(config.after !== undefined && { after: config.after }),\n ...(config.before !== undefined && { before: config.before }),\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n const asyncSource = createAsyncSource<CursorSourceState<T, TParams, TCursor>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(requested.pageSize),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams, TCursor>): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagination(request.pageSize, result),\n params: request.params,\n }),\n });\n\n const source: CursorSource<T, TParams, TCursor> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async next() {\n assertLive();\n const cursor = asyncSource.state.pagination.nextCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { after: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n async previous() {\n assertLive();\n const cursor = asyncSource.state.pagination.previousCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { before: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.after === undefined && requested.before === undefined) {\n if (\n asyncSource.state.loading ||\n (asyncSource.state.pagination.pageSize === normalized &&\n Object.is(asyncSource.state.params, requested.params))\n )\n return;\n await fetch(requested);\n return;\n }\n requested = { pageSize: normalized, params: requested.params };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await fetch(requested);\n return;\n }\n requested = { pageSize: requested.pageSize, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"A,6FAYA,IAAM,kBACJ,EACA,KAC+B,CAC/B,GAAIQ,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYA,EAAO,UAAW,EACxE,WACA,GAAIA,GAAQ,iBAAmB,IAAA,IAAa,CAAE,eAAgBA,EAAO,cAAe,EACpF,GAAIA,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAYR,EAAAA,WAAWQ,EAAO,UAAU,CAAE,CACtF,GAGA,SAAgB,mBACd,EACmC,CACnC,GAAIC,EAAO,QAAU,IAAA,IAAaA,EAAO,SAAW,IAAA,GAClD,MAAM,IAAIR,EAAAA,4BAA4B,uDAAuD,EAG/F,IAAIS,EAAuC,CACzC,GAAID,EAAO,QAAU,IAAA,IAAa,CAAE,MAAOA,EAAO,KAAM,EACxD,GAAIA,EAAO,SAAW,IAAA,IAAa,CAAE,OAAQA,EAAO,MAAO,EAC3D,SAAUP,EAAAA,gBAAgBO,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACME,EAAcR,EAAAA,kBAA0D,CAC5E,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiBO,EAAU,QAAQ,EAC/C,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAIC,EAAY,SAAU,MAAM,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AAW,IAAA,GAAW,CACxG,GACEC,EAAY,MAAM,SACjBA,EAAY,MAAM,WAAW,WAAaK,GACzC,OAAO,GAAGL,EAAY,MAAM,OAAQD,EAAU,MAAM,EAEtD,OACF,MAAM,MAAMA,CAAS,CAEvB,KACA,GAAY,CAAE,SAAUM,EAAY,OAAQN,EAAU,MAAO,EAC7D,MAAM,MAAMA,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CACtB,WAAW,EACP,OAAO,GAAGQ,EAAQR,EAAU,MAAM,EAChC,CAACC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAGO,EAAQP,EAAY,MAAM,MAAM,GAAG,MAAM,MAAMD,CAAS,GAGvG,EAAY,CAAE,SAAUA,EAAU,SAAU,QAAO,EACnD,MAAM,MAAMA,CAAS,EACvB,EAEA,IAAI,OAAQ,CACV,OAAOC,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBG,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
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,E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kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,GAC9C,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CA+GnC"}
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"}
@@ -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, totalItems as i } from "./pagination.js";
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: i(t.totalItems) }
9
+ ...t?.totalItems !== void 0 && { totalItems: a(t.totalItems) }
10
10
  });
11
- function createCursorSource(i) {
12
- if (i.after !== void 0 && i.before !== void 0) throw new t("Cursor source cannot start with both after and before");
13
- let a = {
14
- ...i.after !== void 0 && { after: i.after },
15
- ...i.before !== void 0 && { before: i.before },
16
- pageSize: r(i.pageSize ?? 20, "pageSize"),
17
- params: i.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(a.pageSize),
23
- params: a.params
22
+ pagination: createPagination(s.pageSize),
23
+ params: s.params
24
24
  }), assertLive = () => {
25
- if (o.disposed) throw new n();
26
- }, fetch = (e) => o.fetch({
27
- failure: (e, t) => ({
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) => i.load({
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
- ...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
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
- }), s = {
55
+ }), d = {
51
56
  get disposalSignal() {
52
- return o.disposalSignal;
57
+ return u.disposalSignal;
53
58
  },
54
- dispose: o.dispose,
59
+ dispose: u.dispose,
55
60
  get disposed() {
56
- return o.disposed;
61
+ return u.disposed;
57
62
  },
58
63
  async next() {
59
64
  assertLive();
60
- let e = o.state.pagination.nextCursor;
61
- o.state.loading || e === void 0 || (a = {
65
+ let e = u.state.pagination.nextCursor;
66
+ u.state.loading || e === void 0 || (s = {
62
67
  after: e,
63
- pageSize: a.pageSize,
64
- params: a.params
65
- }, await fetch(a));
68
+ pageSize: s.pageSize,
69
+ params: s.params
70
+ }, await fetch(s));
66
71
  },
67
72
  async previous() {
68
73
  assertLive();
69
- let e = o.state.pagination.previousCursor;
70
- o.state.loading || e === void 0 || (a = {
74
+ let e = u.state.pagination.previousCursor;
75
+ u.state.loading || e === void 0 || (s = {
71
76
  before: e,
72
- pageSize: a.pageSize,
73
- params: a.params
74
- }, await fetch(a));
77
+ pageSize: s.pageSize,
78
+ params: s.params
79
+ }, await fetch(s));
75
80
  },
76
81
  reload() {
77
- return fetch(a);
82
+ return fetch(s);
78
83
  },
79
84
  async setPageSize(e) {
80
85
  assertLive();
81
- let t = r(e, "pageSize");
82
- if (t === a.pageSize && a.after === void 0 && a.before === void 0) {
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: a.params
88
- }, await fetch(a);
89
+ params: s.params
90
+ }, await fetch(s));
89
91
  },
90
92
  async setParams(e) {
91
- assertLive(), Object.is(e, a.params) ? !o.state.loading && !Object.is(e, o.state.params) && await fetch(a) : (a = {
92
- pageSize: a.pageSize,
93
+ assertLive(), i(e, s.params) ? await ensureLoaded() : (s = {
94
+ pageSize: s.pageSize,
93
95
  params: e
94
- }, await fetch(a));
96
+ }, await fetch(s));
95
97
  },
96
98
  get state() {
97
- return o.state;
99
+ return u.state;
98
100
  },
99
- subscribe: o.subscribe,
101
+ subscribe: u.subscribe,
100
102
  [Symbol.dispose]() {
101
- s.dispose();
103
+ d.dispose();
102
104
  }
103
105
  };
104
- return s;
106
+ return d;
105
107
  }
106
108
  export { createCursorSource };
107
109
 
@@ -1 +1 @@
1
- {"version":3,"file":"cursorSource.js","names":["result","totalItems","config","SourcererConfigurationError","requested","positiveInteger","asyncSource","createAsyncSource","SourcererDisposedError","previous","request","source","cursor","normalized","pageSize","params"],"sources":["../src/cursorSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererConfigurationError, SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { CursorPagination, CursorResult, CursorSource, CursorSourceConfig, CursorSourceState } from './types';\n\ntype Request<TParams, TCursor> = Readonly<{\n after?: TCursor;\n before?: TCursor;\n pageSize: number;\n params: TParams;\n}>;\n\nconst createPagination = <TCursor>(\n pageSize: number,\n result?: CursorResult<unknown, TCursor>,\n): CursorPagination<TCursor> => ({\n ...(result?.nextCursor !== undefined && { nextCursor: result.nextCursor }),\n pageSize,\n ...(result?.previousCursor !== undefined && { previousCursor: result.previousCursor }),\n ...(result?.totalItems !== undefined && { totalItems: totalItems(result.totalItems) }),\n});\n\n/** Cursor sources retain committed items and cursors while replacement work is pending. */\nexport function createCursorSource<T, TParams = undefined, TCursor = string>(\n config: CursorSourceConfig<T, TParams, TCursor>,\n): CursorSource<T, TParams, TCursor> {\n if (config.after !== undefined && config.before !== undefined) {\n throw new SourcererConfigurationError('Cursor source cannot start with both after and before');\n }\n\n let requested: Request<TParams, TCursor> = {\n ...(config.after !== undefined && { after: config.after }),\n ...(config.before !== undefined && { before: config.before }),\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n const asyncSource = createAsyncSource<CursorSourceState<T, TParams, TCursor>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(requested.pageSize),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams, TCursor>): Promise<void> =>\n asyncSource.fetch({\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ ...request, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => ({\n error: null,\n items: [...result.items],\n loading: false,\n pagination: createPagination(request.pageSize, result),\n params: request.params,\n }),\n });\n\n const source: CursorSource<T, TParams, TCursor> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async next() {\n assertLive();\n const cursor = asyncSource.state.pagination.nextCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { after: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n async previous() {\n assertLive();\n const cursor = asyncSource.state.pagination.previousCursor;\n if (asyncSource.state.loading || cursor === undefined) return;\n requested = { before: cursor, pageSize: requested.pageSize, params: requested.params };\n await fetch(requested);\n },\n\n reload() {\n return fetch(requested);\n },\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize && requested.after === undefined && requested.before === undefined) {\n if (\n asyncSource.state.loading ||\n (asyncSource.state.pagination.pageSize === normalized &&\n Object.is(asyncSource.state.params, requested.params))\n )\n return;\n await fetch(requested);\n return;\n }\n requested = { pageSize: normalized, params: requested.params };\n await fetch(requested);\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await fetch(requested);\n return;\n }\n requested = { pageSize: requested.pageSize, params };\n await fetch(requested);\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":";;;;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;AACt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AWM,MAAW,KAAA,MAC5C,IAAY;IAAE,OAAOA;IAAQ,UAAUR,EAAU;IAAU,QAAQA,EAAU;GAAO,GACpF,MAAM,MAAMA,CAAS;EACvB;EAEA,MAAM,WAAW;GACf,WAAW;GACX,IAAMQ,IAASN,EAAY,MAAM,WAAW;GAC5C,AAAIA,EAAY,MAAM,WAAWM,MAAW,KAAA,MAC5C,IAAY;IAAE,QAAQA;IAAQ,UAAUR,EAAU;IAAU,QAAQA,EAAU;GAAO,GACrF,MAAM,MAAMA,CAAS;EACvB;EAEA,SAAS;GACP,OAAO,MAAMA,CAAS;EACxB;EAEA,MAAM,YAAY,GAAU;GAC1B,WAAW;GACX,IAAMS,IAAaR,EAAgBS,GAAU,UAAU;GACvD,IAAID,MAAeT,EAAU,YAAYA,EAAU,UAAU,KAAA,KAAaA,EAAU,WAAW,KAAA,GAAW;IACxG,IACEE,EAAY,MAAM,WACjBA,EAAY,MAAM,WAAW,aAAaO,KACzC,OAAO,GAAGP,EAAY,MAAM,QAAQF,EAAU,MAAM,GAEtD;IACF,MAAM,MAAMA,CAAS;GAEvB,OAEA,AADA,IAAY;IAAE,UAAUS;IAAY,QAAQT,EAAU;GAAO,GAC7D,MAAM,MAAMA,CAAS;EACvB;EAEA,MAAM,UAAU,GAAQ;GAEtB,AADA,WAAW,GACP,OAAO,GAAGW,GAAQX,EAAU,MAAM,IAChC,CAACE,EAAY,MAAM,WAAW,CAAC,OAAO,GAAGS,GAAQT,EAAY,MAAM,MAAM,KAAG,MAAM,MAAMF,CAAS,KAGvG,IAAY;IAAE,UAAUA,EAAU;IAAU;GAAO,GACnD,MAAM,MAAMA,CAAS;EACvB;EAEA,IAAI,QAAQ;GACV,OAAOE,EAAY;EACrB;EAEA,WAAWA,EAAY;EAEvB,CAAC,OAAO,WAAW;GACjB,EAAO,QAAQ;EACjB;CACF;CAEA,OAAOK;AACT"}
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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;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"}
@@ -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,o=!1,s=e.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(0,i.pageSize,0),params:i.params}),assertLive=()=>{if(s.disposed)throw new t.SourcererDisposedError},fetch=(e,t,c)=>{let l=s.state.items;return s.fetch({commit:()=>{o=!0,a=t+1,i=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:n=>r.load({page:t,pageSize:e.pageSize,params:e.params,signal:n}),pending:t=>({...t,error:null,loading:!0,...c||Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>{let r=c?[...l,...t.items]:[...t.items];return{error:null,items:r,loading:!1,pagination:createPagination(r.length,e.pageSize,n.totalItems(t.totalItems)),params:e.params}}})},reload=()=>fetch(i,1,!1),c={get disposalSignal(){return s.disposalSignal},dispose:s.dispose,get disposed(){return s.disposed},async loadMore(){assertLive(),s.state.loading||(!o||!Object.is(i.params,s.state.params)?await reload():s.state.pagination.hasMore&&await fetch(i,a,!0))},reload,async setPageSize(e){assertLive();let t=n.positiveInteger(e,`pageSize`);if(t===i.pageSize){if(s.state.loading||s.state.pagination.pageSize===t)return;await reload()}else i={...i,pageSize:t},a=1,await reload()},async setParams(e){assertLive(),Object.is(e,i.params)?!s.state.loading&&!Object.is(e,s.state.params)&&await reload():(i={...i,params:e},a=1,await reload())},get state(){return s.state},subscribe:s.subscribe,[Symbol.dispose](){c.dispose()}};return c}exports.createInfiniteSource=createInfiniteSource;
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","requested","config","nextPage","hasLoaded","asyncSource","page","request","previous","append","items","result","source","normalized","pageSize","params"],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"A,6FAOA,IAAM,kBAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAASO,EAAcC,EACvB,cACA,WACA,WAAYA,CACd,GAGA,SAAgB,qBACd,EAC4B,CAC5B,IAAIC,EAA8B,CAChC,SAAUT,EAAAA,gBAAgBU,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQA,EAAO,MACjB,EACIC,EAAW,EACXC,EAAY,GACVC,EAAcZ,EAAAA,kBAAmD,CACrE,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiB,EAAGQ,EAAU,SAAU,CAAC,EACrD,OAAQA,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAII,EAAY,SAAU,MAAM,IAAIX,EAAAA,sBACtC,EAEM,OAAS,EAA2B,EAAc,IAAmC,CACzF,IAAMK,EAAcM,EAAY,MAA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A,EAAY,MAAM,WAAW,WAAaQ,EAAY,OACvF,MAAM,OAAO,CAEf,KACA,GAAY,CAAE,GAAGZ,EAAW,SAAUY,CAAW,EACjD,EAAW,EACX,MAAM,OAAO,CACf,EAEA,MAAM,UAAU,EAAQ,CACtB,WAAW,EACP,OAAO,GAAGE,EAAQd,EAAU,MAAM,EAChC,CAACI,EAAY,MAAM,SAAW,CAAC,OAAO,GAAGU,EAAQV,EAAY,MAAM,MAAM,GAAG,MAAM,OAAO,GAG/F,EAAY,CAAE,GAAGJ,EAAW,QAAO,EACnC,EAAW,EACX,MAAM,OAAO,EACf,EAEA,IAAI,OAAQ,CACV,OAAOI,EAAY,KACrB,EAEA,UAAWA,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjBO,EAAO,QAAQ,CACjB,CACF,EAEA,OAAOA,CACT,C"}
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,GAAGK,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,GAExE,KAAO,GAAWT,EAAO,KAAK,CAAE,OAAM,SAAUO,EAAQ,SAAU,OAAQA,EAAQ,OAAQ,QAAO,CAAC,EAClG,QAAU,IAAc,CACtB,GAAGE,EACH,MAAO,KACP,QAAS,GACT,GAAIC,GAAUlB,EAAAA,WAAWe,EAAQ,OAAQE,EAAS,MAAM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@ -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,CA6G5B"}
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"}