moderator-toolbox-nxg-for-reddit / util/data/async
util/data/async¶
Functions¶
createDeferredProcessQueue()¶
createDeferredProcessQueue<
Item,Result>(bulkProcess,delayTime?,maxQueueLength?,matchOptions?): (item) =>Promise<Result>
Defined in: extension/data/util/data/async.ts:52
Builds a batching queue: callers insert items one at a time, and processing is held off until either the gap between inserts exceeds a set delay or the queue reaches a maximum length. Each insert call returns a promise that resolves with that specific item’s processed result.
The queue is constructed with a processing function that takes an array of queued items and returns a promise resolving to the array of matching results.
When matchOptions is provided, results are matched to callers by key rather
than by position. Callers whose key is absent from the bulk response receive
a rejected promise with a descriptive error. Without matchOptions, results
are matched positionally; any callers beyond the end of the results array are
also rejected.
Type Parameters¶
Item¶
Item
Result¶
Result
Parameters¶
bulkProcess¶
(items) => Promise<Result[]>
delayTime?¶
number = 100
maxQueueLength?¶
number = Infinity
matchOptions?¶
DeferredQueueMatchOptions<Item, Result>
Returns¶
(item) => Promise<Result>
debounce()¶
debounce<
T>(func,debounceTime?):T
Defined in: extension/data/util/data/async.ts:13
Wraps a function so bursts of calls within the timeout collapse into one.
Type Parameters¶
T¶
T extends (…args) => void
Parameters¶
func¶
T
debounceTime?¶
number = 100
Returns¶
T
delay()¶
delay(
ms):Promise<void>
Defined in: extension/data/util/data/async.ts:7
Produces a promise that settles once the given duration has elapsed.
Parameters¶
ms¶
number
Number of milliseconds to delay
Returns¶
Promise<void>
mapWithConcurrency()¶
mapWithConcurrency<
T,R>(items,limit,fn):Promise<R[]>
Defined in: extension/data/util/data/async.ts:137
Maps items through fn with at most limit calls in flight at once, preserving
input order in the result. A small bounded worker-pool so a fan-out doesn’t start one
operation per item simultaneously (e.g. a cross-subreddit wiki read across dozens of
subs). fn is expected to handle its own errors - a rejection propagates and aborts
the batch.
Type Parameters¶
T¶
T
R¶
R
Parameters¶
items¶
readonly T[]
The inputs to map.
limit¶
number
Maximum number of concurrent fn calls (coerced to at least 1).
fn¶
(item, index) => Promise<R>
The async mapper, receiving each item and its index.
Returns¶
Promise<R[]>
wrapWithLastValue()¶
wrapWithLastValue<
T>(iterable):AsyncGenerator<{item:T;last:boolean; },void,unknown>
Defined in: extension/data/util/data/async.ts:112
Wraps an iterable so each yielded value becomes {item, last} where last
is true for the final item. Used by the pager to preload the next page.
Type Parameters¶
T¶
T
Parameters¶
iterable¶
AsyncIterable<T, any, any> | Iterable<T, any, any>
Returns¶
AsyncGenerator<{ item: T; last: boolean; }, void, unknown>
Interfaces¶
DeferredQueueMatchOptions¶
Defined in: extension/data/util/data/async.ts:30
Options for key-based result matching in createDeferredProcessQueue.
When provided, the queue uses these functions to correlate results back to
their callers by key instead of by position - required when the bulk
processor may return results in a different order than the inputs, or may
omit some entirely (e.g. Reddit’s /api/info silently drops deleted things).
Type Parameters¶
Item¶
Item
Result¶
Result
Properties¶
getItemKey¶
getItemKey: (
item) =>string
Defined in: extension/data/util/data/async.ts:32
Extract a lookup key from a queued item.
Parameters¶
item¶
Item
Returns¶
string
getResultKey¶
getResultKey: (
result) =>string
Defined in: extension/data/util/data/async.ts:34
Extract a lookup key from a bulk result.
Parameters¶
result¶
Result
Returns¶
string