Add fast n=1 semaphore
This commit is contained in:
parent
ec741cb6c0
commit
0403a8611a
12
async.ts
12
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,3 +143,11 @@ export function semaphore(count = 1) {
|
|||||||
|
|
||||||
return acquire;
|
return acquire;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function semaphore_fast() {
|
||||||
|
let last = Promise.resolve<unknown>(undefined);
|
||||||
|
|
||||||
|
return function acquire<T>(f: () => T | PromiseLike<T>) {
|
||||||
|
return (last = last.then(f, f));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "@luaneko/lstd",
|
"name": "@luaneko/lstd",
|
||||||
"version": "0.1.2",
|
"version": "0.1.3",
|
||||||
"exports": "./mod.ts"
|
"exports": "./mod.ts"
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user