@orpc/shared 2.0.0-beta.49 → 2.0.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/args.d.ts +1 -1
- package/dist/args.js +4 -2
- package/dist/array.d.ts +4 -1
- package/dist/array.js +11 -5
- package/dist/bigint.d.ts +1 -1
- package/dist/bigint.js +12 -16
- package/dist/buffer.d.ts +4 -4
- package/dist/buffer.js +44 -67
- package/dist/compare.d.ts +1 -1
- package/dist/compare.js +23 -42
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +3 -1
- package/dist/error.d.ts +1 -1
- package/dist/error.js +4 -2
- package/dist/function.d.ts +1 -1
- package/dist/function.js +27 -32
- package/dist/http.d.ts +29 -0
- package/dist/http.js +128 -94
- package/dist/id.d.ts +1 -1
- package/dist/id.js +11 -11
- package/dist/index.d.ts +26 -34
- package/dist/index.js +23 -32
- package/dist/interceptor.d.ts +17 -17
- package/dist/interceptor.js +106 -126
- package/dist/iterator.d.ts +41 -41
- package/dist/iterator.js +136 -152
- package/dist/object.d.ts +10 -8
- package/dist/object.js +176 -227
- package/dist/plugin.d.ts +7 -7
- package/dist/plugin.js +41 -57
- package/dist/promise.d.ts +4 -4
- package/dist/promise.js +28 -28
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.js +23 -22
- package/dist/queue.d.ts +13 -13
- package/dist/queue.js +64 -79
- package/dist/regex.d.ts +9 -0
- package/dist/regex.js +11 -0
- package/dist/signal.d.ts +7 -2
- package/dist/signal.js +83 -80
- package/dist/stream.d.ts +23 -23
- package/dist/stream.js +118 -144
- package/dist/tracing.d.ts +50 -50
- package/dist/tracing.js +60 -85
- package/dist/types.d.ts +7 -10
- package/dist/value.d.ts +1 -1
- package/dist/value.js +5 -5
- package/package.json +2 -2
- package/dist/types.js +0 -1
package/dist/interceptor.js
CHANGED
|
@@ -1,139 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { wrapAsyncIterator } from "./iterator.js";
|
|
2
|
+
import { override } from "./proxy.js";
|
|
3
|
+
import { wrapReadableStream } from "./stream.js";
|
|
4
|
+
import { isAsyncIteratorObject } from "@standard-server/shared";
|
|
5
|
+
// src/interceptor.ts
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
7
|
+
* Can used for interceptors or middlewares
|
|
8
|
+
*/
|
|
9
|
+
function onStart(callback) {
|
|
10
|
+
return async (options, ...rest) => {
|
|
11
|
+
await callback(options, ...rest);
|
|
12
|
+
return await options.next();
|
|
13
|
+
};
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
16
|
+
* Can used for interceptors or middlewares
|
|
17
|
+
*/
|
|
18
|
+
function onSuccess(callback) {
|
|
19
|
+
return async (options, ...rest) => {
|
|
20
|
+
const result = await options.next();
|
|
21
|
+
await callback(result, options, ...rest);
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
};
|
|
26
|
+
* Creates an interceptor or middleware that invokes a callback whenever an error
|
|
27
|
+
* is thrown. The error is rethrown after the callback completes.
|
|
28
|
+
*
|
|
29
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
30
|
+
*/
|
|
31
|
+
function onError(callback) {
|
|
32
|
+
return async (options, ...rest) => {
|
|
33
|
+
try {
|
|
34
|
+
return await options.next();
|
|
35
|
+
} catch (error) {
|
|
36
|
+
await callback(error, options, ...rest);
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
43
40
|
}
|
|
44
41
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
42
|
+
* Can used for interceptors or middlewares
|
|
43
|
+
*/
|
|
44
|
+
function onFinish(callback) {
|
|
45
|
+
return async (options, ...rest) => {
|
|
46
|
+
let state;
|
|
47
|
+
try {
|
|
48
|
+
const result = await options.next();
|
|
49
|
+
state = [
|
|
50
|
+
null,
|
|
51
|
+
result,
|
|
52
|
+
true
|
|
53
|
+
];
|
|
54
|
+
return result;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
state = [
|
|
57
|
+
error,
|
|
58
|
+
void 0,
|
|
59
|
+
false
|
|
60
|
+
];
|
|
61
|
+
throw error;
|
|
62
|
+
} finally {
|
|
63
|
+
await callback(state, options, ...rest);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
64
66
|
}
|
|
65
67
|
/**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @warning
|
|
87
|
-
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
88
|
-
*/
|
|
89
|
-
return override(output, wrapAsyncIterator(output, {
|
|
90
|
-
onError: (error) => callback(error, options, ...rest),
|
|
91
|
-
}));
|
|
92
|
-
};
|
|
68
|
+
* Creates a middleware or interceptor that invokes a callback when the returned async
|
|
69
|
+
* iterator object throws an error while being consumed.
|
|
70
|
+
*
|
|
71
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
72
|
+
* initial call (before the interceptor returns the iterator), whereas this
|
|
73
|
+
* callback only fires while consuming the iterator. Use both together to
|
|
74
|
+
* catch all possible errors.
|
|
75
|
+
*/
|
|
76
|
+
function onAsyncIteratorObjectError(callback) {
|
|
77
|
+
return async (options, ...rest) => {
|
|
78
|
+
const output = await options.next();
|
|
79
|
+
if (!isAsyncIteratorObject(output)) return output;
|
|
80
|
+
/**
|
|
81
|
+
* @warning
|
|
82
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
83
|
+
*/
|
|
84
|
+
return override(output, wrapAsyncIterator(output, { onError: (error) => callback(error, options, ...rest) }));
|
|
85
|
+
};
|
|
93
86
|
}
|
|
94
87
|
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return output;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* @warning
|
|
117
|
-
* Remember use `override` for ReadableStream to remain other special properties
|
|
118
|
-
*/
|
|
119
|
-
return override(output, wrapReadableStream(output, {
|
|
120
|
-
onError: (error) => callback(error, options, ...rest),
|
|
121
|
-
}));
|
|
122
|
-
};
|
|
88
|
+
* Creates an interceptor that invokes a callback when the returned readable
|
|
89
|
+
* stream errors while being consumed.
|
|
90
|
+
*
|
|
91
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
92
|
+
* initial call (before the interceptor returns the stream), whereas this
|
|
93
|
+
* callback only fires while consuming the stream. Use both together to catch
|
|
94
|
+
* all possible errors.
|
|
95
|
+
*/
|
|
96
|
+
function onReadableStreamError(callback) {
|
|
97
|
+
return async (options, ...rest) => {
|
|
98
|
+
const output = await options.next();
|
|
99
|
+
if (!(output instanceof ReadableStream)) return output;
|
|
100
|
+
/**
|
|
101
|
+
* @warning
|
|
102
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
103
|
+
*/
|
|
104
|
+
return override(output, wrapReadableStream(output, { onError: (error) => callback(error, options, ...rest) }));
|
|
105
|
+
};
|
|
123
106
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
next: (newOptions = options) => next(newOptions, index + 1),
|
|
136
|
-
});
|
|
137
|
-
};
|
|
138
|
-
return next(options, 0);
|
|
107
|
+
function intercept(interceptors, options, main) {
|
|
108
|
+
if (!interceptors?.length) return main(options);
|
|
109
|
+
const next = (options, index) => {
|
|
110
|
+
const interceptor = interceptors[index];
|
|
111
|
+
if (!interceptor) return main(options);
|
|
112
|
+
return interceptor({
|
|
113
|
+
...options,
|
|
114
|
+
next: (newOptions = options) => next(newOptions, index + 1)
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
return next(options, 0);
|
|
139
118
|
}
|
|
119
|
+
export { intercept, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess };
|
package/dist/iterator.d.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { StartSpanOptions } from "./tracing.js";
|
|
2
|
+
import { PromiseWithError, ThrowableError } from "./types.js";
|
|
3
|
+
import { AsyncIteratorClass } from "@standard-server/shared";
|
|
4
|
+
import { Promisable } from "type-fest";
|
|
5
5
|
export interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Any call to the original iterator will be executed inside this function.
|
|
8
|
+
* Useful when you want execution to happen within a specific context,
|
|
9
|
+
* such as AsyncLocalStorage.
|
|
10
|
+
*/
|
|
11
|
+
runWith?: (<T>(run: () => Promise<T>) => Promisable<T>) | undefined;
|
|
12
|
+
mapResult?: ((result: IteratorResult<TYield, TReturn>) => Promisable<IteratorResult<TMappedYield, TMappedReturn>>) | undefined;
|
|
13
|
+
mapError?: ((error: ThrowableError) => Promisable<ThrowableError>) | undefined;
|
|
14
|
+
onError?: ((error: ThrowableError) => Promisable<void>) | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Execute after the stream finishes or is cancelled.
|
|
17
|
+
*/
|
|
18
|
+
onFinish?: (() => Promisable<void>) | undefined;
|
|
19
19
|
}
|
|
20
|
-
export declare function wrapAsyncIterator<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { runWith, mapResult, mapError, onError, onFinish
|
|
20
|
+
export declare function wrapAsyncIterator<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { runWith, mapResult, mapError, onError, onFinish }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): NoInfer<AsyncIteratorClass<TMappedYield, TMappedReturn>>;
|
|
21
21
|
export declare function traceAsyncIterator<T, TReturn, TNext>(options: StartSpanOptions | string, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
22
22
|
export declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): AsyncIteratorClass<T, TReturn, TNext>[];
|
|
23
23
|
export interface ConsumeAsyncIteratorOptions<T, TReturn, TError> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Called on each event
|
|
26
|
+
*/
|
|
27
|
+
onEvent: (event: T) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Called once error happens
|
|
30
|
+
*/
|
|
31
|
+
onError?: ((error: TError) => void) | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Called once AsyncIteratorObject is done
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* **Note**: If iterator is canceled, `undefined` can be passed on success
|
|
37
|
+
*/
|
|
38
|
+
onSuccess?: ((value: TReturn | undefined) => void) | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Called once after onError or onSuccess
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* **Note**: If iterator is canceled, `undefined` can be passed on success
|
|
44
|
+
*/
|
|
45
|
+
onFinish?: ((state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void) | undefined;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Consumes an AsyncIteratorObject with lifecycle callbacks.
|
|
@@ -53,4 +53,4 @@ export interface ConsumeAsyncIteratorOptions<T, TReturn, TError> {
|
|
|
53
53
|
* @returns An unsubscribe callback that stops consuming and cleans up the iterator.
|
|
54
54
|
* @see {@link https://orpc.dev/docs/client/async-iterator-object#using-consumeasynciterator | AsyncIteratorObject in Client - Using consumeAsyncIterator}
|
|
55
55
|
*/
|
|
56
|
-
export declare function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeAsyncIteratorOptions<T, TReturn, TError | ThrowableError>): () => Promise<void>;
|
|
56
|
+
export declare function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeAsyncIteratorOptions<T, TReturn, TError | ThrowableError>): () => Promise<void>;
|
package/dist/iterator.js
CHANGED
|
@@ -1,157 +1,141 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
finally {
|
|
40
|
-
await onFinish?.();
|
|
41
|
-
}
|
|
42
|
-
});
|
|
1
|
+
import { once } from "./function.js";
|
|
2
|
+
import { AsyncIdQueue } from "./queue.js";
|
|
3
|
+
import { recordSpanError, runInSpanContext, startSpan } from "./tracing.js";
|
|
4
|
+
import { AsyncIteratorClass } from "@standard-server/shared";
|
|
5
|
+
// src/iterator.ts
|
|
6
|
+
function wrapAsyncIterator(iterator, { runWith, mapResult, mapError, onError, onFinish }) {
|
|
7
|
+
runWith ??= (run) => run();
|
|
8
|
+
let isDone;
|
|
9
|
+
return new AsyncIteratorClass(async () => {
|
|
10
|
+
try {
|
|
11
|
+
let result;
|
|
12
|
+
try {
|
|
13
|
+
result = await runWith(() => iterator.next());
|
|
14
|
+
isDone = result.done;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
isDone = true;
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
return mapResult ? await mapResult(result) : result;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
await onError?.(error);
|
|
22
|
+
throw mapError ? await mapError(error) : error;
|
|
23
|
+
}
|
|
24
|
+
}, async (_state) => {
|
|
25
|
+
try {
|
|
26
|
+
if (!isDone) try {
|
|
27
|
+
await runWith(async () => iterator.return?.());
|
|
28
|
+
} catch (error) {
|
|
29
|
+
await onError?.(error);
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
} finally {
|
|
33
|
+
await onFinish?.();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
43
36
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
37
|
+
function traceAsyncIterator(options, iterator) {
|
|
38
|
+
const getSpan = once(() => startSpan(options));
|
|
39
|
+
return wrapAsyncIterator(iterator, {
|
|
40
|
+
runWith: (run) => runInSpanContext(getSpan(), run),
|
|
41
|
+
mapResult(result) {
|
|
42
|
+
getSpan()?.addEvent(result.done ? "completed" : "yielded");
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
onError(error) {
|
|
46
|
+
recordSpanError(getSpan(), error);
|
|
47
|
+
},
|
|
48
|
+
onFinish() {
|
|
49
|
+
getSpan()?.end();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
59
52
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
queue.close({ id, reason: error });
|
|
100
|
-
if (kind === 'cancelled' && !queue.size && !isSourceFinished) {
|
|
101
|
-
isSourceFinished = true;
|
|
102
|
-
await source?.return?.();
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
return replicated;
|
|
53
|
+
function replicateAsyncIterator(source, count) {
|
|
54
|
+
const queue = new AsyncIdQueue();
|
|
55
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
56
|
+
let isSourceFinished = false;
|
|
57
|
+
const start = once(async () => {
|
|
58
|
+
try {
|
|
59
|
+
while (true) {
|
|
60
|
+
const item = await source.next();
|
|
61
|
+
ids.forEach((id) => {
|
|
62
|
+
if (queue.isOpen(id)) queue.push(id, { next: item });
|
|
63
|
+
});
|
|
64
|
+
if (item.done) break;
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
ids.forEach((id) => {
|
|
68
|
+
if (queue.isOpen(id)) queue.push(id, { error });
|
|
69
|
+
});
|
|
70
|
+
} finally {
|
|
71
|
+
isSourceFinished = true;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return ids.map((id) => {
|
|
75
|
+
queue.open(id);
|
|
76
|
+
return new AsyncIteratorClass(async () => {
|
|
77
|
+
start();
|
|
78
|
+
const item = await queue.pull(id);
|
|
79
|
+
if (item.next) return item.next;
|
|
80
|
+
throw item.error;
|
|
81
|
+
}, async ({ kind, error }) => {
|
|
82
|
+
queue.close({
|
|
83
|
+
id,
|
|
84
|
+
reason: error
|
|
85
|
+
});
|
|
86
|
+
if (kind === "cancelled" && !queue.size && !isSourceFinished) {
|
|
87
|
+
isSourceFinished = true;
|
|
88
|
+
await source?.return?.();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
107
92
|
}
|
|
108
93
|
/**
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
await (await iterator)?.return?.();
|
|
156
|
-
};
|
|
94
|
+
* Consumes an AsyncIteratorObject with lifecycle callbacks.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* **Warning**: If no `onError` or `onFinish` is provided, errors are thrown into the unhandled rejection channel.
|
|
98
|
+
*
|
|
99
|
+
* @returns An unsubscribe callback that stops consuming and cleans up the iterator.
|
|
100
|
+
* @see {@link https://orpc.dev/docs/client/async-iterator-object#using-consumeasynciterator | AsyncIteratorObject in Client - Using consumeAsyncIterator}
|
|
101
|
+
*/
|
|
102
|
+
function consumeAsyncIterator(iterator, options) {
|
|
103
|
+
(async () => {
|
|
104
|
+
let onFinishState;
|
|
105
|
+
try {
|
|
106
|
+
const resolvedIterator = await iterator;
|
|
107
|
+
while (true) {
|
|
108
|
+
const { done, value } = await resolvedIterator.next();
|
|
109
|
+
if (done) {
|
|
110
|
+
const realValue = value;
|
|
111
|
+
onFinishState = [
|
|
112
|
+
null,
|
|
113
|
+
realValue,
|
|
114
|
+
true
|
|
115
|
+
];
|
|
116
|
+
options.onSuccess?.(realValue);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
options.onEvent(value);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
await resolvedIterator.return?.();
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
onFinishState = [
|
|
128
|
+
error,
|
|
129
|
+
void 0,
|
|
130
|
+
false
|
|
131
|
+
];
|
|
132
|
+
options.onError?.(error);
|
|
133
|
+
} finally {
|
|
134
|
+
options.onFinish?.(onFinishState);
|
|
135
|
+
}
|
|
136
|
+
})();
|
|
137
|
+
return async () => {
|
|
138
|
+
await (await iterator)?.return?.();
|
|
139
|
+
};
|
|
157
140
|
}
|
|
141
|
+
export { consumeAsyncIterator, replicateAsyncIterator, traceAsyncIterator, wrapAsyncIterator };
|