Export everything from mod.ts
This commit is contained in:
parent
574ccc87bf
commit
f4875756d9
63
async.ts
63
async.ts
@ -104,7 +104,7 @@ export function channel<T = void>(): Channel<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
channel.sender = function sender<T>(
|
channel.sender = function sender<T>(
|
||||||
f: (recv: Receiver<T>) => void | PromiseLike<void>
|
f: (recv: Receiver<T>) => void | PromiseLike<void>,
|
||||||
): Sender<T> {
|
): Sender<T> {
|
||||||
const { send, recv } = channel<T>();
|
const { send, recv } = channel<T>();
|
||||||
Promise.resolve(f(recv)).then(noop).then(recv.close, recv.close);
|
Promise.resolve(f(recv)).then(noop).then(recv.close, recv.close);
|
||||||
@ -112,7 +112,7 @@ channel.sender = function sender<T>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
channel.receiver = function receiver<T>(
|
channel.receiver = function receiver<T>(
|
||||||
f: (send: Sender<T>) => void | PromiseLike<void>
|
f: (send: Sender<T>) => void | PromiseLike<void>,
|
||||||
): Receiver<T> {
|
): Receiver<T> {
|
||||||
const { send, recv } = channel<T>();
|
const { send, recv } = channel<T>();
|
||||||
Promise.resolve(f(send)).then(noop).then(send.close, send.close);
|
Promise.resolve(f(send)).then(noop).then(send.close, send.close);
|
||||||
@ -143,62 +143,3 @@ export function semaphore(count = 1) {
|
|||||||
|
|
||||||
return acquire;
|
return acquire;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PoolToken<T> extends Iterable<T, void, void> {
|
|
||||||
readonly value: T;
|
|
||||||
release(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function pool<T>(size: number, f: () => T | PromiseLike<T>) {
|
|
||||||
const lock = semaphore(size);
|
|
||||||
const all = new Set<T>();
|
|
||||||
const free: T[] = [];
|
|
||||||
const Token = class implements PoolToken<T> {
|
|
||||||
#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<PoolToken<T>> {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
@ -114,7 +114,7 @@ export class TypedEmitter<T extends EventMap> {
|
|||||||
b: any,
|
b: any,
|
||||||
c: any,
|
c: any,
|
||||||
d: any,
|
d: any,
|
||||||
e: any
|
e: any,
|
||||||
) {
|
) {
|
||||||
const event = this.#events[name];
|
const event = this.#events[name];
|
||||||
if (!event) return false;
|
if (!event) return false;
|
||||||
|
6
jit.ts
6
jit.ts
@ -30,7 +30,7 @@ export class JitCompiler {
|
|||||||
compile() {
|
compile() {
|
||||||
return new Function(
|
return new Function(
|
||||||
...this.#args.values(),
|
...this.#args.values(),
|
||||||
`"use strict"; return (${this.#body});`
|
`"use strict"; return (${this.#body});`,
|
||||||
)(...this.#args.keys());
|
)(...this.#args.keys());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ export function literal(x: unknown) {
|
|||||||
export function map<T>(
|
export function map<T>(
|
||||||
sep: string | JitFragment,
|
sep: string | JitFragment,
|
||||||
xs: Iterable<T>,
|
xs: Iterable<T>,
|
||||||
f: (value: T, index: number) => unknown
|
f: (value: T, index: number) => unknown,
|
||||||
): JitFragment {
|
): JitFragment {
|
||||||
return fragment(sep, ...Iterator.from(xs).map(f));
|
return fragment(sep, ...Iterator.from(xs).map(f));
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ export function map<T>(
|
|||||||
export function condition(
|
export function condition(
|
||||||
test: unknown,
|
test: unknown,
|
||||||
consequent: JitFragment,
|
consequent: JitFragment,
|
||||||
alternate: JitFragment = jit``
|
alternate: JitFragment = jit``,
|
||||||
): JitFragment {
|
): JitFragment {
|
||||||
return test ? consequent : alternate;
|
return test ? consequent : alternate;
|
||||||
}
|
}
|
||||||
|
30
mod.ts
30
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";
|
Loading…
Reference in New Issue
Block a user