Replace @repeaterjs/repeater dependency with inlined Repeater class

Port the core Repeater class (push/stop executor, async iterator protocol,
overflow protection) from @repeaterjs/repeater into src/repeater.ts, stripping
buffers and combinators we don't use. This removes the 7-year-old dependency
while keeping the same API surface for all current and future adapters
(Redis, WebSocket, SSE, Iroh).

Also add the async utility research doc for reference.
This commit is contained in:
2026-04-30 12:10:11 +00:00
parent 8c025c3433
commit 9c332529df
7 changed files with 680 additions and 15 deletions

View File

@@ -24,7 +24,7 @@
* SOFTWARE.
*/
import { Repeater } from "@repeaterjs/repeater";
import { Repeater } from "./repeater.js";
import type { TypedEventTarget, TypedEvent } from "./types.js";
export type PubSubPublishArgsByKey = {

View File

@@ -1,4 +1,4 @@
export { createPubSub, type PubSub, type PubSubConfig, type PubSubEvent, type PubSubEventTarget, type PubSubPublishArgsByKey } from "./create_pubsub.js";
export { type TypedEvent, type TypedEventTarget, type TypedEventListener, type TypedEventListenerObject, type TypedEventListenerOrEventListenerObject } from "./types.js";
export { filter, map, pipe } from "./operators.js";
export { Repeater } from "@repeaterjs/repeater";
export { Repeater, RepeaterOverflowError, type Push, type Stop, type RepeaterExecutor, type RepeaterBuffer } from "./repeater.js";

View File

@@ -4,7 +4,7 @@
* License: MIT
*/
import { Repeater, type Stop, type Push } from "@repeaterjs/repeater";
import { Repeater, type Stop, type Push } from "./repeater.js";
export function filter<T, U extends T>(
filterFn: (input: T) => input is U,

413
src/repeater.ts Normal file
View File

@@ -0,0 +1,413 @@
/*
* Adapted from @repeaterjs/repeater
* Original source: https://github.com/repeaterjs/repeater
* License: MIT
*
* Copyright (c) 2022 Repeater Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export class RepeaterOverflowError extends Error {
constructor(message: string) {
super(message);
Object.defineProperty(this, "name", {
value: "RepeaterOverflowError",
enumerable: false,
});
if (typeof Object.setPrototypeOf === "function") {
Object.setPrototypeOf(this, this.constructor.prototype);
} else {
(this as any).__proto__ = this.constructor.prototype;
}
if (typeof (Error as any).captureStackTrace === "function") {
(Error as any).captureStackTrace(this, this.constructor);
}
}
}
export type Push<T, TNext = unknown> = (
value: PromiseLike<T> | T,
) => Promise<TNext | undefined>;
export type Stop = ((err?: unknown) => undefined) & Promise<undefined>;
export type RepeaterExecutor<T, TReturn = any, TNext = unknown> = (
push: Push<T, TNext>,
stop: Stop,
) => PromiseLike<TReturn> | TReturn;
interface PushOperation<T, TNext> {
value: Promise<T | undefined>;
resolve(next?: PromiseLike<TNext> | TNext): unknown;
}
interface NextOperation<T, TReturn, TNext> {
value: PromiseLike<TNext> | TNext | undefined;
resolve(iteration: Promise<IteratorResult<T, TReturn>>): unknown;
}
const Initial = 0;
const Started = 1;
const Stopped = 2;
const Done = 3;
const Rejected = 4;
export const MAX_QUEUE_LENGTH = 1024;
const NOOP = () => {};
interface RepeaterRecord<T, TReturn, TNext> {
state: number;
executor: RepeaterExecutor<T, TReturn, TNext>;
buffer: RepeaterBuffer | undefined;
pushes: Array<PushOperation<T, TNext>>;
nexts: Array<NextOperation<T, TReturn, TNext>>;
pending: Promise<unknown> | undefined;
execution: Promise<TReturn | undefined> | undefined;
err: unknown;
onnext: (value?: PromiseLike<TNext> | TNext) => unknown;
onstop: () => unknown;
}
export interface RepeaterBuffer<TValue = unknown> {
empty: boolean;
full: boolean;
add(value: TValue): unknown;
remove(): TValue;
}
function swallow(value: any): void {
if (value != null && typeof value.then === "function") {
value.then(NOOP, NOOP);
}
}
function consumeExecution<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
): Promise<TReturn | undefined> {
const err = r.err;
const execution = Promise.resolve(r.execution).then((value) => {
if (err != null) {
throw err;
}
return value;
});
r.err = undefined;
r.execution = execution.then(
() => undefined,
() => undefined,
);
return r.pending === undefined ? execution : r.pending.then(() => execution);
}
function createIteration<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
value: Promise<T | TReturn | undefined> | T | TReturn | undefined,
): Promise<IteratorResult<T, TReturn>> {
const done = r.state >= Done;
return Promise.resolve(value).then((value: any) => {
if (!done && r.state >= Rejected) {
return consumeExecution<T, TReturn, TNext>(r).then((value: any) => ({
value,
done: true,
}));
}
return { value, done };
});
}
function stop<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
err?: unknown,
): void {
if (r.state >= Stopped) {
return;
}
r.state = Stopped;
r.onnext();
r.onstop();
if (r.err == null) {
r.err = err;
}
if (
r.pushes.length === 0 &&
(typeof r.buffer === "undefined" || r.buffer.empty)
) {
finish(r);
} else {
for (const push of r.pushes) {
push.resolve();
}
}
}
function finish<T, TReturn, TNext>(r: RepeaterRecord<T, TReturn, TNext>): void {
if (r.state >= Done) {
return;
}
if (r.state < Stopped) {
stop(r);
}
r.state = Done;
r.buffer = undefined;
for (const next of r.nexts) {
const execution: Promise<TReturn | undefined> =
r.pending === undefined
? consumeExecution<T, TReturn, TNext>(r)
: r.pending.then(() => consumeExecution<T, TReturn, TNext>(r));
next.resolve(createIteration<T, TReturn, TNext>(r, execution));
}
r.pushes = [];
r.nexts = [];
}
function reject(r: RepeaterRecord<any, any, any>): void {
if (r.state >= Rejected) {
return;
}
if (r.state < Done) {
finish(r);
}
r.state = Rejected;
}
function push<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
value: PromiseLike<T> | T,
): Promise<TNext | undefined> {
swallow(value);
if (r.pushes.length >= MAX_QUEUE_LENGTH) {
throw new RepeaterOverflowError(
`No more than ${MAX_QUEUE_LENGTH} pending calls to push are allowed on a single repeater.`,
);
} else if (r.state >= Stopped) {
return Promise.resolve(undefined);
}
let valueP: Promise<T | undefined> =
r.pending === undefined
? Promise.resolve(value)
: r.pending.then(() => value);
valueP = valueP.catch((err) => {
if (r.state < Stopped) {
r.err = err;
}
reject(r);
return undefined;
});
let nextP: Promise<TNext | undefined>;
if (r.nexts.length) {
const next = r.nexts.shift()!;
next.resolve(createIteration<T, TReturn, TNext>(r, valueP));
if (r.nexts.length) {
nextP = Promise.resolve(r.nexts[0].value);
} else if (typeof r.buffer !== "undefined" && !r.buffer.full) {
nextP = Promise.resolve(undefined);
} else {
nextP = new Promise((resolve) => (r.onnext = resolve));
}
} else if (typeof r.buffer !== "undefined" && !r.buffer.full) {
r.buffer.add(valueP);
nextP = Promise.resolve(undefined);
} else {
nextP = new Promise((resolve) => r.pushes.push({ resolve, value: valueP }));
}
let floating = true;
let next = {} as Promise<TNext | undefined>;
const unhandled = nextP.catch((err) => {
if (floating) {
throw err;
}
return undefined;
});
next.then = (onfulfilled, onrejected): any => {
floating = false;
return Promise.prototype.then.call(nextP, onfulfilled, onrejected);
};
next.catch = (onrejected): any => {
floating = false;
return Promise.prototype.catch.call(nextP, onrejected);
};
next.finally = nextP.finally.bind(nextP);
r.pending = valueP
.then(() => unhandled)
.catch((err) => {
r.err = err;
reject(r);
});
return next;
}
function createStop<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
): Stop {
const stop1 = stop.bind(null, r as RepeaterRecord<unknown, unknown, unknown>) as Stop;
const stopP = new Promise<undefined>((resolve) => (r.onstop = resolve as () => unknown));
stop1.then = stopP.then.bind(stopP);
stop1.catch = stopP.catch.bind(stopP);
stop1.finally = stopP.finally.bind(stopP);
return stop1;
}
function execute<T, TReturn, TNext>(
r: RepeaterRecord<T, TReturn, TNext>,
): void {
if (r.state >= Started) {
return;
}
r.state = Started;
const push1 = (push as any).bind(null, r) as Push<T, TNext>;
const stop1 = createStop(r);
r.execution = new Promise((resolve) => resolve(r.executor(push1, stop1)));
r.execution.catch(() => stop(r));
}
type RecordMap<T, TResult, TNext> = WeakMap<
Repeater<T, TResult, TNext>,
RepeaterRecord<T, TResult, TNext>
>;
const records: RecordMap<any, any, any> = new WeakMap();
export class Repeater<T, TReturn = any, TNext = unknown> {
constructor(
executor: RepeaterExecutor<T, TReturn, TNext>,
buffer?: RepeaterBuffer | undefined,
) {
records.set(this, {
executor,
buffer,
err: undefined,
state: Initial,
pushes: [],
nexts: [],
pending: undefined,
execution: undefined,
onnext: NOOP,
onstop: NOOP,
});
}
next(
value?: PromiseLike<TNext> | TNext,
): Promise<IteratorResult<T, TReturn>> {
swallow(value);
const r = records.get(this);
if (r === undefined) {
throw new Error("WeakMap error");
}
if (r.nexts.length >= MAX_QUEUE_LENGTH) {
throw new RepeaterOverflowError(
`No more than ${MAX_QUEUE_LENGTH} pending calls to next are allowed on a single repeater.`,
);
}
if (r.state <= Initial) {
execute(r);
}
r.onnext(value);
if (typeof r.buffer !== "undefined" && !r.buffer.empty) {
const result = createIteration(
r,
r.buffer.remove() as Promise<T | undefined>,
);
if (r.pushes.length) {
const push = r.pushes.shift()!;
r.buffer.add(push.value);
r.onnext = push.resolve;
}
return result;
} else if (r.pushes.length) {
const push = r.pushes.shift()!;
r.onnext = push.resolve;
return createIteration(r, push.value);
} else if (r.state >= Stopped) {
finish(r);
return createIteration(r, consumeExecution(r));
}
return new Promise((resolve) => r.nexts.push({ resolve, value }));
}
return(
value?: PromiseLike<TReturn> | TReturn,
): Promise<IteratorResult<T, TReturn>> {
swallow(value);
const r = records.get(this);
if (r === undefined) {
throw new Error("WeakMap error");
}
finish(r);
r.execution = Promise.resolve(r.execution).then(() => value);
return createIteration(r, consumeExecution(r));
}
throw(err: unknown): Promise<IteratorResult<T, TReturn>> {
const r = records.get(this);
if (r === undefined) {
throw new Error("WeakMap error");
}
if (
r.state <= Initial ||
r.state >= Stopped ||
(typeof r.buffer !== "undefined" && !r.buffer.empty)
) {
finish(r);
if (r.err == null) {
r.err = err;
}
return createIteration(r, consumeExecution(r));
}
return this.next(Promise.reject(err));
}
[Symbol.asyncIterator](): this {
return this;
}
}