@vielzeug/sourcerer 2.2.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -18
- package/dist/asyncSource.cjs +1 -1
- package/dist/asyncSource.cjs.map +1 -1
- package/dist/asyncSource.d.ts +13 -12
- package/dist/asyncSource.d.ts.map +1 -1
- package/dist/asyncSource.js +46 -23
- package/dist/asyncSource.js.map +1 -1
- package/dist/cursorSource.cjs +1 -1
- package/dist/cursorSource.cjs.map +1 -1
- package/dist/cursorSource.d.ts +2 -2
- package/dist/cursorSource.d.ts.map +1 -1
- package/dist/cursorSource.js +82 -59
- package/dist/cursorSource.js.map +1 -1
- package/dist/errors.cjs +2 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +18 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -5
- package/dist/infiniteSource.cjs +1 -1
- package/dist/infiniteSource.cjs.map +1 -1
- package/dist/infiniteSource.d.ts +2 -2
- package/dist/infiniteSource.d.ts.map +1 -1
- package/dist/infiniteSource.js +76 -53
- package/dist/infiniteSource.js.map +1 -1
- package/dist/localSource.cjs +1 -1
- package/dist/localSource.cjs.map +1 -1
- package/dist/localSource.d.ts +3 -2
- package/dist/localSource.d.ts.map +1 -1
- package/dist/localSource.js +54 -57
- package/dist/localSource.js.map +1 -1
- package/dist/pageSource.cjs +1 -1
- package/dist/pageSource.cjs.map +1 -1
- package/dist/pageSource.d.ts +2 -2
- package/dist/pageSource.d.ts.map +1 -1
- package/dist/pageSource.js +84 -63
- package/dist/pageSource.js.map +1 -1
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.cjs.map +1 -1
- package/dist/pagination.d.ts +1 -2
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +16 -19
- package/dist/pagination.js.map +1 -1
- package/dist/sourceStore.cjs.map +1 -1
- package/dist/sourceStore.d.ts +5 -2
- package/dist/sourceStore.d.ts.map +1 -1
- package/dist/sourceStore.js.map +1 -1
- package/dist/sourcerer.cjs +1 -1
- package/dist/sourcerer.cjs.map +1 -1
- package/dist/sourcerer.iife.js +1 -1
- package/dist/sourcerer.iife.js.map +1 -1
- package/dist/sourcerer.js +1 -1
- package/dist/sourcerer.js.map +1 -1
- package/dist/types.d.ts +130 -117
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -7
- package/dist/_dev.cjs +0 -2
- package/dist/_dev.cjs.map +0 -1
- package/dist/_dev.d.ts +0 -2
- package/dist/_dev.d.ts.map +0 -1
- package/dist/_dev.js +0 -6
- package/dist/_dev.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,43 +1,42 @@
|
|
|
1
1
|
# @vielzeug/sourcerer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Reactive collection sources with local, page, cursor, and infinite pagination
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
4
6
|
|
|
5
7
|
```sh
|
|
6
8
|
pnpm add @vielzeug/sourcerer
|
|
9
|
+
npm install @vielzeug/sourcerer
|
|
10
|
+
yarn add @vielzeug/sourcerer
|
|
7
11
|
```
|
|
8
12
|
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
9
15
|
```ts
|
|
10
16
|
import { createPageSource } from '@vielzeug/sourcerer';
|
|
11
17
|
|
|
12
18
|
const users = createPageSource({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
load: async ({ page, pageSize }) => ({
|
|
20
|
+
items: [{ id: page, name: 'Ada' }].slice(0, pageSize),
|
|
21
|
+
totalItems: 1,
|
|
22
|
+
}),
|
|
23
|
+
pageSize: 20,
|
|
16
24
|
});
|
|
17
25
|
|
|
18
26
|
await users.reload();
|
|
19
|
-
console.log(users.
|
|
20
|
-
console.log(users.
|
|
27
|
+
console.log(users.state.items);
|
|
28
|
+
console.log(users.state.pagination.totalItems);
|
|
21
29
|
users.dispose();
|
|
22
30
|
```
|
|
23
31
|
|
|
24
|
-
Every source exposes an atomic `snapshot` and `subscribe(listener)`. `setQuery()` changes query fields; `page.*` changes page index. While newer work is active, `pendingQuery` records it without mixing it into loaded data. Page sources cancel superseded requests and preserve successful data on background refresh failures.
|
|
25
|
-
|
|
26
|
-
| Factory | Purpose |
|
|
27
|
-
| --- | --- |
|
|
28
|
-
| `createLocalSource()` | Synchronous in-memory collection |
|
|
29
|
-
| `createPageSource()` | Numbered async pages |
|
|
30
|
-
| `createCursorSource()` | Cursor-based async pages |
|
|
31
|
-
| `createInfiniteSource()` | Appended async pages |
|
|
32
|
-
|
|
33
|
-
Sourcerer manages collection state and request succession. Configure retries, cache, polling, URL serialization, and optimistic mutation with your transport or application layer.
|
|
34
|
-
|
|
35
32
|
## Documentation
|
|
36
33
|
|
|
37
34
|
- [Overview](https://vielzeug.dev/sourcerer/)
|
|
38
35
|
- [Usage Guide](https://vielzeug.dev/sourcerer/usage)
|
|
39
36
|
- [API Reference](https://vielzeug.dev/sourcerer/api)
|
|
37
|
+
- [Examples](https://vielzeug.dev/sourcerer/examples)
|
|
38
|
+
- [Migration Guide](https://vielzeug.dev/sourcerer/migration)
|
|
40
39
|
|
|
41
40
|
## License
|
|
42
41
|
|
|
43
|
-
MIT © Helmuth Saatkamp — part of Vielzeug.
|
|
42
|
+
MIT © [Helmuth Saatkamp](https://github.com/helmuthdu) — part of the [Vielzeug](https://github.com/helmuthdu/vielzeug) monorepo.
|
package/dist/asyncSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./requestController.cjs"),n=require("./sourceStore.cjs");let r=require("@vielzeug/arsenal");var i=(e,t)=>new Promise((n,i)=>{let a=()=>i((0,r.abortError)(t,`The operation was aborted.`));if(t.aborted){a();return}t.addEventListener(`abort`,a,{once:!0});let o;try{o=e(t)}catch(e){t.removeEventListener(`abort`,a),i(e);return}o.then(e=>{t.removeEventListener(`abort`,a),n(e)},e=>{t.removeEventListener(`abort`,a),i(e)})});function a(r){let a=n.createSourceStore(r),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 r=o.begin(),s=a.value;a.set(n.pending(s));try{let e=await i(n.load,r.signal);if(!r.isCurrent()||a.disposed)return;let t=n.success(e);n.commit?.(t,e),a.set(t)}catch(e){if(!r.isCurrent()||a.disposed)return;let i=t.toError(e);throw a.set(n.failure(s,i)),i}finally{r.finish()}},get state(){return a.value},subscribe:a.subscribe}}exports.createAsyncSource=a;
|
|
2
2
|
//# sourceMappingURL=asyncSource.cjs.map
|
package/dist/asyncSource.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.cjs","names":[],"sources":["../src/asyncSource.ts"],"sourcesContent":["import {
|
|
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,GAAqB,EAA2C,IACpE,IAAI,SAAS,EAAS,IAAW,CAC/B,IAAM,MAAoB,GAAA,EAAO,EAAA,WAAA,CAAW,EAAQ,4BAA4B,CAAC,EACjF,GAAI,EAAO,QAAS,CAClB,EAAM,EACN,MACF,CAEA,EAAO,iBAAiB,QAAS,EAAO,CAAE,KAAM,EAAK,CAAC,EACtD,IAAI,EACJ,GAAI,CACF,EAAS,EAAK,CAAM,CACtB,OAAS,EAAO,CACd,EAAO,oBAAoB,QAAS,CAAK,EACzC,EAAO,CAAK,EACZ,MACF,CAEA,EAAO,KACJ,GAAU,CACT,EAAO,oBAAoB,QAAS,CAAK,EACzC,EAAQ,CAAK,CACf,EACC,GAAmB,CAClB,EAAO,oBAAoB,QAAS,CAAK,EACzC,EAAO,CAAK,CACd,CACF,CACF,CAAC,EAoBH,SAAgB,EAA0B,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"}
|
package/dist/asyncSource.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type FetchOptions<TState, TResult> = {
|
|
2
|
+
load(signal: AbortSignal): Promise<TResult>;
|
|
3
|
+
success(result: TResult): TState;
|
|
4
|
+
commit?(state: TState, result: TResult): void;
|
|
5
|
+
pending(previous: TState): TState;
|
|
6
|
+
failure(previous: TState, error: Error): TState;
|
|
7
|
+
};
|
|
8
|
+
type AsyncSource<TState> = Readonly<{
|
|
3
9
|
readonly disposalSignal: AbortSignal;
|
|
4
10
|
dispose(): void;
|
|
5
11
|
readonly disposed: boolean;
|
|
6
|
-
fetch<TResult>(options:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
pending(previous: SourceSnapshot<T, TQuery, TPagination>): SourceSnapshot<T, TQuery, TPagination>;
|
|
10
|
-
success(result: TResult): SourceSnapshot<T, TQuery, TPagination>;
|
|
11
|
-
}): Promise<void>;
|
|
12
|
-
readonly snapshot: SourceSnapshot<T, TQuery, TPagination>;
|
|
13
|
-
subscribe(listener: (snapshot: SourceSnapshot<T, TQuery, TPagination>) => void): () => void;
|
|
12
|
+
fetch<TResult>(options: FetchOptions<TState, TResult>): Promise<void>;
|
|
13
|
+
readonly state: TState;
|
|
14
|
+
subscribe(listener: (state: TState) => void): () => void;
|
|
14
15
|
}>;
|
|
15
|
-
/**
|
|
16
|
-
export declare function createAsyncSource<
|
|
16
|
+
/** Only the current request may commit; superseded work settles without changing source state. */
|
|
17
|
+
export declare function createAsyncSource<TState>(initial: TState): AsyncSource<TState>;
|
|
17
18
|
export {};
|
|
18
19
|
//# sourceMappingURL=asyncSource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.d.ts","sourceRoot":"","sources":["../src/asyncSource.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"asyncSource.d.ts","sourceRoot":"","sources":["../src/asyncSource.ts"],"names":[],"mappings":"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;IAC3B,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CAC1D,CAAC,CAAC;AAEH,kGAAkG;AAClG,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAiD9E"}
|
package/dist/asyncSource.js
CHANGED
|
@@ -1,41 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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";
|
|
3
5
|
//#region src/asyncSource.ts
|
|
4
|
-
|
|
5
|
-
let
|
|
6
|
+
var a = (e, t) => new Promise((n, r) => {
|
|
7
|
+
let a = () => r(i(t, "The operation was aborted."));
|
|
8
|
+
if (t.aborted) {
|
|
9
|
+
a();
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
t.addEventListener("abort", a, { once: !0 });
|
|
13
|
+
let o;
|
|
14
|
+
try {
|
|
15
|
+
o = e(t);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
t.removeEventListener("abort", a), r(e);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
o.then((e) => {
|
|
21
|
+
t.removeEventListener("abort", a), n(e);
|
|
22
|
+
}, (e) => {
|
|
23
|
+
t.removeEventListener("abort", a), r(e);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
function o(i) {
|
|
27
|
+
let o = r(i), s = t();
|
|
6
28
|
return {
|
|
7
29
|
get disposalSignal() {
|
|
8
|
-
return
|
|
30
|
+
return o.disposalSignal;
|
|
9
31
|
},
|
|
10
32
|
dispose() {
|
|
11
|
-
|
|
33
|
+
s.dispose(), o.dispose();
|
|
12
34
|
},
|
|
13
35
|
get disposed() {
|
|
14
|
-
return
|
|
36
|
+
return o.disposed;
|
|
15
37
|
},
|
|
16
|
-
async fetch(
|
|
17
|
-
if (
|
|
18
|
-
let
|
|
19
|
-
|
|
20
|
-
let o;
|
|
38
|
+
async fetch(t) {
|
|
39
|
+
if (o.disposed) throw new e();
|
|
40
|
+
let r = s.begin(), i = o.value;
|
|
41
|
+
o.set(t.pending(i));
|
|
21
42
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
43
|
+
let e = await a(t.load, r.signal);
|
|
44
|
+
if (!r.isCurrent() || o.disposed) return;
|
|
45
|
+
let n = t.success(e);
|
|
46
|
+
t.commit?.(n, e), o.set(n);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
if (!r.isCurrent() || o.disposed) return;
|
|
49
|
+
let a = n(e);
|
|
50
|
+
throw o.set(t.failure(i, a)), a;
|
|
27
51
|
} finally {
|
|
28
|
-
|
|
52
|
+
r.finish();
|
|
29
53
|
}
|
|
30
|
-
!n.isCurrent() || i.disposed || i.set(o);
|
|
31
54
|
},
|
|
32
|
-
get
|
|
33
|
-
return
|
|
55
|
+
get state() {
|
|
56
|
+
return o.value;
|
|
34
57
|
},
|
|
35
|
-
subscribe:
|
|
58
|
+
subscribe: o.subscribe
|
|
36
59
|
};
|
|
37
60
|
}
|
|
38
61
|
//#endregion
|
|
39
|
-
export {
|
|
62
|
+
export { o as createAsyncSource };
|
|
40
63
|
|
|
41
64
|
//# sourceMappingURL=asyncSource.js.map
|
package/dist/asyncSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncSource.js","names":[],"sources":["../src/asyncSource.ts"],"sourcesContent":["import {
|
|
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":";;;;;AAMA,IAAM,KAAqB,GAA2C,MACpE,IAAI,SAAS,GAAS,MAAW;CAC/B,IAAM,UAAoB,EAAO,EAAW,GAAQ,4BAA4B,CAAC;CACjF,IAAI,EAAO,SAAS;EAClB,EAAM;EACN;CACF;CAEA,EAAO,iBAAiB,SAAS,GAAO,EAAE,MAAM,GAAK,CAAC;CACtD,IAAI;CACJ,IAAI;EACF,IAAS,EAAK,CAAM;CACtB,SAAS,GAAO;EAEd,AADA,EAAO,oBAAoB,SAAS,CAAK,GACzC,EAAO,CAAK;EACZ;CACF;CAEA,EAAO,MACJ,MAAU;EAET,AADA,EAAO,oBAAoB,SAAS,CAAK,GACzC,EAAQ,CAAK;CACf,IACC,MAAmB;EAElB,AADA,EAAO,oBAAoB,SAAS,CAAK,GACzC,EAAO,CAAK;CACd,CACF;AACF,CAAC;AAoBH,SAAgB,EAA0B,GAAsC;CAC9E,IAAM,IAAQ,EAAkB,CAAO,GACjC,IAAW,EAAwB;CAEzC,OAAO;EACL,IAAI,iBAAiB;GACnB,OAAO,EAAM;EACf;EAEA,UAAU;GAER,AADA,EAAS,QAAQ,GACjB,EAAM,QAAQ;EAChB;EAEA,IAAI,WAAW;GACb,OAAO,EAAM;EACf;EAEA,MAAM,MAAM,GAAS;GACnB,IAAI,EAAM,UAAU,MAAM,IAAI,EAAuB;GAErD,IAAM,IAAU,EAAS,MAAM,GACzB,IAAW,EAAM;GACvB,EAAM,IAAI,EAAQ,QAAQ,CAAQ,CAAC;GAEnC,IAAI;IACF,IAAM,IAAS,MAAM,EAAe,EAAQ,MAAM,EAAQ,MAAM;IAChE,IAAI,CAAC,EAAQ,UAAU,KAAK,EAAM,UAAU;IAE5C,IAAM,IAAQ,EAAQ,QAAQ,CAAM;IAEpC,AADA,EAAQ,SAAS,GAAO,CAAM,GAC9B,EAAM,IAAI,CAAK;GACjB,SAAS,GAAiB;IACxB,IAAI,CAAC,EAAQ,UAAU,KAAK,EAAM,UAAU;IAE5C,IAAM,IAAQ,EAAQ,CAAM;IAE5B,MADA,EAAM,IAAI,EAAQ,QAAQ,GAAU,CAAK,CAAC,GACpC;GACR,UAAU;IACR,EAAQ,OAAO;GACjB;EACF;EAEA,IAAI,QAAQ;GACV,OAAO,EAAM;EACf;EAEA,WAAW,EAAM;CACnB;AACF"}
|
package/dist/cursorSource.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./asyncSource.cjs"),n=require("./pagination.cjs");var r=(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 i(i){if(i.after!==void 0&&i.before!==void 0)throw new e.SourcererConfigurationError(`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:n.positiveInteger(i.pageSize??20,`pageSize`),params:i.params},o=t.createAsyncSource({error:null,items:[],loading:!1,pagination:r(a.pageSize),params:a.params}),s=()=>{if(o.disposed)throw new e.SourcererDisposedError},c=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:r(e.pageSize,t),params:e.params})}),l={get disposalSignal(){return o.disposalSignal},dispose:o.dispose,get disposed(){return o.disposed},async next(){s();let e=o.state.pagination.nextCursor;o.state.loading||e===void 0||(a={after:e,pageSize:a.pageSize,params:a.params},await c(a))},async previous(){s();let e=o.state.pagination.previousCursor;o.state.loading||e===void 0||(a={before:e,pageSize:a.pageSize,params:a.params},await c(a))},reload(){return c(a)},async setPageSize(e){s();let t=n.positiveInteger(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 c(a);return}a={pageSize:t,params:a.params},await c(a)},async setParams(e){if(s(),Object.is(e,a.params)){!o.state.loading&&!Object.is(e,o.state.params)&&await c(a);return}a={pageSize:a.pageSize,params:e},await c(a)},get state(){return o.state},subscribe:o.subscribe,[Symbol.dispose](){l.dispose()}};return l}exports.createCursorSource=i;
|
|
2
2
|
//# sourceMappingURL=cursorSource.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.cjs","names":[],"sources":["../src/cursorSource.ts"],"sourcesContent":["import {
|
|
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":"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"}
|
package/dist/cursorSource.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { CursorSource, CursorSourceConfig } from './types';
|
|
2
|
-
/** Cursor sources retain
|
|
3
|
-
export declare function createCursorSource<T, TCursor = string>(config: CursorSourceConfig<T, TCursor>): CursorSource<T, TCursor>;
|
|
2
|
+
/** Cursor sources retain committed items and cursors while replacement work is pending. */
|
|
3
|
+
export declare function createCursorSource<T, TParams = undefined, TCursor = string>(config: CursorSourceConfig<T, TParams, TCursor>): CursorSource<T, TParams, TCursor>;
|
|
4
4
|
//# sourceMappingURL=cursorSource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.d.ts","sourceRoot":"","sources":["../src/cursorSource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
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,CA+GnC"}
|
package/dist/cursorSource.js
CHANGED
|
@@ -1,62 +1,53 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createAsyncSource as
|
|
3
|
-
import { positiveInteger as
|
|
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
4
|
//#region src/cursorSource.ts
|
|
5
|
-
var a = (e) => ({
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
...
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
search: t.search ?? e.search
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
function s(n) {
|
|
23
|
-
let i = o({
|
|
24
|
-
pageSize: 20,
|
|
25
|
-
search: ""
|
|
26
|
-
}, n.initialQuery), s = t({
|
|
27
|
-
data: [],
|
|
5
|
+
var a = (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 o(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 o = {
|
|
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
|
+
}, s = n({
|
|
28
19
|
error: null,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
items: [],
|
|
21
|
+
loading: !1,
|
|
22
|
+
pagination: a(o.pageSize),
|
|
23
|
+
params: o.params
|
|
24
|
+
}), c = () => {
|
|
25
|
+
if (s.disposed) throw new t();
|
|
26
|
+
}, l = (e) => s.fetch({
|
|
33
27
|
failure: (e, t) => ({
|
|
34
28
|
...e,
|
|
35
29
|
error: t,
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
loading: !1,
|
|
31
|
+
pendingParams: void 0
|
|
38
32
|
}),
|
|
39
|
-
load: (t) =>
|
|
40
|
-
|
|
33
|
+
load: (t) => i.load({
|
|
34
|
+
...e,
|
|
41
35
|
signal: t
|
|
42
36
|
}),
|
|
43
37
|
pending: (t) => ({
|
|
44
38
|
...t,
|
|
45
39
|
error: null,
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
loading: !0,
|
|
41
|
+
...Object.is(e.params, t.params) ? {} : { pendingParams: e.params }
|
|
48
42
|
}),
|
|
49
43
|
success: (t) => ({
|
|
50
|
-
data: t.data,
|
|
51
44
|
error: null,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
items: [...t.items],
|
|
46
|
+
loading: !1,
|
|
47
|
+
pagination: a(e.pageSize, t),
|
|
48
|
+
params: e.params
|
|
55
49
|
})
|
|
56
|
-
}),
|
|
57
|
-
let t = o(s.snapshot.pendingQuery ?? s.snapshot.query, e);
|
|
58
|
-
r(t, i) || (i = t, await c(t));
|
|
59
|
-
}, d = {
|
|
50
|
+
}), u = {
|
|
60
51
|
get disposalSignal() {
|
|
61
52
|
return s.disposalSignal;
|
|
62
53
|
},
|
|
@@ -64,29 +55,61 @@ function s(n) {
|
|
|
64
55
|
get disposed() {
|
|
65
56
|
return s.disposed;
|
|
66
57
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
58
|
+
async next() {
|
|
59
|
+
c();
|
|
60
|
+
let e = s.state.pagination.nextCursor;
|
|
61
|
+
s.state.loading || e === void 0 || (o = {
|
|
62
|
+
after: e,
|
|
63
|
+
pageSize: o.pageSize,
|
|
64
|
+
params: o.params
|
|
65
|
+
}, await l(o));
|
|
66
|
+
},
|
|
67
|
+
async previous() {
|
|
68
|
+
c();
|
|
69
|
+
let e = s.state.pagination.previousCursor;
|
|
70
|
+
s.state.loading || e === void 0 || (o = {
|
|
71
|
+
before: e,
|
|
72
|
+
pageSize: o.pageSize,
|
|
73
|
+
params: o.params
|
|
74
|
+
}, await l(o));
|
|
75
|
+
},
|
|
76
|
+
reload() {
|
|
77
|
+
return l(o);
|
|
78
|
+
},
|
|
79
|
+
async setPageSize(e) {
|
|
80
|
+
c();
|
|
81
|
+
let t = r(e, "pageSize");
|
|
82
|
+
if (t === o.pageSize && o.after === void 0 && o.before === void 0) {
|
|
83
|
+
if (s.state.loading || s.state.pagination.pageSize === t && Object.is(s.state.params, o.params)) return;
|
|
84
|
+
await l(o);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
o = {
|
|
88
|
+
pageSize: t,
|
|
89
|
+
params: o.params
|
|
90
|
+
}, await l(o);
|
|
91
|
+
},
|
|
92
|
+
async setParams(e) {
|
|
93
|
+
if (c(), Object.is(e, o.params)) {
|
|
94
|
+
!s.state.loading && !Object.is(e, s.state.params) && await l(o);
|
|
95
|
+
return;
|
|
75
96
|
}
|
|
97
|
+
o = {
|
|
98
|
+
pageSize: o.pageSize,
|
|
99
|
+
params: e
|
|
100
|
+
}, await l(o);
|
|
76
101
|
},
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
get snapshot() {
|
|
80
|
-
return s.snapshot;
|
|
102
|
+
get state() {
|
|
103
|
+
return s.state;
|
|
81
104
|
},
|
|
82
105
|
subscribe: s.subscribe,
|
|
83
106
|
[Symbol.dispose]() {
|
|
84
|
-
|
|
107
|
+
u.dispose();
|
|
85
108
|
}
|
|
86
109
|
};
|
|
87
|
-
return
|
|
110
|
+
return u;
|
|
88
111
|
}
|
|
89
112
|
//#endregion
|
|
90
|
-
export {
|
|
113
|
+
export { o as createCursorSource };
|
|
91
114
|
|
|
92
115
|
//# sourceMappingURL=cursorSource.js.map
|
package/dist/cursorSource.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursorSource.js","names":[],"sources":["../src/cursorSource.ts"],"sourcesContent":["import {
|
|
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":"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"}
|
package/dist/errors.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=class extends Error{static errorName=`SourcererError`;constructor(e,t){super(e,t),this.name=new.target.errorName,Object.setPrototypeOf(this,new.target.prototype)}},t=class extends e{static errorName=`SourcererConfigurationError`},n=class extends e{static errorName=`SourcererDisposedError`;constructor(e=`Sourcerer source disposed`){super(e)}};exports.SourcererConfigurationError=t,exports.SourcererDisposedError=n,exports.SourcererError=e;
|
|
2
|
+
//# sourceMappingURL=errors.cjs.map
|
|
@@ -0,0 +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 protected static readonly errorName: string = 'SourcererError';\n\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = (new.target as typeof SourcererError).errorName;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when a source is configured or queried with invalid arguments. */\nexport class SourcererConfigurationError extends SourcererError {\n protected static override readonly errorName = 'SourcererConfigurationError';\n}\n\nexport class SourcererDisposedError extends SourcererError {\n protected static override readonly errorName = 'SourcererDisposedError';\n\n constructor(message = 'Sourcerer source disposed') {\n super(message);\n }\n}\n"],"mappings":"AACA,IAAa,EAAb,cAAoC,KAAM,CACxC,OAA0B,UAAoB,iBAE9C,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAQ,WAAqC,UAClD,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,EAAb,cAAiD,CAAe,CAC9D,OAAmC,UAAY,6BACjD,EAEa,EAAb,cAA4C,CAAe,CACzD,OAAmC,UAAY,yBAE/C,YAAY,EAAU,4BAA6B,CACjD,MAAM,CAAO,CACf,CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Base class for all sourcerer errors. Use `instanceof SourcererError` to catch any sourcerer-originated error. */
|
|
2
|
+
export declare class SourcererError extends Error {
|
|
3
|
+
protected static readonly errorName: string;
|
|
4
|
+
constructor(message: string, opts?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
/** Thrown when a source is configured or queried with invalid arguments. */
|
|
7
|
+
export declare class SourcererConfigurationError extends SourcererError {
|
|
8
|
+
protected static readonly errorName = "SourcererConfigurationError";
|
|
9
|
+
}
|
|
10
|
+
export declare class SourcererDisposedError extends SourcererError {
|
|
11
|
+
protected static readonly errorName = "SourcererDisposedError";
|
|
12
|
+
constructor(message?: string);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,oHAAoH;AACpH,qBAAa,cAAe,SAAQ,KAAK;IACvC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAoB;gBAEnD,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY;CAKjD;AAED,4EAA4E;AAC5E,qBAAa,2BAA4B,SAAQ,cAAc;IAC7D,0BAAmC,SAAS,iCAAiC;CAC9E;AAED,qBAAa,sBAAuB,SAAQ,cAAc;IACxD,0BAAmC,SAAS,4BAA4B;gBAE5D,OAAO,SAA8B;CAGlD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
var e = class extends Error {
|
|
3
|
+
static errorName = "SourcererError";
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
super(e, t), this.name = new.target.errorName, Object.setPrototypeOf(this, new.target.prototype);
|
|
6
|
+
}
|
|
7
|
+
}, t = class extends e {
|
|
8
|
+
static errorName = "SourcererConfigurationError";
|
|
9
|
+
}, n = class extends e {
|
|
10
|
+
static errorName = "SourcererDisposedError";
|
|
11
|
+
constructor(e = "Sourcerer source disposed") {
|
|
12
|
+
super(e);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { t as SourcererConfigurationError, n as SourcererDisposedError, e as SourcererError };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=errors.js.map
|