diff --git a/async.ts b/async.ts index ac222b9..1a8ef7b 100644 --- a/async.ts +++ b/async.ts @@ -104,7 +104,7 @@ export function channel(): Channel { } channel.sender = function sender( - f: (recv: Receiver) => void | PromiseLike + f: (recv: Receiver) => void | PromiseLike, ): Sender { const { send, recv } = channel(); Promise.resolve(f(recv)).then(noop).then(recv.close, recv.close); @@ -112,7 +112,7 @@ channel.sender = function sender( }; channel.receiver = function receiver( - f: (send: Sender) => void | PromiseLike + f: (send: Sender) => void | PromiseLike, ): Receiver { const { send, recv } = channel(); Promise.resolve(f(send)).then(noop).then(send.close, send.close); @@ -143,62 +143,3 @@ export function semaphore(count = 1) { return acquire; } - -export interface PoolToken extends Iterable { - readonly value: T; - release(): void; -} - -export function pool(size: number, f: () => T | PromiseLike) { - const lock = semaphore(size); - const all = new Set(); - const free: T[] = []; - const Token = class implements PoolToken { - #value; - #open = true; - - get value() { - if (this.#open) return this.#value; - else throw new TypeError(`borrowed value is already released`); - } - - constructor(value: T) { - this.#value = value; - } - - *[Symbol.iterator]() { - yield this.value; - } - - release() { - this.#open &&= - (lock.release(), all.has(this.#value) && free.push(this.#value), false); - } - - [Symbol.dispose]() { - this.release(); - } - }; - - async function acquire(): Promise> { - await lock(); - try { - let value; - if (free.length !== 0) value = free.pop() as T; - else value = await f(); - return all.add(value), new Token(value); - } catch (e) { - throw (lock.release(), e); - } - } - - function forget(value: T) { - if (all.delete(value)) { - const idx = free.indexOf(value); - if (idx !== -1) free.splice(idx, 1); - } - } - - acquire.forget = forget; - return acquire; -} diff --git a/events.ts b/events.ts index e0413dc..057a908 100644 --- a/events.ts +++ b/events.ts @@ -114,7 +114,7 @@ export class TypedEmitter { b: any, c: any, d: any, - e: any + e: any, ) { const event = this.#events[name]; if (!event) return false; diff --git a/jit.ts b/jit.ts index 394bc27..7524e0c 100644 --- a/jit.ts +++ b/jit.ts @@ -30,7 +30,7 @@ export class JitCompiler { compile() { return new Function( ...this.#args.values(), - `"use strict"; return (${this.#body});` + `"use strict"; return (${this.#body});`, )(...this.#args.keys()); } } @@ -99,7 +99,7 @@ export function literal(x: unknown) { export function map( sep: string | JitFragment, xs: Iterable, - f: (value: T, index: number) => unknown + f: (value: T, index: number) => unknown, ): JitFragment { return fragment(sep, ...Iterator.from(xs).map(f)); } @@ -107,7 +107,7 @@ export function map( export function condition( test: unknown, consequent: JitFragment, - alternate: JitFragment = jit`` + alternate: JitFragment = jit``, ): JitFragment { return test ? consequent : alternate; } diff --git a/mod.ts b/mod.ts index e69de29..1d0b00a 100644 --- a/mod.ts +++ b/mod.ts @@ -0,0 +1,30 @@ +export { + type BinaryLike, + from_utf8, + to_utf8, + encode_utf8, + from_hex, + to_hex, + from_base64, + to_base64, + buf_eq, + buf_concat, + buf_concat_fast, + buf_resize, + buf_xor, +} from "./bytes.ts"; + +export { + type Deferred, + deferred, + notifier, + type Channel, + type Sender, + type Receiver, + channel, + semaphore, +} from "./async.ts"; + +export { Callable } from "./func.ts"; +export { Emitter, TypedEmitter, type EventMap } from "./events.ts"; +export { jit, is_jit, type JitFragment } from "./jit.ts";