@orpc/shared 1.4.3 → 1.4.5
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/index.d.mts +23 -5
- package/dist/index.d.ts +23 -5
- package/dist/index.mjs +52 -45
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -135,11 +135,29 @@ declare function onFinish<T, TOptions extends {
|
|
|
135
135
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
136
136
|
|
|
137
137
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
138
|
-
interface
|
|
138
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
139
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
140
|
+
}
|
|
141
|
+
interface AsyncIteratorClassCleanupFn {
|
|
139
142
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
140
143
|
}
|
|
141
|
-
declare
|
|
142
|
-
declare
|
|
144
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
145
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
146
|
+
asyncDispose: infer T;
|
|
147
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
148
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
149
|
+
#private;
|
|
150
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
151
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
152
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
153
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
154
|
+
/**
|
|
155
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
156
|
+
*/
|
|
157
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
158
|
+
[Symbol.asyncIterator](): this;
|
|
159
|
+
}
|
|
160
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
143
161
|
|
|
144
162
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
145
163
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -184,5 +202,5 @@ declare class AsyncIdQueue<T> {
|
|
|
184
202
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
185
203
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
186
204
|
|
|
187
|
-
export { AsyncIdQueue, EventPublisher, NullProtoObj, SequentialIdGenerator, clone,
|
|
188
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
205
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
206
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -135,11 +135,29 @@ declare function onFinish<T, TOptions extends {
|
|
|
135
135
|
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
136
136
|
|
|
137
137
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
138
|
-
interface
|
|
138
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
139
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
140
|
+
}
|
|
141
|
+
interface AsyncIteratorClassCleanupFn {
|
|
139
142
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
140
143
|
}
|
|
141
|
-
declare
|
|
142
|
-
declare
|
|
144
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
145
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
146
|
+
asyncDispose: infer T;
|
|
147
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
148
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
149
|
+
#private;
|
|
150
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
151
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
152
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
153
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
154
|
+
/**
|
|
155
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
156
|
+
*/
|
|
157
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
158
|
+
[Symbol.asyncIterator](): this;
|
|
159
|
+
}
|
|
160
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
143
161
|
|
|
144
162
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
145
163
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -184,5 +202,5 @@ declare class AsyncIdQueue<T> {
|
|
|
184
202
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
185
203
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
186
204
|
|
|
187
|
-
export { AsyncIdQueue, EventPublisher, NullProtoObj, SequentialIdGenerator, clone,
|
|
188
|
-
export type { AnyFunction, AsyncIdQueueCloseOptions,
|
|
205
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
206
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -122,61 +122,68 @@ function isAsyncIteratorObject(maybe) {
|
|
|
122
122
|
}
|
|
123
123
|
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
124
124
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
126
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
127
|
+
class AsyncIteratorClass {
|
|
128
|
+
#isDone = false;
|
|
129
|
+
#isExecuteComplete = false;
|
|
130
|
+
#cleanup;
|
|
131
|
+
#next;
|
|
132
|
+
constructor(next, cleanup) {
|
|
133
|
+
this.#cleanup = cleanup;
|
|
134
|
+
this.#next = sequential(async () => {
|
|
135
|
+
if (this.#isDone) {
|
|
131
136
|
return { done: true, value: void 0 };
|
|
132
137
|
}
|
|
133
138
|
try {
|
|
134
139
|
const result = await next();
|
|
135
140
|
if (result.done) {
|
|
136
|
-
isDone = true;
|
|
141
|
+
this.#isDone = true;
|
|
137
142
|
}
|
|
138
143
|
return result;
|
|
139
144
|
} catch (err) {
|
|
140
|
-
isDone = true;
|
|
145
|
+
this.#isDone = true;
|
|
141
146
|
throw err;
|
|
142
147
|
} finally {
|
|
143
|
-
if (isDone && !isExecuteComplete) {
|
|
144
|
-
isExecuteComplete = true;
|
|
145
|
-
await cleanup("next");
|
|
148
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
149
|
+
this.#isExecuteComplete = true;
|
|
150
|
+
await this.#cleanup("next");
|
|
146
151
|
}
|
|
147
152
|
}
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
isDone = true;
|
|
159
|
-
if (!isExecuteComplete) {
|
|
160
|
-
isExecuteComplete = true;
|
|
161
|
-
await cleanup("throw");
|
|
162
|
-
}
|
|
163
|
-
throw err;
|
|
164
|
-
},
|
|
165
|
-
/**
|
|
166
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
167
|
-
*/
|
|
168
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
169
|
-
isDone = true;
|
|
170
|
-
if (!isExecuteComplete) {
|
|
171
|
-
isExecuteComplete = true;
|
|
172
|
-
await cleanup("dispose");
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
[Symbol.asyncIterator]() {
|
|
176
|
-
return iterator;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
next() {
|
|
156
|
+
return this.#next();
|
|
157
|
+
}
|
|
158
|
+
async return(value) {
|
|
159
|
+
this.#isDone = true;
|
|
160
|
+
if (!this.#isExecuteComplete) {
|
|
161
|
+
this.#isExecuteComplete = true;
|
|
162
|
+
await this.#cleanup("return");
|
|
177
163
|
}
|
|
178
|
-
|
|
179
|
-
|
|
164
|
+
return { done: true, value };
|
|
165
|
+
}
|
|
166
|
+
async throw(err) {
|
|
167
|
+
this.#isDone = true;
|
|
168
|
+
if (!this.#isExecuteComplete) {
|
|
169
|
+
this.#isExecuteComplete = true;
|
|
170
|
+
await this.#cleanup("throw");
|
|
171
|
+
}
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
176
|
+
*/
|
|
177
|
+
async [asyncDisposeSymbol]() {
|
|
178
|
+
this.#isDone = true;
|
|
179
|
+
if (!this.#isExecuteComplete) {
|
|
180
|
+
this.#isExecuteComplete = true;
|
|
181
|
+
await this.#cleanup("dispose");
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
[Symbol.asyncIterator]() {
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
180
187
|
}
|
|
181
188
|
function replicateAsyncIterator(source, count) {
|
|
182
189
|
const queue = new AsyncIdQueue();
|
|
@@ -201,7 +208,7 @@ function replicateAsyncIterator(source, count) {
|
|
|
201
208
|
});
|
|
202
209
|
for (let id = 0; id < count; id++) {
|
|
203
210
|
queue.open(id);
|
|
204
|
-
replicated.push(
|
|
211
|
+
replicated.push(new AsyncIteratorClass(
|
|
205
212
|
() => {
|
|
206
213
|
start();
|
|
207
214
|
return new Promise((resolve, reject) => {
|
|
@@ -283,7 +290,7 @@ class EventPublisher {
|
|
|
283
290
|
bufferedEvents.length = 0;
|
|
284
291
|
};
|
|
285
292
|
signal?.addEventListener("abort", abortListener, { once: true });
|
|
286
|
-
return
|
|
293
|
+
return new AsyncIteratorClass(async () => {
|
|
287
294
|
if (signal?.aborted) {
|
|
288
295
|
throw signal.reason;
|
|
289
296
|
}
|
|
@@ -443,4 +450,4 @@ function value(value2, ...args) {
|
|
|
443
450
|
return value2;
|
|
444
451
|
}
|
|
445
452
|
|
|
446
|
-
export { AsyncIdQueue, EventPublisher, NullProtoObj, SequentialIdGenerator, clone,
|
|
453
|
+
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|