@mtkruto/browser 0.128.0 → 0.130.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/esm/0_deps.d.ts +4 -3
- package/esm/0_deps.d.ts.map +1 -1
- package/esm/0_deps.js +4 -3
- package/esm/3_types.d.ts +4 -0
- package/esm/3_types.d.ts.map +1 -1
- package/esm/3_types.js +4 -0
- package/esm/client/0_params.d.ts +26 -0
- package/esm/client/0_params.d.ts.map +1 -1
- package/esm/client/1_client_generic.d.ts +41 -2
- package/esm/client/1_client_generic.d.ts.map +1 -1
- package/esm/client/1_types.d.ts +1 -0
- package/esm/client/1_types.d.ts.map +1 -1
- package/esm/client/2_account_manager.d.ts +3 -0
- package/esm/client/2_account_manager.d.ts.map +1 -1
- package/esm/client/2_account_manager.js +16 -1
- package/esm/client/2_takeout_manager.d.ts +29 -0
- package/esm/client/2_takeout_manager.d.ts.map +1 -0
- package/esm/client/2_takeout_manager.js +41 -0
- package/esm/client/3_message_manager.js +1 -1
- package/esm/client/6_client.d.ts +41 -2
- package/esm/client/6_client.d.ts.map +1 -1
- package/esm/client/6_client.js +64 -3
- package/esm/client/6_client_dispatcher.d.ts +41 -2
- package/esm/client/6_client_dispatcher.d.ts.map +1 -1
- package/esm/client/6_client_dispatcher.js +54 -0
- package/esm/types/0_calling_code.d.ts +31 -0
- package/esm/types/0_calling_code.d.ts.map +1 -0
- package/esm/types/0_calling_code.js +26 -0
- package/esm/types/0_chat_action_type.d.ts +13 -0
- package/esm/types/0_chat_action_type.d.ts.map +1 -1
- package/esm/types/0_timezone.d.ts +31 -0
- package/esm/types/0_timezone.d.ts.map +1 -0
- package/esm/types/0_timezone.js +26 -0
- package/esm/types/1_available_reactions.d.ts +1 -0
- package/esm/types/1_available_reactions.d.ts.map +1 -1
- package/esm/types/1_chat_p.d.ts +1 -1
- package/esm/types/1_chat_p.d.ts.map +1 -1
- package/esm/types/1_chat_p.js +1 -1
- package/esm/types/1_country.d.ts +36 -0
- package/esm/types/1_country.d.ts.map +1 -0
- package/esm/types/1_country.js +30 -0
- package/esm/types/2_left_channel_list.d.ts +30 -0
- package/esm/types/2_left_channel_list.d.ts.map +1 -0
- package/esm/types/2_left_channel_list.js +25 -0
- package/esm/types/2_story_album_icon.d.ts +1 -0
- package/esm/types/2_story_album_icon.d.ts.map +1 -1
- package/esm/types/2_user.d.ts +1 -1
- package/esm/types/2_user.d.ts.map +1 -1
- package/esm/types/2_user.js +2 -2
- package/esm/types/3_app_support.d.ts +3 -0
- package/esm/types/3_app_support.d.ts.map +1 -1
- package/package.json +1 -1
- package/script/0_deps.d.ts +4 -3
- package/script/0_deps.d.ts.map +1 -1
- package/script/0_deps.js +13 -12
- package/script/3_types.d.ts +4 -0
- package/script/3_types.d.ts.map +1 -1
- package/script/3_types.js +4 -0
- package/script/client/0_params.d.ts +26 -0
- package/script/client/0_params.d.ts.map +1 -1
- package/script/client/1_client_generic.d.ts +41 -2
- package/script/client/1_client_generic.d.ts.map +1 -1
- package/script/client/1_types.d.ts +1 -0
- package/script/client/1_types.d.ts.map +1 -1
- package/script/client/2_account_manager.d.ts +3 -0
- package/script/client/2_account_manager.d.ts.map +1 -1
- package/script/client/2_account_manager.js +15 -0
- package/script/client/2_takeout_manager.d.ts +29 -0
- package/script/client/2_takeout_manager.d.ts.map +1 -0
- package/script/client/2_takeout_manager.js +45 -0
- package/script/client/3_message_manager.js +1 -1
- package/script/client/6_client.d.ts +41 -2
- package/script/client/6_client.d.ts.map +1 -1
- package/script/client/6_client.js +64 -3
- package/script/client/6_client_dispatcher.d.ts +41 -2
- package/script/client/6_client_dispatcher.d.ts.map +1 -1
- package/script/client/6_client_dispatcher.js +54 -0
- package/script/types/0_calling_code.d.ts +31 -0
- package/script/types/0_calling_code.d.ts.map +1 -0
- package/script/types/0_calling_code.js +29 -0
- package/script/types/0_chat_action_type.d.ts +13 -0
- package/script/types/0_chat_action_type.d.ts.map +1 -1
- package/script/types/0_timezone.d.ts +31 -0
- package/script/types/0_timezone.d.ts.map +1 -0
- package/script/types/0_timezone.js +29 -0
- package/script/types/1_available_reactions.d.ts +1 -0
- package/script/types/1_available_reactions.d.ts.map +1 -1
- package/script/types/1_chat_p.d.ts +1 -1
- package/script/types/1_chat_p.d.ts.map +1 -1
- package/script/types/1_chat_p.js +1 -1
- package/script/types/1_country.d.ts +36 -0
- package/script/types/1_country.d.ts.map +1 -0
- package/script/types/1_country.js +33 -0
- package/script/types/2_left_channel_list.d.ts +30 -0
- package/script/types/2_left_channel_list.d.ts.map +1 -0
- package/script/types/2_left_channel_list.js +28 -0
- package/script/types/2_story_album_icon.d.ts +1 -0
- package/script/types/2_story_album_icon.d.ts.map +1 -1
- package/script/types/2_user.d.ts +1 -1
- package/script/types/2_user.d.ts.map +1 -1
- package/script/types/2_user.js +2 -2
- package/script/types/3_app_support.d.ts +3 -0
- package/script/types/3_app_support.d.ts.map +1 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/_util.d.ts +0 -2
- package/esm/deps/jsr.io/@std/async/1.2.0/_util.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/_util.js +0 -6
- package/esm/deps/jsr.io/@std/async/1.2.0/abortable.d.ts +0 -107
- package/esm/deps/jsr.io/@std/async/1.2.0/abortable.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/abortable.js +0 -46
- package/esm/deps/jsr.io/@std/async/1.2.0/deadline.d.ts +0 -37
- package/esm/deps/jsr.io/@std/async/1.2.0/deadline.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/deadline.js +0 -41
- package/esm/deps/jsr.io/@std/async/1.2.0/debounce.d.ts +0 -44
- package/esm/deps/jsr.io/@std/async/1.2.0/debounce.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/debounce.js +0 -57
- package/esm/deps/jsr.io/@std/async/1.2.0/mod.d.ts +0 -22
- package/esm/deps/jsr.io/@std/async/1.2.0/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/mod.js +0 -23
- package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.d.ts +0 -111
- package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +0 -154
- package/esm/deps/jsr.io/@std/async/1.2.0/retry.d.ts +0 -155
- package/esm/deps/jsr.io/@std/async/1.2.0/retry.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/retry.js +0 -147
- package/esm/deps/jsr.io/@std/async/1.2.0/tee.d.ts +0 -43
- package/esm/deps/jsr.io/@std/async/1.2.0/tee.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.2.0/tee.js +0 -79
- package/script/deps/jsr.io/@std/async/1.2.0/_util.d.ts +0 -2
- package/script/deps/jsr.io/@std/async/1.2.0/_util.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/_util.js +0 -9
- package/script/deps/jsr.io/@std/async/1.2.0/abortable.d.ts +0 -107
- package/script/deps/jsr.io/@std/async/1.2.0/abortable.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/abortable.js +0 -49
- package/script/deps/jsr.io/@std/async/1.2.0/deadline.d.ts +0 -37
- package/script/deps/jsr.io/@std/async/1.2.0/deadline.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/deadline.js +0 -44
- package/script/deps/jsr.io/@std/async/1.2.0/debounce.d.ts +0 -44
- package/script/deps/jsr.io/@std/async/1.2.0/debounce.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/debounce.js +0 -60
- package/script/deps/jsr.io/@std/async/1.2.0/mod.d.ts +0 -22
- package/script/deps/jsr.io/@std/async/1.2.0/mod.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/mod.js +0 -39
- package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.d.ts +0 -111
- package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +0 -158
- package/script/deps/jsr.io/@std/async/1.2.0/retry.d.ts +0 -155
- package/script/deps/jsr.io/@std/async/1.2.0/retry.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/retry.js +0 -152
- package/script/deps/jsr.io/@std/async/1.2.0/tee.d.ts +0 -43
- package/script/deps/jsr.io/@std/async/1.2.0/tee.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.2.0/tee.js +0 -82
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RetryError = void 0;
|
|
4
|
-
exports.retry = retry;
|
|
5
|
-
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
6
|
-
// This module is browser compatible.
|
|
7
|
-
const delay_js_1 = require("./delay.js");
|
|
8
|
-
const _util_js_1 = require("./_util.js");
|
|
9
|
-
/**
|
|
10
|
-
* Error thrown in {@linkcode retry} once the maximum number of failed attempts
|
|
11
|
-
* has been reached.
|
|
12
|
-
*
|
|
13
|
-
* @example Usage
|
|
14
|
-
* ```ts no-assert ignore
|
|
15
|
-
* import { RetryError } from "@std/async/retry";
|
|
16
|
-
*
|
|
17
|
-
* throw new RetryError({ foo: "bar" }, 3);
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
class RetryError extends Error {
|
|
21
|
-
/**
|
|
22
|
-
* Constructs a new {@linkcode RetryError} instance.
|
|
23
|
-
*
|
|
24
|
-
* @param cause the cause for this error.
|
|
25
|
-
* @param attempts the number of retry attempts made.
|
|
26
|
-
*/
|
|
27
|
-
constructor(cause, attempts) {
|
|
28
|
-
super(`Retrying exceeded the maxAttempts (${attempts}).`);
|
|
29
|
-
this.name = "RetryError";
|
|
30
|
-
this.cause = cause;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.RetryError = RetryError;
|
|
34
|
-
/**
|
|
35
|
-
* Calls the given (possibly asynchronous) function up to `maxAttempts` times.
|
|
36
|
-
* Retries as long as the given function throws. If the attempts are exhausted,
|
|
37
|
-
* throws a {@linkcode RetryError} with `cause` set to the inner exception.
|
|
38
|
-
*
|
|
39
|
-
* The backoff is calculated by multiplying `minTimeout` with `multiplier` to the power of the current attempt counter (starting at 0 up to `maxAttempts - 1`). It is capped at `maxTimeout` however.
|
|
40
|
-
* How long the actual delay is, depends on `jitter`.
|
|
41
|
-
*
|
|
42
|
-
* When `jitter` is the default value of `1`, waits between two attempts for a
|
|
43
|
-
* randomized amount between 0 and the backoff time. With the default options
|
|
44
|
-
* the maximal delay will be `15s = 1s + 2s + 4s + 8s`. If all five attempts
|
|
45
|
-
* are exhausted the mean delay will be `9.5s = ½(4s + 15s)`.
|
|
46
|
-
*
|
|
47
|
-
* When `jitter` is `0`, waits the full backoff time.
|
|
48
|
-
*
|
|
49
|
-
* @example Example configuration 1
|
|
50
|
-
* ```ts no-assert
|
|
51
|
-
* import { retry } from "@std/async/retry";
|
|
52
|
-
* const req = async () => {
|
|
53
|
-
* // some function that throws sometimes
|
|
54
|
-
* };
|
|
55
|
-
*
|
|
56
|
-
* // Below resolves to the first non-error result of `req`
|
|
57
|
-
* const retryPromise = await retry(req, {
|
|
58
|
-
* multiplier: 2,
|
|
59
|
-
* maxTimeout: 60000,
|
|
60
|
-
* maxAttempts: 5,
|
|
61
|
-
* minTimeout: 100,
|
|
62
|
-
* jitter: 1,
|
|
63
|
-
* });
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* @example Example configuration 2
|
|
67
|
-
* ```ts no-assert
|
|
68
|
-
* import { retry } from "@std/async/retry";
|
|
69
|
-
* const req = async () => {
|
|
70
|
-
* // some function that throws sometimes
|
|
71
|
-
* };
|
|
72
|
-
*
|
|
73
|
-
* // Make sure we wait at least 1 minute, but at most 2 minutes
|
|
74
|
-
* const retryPromise = await retry(req, {
|
|
75
|
-
* multiplier: 2.34,
|
|
76
|
-
* maxTimeout: 80000,
|
|
77
|
-
* maxAttempts: 7,
|
|
78
|
-
* minTimeout: 1000,
|
|
79
|
-
* jitter: 0.5,
|
|
80
|
-
* });
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* @example Only retry on specific error types
|
|
84
|
-
* ```ts no-assert
|
|
85
|
-
* import { retry } from "@std/async/retry";
|
|
86
|
-
*
|
|
87
|
-
* class HttpError extends Error {
|
|
88
|
-
* status: number;
|
|
89
|
-
* constructor(status: number) {
|
|
90
|
-
* super(`HTTP ${status}`);
|
|
91
|
-
* this.status = status;
|
|
92
|
-
* }
|
|
93
|
-
* }
|
|
94
|
-
*
|
|
95
|
-
* const req = async () => {
|
|
96
|
-
* // some function that throws HttpError
|
|
97
|
-
* };
|
|
98
|
-
*
|
|
99
|
-
* // Only retry on 429 (rate limit) or 5xx (server) errors
|
|
100
|
-
* const retryPromise = await retry(req, {
|
|
101
|
-
* isRetriable: (err) =>
|
|
102
|
-
* err instanceof HttpError && (err.status === 429 || err.status >= 500),
|
|
103
|
-
* });
|
|
104
|
-
* ```
|
|
105
|
-
*
|
|
106
|
-
* @typeParam T The return type of the function to retry and returned promise.
|
|
107
|
-
* @param fn The function to retry.
|
|
108
|
-
* @param options Additional options.
|
|
109
|
-
* @returns The promise that resolves with the value returned by the function to retry.
|
|
110
|
-
* @throws {RetryError} If the function fails after `maxAttempts` attempts.
|
|
111
|
-
* @throws If the `signal` is aborted, throws the signal's reason.
|
|
112
|
-
* @throws If `isRetriable` returns `false` for an error, throws that error immediately.
|
|
113
|
-
*/
|
|
114
|
-
async function retry(fn, options) {
|
|
115
|
-
const { multiplier = 2, maxTimeout = 60000, maxAttempts = 5, minTimeout = 1000, jitter = 1, isRetriable = () => true, signal, } = options ?? {};
|
|
116
|
-
if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
|
|
117
|
-
throw new RangeError(`Cannot retry as 'maxAttempts' must be a positive integer: current value is ${maxAttempts}`);
|
|
118
|
-
}
|
|
119
|
-
if (!Number.isFinite(multiplier) || multiplier < 1) {
|
|
120
|
-
throw new RangeError(`Cannot retry as 'multiplier' must be a finite number >= 1: current value is ${multiplier}`);
|
|
121
|
-
}
|
|
122
|
-
if (Number.isNaN(maxTimeout) || maxTimeout <= 0) {
|
|
123
|
-
throw new RangeError(`Cannot retry as 'maxTimeout' must be a positive number: current value is ${maxTimeout}`);
|
|
124
|
-
}
|
|
125
|
-
if (Number.isNaN(minTimeout) || minTimeout < 0) {
|
|
126
|
-
throw new RangeError(`Cannot retry as 'minTimeout' must be >= 0: current value is ${minTimeout}`);
|
|
127
|
-
}
|
|
128
|
-
if (minTimeout > maxTimeout) {
|
|
129
|
-
throw new RangeError(`Cannot retry as 'minTimeout' must be <= 'maxTimeout': current values 'minTimeout=${minTimeout}', 'maxTimeout=${maxTimeout}'`);
|
|
130
|
-
}
|
|
131
|
-
if (Number.isNaN(jitter) || jitter < 0 || jitter > 1) {
|
|
132
|
-
throw new RangeError(`Cannot retry as 'jitter' must be between 0 and 1: current value is ${jitter}`);
|
|
133
|
-
}
|
|
134
|
-
let attempt = 0;
|
|
135
|
-
while (true) {
|
|
136
|
-
signal?.throwIfAborted();
|
|
137
|
-
try {
|
|
138
|
-
return await fn();
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
if (!isRetriable(error)) {
|
|
142
|
-
throw error;
|
|
143
|
-
}
|
|
144
|
-
if (attempt + 1 >= maxAttempts) {
|
|
145
|
-
throw new RetryError(error, maxAttempts);
|
|
146
|
-
}
|
|
147
|
-
const timeout = (0, _util_js_1.exponentialBackoffWithJitter)(maxTimeout, minTimeout, attempt, multiplier, jitter);
|
|
148
|
-
await (0, delay_js_1.delay)(timeout, signal ? { signal } : undefined);
|
|
149
|
-
}
|
|
150
|
-
attempt++;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility for representing n-tuple. Used in {@linkcode tee}.
|
|
3
|
-
*
|
|
4
|
-
* @internal
|
|
5
|
-
*/
|
|
6
|
-
export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : TupleOf<T, N, []> : never;
|
|
7
|
-
/**
|
|
8
|
-
* Utility for representing n-tuple of. Used in {@linkcode Tuple}.
|
|
9
|
-
*
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
export type TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : TupleOf<T, N, [T, ...R]>;
|
|
13
|
-
/**
|
|
14
|
-
* Branches the given async iterable into the `n` branches.
|
|
15
|
-
*
|
|
16
|
-
* @example Usage
|
|
17
|
-
* ```ts
|
|
18
|
-
* import { tee } from "@std/async/tee";
|
|
19
|
-
* import { assertEquals } from "@std/assert";
|
|
20
|
-
*
|
|
21
|
-
* const gen = async function* gen() {
|
|
22
|
-
* yield 1;
|
|
23
|
-
* yield 2;
|
|
24
|
-
* yield 3;
|
|
25
|
-
* };
|
|
26
|
-
*
|
|
27
|
-
* const [branch1, branch2] = tee(gen());
|
|
28
|
-
*
|
|
29
|
-
* const result1 = await Array.fromAsync(branch1);
|
|
30
|
-
* assertEquals(result1, [1, 2, 3]);
|
|
31
|
-
*
|
|
32
|
-
* const result2 = await Array.fromAsync(branch2);
|
|
33
|
-
* assertEquals(result2, [1, 2, 3]);
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @typeParam T The type of the provided async iterable and the returned async iterables.
|
|
37
|
-
* @typeParam N The amount of branches to tee into.
|
|
38
|
-
* @param iterable The iterable to tee.
|
|
39
|
-
* @param n The amount of branches to tee into.
|
|
40
|
-
* @returns The tuple where each element is an async iterable.
|
|
41
|
-
*/
|
|
42
|
-
export declare function tee<T, N extends number = 2>(iterable: AsyncIterable<T>, n?: N): Tuple<AsyncIterable<T>, N>;
|
|
43
|
-
//# sourceMappingURL=tee.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tee.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/async/1.2.0/tee.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,CAAC,GAChD,MAAM,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GAC1C,KAAK,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,EAAE,IAC1D,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,GACrB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;AAuC/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,CAAC,EACzC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,EAC1B,CAAC,GAAE,CAAU,GACZ,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAuB5B"}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
3
|
-
// This module is browser compatible.
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.tee = tee;
|
|
6
|
-
class Queue {
|
|
7
|
-
#source;
|
|
8
|
-
#queue;
|
|
9
|
-
head;
|
|
10
|
-
done;
|
|
11
|
-
constructor(iterable) {
|
|
12
|
-
this.#source = iterable[Symbol.asyncIterator]();
|
|
13
|
-
this.#queue = {
|
|
14
|
-
value: undefined,
|
|
15
|
-
next: undefined,
|
|
16
|
-
};
|
|
17
|
-
this.head = this.#queue;
|
|
18
|
-
this.done = false;
|
|
19
|
-
}
|
|
20
|
-
async next() {
|
|
21
|
-
const result = await this.#source.next();
|
|
22
|
-
if (!result.done) {
|
|
23
|
-
const nextNode = {
|
|
24
|
-
value: result.value,
|
|
25
|
-
next: undefined,
|
|
26
|
-
};
|
|
27
|
-
this.#queue.next = nextNode;
|
|
28
|
-
this.#queue = nextNode;
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
this.done = true;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Branches the given async iterable into the `n` branches.
|
|
37
|
-
*
|
|
38
|
-
* @example Usage
|
|
39
|
-
* ```ts
|
|
40
|
-
* import { tee } from "@std/async/tee";
|
|
41
|
-
* import { assertEquals } from "@std/assert";
|
|
42
|
-
*
|
|
43
|
-
* const gen = async function* gen() {
|
|
44
|
-
* yield 1;
|
|
45
|
-
* yield 2;
|
|
46
|
-
* yield 3;
|
|
47
|
-
* };
|
|
48
|
-
*
|
|
49
|
-
* const [branch1, branch2] = tee(gen());
|
|
50
|
-
*
|
|
51
|
-
* const result1 = await Array.fromAsync(branch1);
|
|
52
|
-
* assertEquals(result1, [1, 2, 3]);
|
|
53
|
-
*
|
|
54
|
-
* const result2 = await Array.fromAsync(branch2);
|
|
55
|
-
* assertEquals(result2, [1, 2, 3]);
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @typeParam T The type of the provided async iterable and the returned async iterables.
|
|
59
|
-
* @typeParam N The amount of branches to tee into.
|
|
60
|
-
* @param iterable The iterable to tee.
|
|
61
|
-
* @param n The amount of branches to tee into.
|
|
62
|
-
* @returns The tuple where each element is an async iterable.
|
|
63
|
-
*/
|
|
64
|
-
function tee(iterable, n = 2) {
|
|
65
|
-
const queue = new Queue(iterable);
|
|
66
|
-
async function* generator() {
|
|
67
|
-
let buffer = queue.head;
|
|
68
|
-
while (true) {
|
|
69
|
-
if (buffer.next) {
|
|
70
|
-
buffer = buffer.next;
|
|
71
|
-
yield buffer.value;
|
|
72
|
-
}
|
|
73
|
-
else if (queue.done) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
await queue.next();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return Array.from({ length: n }).map(() => generator());
|
|
82
|
-
}
|