@orpc/shared 1.1.1 → 1.2.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/dist/index.d.mts +29 -2
- package/dist/index.d.ts +29 -2
- package/dist/index.mjs +153 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -10,11 +10,17 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
10
10
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
|
+
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
13
14
|
|
|
14
15
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
15
16
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
16
17
|
};
|
|
17
18
|
|
|
19
|
+
declare class SequentialIdGenerator {
|
|
20
|
+
private nextId;
|
|
21
|
+
generate(): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
19
25
|
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
20
26
|
type PromiseWithError<T, TError> = Promise<T> & {
|
|
@@ -66,6 +72,10 @@ declare function onFinish<T, TOptions extends {
|
|
|
66
72
|
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
67
73
|
|
|
68
74
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
+
interface CreateAsyncIteratorObjectCleanupFn {
|
|
76
|
+
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
69
79
|
|
|
70
80
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
71
81
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -86,8 +96,25 @@ declare function isTypescriptObject(value: unknown): value is object & Record<Pr
|
|
|
86
96
|
declare function clone<T>(value: T): T;
|
|
87
97
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
88
98
|
|
|
99
|
+
interface AsyncIdQueueCloseOptions {
|
|
100
|
+
id?: number;
|
|
101
|
+
reason?: Error;
|
|
102
|
+
}
|
|
103
|
+
declare class AsyncIdQueue<T> {
|
|
104
|
+
private readonly openIds;
|
|
105
|
+
private readonly items;
|
|
106
|
+
private readonly pendingPulls;
|
|
107
|
+
get length(): number;
|
|
108
|
+
open(id: number): void;
|
|
109
|
+
isOpen(id: number): boolean;
|
|
110
|
+
push(id: number, item: T): void;
|
|
111
|
+
pull(id: number): Promise<T>;
|
|
112
|
+
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
113
|
+
assertOpen(id: number): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
89
116
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
90
117
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
91
118
|
|
|
92
|
-
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
|
93
|
-
export type { AnyFunction, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
|
119
|
+
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
120
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -10,11 +10,17 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
10
10
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
|
+
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
13
14
|
|
|
14
15
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
15
16
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
16
17
|
};
|
|
17
18
|
|
|
19
|
+
declare class SequentialIdGenerator {
|
|
20
|
+
private nextId;
|
|
21
|
+
generate(): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
19
25
|
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
20
26
|
type PromiseWithError<T, TError> = Promise<T> & {
|
|
@@ -66,6 +72,10 @@ declare function onFinish<T, TOptions extends {
|
|
|
66
72
|
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
67
73
|
|
|
68
74
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
|
+
interface CreateAsyncIteratorObjectCleanupFn {
|
|
76
|
+
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
69
79
|
|
|
70
80
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
71
81
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -86,8 +96,25 @@ declare function isTypescriptObject(value: unknown): value is object & Record<Pr
|
|
|
86
96
|
declare function clone<T>(value: T): T;
|
|
87
97
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
88
98
|
|
|
99
|
+
interface AsyncIdQueueCloseOptions {
|
|
100
|
+
id?: number;
|
|
101
|
+
reason?: Error;
|
|
102
|
+
}
|
|
103
|
+
declare class AsyncIdQueue<T> {
|
|
104
|
+
private readonly openIds;
|
|
105
|
+
private readonly items;
|
|
106
|
+
private readonly pendingPulls;
|
|
107
|
+
get length(): number;
|
|
108
|
+
open(id: number): void;
|
|
109
|
+
isOpen(id: number): boolean;
|
|
110
|
+
push(id: number, item: T): void;
|
|
111
|
+
pull(id: number): Promise<T>;
|
|
112
|
+
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
113
|
+
assertOpen(id: number): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
89
116
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
90
117
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
91
118
|
|
|
92
|
-
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
|
93
|
-
export type { AnyFunction, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
|
119
|
+
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
120
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -23,6 +23,26 @@ function once(fn) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
function sequential(fn) {
|
|
27
|
+
let lastOperationPromise = Promise.resolve();
|
|
28
|
+
return (...args) => {
|
|
29
|
+
return lastOperationPromise = lastOperationPromise.catch(() => {
|
|
30
|
+
}).then(() => {
|
|
31
|
+
return fn(...args);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class SequentialIdGenerator {
|
|
37
|
+
nextId = 0;
|
|
38
|
+
generate() {
|
|
39
|
+
if (this.nextId === Number.MAX_SAFE_INTEGER) {
|
|
40
|
+
this.nextId = 0;
|
|
41
|
+
return Number.MAX_SAFE_INTEGER;
|
|
42
|
+
}
|
|
43
|
+
return this.nextId++;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
26
46
|
|
|
27
47
|
function onStart(callback) {
|
|
28
48
|
return async (options, ...rest) => {
|
|
@@ -82,6 +102,62 @@ function isAsyncIteratorObject(maybe) {
|
|
|
82
102
|
}
|
|
83
103
|
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
84
104
|
}
|
|
105
|
+
function createAsyncIteratorObject(next, cleanup) {
|
|
106
|
+
let isExecuteComplete = false;
|
|
107
|
+
let isDone = false;
|
|
108
|
+
const iterator = {
|
|
109
|
+
next: sequential(async () => {
|
|
110
|
+
if (isDone) {
|
|
111
|
+
return { done: true, value: void 0 };
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const result = await next();
|
|
115
|
+
if (result.done) {
|
|
116
|
+
isDone = true;
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
} catch (err) {
|
|
120
|
+
isDone = true;
|
|
121
|
+
throw err;
|
|
122
|
+
} finally {
|
|
123
|
+
if (isDone && !isExecuteComplete) {
|
|
124
|
+
isExecuteComplete = true;
|
|
125
|
+
await cleanup("next");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}),
|
|
129
|
+
async return(value) {
|
|
130
|
+
isDone = true;
|
|
131
|
+
if (!isExecuteComplete) {
|
|
132
|
+
isExecuteComplete = true;
|
|
133
|
+
await cleanup("return");
|
|
134
|
+
}
|
|
135
|
+
return { done: true, value };
|
|
136
|
+
},
|
|
137
|
+
async throw(err) {
|
|
138
|
+
isDone = true;
|
|
139
|
+
if (!isExecuteComplete) {
|
|
140
|
+
isExecuteComplete = true;
|
|
141
|
+
await cleanup("throw");
|
|
142
|
+
}
|
|
143
|
+
throw err;
|
|
144
|
+
},
|
|
145
|
+
/**
|
|
146
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
147
|
+
*/
|
|
148
|
+
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
149
|
+
isDone = true;
|
|
150
|
+
if (!isExecuteComplete) {
|
|
151
|
+
isExecuteComplete = true;
|
|
152
|
+
await cleanup("dispose");
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[Symbol.asyncIterator]() {
|
|
156
|
+
return iterator;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
return iterator;
|
|
160
|
+
}
|
|
85
161
|
|
|
86
162
|
function parseEmptyableJSON(text) {
|
|
87
163
|
if (!text) {
|
|
@@ -142,6 +218,82 @@ function get(object, path) {
|
|
|
142
218
|
return current;
|
|
143
219
|
}
|
|
144
220
|
|
|
221
|
+
class AsyncIdQueue {
|
|
222
|
+
openIds = /* @__PURE__ */ new Set();
|
|
223
|
+
items = /* @__PURE__ */ new Map();
|
|
224
|
+
pendingPulls = /* @__PURE__ */ new Map();
|
|
225
|
+
get length() {
|
|
226
|
+
return this.openIds.size;
|
|
227
|
+
}
|
|
228
|
+
open(id) {
|
|
229
|
+
this.openIds.add(id);
|
|
230
|
+
}
|
|
231
|
+
isOpen(id) {
|
|
232
|
+
return this.openIds.has(id);
|
|
233
|
+
}
|
|
234
|
+
push(id, item) {
|
|
235
|
+
this.assertOpen(id);
|
|
236
|
+
const pending = this.pendingPulls.get(id);
|
|
237
|
+
if (pending?.length) {
|
|
238
|
+
pending.shift()[0](item);
|
|
239
|
+
if (pending.length === 0) {
|
|
240
|
+
this.pendingPulls.delete(id);
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
const items = this.items.get(id);
|
|
244
|
+
if (items) {
|
|
245
|
+
items.push(item);
|
|
246
|
+
} else {
|
|
247
|
+
this.items.set(id, [item]);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async pull(id) {
|
|
252
|
+
this.assertOpen(id);
|
|
253
|
+
const items = this.items.get(id);
|
|
254
|
+
if (items?.length) {
|
|
255
|
+
const item = items.shift();
|
|
256
|
+
if (items.length === 0) {
|
|
257
|
+
this.items.delete(id);
|
|
258
|
+
}
|
|
259
|
+
return item;
|
|
260
|
+
}
|
|
261
|
+
return new Promise((resolve, reject) => {
|
|
262
|
+
const waitingPulls = this.pendingPulls.get(id);
|
|
263
|
+
const pending = [resolve, reject];
|
|
264
|
+
if (waitingPulls) {
|
|
265
|
+
waitingPulls.push(pending);
|
|
266
|
+
} else {
|
|
267
|
+
this.pendingPulls.set(id, [pending]);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
close({ id, reason } = {}) {
|
|
272
|
+
if (id === void 0) {
|
|
273
|
+
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
274
|
+
pendingPulls.forEach(([, reject]) => {
|
|
275
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
this.pendingPulls.clear();
|
|
279
|
+
this.openIds.clear();
|
|
280
|
+
this.items.clear();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
284
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
285
|
+
});
|
|
286
|
+
this.pendingPulls.delete(id);
|
|
287
|
+
this.openIds.delete(id);
|
|
288
|
+
this.items.delete(id);
|
|
289
|
+
}
|
|
290
|
+
assertOpen(id) {
|
|
291
|
+
if (!this.isOpen(id)) {
|
|
292
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
145
297
|
function value(value2, ...args) {
|
|
146
298
|
if (typeof value2 === "function") {
|
|
147
299
|
return value2(...args);
|
|
@@ -149,4 +301,4 @@ function value(value2, ...args) {
|
|
|
149
301
|
return value2;
|
|
150
302
|
}
|
|
151
303
|
|
|
152
|
-
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
|
304
|
+
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|