@vielzeug/sourcerer 26.10.15 → 26.10.16

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 (41) hide show
  1. package/dist/asyncSource.cjs.map +1 -1
  2. package/dist/asyncSource.js +63 -1
  3. package/dist/asyncSource.js.map +1 -1
  4. package/dist/cursorSource.cjs +1 -1
  5. package/dist/cursorSource.cjs.map +1 -1
  6. package/dist/cursorSource.js +108 -1
  7. package/dist/cursorSource.js.map +1 -1
  8. package/dist/errors.cjs.map +1 -1
  9. package/dist/errors.js +13 -1
  10. package/dist/errors.js.map +1 -1
  11. package/dist/index.js +7 -1
  12. package/dist/infiniteSource.cjs +1 -1
  13. package/dist/infiniteSource.cjs.map +1 -1
  14. package/dist/infiniteSource.js +99 -1
  15. package/dist/infiniteSource.js.map +1 -1
  16. package/dist/localSource.cjs.map +1 -1
  17. package/dist/localSource.js +70 -1
  18. package/dist/localSource.js.map +1 -1
  19. package/dist/pageSource.cjs +1 -1
  20. package/dist/pageSource.cjs.map +1 -1
  21. package/dist/pageSource.js +114 -1
  22. package/dist/pageSource.js.map +1 -1
  23. package/dist/pagination.cjs.map +1 -1
  24. package/dist/pagination.js +24 -1
  25. package/dist/pagination.js.map +1 -1
  26. package/dist/requestController.cjs.map +1 -1
  27. package/dist/requestController.js +30 -1
  28. package/dist/requestController.js.map +1 -1
  29. package/dist/resourceSource.cjs.map +1 -1
  30. package/dist/resourceSource.js +50 -1
  31. package/dist/resourceSource.js.map +1 -1
  32. package/dist/sourceStore.cjs.map +1 -1
  33. package/dist/sourceStore.js +37 -1
  34. package/dist/sourceStore.js.map +1 -1
  35. package/dist/sourcerer.cjs +1 -1
  36. package/dist/sourcerer.cjs.map +1 -1
  37. package/dist/sourcerer.iife.js +1 -1
  38. package/dist/sourcerer.iife.js.map +1 -1
  39. package/dist/sourcerer.js +571 -1
  40. package/dist/sourcerer.js.map +1 -1
  41. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"asyncSource.cjs","names":[],"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":"uIAMA,IAAM,gBAAqB,EAA2C,IACpE,IAAI,SAAS,EAAS,IAAW,CAC/B,IAAM,UAAoB,GAAA,EAAO,EAAA,WAAA,CAAW,EAAQ,4BAA4B,CAAC,EACjF,GAAI,EAAO,QAAS,CAClB,MAAM,EACN,MACF,CAEA,EAAO,iBAAiB,QAAS,MAAO,CAAE,KAAM,EAAK,CAAC,EACtD,IAAI,EACJ,GAAI,CACF,EAAS,EAAK,CAAM,CACtB,OAAS,EAAO,CACd,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAO,CAAK,EACZ,MACF,CAEA,EAAO,KACJ,GAAU,CACT,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAQ,CAAK,CACf,EACC,GAAmB,CAClB,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAO,CAAK,CACd,CACF,CACF,CAAC,EAoBH,SAAgB,kBAA0B,EAAsC,CAC9E,IAAM,EAAQ,EAAA,kBAAkB,CAAO,EACjC,EAAW,EAAA,wBAAwB,EAEzC,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAO,EAAM,cACf,EAEA,SAAU,CACR,EAAS,QAAQ,EACjB,EAAM,QAAQ,CAChB,EAEA,IAAI,UAAW,CACb,OAAO,EAAM,QACf,EAEA,MAAM,MAAM,EAAS,CACnB,GAAI,EAAM,SAAU,MAAM,IAAI,EAAA,uBAE9B,IAAM,EAAU,EAAS,MAAM,EACzB,EAAW,EAAM,MACvB,EAAM,IAAI,EAAQ,QAAQ,CAAQ,CAAC,EAEnC,GAAI,CACF,IAAM,EAAS,MAAM,eAAe,EAAQ,KAAM,EAAQ,MAAM,EAChE,GAAI,CAAC,EAAQ,UAAU,GAAK,EAAM,SAAU,OAE5C,IAAM,EAAQ,EAAQ,QAAQ,CAAM,EACpC,EAAQ,SAAS,EAAO,CAAM,EAC9B,EAAM,IAAI,CAAK,CACjB,OAAS,EAAiB,CACxB,GAAI,CAAC,EAAQ,UAAU,GAAK,EAAM,SAAU,OAE5C,IAAM,EAAQ,EAAA,QAAQ,CAAM,EAE5B,MADA,EAAM,IAAI,EAAQ,QAAQ,EAAU,CAAK,CAAC,EACpC,CACR,QAAU,CACR,EAAQ,OAAO,CACjB,CACF,EAEA,IAAI,OAAQ,CACV,OAAO,EAAM,KACf,EAEA,UAAW,EAAM,SACnB,CACF"}
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,2 +1,64 @@
1
- import{SourcererDisposedError as e}from"./errors.js";import{createRequestController as t,toError as n}from"./requestController.js";import{createSourceStore as r}from"./sourceStore.js";import{abortError as i}from"@vielzeug/arsenal";var loadWithSignal=(e,t)=>new Promise((n,r)=>{let abort=()=>r(i(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),r(e);return}a.then(e=>{t.removeEventListener(`abort`,abort),n(e)},e=>{t.removeEventListener(`abort`,abort),r(e)})});function createAsyncSource(i){let a=r(i),o=t();return{get disposalSignal(){return a.disposalSignal},dispose(){o.dispose(),a.dispose()},get disposed(){return a.disposed},async fetch(t){if(a.disposed)throw new e;let r=o.begin(),i=a.value;a.set(t.pending(i));try{let e=await loadWithSignal(t.load,r.signal);if(!r.isCurrent()||a.disposed)return;let n=t.success(e);t.commit?.(n,e),a.set(n)}catch(e){if(!r.isCurrent()||a.disposed)return;let o=n(e);throw a.set(t.failure(i,o)),o}finally{r.finish()}},get state(){return a.value},subscribe:a.subscribe}}export{createAsyncSource};
1
+ import { SourcererDisposedError as e } from "./errors.js";
2
+ import { createRequestController as t, toError as n } from "./requestController.js";
3
+ import { createSourceStore as r } from "./sourceStore.js";
4
+ import { abortError as i } from "@vielzeug/arsenal";
5
+ //#region 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();
28
+ return {
29
+ get disposalSignal() {
30
+ return a.disposalSignal;
31
+ },
32
+ dispose() {
33
+ o.dispose(), a.dispose();
34
+ },
35
+ get disposed() {
36
+ return a.disposed;
37
+ },
38
+ async fetch(t) {
39
+ if (a.disposed) throw new e();
40
+ let r = o.begin(), i = a.value;
41
+ a.set(t.pending(i));
42
+ try {
43
+ let e = await loadWithSignal(t.load, r.signal);
44
+ if (!r.isCurrent() || a.disposed) return;
45
+ let n = t.success(e);
46
+ t.commit?.(n, e), a.set(n);
47
+ } catch (e) {
48
+ if (!r.isCurrent() || a.disposed) return;
49
+ let o = n(e);
50
+ throw a.set(t.failure(i, o)), o;
51
+ } finally {
52
+ r.finish();
53
+ }
54
+ },
55
+ get state() {
56
+ return a.value;
57
+ },
58
+ subscribe: a.subscribe
59
+ };
60
+ }
61
+ //#endregion
62
+ export { createAsyncSource };
63
+
2
64
  //# sourceMappingURL=asyncSource.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"asyncSource.js","names":[],"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":"uOAMA,IAAM,gBAAqB,EAA2C,IACpE,IAAI,SAAS,EAAS,IAAW,CAC/B,IAAM,UAAoB,EAAO,EAAW,EAAQ,4BAA4B,CAAC,EACjF,GAAI,EAAO,QAAS,CAClB,MAAM,EACN,MACF,CAEA,EAAO,iBAAiB,QAAS,MAAO,CAAE,KAAM,EAAK,CAAC,EACtD,IAAI,EACJ,GAAI,CACF,EAAS,EAAK,CAAM,CACtB,OAAS,EAAO,CACd,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAO,CAAK,EACZ,MACF,CAEA,EAAO,KACJ,GAAU,CACT,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAQ,CAAK,CACf,EACC,GAAmB,CAClB,EAAO,oBAAoB,QAAS,KAAK,EACzC,EAAO,CAAK,CACd,CACF,CACF,CAAC,EAoBH,SAAgB,kBAA0B,EAAsC,CAC9E,IAAM,EAAQ,EAAkB,CAAO,EACjC,EAAW,EAAwB,EAEzC,MAAO,CACL,IAAI,gBAAiB,CACnB,OAAO,EAAM,cACf,EAEA,SAAU,CACR,EAAS,QAAQ,EACjB,EAAM,QAAQ,CAChB,EAEA,IAAI,UAAW,CACb,OAAO,EAAM,QACf,EAEA,MAAM,MAAM,EAAS,CACnB,GAAI,EAAM,SAAU,MAAM,IAAI,EAE9B,IAAM,EAAU,EAAS,MAAM,EACzB,EAAW,EAAM,MACvB,EAAM,IAAI,EAAQ,QAAQ,CAAQ,CAAC,EAEnC,GAAI,CACF,IAAM,EAAS,MAAM,eAAe,EAAQ,KAAM,EAAQ,MAAM,EAChE,GAAI,CAAC,EAAQ,UAAU,GAAK,EAAM,SAAU,OAE5C,IAAM,EAAQ,EAAQ,QAAQ,CAAM,EACpC,EAAQ,SAAS,EAAO,CAAM,EAC9B,EAAM,IAAI,CAAK,CACjB,OAAS,EAAiB,CACxB,GAAI,CAAC,EAAQ,UAAU,GAAK,EAAM,SAAU,OAE5C,IAAM,EAAQ,EAAQ,CAAM,EAE5B,MADA,EAAM,IAAI,EAAQ,QAAQ,EAAU,CAAK,CAAC,EACpC,CACR,QAAU,CACR,EAAQ,OAAO,CACjB,CACF,EAEA,IAAI,OAAQ,CACV,OAAO,EAAM,KACf,EAEA,UAAW,EAAM,SACnB,CACF"}
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,2 +1,2 @@
1
- const e=require("./errors.cjs"),t=require("./asyncSource.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 e.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=t.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(i.pageSize),params:i.params}),assertLive=()=>{if(a.disposed)throw new e.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);return}i={pageSize:t,params:i.params},await fetch(i)},async setParams(e){if(assertLive(),Object.is(e,i.params)){!a.state.loading&&!Object.is(e,a.state.params)&&await fetch(i);return}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("./errors.cjs"),t=require("./asyncSource.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 e.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=t.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(i.pageSize),params:i.params}),assertLive=()=>{if(a.disposed)throw new e.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;
2
2
  //# sourceMappingURL=cursorSource.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"cursorSource.cjs","names":[],"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":"6FAYA,IAAM,kBACJ,EACA,KAC+B,CAC/B,GAAI,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAY,EAAO,UAAW,EACxE,WACA,GAAI,GAAQ,iBAAmB,IAAA,IAAa,CAAE,eAAgB,EAAO,cAAe,EACpF,GAAI,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAY,EAAA,WAAW,EAAO,UAAU,CAAE,CACtF,GAGA,SAAgB,mBACd,EACmC,CACnC,GAA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AAU,OAAQ,EAAU,MAAO,EACpF,MAAM,MAAM,CAAS,EACvB,EAEA,MAAM,UAAW,CACf,WAAW,EACX,IAAM,EAAS,EAAY,MAAM,WAAW,eACxC,EAAY,MAAM,SAAW,IAAW,IAAA,KAC5C,EAAY,CAAE,OAAQ,EAAQ,SAAU,EAAU,SAAU,OAAQ,EAAU,MAAO,EACrF,MAAM,MAAM,CAAS,EACvB,EAEA,QAAS,CACP,OAAO,MAAM,CAAS,CACxB,EAEA,MAAM,YAAY,EAAU,CAC1B,WAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAU,UAAU,EACvD,GAAI,IAAe,EAAU,UAAY,EAAU,QAAU,IAAA,IAAa,EAAU,SAAW,IAAA,GAAW,CACxG,GACE,EAAY,MAAM,SACjB,EAAY,MAAM,WAAW,WAAa,GACzC,OAAO,GAAG,EAAY,MAAM,OAAQ,EAAU,MAAM,EAEtD,OACF,MAAM,MAAM,CAAS,EACrB,MACF,CACA,EAAY,CAAE,SAAU,EAAY,OAAQ,EAAU,MAAO,EAC7D,MAAM,MAAM,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAG,EAAQ,EAAY,MAAM,MAAM,GAAG,MAAM,MAAM,CAAS,EACrG,MACF,CACA,EAAY,CAAE,SAAU,EAAU,SAAU,QAAO,EACnD,MAAM,MAAM,CAAS,CACvB,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
1
+ {"version":3,"file":"cursorSource.cjs","names":["totalItems","SourcererConfigurationError","positiveInteger","createAsyncSource","SourcererDisposedError","require_errors","require_asyncSource","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,E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asyncSource.js";import{positiveInteger as r,totalItems as i}from"./pagination.js";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:i(t.totalItems)}});function createCursorSource(i){if(i.after!==void 0&&i.before!==void 0)throw new e(`Cursor source cannot start with both after and before`);let a={...i.after!==void 0&&{after:i.after},...i.before!==void 0&&{before:i.before},pageSize:r(i.pageSize??20,`pageSize`),params:i.params},o=n({error:null,items:[],loading:!1,pagination:createPagination(a.pageSize),params:a.params}),assertLive=()=>{if(o.disposed)throw new t},fetch=e=>o.fetch({failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:t=>i.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})}),s={get disposalSignal(){return o.disposalSignal},dispose:o.dispose,get disposed(){return o.disposed},async next(){assertLive();let e=o.state.pagination.nextCursor;o.state.loading||e===void 0||(a={after:e,pageSize:a.pageSize,params:a.params},await fetch(a))},async previous(){assertLive();let e=o.state.pagination.previousCursor;o.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=r(e,`pageSize`);if(t===a.pageSize&&a.after===void 0&&a.before===void 0){if(o.state.loading||o.state.pagination.pageSize===t&&Object.is(o.state.params,a.params))return;await fetch(a);return}a={pageSize:t,params:a.params},await fetch(a)},async setParams(e){if(assertLive(),Object.is(e,a.params)){!o.state.loading&&!Object.is(e,o.state.params)&&await fetch(a);return}a={pageSize:a.pageSize,params:e},await fetch(a)},get state(){return o.state},subscribe:o.subscribe,[Symbol.dispose](){s.dispose()}};return s}export{createCursorSource};
1
+ import { SourcererConfigurationError as e, SourcererDisposedError as t } from "./errors.js";
2
+ import { createAsyncSource as n } from "./asyncSource.js";
3
+ import { positiveInteger as r, totalItems as i } from "./pagination.js";
4
+ //#region src/cursorSource.ts
5
+ var createPagination = (e, t) => ({
6
+ ...t?.nextCursor !== void 0 && { nextCursor: t.nextCursor },
7
+ pageSize: e,
8
+ ...t?.previousCursor !== void 0 && { previousCursor: t.previousCursor },
9
+ ...t?.totalItems !== void 0 && { totalItems: i(t.totalItems) }
10
+ });
11
+ function createCursorSource(i) {
12
+ if (i.after !== void 0 && i.before !== void 0) throw new e("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 = n({
19
+ error: null,
20
+ items: [],
21
+ loading: !1,
22
+ pagination: createPagination(a.pageSize),
23
+ params: a.params
24
+ }), assertLive = () => {
25
+ if (o.disposed) throw new t();
26
+ }, fetch = (e) => o.fetch({
27
+ failure: (e, t) => ({
28
+ ...e,
29
+ error: t,
30
+ loading: !1,
31
+ pendingParams: void 0
32
+ }),
33
+ load: (t) => i.load({
34
+ ...e,
35
+ signal: t
36
+ }),
37
+ pending: (t) => ({
38
+ ...t,
39
+ error: null,
40
+ loading: !0,
41
+ ...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
42
+ }),
43
+ success: (t) => ({
44
+ error: null,
45
+ items: [...t.items],
46
+ loading: !1,
47
+ pagination: createPagination(e.pageSize, t),
48
+ params: e.params
49
+ })
50
+ }), s = {
51
+ get disposalSignal() {
52
+ return o.disposalSignal;
53
+ },
54
+ dispose: o.dispose,
55
+ get disposed() {
56
+ return o.disposed;
57
+ },
58
+ async next() {
59
+ assertLive();
60
+ let e = o.state.pagination.nextCursor;
61
+ o.state.loading || e === void 0 || (a = {
62
+ after: e,
63
+ pageSize: a.pageSize,
64
+ params: a.params
65
+ }, await fetch(a));
66
+ },
67
+ async previous() {
68
+ assertLive();
69
+ let e = o.state.pagination.previousCursor;
70
+ o.state.loading || e === void 0 || (a = {
71
+ before: e,
72
+ pageSize: a.pageSize,
73
+ params: a.params
74
+ }, await fetch(a));
75
+ },
76
+ reload() {
77
+ return fetch(a);
78
+ },
79
+ async setPageSize(e) {
80
+ 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
+ pageSize: t,
87
+ params: a.params
88
+ }, await fetch(a);
89
+ },
90
+ 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
+ params: e
94
+ }, await fetch(a));
95
+ },
96
+ get state() {
97
+ return o.state;
98
+ },
99
+ subscribe: o.subscribe,
100
+ [Symbol.dispose]() {
101
+ s.dispose();
102
+ }
103
+ };
104
+ return s;
105
+ }
106
+ //#endregion
107
+ export { createCursorSource };
108
+
2
109
  //# sourceMappingURL=cursorSource.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cursorSource.js","names":[],"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":"6MAYA,IAAM,kBACJ,EACA,KAC+B,CAC/B,GAAI,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAY,EAAO,UAAW,EACxE,WACA,GAAI,GAAQ,iBAAmB,IAAA,IAAa,CAAE,eAAgB,EAAO,cAAe,EACpF,GAAI,GAAQ,aAAe,IAAA,IAAa,CAAE,WAAY,EAAW,EAAO,UAAU,CAAE,CACtF,GAGA,SAAgB,mBACd,EACmC,CACnC,GAAI,EAAO,QAAU,IAAA,IAAa,EAAO,SAAW,IAAA,GAClD,MAAM,IAAI,EAA4B,uDAAuD,EAG/F,IAAI,EAAuC,CACzC,GAAI,EAAO,QAAU,IAAA,IAAa,CAAE,MAAO,EAAO,KAAM,EACxD,GAAI,EAAO,SAAW,IAAA,IAAa,CAAE,OAAQ,EAAO,MAAO,EAC3D,SAAU,EAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,EACM,EAAc,EAA0D,CAC5E,MAAO,KACP,MAAO,CAAC,EACR,QAAS,GACT,WAAY,iBAAiB,EAAU,QAAQ,EAC/C,OAAQ,EAAU,MACpB,CAAC,EACK,eAAyB,CAC7B,GAAI,EAAY,SAAU,MAAM,IAAI,CACtC,EAEM,MAAS,GACb,EAAY,MAAM,CAChB,S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,EAAU,MAAM,EAEtD,OACF,MAAM,MAAM,CAAS,EACrB,MACF,CACA,EAAY,CAAE,SAAU,EAAY,OAAQ,EAAU,MAAO,EAC7D,MAAM,MAAM,CAAS,CACvB,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAG,EAAQ,EAAY,MAAM,MAAM,GAAG,MAAM,MAAM,CAAS,EACrG,MACF,CACA,EAAY,CAAE,SAAU,EAAU,SAAU,QAAO,EACnD,MAAM,MAAM,CAAS,CACvB,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
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;AACtF;AAGA,SAAgB,mBACd,GACmC;CACnC,IAAIE,EAAO,UAAU,KAAA,KAAaA,EAAO,WAAW,KAAA,GAClD,MAAM,IAAIC,EAA4B,uDAAuD;CAG/F,IAAIC,IAAuC;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,GACMI,IAAcC,EAA0D;EAC5E,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,iBAAiBH,EAAU,QAAQ;EAC/C,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAIE,EAAY,UAAU,MAAM,IAAIE,EAAuB;CAC7D,GAEM,SAAS,MACbF,EAAY,MAAM;EAChB,UAAU,GAAU,OAAW;GAAE,GAAG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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 +1 @@
1
- {"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {}\n\nexport class SourcererDisposedError extends SourcererError {\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":"AACA,IAAa,eAAb,cAAoC,KAAM,CACxC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,4BAAb,cAAiD,cAAe,CAAC,EAEpD,uBAAb,cAA4C,cAAe,CACzD,YAAY,EAAU,4BAA6B,CACjD,MAAM,CAAO,CACf,CACF"}
1
+ {"version":3,"file":"errors.cjs","names":["message","opts"],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {}\n\nexport class SourcererDisposedError extends SourcererError {\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":"AACA,IAAa,eAAb,cAAoC,KAAM,CACxC,YAAY,EAAiB,EAAqB,CAChD,MAAMA,EAASC,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,4BAAb,cAAiD,cAAe,CAAC,EAEpD,uBAAb,cAA4C,cAAe,CACzD,YAAY,EAAU,4BAA6B,CACjD,MAAMD,CAAO,CACf,CACF,E"}
package/dist/errors.js CHANGED
@@ -1,2 +1,14 @@
1
- var SourcererError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},SourcererConfigurationError=class extends SourcererError{},SourcererDisposedError=class extends SourcererError{constructor(e=`Sourcerer source disposed`){super(e)}};export{SourcererConfigurationError,SourcererDisposedError,SourcererError};
1
+ //#region src/errors.ts
2
+ var SourcererError = class extends Error {
3
+ constructor(e, t) {
4
+ super(e, t), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
5
+ }
6
+ }, SourcererConfigurationError = class extends SourcererError {}, SourcererDisposedError = class extends SourcererError {
7
+ constructor(e = "Sourcerer source disposed") {
8
+ super(e);
9
+ }
10
+ };
11
+ //#endregion
12
+ export { SourcererConfigurationError, SourcererDisposedError, SourcererError };
13
+
2
14
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {}\n\nexport class SourcererDisposedError extends SourcererError {\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":"AACA,IAAa,eAAb,cAAoC,KAAM,CACxC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,4BAAb,cAAiD,cAAe,CAAC,EAEpD,uBAAb,cAA4C,cAAe,CACzD,YAAY,EAAU,4BAA6B,CACjD,MAAM,CAAO,CACf,CACF"}
1
+ {"version":3,"file":"errors.js","names":["message","opts"],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */\nexport class SourcererError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {}\n\nexport class SourcererDisposedError extends SourcererError {\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":";AACA,IAAa,iBAAb,cAAoC,MAAM;CACxC,YAAY,GAAiB,GAAqB;EAGhD,AAFA,MAAMA,GAASC,CAAI,GACnB,KAAK,OAAO,WAAW,MACvB,OAAO,eAAe,MAAM,WAAW,SAAS;CAClD;AACF,GAGa,8BAAb,cAAiD,eAAe,CAAC,GAEpD,yBAAb,cAA4C,eAAe;CACzD,YAAY,IAAU,6BAA6B;EACjD,MAAMD,CAAO;CACf;AACF"}
package/dist/index.js CHANGED
@@ -1 +1,7 @@
1
- import{SourcererConfigurationError as e,SourcererDisposedError as t,SourcererError as n}from"./errors.js";import{createCursorSource as r}from"./cursorSource.js";import{createInfiniteSource as i}from"./infiniteSource.js";import{createLocalSource as a}from"./localSource.js";import{createPageSource as o}from"./pageSource.js";import{createResource as s}from"./resourceSource.js";export{e as SourcererConfigurationError,t as SourcererDisposedError,n as SourcererError,r as createCursorSource,i as createInfiniteSource,a as createLocalSource,o as createPageSource,s as createResource};
1
+ import { SourcererConfigurationError as e, SourcererDisposedError as t, SourcererError as n } from "./errors.js";
2
+ import { createCursorSource as r } from "./cursorSource.js";
3
+ import { createInfiniteSource as i } from "./infiniteSource.js";
4
+ import { createLocalSource as a } from "./localSource.js";
5
+ import { createPageSource as o } from "./pageSource.js";
6
+ import { createResource as s } from "./resourceSource.js";
7
+ export { e as SourcererConfigurationError, t as SourcererDisposedError, n as SourcererError, r as createCursorSource, i as createInfiniteSource, a as createLocalSource, o as createPageSource, s as createResource };
@@ -1,2 +1,2 @@
1
- const e=require("./errors.cjs"),t=require("./asyncSource.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=t.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(0,i.pageSize,0),params:i.params}),assertLive=()=>{if(s.disposed)throw new e.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(){if(assertLive(),!s.state.loading){if(!o||!Object.is(i.params,s.state.params)){await reload();return}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();return}i={...i,pageSize:t},a=1,await reload()},async setParams(e){if(assertLive(),Object.is(e,i.params)){!s.state.loading&&!Object.is(e,s.state.params)&&await reload();return}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("./errors.cjs"),t=require("./asyncSource.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=t.createAsyncSource({error:null,items:[],loading:!1,pagination:createPagination(0,i.pageSize,0),params:i.params}),assertLive=()=>{if(s.disposed)throw new e.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;
2
2
  //# sourceMappingURL=infiniteSource.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"infiniteSource.cjs","names":[],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"6FAOA,IAAM,kBAAoB,EAAqB,EAAkB,KAAuC,CACtG,QAAS,EAAc,EACvB,cACA,WACA,WAAY,CACd,GAGA,SAAgB,qBACd,EAC4B,CAC5B,IAAI,EAA8B,CAChC,SAAU,EAAA,gBAAgB,EAAO,UAAY,GAAI,UAAU,EAC3D,OAAQ,EAAO,MACjB,E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,MAAM,MAAM,EAAG,CACxE,MAAM,OAAO,EACb,MACF,CACI,EAAY,MAAM,WAAW,SAAS,MAAM,MAAM,EAAW,EAAU,EAAI,CAD/E,CAEF,EAEA,OAEA,MAAM,YAAY,EAAU,CAC1B,WAAW,EACX,IAAM,EAAa,EAAA,gBAAgB,EAAU,UAAU,EACvD,GAAI,IAAe,EAAU,SAAU,CACrC,GAAI,EAAY,MAAM,SAAW,EAAY,MAAM,WAAW,WAAa,EAAY,OACvF,MAAM,OAAO,EACb,MACF,CACA,EAAY,CAAE,GAAG,EAAW,SAAU,CAAW,EACjD,EAAW,EACX,MAAM,OAAO,CACf,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAG,EAAQ,EAAY,MAAM,MAAM,GAAG,MAAM,OAAO,EAC7F,MACF,CACA,EAAY,CAAE,GAAG,EAAW,QAAO,EACnC,EAAW,EACX,MAAM,OAAO,CACf,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
1
+ {"version":3,"file":"infiniteSource.cjs","names":["positiveInteger","createAsyncSource","SourcererDisposedError","totalItems","require_errors","require_asyncSource","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,MAAM,MAEtC,OAAOA,EAAY,MAAM,CACvB,WAAc,CACZ,EAAY,GACZ,EAAWC,EAAO,EAClB,EAAYC,CACd,EACA,SAAU,EAAU,KAAW,CAAE,GAAGC,EAAU,QAAO,QAAS,GAAO,cAAe,IAAA,EAAU,GAC9F,KAAO,GAAWN,EAAO,KAAK,CAAE,OAAM,SAAUK,EAAQ,SAAU,OAAQA,EAAQ,OAAQ,QAAO,CAAC,EAClG,QAAU,IAAc,CACtB,GAAGC,EACH,MAAO,KACP,QAAS,GACT,GAAIC,GAAU,OA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O,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,2 +1,100 @@
1
- import{SourcererDisposedError as e}from"./errors.js";import{createAsyncSource as t}from"./asyncSource.js";import{positiveInteger as n,totalItems as r}from"./pagination.js";var createPagination=(e,t,n)=>({hasMore:e<n,loadedItems:e,pageSize:t,totalItems:n});function createInfiniteSource(i){let a={pageSize:n(i.pageSize??20,`pageSize`),params:i.params},o=1,s=!1,c=t({error:null,items:[],loading:!1,pagination:createPagination(0,a.pageSize,0),params:a.params}),assertLive=()=>{if(c.disposed)throw new e},fetch=(e,t,n)=>{let l=c.state.items;return c.fetch({commit:()=>{s=!0,o=t+1,a=e},failure:(e,t)=>({...e,error:t,loading:!1,pendingParams:void 0}),load:n=>i.load({page:t,pageSize:e.pageSize,params:e.params,signal:n}),pending:t=>({...t,error:null,loading:!0,...n||Object.is(e.params,t.params)?{}:{pendingParams:e.params}}),success:t=>{let i=n?[...l,...t.items]:[...t.items];return{error:null,items:i,loading:!1,pagination:createPagination(i.length,e.pageSize,r(t.totalItems)),params:e.params}}})},reload=()=>fetch(a,1,!1),l={get disposalSignal(){return c.disposalSignal},dispose:c.dispose,get disposed(){return c.disposed},async loadMore(){if(assertLive(),!c.state.loading){if(!s||!Object.is(a.params,c.state.params)){await reload();return}c.state.pagination.hasMore&&await fetch(a,o,!0)}},reload,async setPageSize(e){assertLive();let t=n(e,`pageSize`);if(t===a.pageSize){if(c.state.loading||c.state.pagination.pageSize===t)return;await reload();return}a={...a,pageSize:t},o=1,await reload()},async setParams(e){if(assertLive(),Object.is(e,a.params)){!c.state.loading&&!Object.is(e,c.state.params)&&await reload();return}a={...a,params:e},o=1,await reload()},get state(){return c.state},subscribe:c.subscribe,[Symbol.dispose](){l.dispose()}};return l}export{createInfiniteSource};
1
+ import { SourcererDisposedError as e } from "./errors.js";
2
+ import { createAsyncSource as t } from "./asyncSource.js";
3
+ import { positiveInteger as n, totalItems as r } from "./pagination.js";
4
+ //#region src/infiniteSource.ts
5
+ var createPagination = (e, t, n) => ({
6
+ hasMore: e < n,
7
+ loadedItems: e,
8
+ pageSize: t,
9
+ totalItems: n
10
+ });
11
+ function createInfiniteSource(i) {
12
+ let a = {
13
+ pageSize: n(i.pageSize ?? 20, "pageSize"),
14
+ params: i.params
15
+ }, o = 1, s = !1, c = t({
16
+ error: null,
17
+ items: [],
18
+ loading: !1,
19
+ pagination: createPagination(0, a.pageSize, 0),
20
+ params: a.params
21
+ }), assertLive = () => {
22
+ if (c.disposed) throw new e();
23
+ }, fetch = (e, t, n) => {
24
+ let l = c.state.items;
25
+ return c.fetch({
26
+ commit: () => {
27
+ s = !0, o = t + 1, a = e;
28
+ },
29
+ failure: (e, t) => ({
30
+ ...e,
31
+ error: t,
32
+ loading: !1,
33
+ pendingParams: void 0
34
+ }),
35
+ load: (n) => i.load({
36
+ page: t,
37
+ pageSize: e.pageSize,
38
+ params: e.params,
39
+ signal: n
40
+ }),
41
+ pending: (t) => ({
42
+ ...t,
43
+ error: null,
44
+ loading: !0,
45
+ ...n || Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
46
+ }),
47
+ success: (t) => {
48
+ let i = n ? [...l, ...t.items] : [...t.items];
49
+ return {
50
+ error: null,
51
+ items: i,
52
+ loading: !1,
53
+ pagination: createPagination(i.length, e.pageSize, r(t.totalItems)),
54
+ params: e.params
55
+ };
56
+ }
57
+ });
58
+ }, reload = () => fetch(a, 1, !1), l = {
59
+ get disposalSignal() {
60
+ return c.disposalSignal;
61
+ },
62
+ dispose: c.dispose,
63
+ get disposed() {
64
+ return c.disposed;
65
+ },
66
+ async loadMore() {
67
+ assertLive(), !c.state.loading && (!s || !Object.is(a.params, c.state.params) ? await reload() : c.state.pagination.hasMore && await fetch(a, o, !0));
68
+ },
69
+ reload,
70
+ async setPageSize(e) {
71
+ assertLive();
72
+ let t = n(e, "pageSize");
73
+ if (t === a.pageSize) {
74
+ if (c.state.loading || c.state.pagination.pageSize === t) return;
75
+ await reload();
76
+ } else a = {
77
+ ...a,
78
+ pageSize: t
79
+ }, o = 1, await reload();
80
+ },
81
+ async setParams(e) {
82
+ assertLive(), Object.is(e, a.params) ? !c.state.loading && !Object.is(e, c.state.params) && await reload() : (a = {
83
+ ...a,
84
+ params: e
85
+ }, o = 1, await reload());
86
+ },
87
+ get state() {
88
+ return c.state;
89
+ },
90
+ subscribe: c.subscribe,
91
+ [Symbol.dispose]() {
92
+ l.dispose();
93
+ }
94
+ };
95
+ return l;
96
+ }
97
+ //#endregion
98
+ export { createInfiniteSource };
99
+
2
100
  //# sourceMappingURL=infiniteSource.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"infiniteSource.js","names":[],"sources":["../src/infiniteSource.ts"],"sourcesContent":["import { createAsyncSource } from './asyncSource';\nimport { SourcererDisposedError } from './errors';\nimport { positiveInteger, totalItems } from './pagination';\nimport type { InfinitePagination, InfiniteSource, InfiniteSourceConfig, InfiniteSourceState } from './types';\n\ntype Request<TParams> = Readonly<{ pageSize: number; params: TParams }>;\n\nconst createPagination = (loadedItems: number, pageSize: number, total: number): InfinitePagination => ({\n hasMore: loadedItems < total,\n loadedItems,\n pageSize,\n totalItems: total,\n});\n\n/** Infinite sources retain committed items while replacement work is pending. */\nexport function createInfiniteSource<T, TParams = undefined>(\n config: InfiniteSourceConfig<T, TParams>,\n): InfiniteSource<T, TParams> {\n let requested: Request<TParams> = {\n pageSize: positiveInteger(config.pageSize ?? 20, 'pageSize'),\n params: config.params as TParams,\n };\n let nextPage = 1;\n let hasLoaded = false;\n const asyncSource = createAsyncSource<InfiniteSourceState<T, TParams>>({\n error: null,\n items: [],\n loading: false,\n pagination: createPagination(0, requested.pageSize, 0),\n params: requested.params,\n });\n const assertLive = (): void => {\n if (asyncSource.disposed) throw new SourcererDisposedError();\n };\n\n const fetch = (request: Request<TParams>, page: number, append: boolean): Promise<void> => {\n const loadedItems = asyncSource.state.items;\n\n return asyncSource.fetch({\n commit: () => {\n hasLoaded = true;\n nextPage = page + 1;\n requested = request;\n },\n failure: (previous, error) => ({ ...previous, error, loading: false, pendingParams: undefined }),\n load: (signal) => config.load({ page, pageSize: request.pageSize, params: request.params, signal }),\n pending: (previous) => ({\n ...previous,\n error: null,\n loading: true,\n ...(append || Object.is(request.params, previous.params) ? {} : { pendingParams: request.params }),\n }),\n success: (result) => {\n const items = append ? [...loadedItems, ...result.items] : [...result.items];\n return {\n error: null,\n items,\n loading: false,\n pagination: createPagination(items.length, request.pageSize, totalItems(result.totalItems)),\n params: request.params,\n };\n },\n });\n };\n\n const reload = (): Promise<void> => fetch(requested, 1, false);\n\n const source: InfiniteSource<T, TParams> = {\n get disposalSignal() {\n return asyncSource.disposalSignal;\n },\n\n dispose: asyncSource.dispose,\n\n get disposed() {\n return asyncSource.disposed;\n },\n\n async loadMore() {\n assertLive();\n if (asyncSource.state.loading) return;\n if (!hasLoaded || !Object.is(requested.params, asyncSource.state.params)) {\n await reload();\n return;\n }\n if (asyncSource.state.pagination.hasMore) await fetch(requested, nextPage, true);\n },\n\n reload,\n\n async setPageSize(pageSize) {\n assertLive();\n const normalized = positiveInteger(pageSize, 'pageSize');\n if (normalized === requested.pageSize) {\n if (asyncSource.state.loading || asyncSource.state.pagination.pageSize === normalized) return;\n await reload();\n return;\n }\n requested = { ...requested, pageSize: normalized };\n nextPage = 1;\n await reload();\n },\n\n async setParams(params) {\n assertLive();\n if (Object.is(params, requested.params)) {\n if (!asyncSource.state.loading && !Object.is(params, asyncSource.state.params)) await reload();\n return;\n }\n requested = { ...requested, params };\n nextPage = 1;\n await reload();\n },\n\n get state() {\n return asyncSource.state;\n },\n\n subscribe: asyncSource.subscribe,\n\n [Symbol.dispose]() {\n source.dispose();\n },\n };\n\n return source;\n}\n"],"mappings":"4KAOA,IAAM,kBAAoB,EAAqB,EAAkB,K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,QAErB,IAAI,UAAW,CACb,OAAO,EAAY,QACrB,EAEA,MAAM,UAAW,CACf,cAAW,EACP,GAAY,MAAM,QACtB,IAAI,CAAC,GAAa,CAAC,OAAO,GAAG,EAAU,OAAQ,EAAY,MAAM,MAAM,EAAG,CACxE,MAAM,OAAO,EACb,MACF,CACI,EAAY,MAAM,WAAW,SAAS,MAAM,MAAM,EAAW,EAAU,EAAI,CAD/E,CAEF,EAEA,OAEA,MAAM,YAAY,EAAU,CAC1B,WAAW,EACX,IAAM,EAAa,EAAgB,EAAU,UAAU,EACvD,GAAI,IAAe,EAAU,SAAU,CACrC,GAAI,EAAY,MAAM,SAAW,EAAY,MAAM,WAAW,WAAa,EAAY,OACvF,MAAM,OAAO,EACb,MACF,CACA,EAAY,CAAE,GAAG,EAAW,SAAU,CAAW,EACjD,EAAW,EACX,MAAM,OAAO,CACf,EAEA,MAAM,UAAU,EAAQ,CAEtB,GADA,WAAW,EACP,OAAO,GAAG,EAAQ,EAAU,MAAM,EAAG,CACnC,CAAC,EAAY,MAAM,SAAW,CAAC,OAAO,GAAG,EAAQ,EAAY,MAAM,MAAM,GAAG,MAAM,OAAO,EAC7F,MACF,CACA,EAAY,CAAE,GAAG,EAAW,QAAO,EACnC,EAAW,EACX,MAAM,OAAO,CACf,EAEA,IAAI,OAAQ,CACV,OAAO,EAAY,KACrB,EAEA,UAAW,EAAY,UAEvB,CAAC,OAAO,UAAW,CACjB,EAAO,QAAQ,CACjB,CACF,EAEA,OAAO,CACT"}
1
+ {"version":3,"file":"infiniteSource.js","names":["loadedItems","total","requested","positiveInteger","config","nextPage","hasLoaded","asyncSource","createAsyncSource","SourcererDisposedError","page","request","previous","append","items","result","totalItems","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":";;;;AAOA,IAAM,oBAAoB,GAAqB,GAAkB,OAAuC;CACtG,SAASA,IAAcC;CACvB;CACA;CACA,YAAYA;AACd;AAGA,SAAgB,qBACd,GAC4B;CAC5B,IAAIC,IAA8B;EAChC,UAAUC,EAAgBC,EAAO,YAAY,IAAI,UAAU;EAC3D,QAAQA,EAAO;CACjB,GACIC,IAAW,GACXC,IAAY,IACVC,IAAcC,EAAmD;EACrE,OAAO;EACP,OAAO,CAAC;EACR,SAAS;EACT,YAAY,iBAAiB,GAAGN,EAAU,UAAU,CAAC;EACrD,QAAQA,EAAU;CACpB,CAAC,GACK,mBAAyB;EAC7B,IAAIK,EAAY,UAAU,MAAM,IAAIE,EAAuB;CAC7D,GAEM,SAAS,GAA2B,GAAc,MAAmC;EACzF,IAAMT,IAAcO,EAAY,MAAM;EAEtC,OAAOA,EAAY,MAAM;GACvB,cAAc;IAGZ,AAFA,IAAY,IACZ,IAAWG,IAAO,GAClB,IAAYC;GACd;GACA,UAAU,GAAU,OAAW;IAAE,GAAGC;IAAU;IAAO,SAAS;IAAO,eAAe,KAAA;GAAU;GAC9F,OAAO,MAAWR,EAAO,KAAK;IAAE;IAAM,UAAUO,EAAQ;IAAU,Q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