@orpc/shared 1.2.0 → 1.4.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 +1 -0
- package/dist/index.d.mts +17 -8
- package/dist/index.d.ts +17 -8
- package/dist/index.mjs +145 -80
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -54,6 +54,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
54
54
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
55
55
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
56
56
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
57
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
|
|
57
58
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
58
59
|
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
59
60
|
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
package/dist/index.d.mts
CHANGED
|
@@ -11,6 +11,10 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
13
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
14
|
+
/**
|
|
15
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
16
|
+
*/
|
|
17
|
+
declare function defer(callback: () => void): void;
|
|
14
18
|
|
|
15
19
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
@@ -40,10 +44,10 @@ type ThrowableError = Registry extends {
|
|
|
40
44
|
} ? T : Error;
|
|
41
45
|
|
|
42
46
|
type InterceptableOptions = Record<string, any>;
|
|
43
|
-
type InterceptorOptions<TOptions extends InterceptableOptions, TResult
|
|
44
|
-
next(options?: TOptions):
|
|
47
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
48
|
+
next(options?: TOptions): TResult;
|
|
45
49
|
};
|
|
46
|
-
type Interceptor<TOptions extends InterceptableOptions, TResult
|
|
50
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult> = (options: InterceptorOptions<TOptions, TResult>) => TResult;
|
|
47
51
|
/**
|
|
48
52
|
* Can used for interceptors or middlewares
|
|
49
53
|
*/
|
|
@@ -69,13 +73,14 @@ type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess
|
|
|
69
73
|
declare function onFinish<T, TOptions extends {
|
|
70
74
|
next(): any;
|
|
71
75
|
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
72
|
-
declare function intercept<TOptions extends InterceptableOptions, TResult
|
|
76
|
+
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
77
|
|
|
74
78
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
79
|
interface CreateAsyncIteratorObjectCleanupFn {
|
|
76
80
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
81
|
}
|
|
78
82
|
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
83
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>)[];
|
|
79
84
|
|
|
80
85
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
86
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -95,10 +100,14 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
95
100
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
101
|
declare function clone<T>(value: T): T;
|
|
97
102
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
103
|
+
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
104
|
+
declare const NullProtoObj: ({
|
|
105
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
106
|
+
});
|
|
98
107
|
|
|
99
108
|
interface AsyncIdQueueCloseOptions {
|
|
100
109
|
id?: number;
|
|
101
|
-
reason?:
|
|
110
|
+
reason?: unknown;
|
|
102
111
|
}
|
|
103
112
|
declare class AsyncIdQueue<T> {
|
|
104
113
|
private readonly openIds;
|
|
@@ -113,8 +122,8 @@ declare class AsyncIdQueue<T> {
|
|
|
113
122
|
assertOpen(id: number): void;
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) =>
|
|
117
|
-
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>):
|
|
125
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
126
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
118
127
|
|
|
119
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
128
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
120
129
|
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
|
11
11
|
type AnyFunction = (...args: any[]) => any;
|
|
12
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
13
13
|
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
14
|
+
/**
|
|
15
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
16
|
+
*/
|
|
17
|
+
declare function defer(callback: () => void): void;
|
|
14
18
|
|
|
15
19
|
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
16
20
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
@@ -40,10 +44,10 @@ type ThrowableError = Registry extends {
|
|
|
40
44
|
} ? T : Error;
|
|
41
45
|
|
|
42
46
|
type InterceptableOptions = Record<string, any>;
|
|
43
|
-
type InterceptorOptions<TOptions extends InterceptableOptions, TResult
|
|
44
|
-
next(options?: TOptions):
|
|
47
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
48
|
+
next(options?: TOptions): TResult;
|
|
45
49
|
};
|
|
46
|
-
type Interceptor<TOptions extends InterceptableOptions, TResult
|
|
50
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult> = (options: InterceptorOptions<TOptions, TResult>) => TResult;
|
|
47
51
|
/**
|
|
48
52
|
* Can used for interceptors or middlewares
|
|
49
53
|
*/
|
|
@@ -69,13 +73,14 @@ type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess
|
|
|
69
73
|
declare function onFinish<T, TOptions extends {
|
|
70
74
|
next(): any;
|
|
71
75
|
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
72
|
-
declare function intercept<TOptions extends InterceptableOptions, TResult
|
|
76
|
+
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
73
77
|
|
|
74
78
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
75
79
|
interface CreateAsyncIteratorObjectCleanupFn {
|
|
76
80
|
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
77
81
|
}
|
|
78
82
|
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>;
|
|
83
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>)[];
|
|
79
84
|
|
|
80
85
|
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
81
86
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
@@ -95,10 +100,14 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
95
100
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
96
101
|
declare function clone<T>(value: T): T;
|
|
97
102
|
declare function get(object: object, path: readonly string[]): unknown;
|
|
103
|
+
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
104
|
+
declare const NullProtoObj: ({
|
|
105
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
106
|
+
});
|
|
98
107
|
|
|
99
108
|
interface AsyncIdQueueCloseOptions {
|
|
100
109
|
id?: number;
|
|
101
|
-
reason?:
|
|
110
|
+
reason?: unknown;
|
|
102
111
|
}
|
|
103
112
|
declare class AsyncIdQueue<T> {
|
|
104
113
|
private readonly openIds;
|
|
@@ -113,8 +122,8 @@ declare class AsyncIdQueue<T> {
|
|
|
113
122
|
assertOpen(id: number): void;
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) =>
|
|
117
|
-
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>):
|
|
125
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
126
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
118
127
|
|
|
119
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
128
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
120
129
|
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -32,6 +32,13 @@ function sequential(fn) {
|
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
function defer(callback) {
|
|
36
|
+
if ("setTimeout" in globalThis && typeof globalThis.setTimeout === "function") {
|
|
37
|
+
globalThis.setTimeout(callback, 0);
|
|
38
|
+
} else {
|
|
39
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
35
42
|
|
|
36
43
|
class SequentialIdGenerator {
|
|
37
44
|
nextId = 0;
|
|
@@ -82,13 +89,13 @@ function onFinish(callback) {
|
|
|
82
89
|
}
|
|
83
90
|
};
|
|
84
91
|
}
|
|
85
|
-
|
|
86
|
-
const next =
|
|
92
|
+
function intercept(interceptors, options, main) {
|
|
93
|
+
const next = (options2, index) => {
|
|
87
94
|
const interceptor = interceptors[index];
|
|
88
95
|
if (!interceptor) {
|
|
89
|
-
return
|
|
96
|
+
return main(options2);
|
|
90
97
|
}
|
|
91
|
-
return
|
|
98
|
+
return interceptor({
|
|
92
99
|
...options2,
|
|
93
100
|
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
94
101
|
});
|
|
@@ -96,6 +103,82 @@ async function intercept(interceptors, options, main) {
|
|
|
96
103
|
return next(options, 0);
|
|
97
104
|
}
|
|
98
105
|
|
|
106
|
+
class AsyncIdQueue {
|
|
107
|
+
openIds = /* @__PURE__ */ new Set();
|
|
108
|
+
items = /* @__PURE__ */ new Map();
|
|
109
|
+
pendingPulls = /* @__PURE__ */ new Map();
|
|
110
|
+
get length() {
|
|
111
|
+
return this.openIds.size;
|
|
112
|
+
}
|
|
113
|
+
open(id) {
|
|
114
|
+
this.openIds.add(id);
|
|
115
|
+
}
|
|
116
|
+
isOpen(id) {
|
|
117
|
+
return this.openIds.has(id);
|
|
118
|
+
}
|
|
119
|
+
push(id, item) {
|
|
120
|
+
this.assertOpen(id);
|
|
121
|
+
const pending = this.pendingPulls.get(id);
|
|
122
|
+
if (pending?.length) {
|
|
123
|
+
pending.shift()[0](item);
|
|
124
|
+
if (pending.length === 0) {
|
|
125
|
+
this.pendingPulls.delete(id);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
const items = this.items.get(id);
|
|
129
|
+
if (items) {
|
|
130
|
+
items.push(item);
|
|
131
|
+
} else {
|
|
132
|
+
this.items.set(id, [item]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async pull(id) {
|
|
137
|
+
this.assertOpen(id);
|
|
138
|
+
const items = this.items.get(id);
|
|
139
|
+
if (items?.length) {
|
|
140
|
+
const item = items.shift();
|
|
141
|
+
if (items.length === 0) {
|
|
142
|
+
this.items.delete(id);
|
|
143
|
+
}
|
|
144
|
+
return item;
|
|
145
|
+
}
|
|
146
|
+
return new Promise((resolve, reject) => {
|
|
147
|
+
const waitingPulls = this.pendingPulls.get(id);
|
|
148
|
+
const pending = [resolve, reject];
|
|
149
|
+
if (waitingPulls) {
|
|
150
|
+
waitingPulls.push(pending);
|
|
151
|
+
} else {
|
|
152
|
+
this.pendingPulls.set(id, [pending]);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
close({ id, reason } = {}) {
|
|
157
|
+
if (id === void 0) {
|
|
158
|
+
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
159
|
+
pendingPulls.forEach(([, reject]) => {
|
|
160
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
this.pendingPulls.clear();
|
|
164
|
+
this.openIds.clear();
|
|
165
|
+
this.items.clear();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
169
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
170
|
+
});
|
|
171
|
+
this.pendingPulls.delete(id);
|
|
172
|
+
this.openIds.delete(id);
|
|
173
|
+
this.items.delete(id);
|
|
174
|
+
}
|
|
175
|
+
assertOpen(id) {
|
|
176
|
+
if (!this.isOpen(id)) {
|
|
177
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
99
182
|
function isAsyncIteratorObject(maybe) {
|
|
100
183
|
if (!maybe || typeof maybe !== "object") {
|
|
101
184
|
return false;
|
|
@@ -158,6 +241,53 @@ function createAsyncIteratorObject(next, cleanup) {
|
|
|
158
241
|
};
|
|
159
242
|
return iterator;
|
|
160
243
|
}
|
|
244
|
+
function replicateAsyncIterator(source, count) {
|
|
245
|
+
const queue = new AsyncIdQueue();
|
|
246
|
+
const replicated = [];
|
|
247
|
+
let error;
|
|
248
|
+
const start = once(async () => {
|
|
249
|
+
try {
|
|
250
|
+
while (true) {
|
|
251
|
+
const item = await source.next();
|
|
252
|
+
for (let id = 0; id < count; id++) {
|
|
253
|
+
if (queue.isOpen(id)) {
|
|
254
|
+
queue.push(id, item);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (item.done) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
} catch (e) {
|
|
262
|
+
error = { value: e };
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
for (let id = 0; id < count; id++) {
|
|
266
|
+
queue.open(id);
|
|
267
|
+
replicated.push(createAsyncIteratorObject(
|
|
268
|
+
() => {
|
|
269
|
+
start();
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
queue.pull(id).then(resolve).catch(reject);
|
|
272
|
+
defer(() => {
|
|
273
|
+
if (error) {
|
|
274
|
+
reject(error.value);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
async (reason) => {
|
|
280
|
+
queue.close({ id });
|
|
281
|
+
if (reason !== "next") {
|
|
282
|
+
if (replicated.every((_, id2) => !queue.isOpen(id2))) {
|
|
283
|
+
await source?.return?.();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
));
|
|
288
|
+
}
|
|
289
|
+
return replicated;
|
|
290
|
+
}
|
|
161
291
|
|
|
162
292
|
function parseEmptyableJSON(text) {
|
|
163
293
|
if (!text) {
|
|
@@ -217,82 +347,17 @@ function get(object, path) {
|
|
|
217
347
|
}
|
|
218
348
|
return current;
|
|
219
349
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
}
|
|
350
|
+
function isPropertyKey(value) {
|
|
351
|
+
const type = typeof value;
|
|
352
|
+
return type === "string" || type === "number" || type === "symbol";
|
|
295
353
|
}
|
|
354
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
355
|
+
const e = function() {
|
|
356
|
+
};
|
|
357
|
+
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
358
|
+
Object.freeze(e.prototype);
|
|
359
|
+
return e;
|
|
360
|
+
})();
|
|
296
361
|
|
|
297
362
|
function value(value2, ...args) {
|
|
298
363
|
if (typeof value2 === "function") {
|
|
@@ -301,4 +366,4 @@ function value(value2, ...args) {
|
|
|
301
366
|
return value2;
|
|
302
367
|
}
|
|
303
368
|
|
|
304
|
-
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
|
369
|
+
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone, createAsyncIteratorObject, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.4.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -27,6 +27,11 @@
|
|
|
27
27
|
"radash": "^12.1.0",
|
|
28
28
|
"type-fest": "^4.39.1"
|
|
29
29
|
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"arktype": "2.1.20",
|
|
32
|
+
"valibot": "^1.1.0",
|
|
33
|
+
"zod": "^3.25.49"
|
|
34
|
+
},
|
|
30
35
|
"scripts": {
|
|
31
36
|
"build": "unbuild",
|
|
32
37
|
"build:watch": "pnpm run build --watch",
|