ciorent 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import { minify } from 'oxc-minify';
2
+ import { LIB } from './utils.ts';
3
+
4
+ const toByte = (num: number) =>
5
+ num >= 1e3 ? (num / 1e3).toFixed(2) + 'KB' : num + 'B';
6
+
7
+ const arr = await Promise.all(
8
+ [...new Bun.Glob('**/*.js').scanSync(LIB)].map(async (path) => {
9
+ const file = Bun.file(LIB + '/' + path);
10
+ const code = await file.text();
11
+ const minfiedCode = minify(path, code).code!;
12
+
13
+ return {
14
+ entry: path,
15
+ size: file.size,
16
+ minified: Buffer.from(minfiedCode).byteLength,
17
+ gzip: Bun.gzipSync(code).byteLength,
18
+ minifiedGzip: Bun.gzipSync(minfiedCode).byteLength,
19
+ };
20
+ }),
21
+ );
22
+
23
+ console.table(
24
+ arr
25
+ .sort((a, b) => a.size - b.size)
26
+ .map((val) => ({
27
+ Entry: val.entry,
28
+ Size: toByte(val.size),
29
+ Minify: toByte(val.minified),
30
+ GZIP: toByte(val.gzip),
31
+ 'Minify GZIP': toByte(val.minifiedGzip),
32
+ })),
33
+ );
@@ -0,0 +1,8 @@
1
+ import { SCRIPTS } from './utils.ts';
2
+
3
+ const task = process.argv[2];
4
+ if (task == null) throw new Error('A task must be specified!');
5
+
6
+ await Bun.$`bun run ${{
7
+ raw: SCRIPTS + '/' + task + '.ts',
8
+ }} ${process.argv.slice(3)}`;
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "isolatedModules": false,
5
+ "isolatedDeclarations": false,
6
+ "allowJs": true,
7
+ "paths": {
8
+ "ciorent": ["../src"],
9
+ "ciorent/*": ["../src/*"]
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,16 @@
1
+ import { join, relative } from 'node:path';
2
+ import { SCRIPTS } from './utils.ts';
3
+
4
+ const paths = process.argv.slice(2);
5
+
6
+ for (const path of paths)
7
+ Bun.write(
8
+ join(path, 'tsconfig.json'),
9
+ JSON.stringify(
10
+ {
11
+ extends: relative(path, SCRIPTS + '/tsconfig.json'),
12
+ },
13
+ null,
14
+ 2,
15
+ ),
16
+ );
@@ -0,0 +1,15 @@
1
+ import { join, resolve } from 'node:path/posix';
2
+ import { $, file, write } from 'bun';
3
+
4
+ export const SCRIPTS = import.meta.dir;
5
+ export const ROOT = resolve(SCRIPTS, '..');
6
+ export const SOURCE = ROOT + '/src';
7
+ export const LIB = ROOT + '/lib';
8
+ export const BENCH = ROOT + '/bench';
9
+ export const EXAMPLES = ROOT + '/examples/src';
10
+
11
+ export const cp = (from: string, to: string, path: string) =>
12
+ write(join(to, path), file(join(from, path)));
13
+ export const exec = (...args: Parameters<typeof $>) =>
14
+ $(...args).catch((err) => process.stderr.write(err.stderr as any));
15
+ export const cd = (dir: string) => $.cwd(dir);
package/src/index.ts ADDED
@@ -0,0 +1,96 @@
1
+ import {
2
+ loadedReject,
3
+ loadedResolve,
4
+ loadResolve,
5
+ loadResolvers,
6
+ } from './utils.js';
7
+
8
+ /**
9
+ * Continue the execution on next event loop cycle.
10
+ *
11
+ * You can `await` this **occasionally** in an expensive synchronous operation to avoid
12
+ * blocking the main thread and let other asynchronous task to run.
13
+ */
14
+ export const nextTick: Promise<void> = Promise.resolve();
15
+
16
+ const getFinishedState = async (s: [number], p: Promise<any>) => {
17
+ try {
18
+ await p;
19
+ s[0] = 1;
20
+ } catch (e) {
21
+ s[0] = 0;
22
+
23
+ // Don't swallow error
24
+ return p;
25
+ }
26
+ };
27
+ /**
28
+ * Get the state of a promise on next tick:
29
+ * - `0`: Input promise rejected
30
+ * - `1`: Input promise resolves
31
+ * - `2`: Input promise pending
32
+ */
33
+ export const state = async (p: Promise<any>): Promise<0 | 1 | 2> => {
34
+ const res = [2] as [0 | 1 | 2];
35
+ getFinishedState(res, p);
36
+ await nextTick;
37
+ return res[0] as any;
38
+ };
39
+
40
+ /**
41
+ * Check whether a value is awaitable
42
+ * @param p
43
+ * @returns
44
+ */
45
+ export const isThenable = <T>(p: unknown): p is PromiseLike<T> =>
46
+ p !== null &&
47
+ typeof p === 'object' &&
48
+ !Array.isArray(p) &&
49
+ // @ts-ignore
50
+ typeof p.then === 'function';
51
+
52
+ /**
53
+ * Timeout a promise
54
+ * @param p
55
+ * @param ms
56
+ */
57
+ export const timeout = <T>(p: Promise<T>, ms: number): Promise<T | void> => {
58
+ const promise = new Promise<void>(loadResolvers);
59
+ setTimeout(loadedResolve, ms);
60
+ p.then(loadedResolve, loadedReject);
61
+ return promise;
62
+ };
63
+
64
+ /**
65
+ * Sleep for a duration.
66
+ * @param ms - Sleep duration in milliseconds
67
+ */
68
+ export const sleep: (ms: number) => Promise<void> =
69
+ globalThis.Bun?.sleep ??
70
+ globalThis.process?.getBuiltinModule?.('timers/promises').setTimeout ??
71
+ ((ms) => {
72
+ const promise = new Promise(loadResolve);
73
+ setTimeout(loadedResolve, ms);
74
+ return promise;
75
+ });
76
+
77
+ const sharedBuf = new Int32Array(new SharedArrayBuffer(4));
78
+
79
+ /**
80
+ * Sleep for a duration synchronously.
81
+ *
82
+ * This method blocks the current thread.
83
+ *
84
+ * On the browser it only works in workers.
85
+ * @param ms - Sleep duration in milliseconds
86
+ */
87
+ export const sleepSync: (ms: number) => void =
88
+ globalThis.Bun?.sleepSync ??
89
+ ((ms) => {
90
+ Atomics.wait(sharedBuf, 0, 0, ms);
91
+ });
92
+
93
+ export * as mutex from './mutex.js';
94
+ export * as limit from './rate-limit.js';
95
+ export * as semaphore from './semaphore.js';
96
+ export * as signal from './signal.js';
package/src/mutex.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { nextTick } from './index.js';
2
+ import { type Extend, loadedResolve, loadResolve } from './utils.js';
3
+
4
+ /**
5
+ * Describe a mutex.
6
+ */
7
+ export type Mutex = [Promise<void>];
8
+
9
+ /**
10
+ * Create a mutex.
11
+ */
12
+ export const init = (): Mutex => [nextTick];
13
+
14
+ /**
15
+ * Acquire a mutex.
16
+ */
17
+ export const acquire = async (mu: Extend<Mutex>): Promise<() => void> => {
18
+ const currentLock = mu[0];
19
+ mu[0] = new Promise<void>(loadResolve);
20
+
21
+ const release = loadedResolve;
22
+ await currentLock;
23
+ return release;
24
+ };
25
+
26
+ const chainLock = async (
27
+ lock: Promise<void>,
28
+ fn: any,
29
+ ...args: any[]
30
+ ): Promise<any> => {
31
+ try {
32
+ await lock;
33
+ } finally {
34
+ return fn(...args);
35
+ }
36
+ };
37
+ /**
38
+ * Automatically acquire and run a task.
39
+ */
40
+ export const run = <const T extends (...args: any[]) => Promise<any>>(
41
+ mu: Extend<Mutex>,
42
+ fn: T,
43
+ ...args: Parameters<T>
44
+ ): ReturnType<T> => (mu[0] = chainLock(mu[0], fn, ...args) as any);
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Describe a rate limiter
3
+ */
4
+ export type Limiter = (limit: number, ms: number) => () => boolean;
5
+
6
+ /**
7
+ * Fixed window strategy
8
+ * @param limit
9
+ * @param ms
10
+ */
11
+ export const fixedWindow: Limiter = (limit, ms) => {
12
+ let cur = limit;
13
+ const unlock = () => {
14
+ cur = limit;
15
+ };
16
+
17
+ return () => {
18
+ if (cur === 0) return false;
19
+ if (cur-- === limit) setTimeout(unlock, ms);
20
+
21
+ return true;
22
+ };
23
+ };
24
+
25
+ /**
26
+ * Sliding window strategy
27
+ * @param limit
28
+ * @param ms
29
+ */
30
+ export const slidingWindow: Limiter = (limit, ms) => {
31
+ let cur = limit;
32
+ const unlock = () => {
33
+ cur++;
34
+ };
35
+
36
+ return () => {
37
+ if (cur === 0) return false;
38
+
39
+ cur--;
40
+ setTimeout(unlock, ms);
41
+ return true;
42
+ };
43
+ };
44
+
45
+ /**
46
+ * Token bucket strategy
47
+ * @param limit
48
+ * @param ms
49
+ */
50
+ export const tokenBucket: Limiter = (limit, ms) => {
51
+ let cur = limit;
52
+
53
+ ms /= limit;
54
+ const unlock = () => {
55
+ if (cur++ < limit) setTimeout(unlock, ms);
56
+ };
57
+
58
+ return () => {
59
+ if (cur === 0) return false;
60
+ if (cur-- === limit) setTimeout(unlock, ms);
61
+
62
+ return true;
63
+ };
64
+ };
@@ -0,0 +1,74 @@
1
+ import { type Extend, loadedResolve, loadResolve } from './utils.js';
2
+
3
+ type QueueItem = () => void;
4
+ type Queue = [(QueueItem | null)[], len: number, head: number, tail: number];
5
+
6
+ const push = (qu: Extend<Queue>, item: QueueItem): void => {
7
+ const tail = qu[3];
8
+ qu[3] = tail + 1 === qu[1] ? 0 : tail + 1;
9
+ qu[0][tail] = item;
10
+ };
11
+
12
+ /**
13
+ * Check whether the semaphore queue is full.
14
+ */
15
+ export const full = (qu: Extend<Queue>): boolean =>
16
+ qu[2] === qu[3] && qu[0][qu[2]] !== null;
17
+
18
+ const pop = (qu: Extend<Queue>): QueueItem => {
19
+ const head = qu[2];
20
+ qu[2] = head + 1 === qu[1] ? 0 : head + 1;
21
+
22
+ const val = qu[0][head];
23
+ qu[0][head] = null;
24
+ return val!;
25
+ };
26
+
27
+ export type Semaphore = [...Queue, remain: number];
28
+
29
+ /**
30
+ * Create a semaphore.
31
+ *
32
+ * @example
33
+ * // maximum of 10 concurrent tasks and 200 waiting tasks.
34
+ * const sem = semaphore.init(10, 200);
35
+ */
36
+ export const init = (permits: number, capacity: number): Semaphore => [
37
+ new Array(capacity).fill(null),
38
+ capacity,
39
+ 0,
40
+ 0,
41
+ permits,
42
+ ];
43
+
44
+ /**
45
+ * Acquire a permit.
46
+ *
47
+ * @example
48
+ *
49
+ * if (semaphore.full(sem)) {
50
+ * // Internal queue is full
51
+ * }
52
+ *
53
+ * await semaphore.acquire(sem);
54
+ *
55
+ * // Do something and then release the permit.
56
+ * semaphore.release(sem);
57
+ */
58
+ export const acquire = (sem: Extend<Semaphore>): Promise<void> | void => {
59
+ if (--sem[4] < 0) {
60
+ const promise = new Promise<void>(loadResolve);
61
+ push(sem, loadedResolve);
62
+ return promise;
63
+ }
64
+ };
65
+
66
+ /**
67
+ * Release a permit.
68
+ *
69
+ * @example
70
+ * semaphore.release(sem);
71
+ */
72
+ export const release = (sem: Extend<Semaphore>): void => {
73
+ sem[4]++ < 0 && pop(sem)();
74
+ };
package/src/signal.ts ADDED
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Describe a signal
3
+ */
4
+ export type Signal = [interrupted: boolean, ...parents: Signal[]];
5
+
6
+ /**
7
+ * Create a signal
8
+ */
9
+ export const init = (): Signal => [false];
10
+
11
+ /**
12
+ * Create a signal that aborts when any of the input signals abort
13
+ * @param sigs
14
+ */
15
+ export const any = (signals: Signal[]): Signal => {
16
+ const sig: Signal = [false];
17
+ for (let i = 0; i < signals.length; i++) signals[i].push(sig);
18
+ return sig;
19
+ };
20
+
21
+ const _ = [false];
22
+ /**
23
+ * Create a signal that when interrupted will interrupt a group of other signals
24
+ */
25
+ export const group = (signals: Signal[]): Signal =>
26
+ _.concat(signals as any) as any;
27
+
28
+ /**
29
+ * Check whether the signal has been aborted
30
+ * @param t
31
+ */
32
+ export const aborted = (t: Signal): boolean => t[0];
33
+
34
+ /**
35
+ * Abort a signal
36
+ * @param t
37
+ */
38
+ export const abort = (t: Signal): void => {
39
+ if (!t[0]) {
40
+ t[0] = true;
41
+ if (t.length > 1) for (let i = 1; i < t.length; i++) abort(t[i] as Signal);
42
+ }
43
+ };
44
+
45
+ /**
46
+ * Abort a signal after a duration
47
+ * @param t
48
+ */
49
+ export const abortAfter = (ms: number, t: Signal): void => {
50
+ setTimeout(() => abort(t), ms);
51
+ };
52
+
53
+ /**
54
+ * Create a signal that aborts after ms
55
+ * @param ms
56
+ */
57
+ export const timeout = (ms: number): Signal => {
58
+ const sig: Signal = [false];
59
+ abortAfter(ms, sig);
60
+ return sig;
61
+ };
62
+
63
+ /**
64
+ * Attach a signal to a `DisposableStack` or `AsyncDisposableStack`
65
+ */
66
+ export const adopt = (
67
+ t: Signal,
68
+ stack: DisposableStack | AsyncDisposableStack,
69
+ ): void => {
70
+ stack.adopt(t, abort);
71
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,16 @@
1
+ export type Extend<T extends any[]> = [...T, ...any[]];
2
+
3
+ export let loadedResolve: (res?: any) => void;
4
+ export let loadedReject: (reason?: any) => void;
5
+
6
+ export const loadResolvers = (
7
+ res: (value?: any) => void,
8
+ rej: (reason?: any) => void,
9
+ ): void => {
10
+ loadedResolve = res;
11
+ loadedReject = rej;
12
+ };
13
+
14
+ export const loadResolve = (res: (value?: any) => void): void => {
15
+ loadedResolve = res;
16
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Enable latest features
4
+ "lib": [
5
+ "ESNext.AsyncIterable",
6
+ "ES2015.Iterable",
7
+ "ESNext",
8
+ "DOM",
9
+ "DOM.Iterable",
10
+ "DOM.AsyncIterable",
11
+ "WebWorker.ImportScripts",
12
+ "Webworker.Iterable",
13
+ "WebWorker.AsyncIterable"
14
+ ],
15
+ "target": "ESNext",
16
+
17
+ // Module
18
+ "module": "Preserve",
19
+ "moduleDetection": "force",
20
+ "resolveJsonModule": true,
21
+ "isolatedModules": true,
22
+ "moduleResolution": "Bundler",
23
+ "allowImportingTsExtensions": true,
24
+ "verbatimModuleSyntax": true,
25
+
26
+ // Best practices
27
+ "strict": true,
28
+ "skipLibCheck": true,
29
+ "noUnusedLocals": false,
30
+ "noImplicitAny": true,
31
+ "noImplicitOverride": true,
32
+ "noImplicitThis": true,
33
+ "noUnusedParameters": false,
34
+ "noFallthroughCasesInSwitch": true,
35
+ "noPropertyAccessFromIndexSignature": false,
36
+
37
+ // Declaration
38
+ "declaration": true,
39
+ "isolatedDeclarations": true,
40
+ "emitDeclarationOnly": true
41
+ }
42
+ }
package/index.d.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- * Continue the execution on next event loop cycle.
3
- *
4
- * You can `await` this **occasionally** in an expensive synchronous operation to avoid
5
- * blocking the main thread and let other asynchronous task to run.
6
- */
7
- export declare const nextTick: Promise<void>;
8
- /**
9
- * Get the state of a promise on next tick:
10
- * - `0`: Input promise rejected
11
- * - `1`: Input promise resolves
12
- * - `2`: Input promise pending
13
- */
14
- export declare const state: (p: Promise<any>) => Promise<0 | 1 | 2>;
15
- /**
16
- * Check whether a value is awaitable
17
- * @param p
18
- * @returns
19
- */
20
- export declare const isThenable: <T>(p: unknown) => p is PromiseLike<T>;
21
- /**
22
- * Timeout a promise
23
- * @param p
24
- * @param ms
25
- */
26
- export declare const timeout: <T>(p: Promise<T>, ms: number) => Promise<T | void>;
27
- /**
28
- * Sleep for a duration.
29
- * @param ms - Sleep duration in milliseconds
30
- */
31
- export declare const sleep: (ms: number) => Promise<void>;
32
- /**
33
- * Sleep for a duration synchronously.
34
- *
35
- * This method blocks the current thread.
36
- *
37
- * On the browser it only works in workers.
38
- * @param ms - Sleep duration in milliseconds
39
- */
40
- export declare const sleepSync: (ms: number) => void;
41
- export * as mutex from "./mutex.js";
42
- export * as limit from "./rate-limit.js";
43
- export * as semaphore from "./semaphore.js";
44
- export * as signal from "./signal.js";
package/index.js DELETED
@@ -1 +0,0 @@
1
- import{loadedReject,loadedResolve,loadResolve,loadResolvers}from"./utils.js";export let nextTick=Promise.resolve();let getFinishedState=async(s,p)=>{try{await p;s[0]=1}catch(e){s[0]=0;return p}};export let state=async p=>{let res=[2];getFinishedState(res,p);await nextTick;return res[0]};export let isThenable=p=>p!==null&&typeof p===`object`&&!Array.isArray(p)&&typeof p.then===`function`;let resolvePromise=async(res,rej,p)=>{try{res(await p)}catch(e){rej(e)}};export let timeout=(p,ms)=>{let promise=new Promise(loadResolvers);setTimeout(loadedResolve,ms);resolvePromise(loadedResolve,loadedReject,p);return promise};export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.(`timers/promises`).setTimeout??(ms=>{let promise=new Promise(loadResolve);setTimeout(loadedResolve,ms);return promise});let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??(ms=>{Atomics.wait(sharedBuf,0,0,ms)});export*as mutex from"./mutex.js";export*as limit from"./rate-limit.js";export*as semaphore from"./semaphore.js";export*as signal from"./signal.js";
package/mutex.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { type Extend } from "./utils.js";
2
- /**
3
- * Describe a mutex.
4
- */
5
- export type Mutex = [Promise<void>];
6
- /**
7
- * Create a mutex.
8
- */
9
- export declare const init: () => Mutex;
10
- /**
11
- * Acquire a mutex.
12
- */
13
- export declare const acquire: (mu: Extend<Mutex>) => Promise<() => void>;
14
- /**
15
- * Automatically acquire and run a task.
16
- */
17
- export declare const run: <const T extends (...args: any[]) => Promise<any>>(mu: Extend<Mutex>, fn: T, ...args: Parameters<T>) => ReturnType<T>;
package/mutex.js DELETED
@@ -1 +0,0 @@
1
- import{nextTick}from"./index.js";import{chainLock,loadedResolve,loadResolve}from"./utils.js";export let init=()=>[nextTick];export let acquire=async mu=>{let currentLock=mu[0];mu[0]=new Promise(loadResolve);let release=loadedResolve;await currentLock;return release};export let run=(mu,fn,...args)=>mu[0]=chainLock(mu[0],fn,...args);
package/rate-limit.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * Describe a rate limiter
3
- */
4
- export type Limiter = (limit: number, ms: number) => () => boolean;
5
- /**
6
- * Fixed window strategy
7
- * @param limit
8
- * @param ms
9
- */
10
- export declare const fixedWindow: Limiter;
11
- /**
12
- * Sliding window strategy
13
- * @param limit
14
- * @param ms
15
- */
16
- export declare const slidingWindow: Limiter;
17
- /**
18
- * Token bucket strategy
19
- * @param limit
20
- * @param ms
21
- */
22
- export declare const tokenBucket: Limiter;
package/rate-limit.js DELETED
@@ -1 +0,0 @@
1
- export let fixedWindow=(limit,ms)=>{let cur=limit;let unlock=()=>{cur=limit};return()=>{if(cur===0)return false;if(cur--===limit)setTimeout(unlock,ms);return true}};export let slidingWindow=(limit,ms)=>{let cur=limit;let unlock=()=>{cur++};return()=>{if(cur===0)return false;cur--;setTimeout(unlock,ms);return true}};export let tokenBucket=(limit,ms)=>{let cur=limit;ms/=limit;let unlock=()=>{if(cur++<limit)setTimeout(unlock,ms)};return()=>{if(cur===0)return false;if(cur--===limit)setTimeout(unlock,ms);return true}};
package/semaphore.d.ts DELETED
@@ -1,39 +0,0 @@
1
- import { type Extend } from "./utils.js";
2
- type QueueItem = () => void;
3
- type Queue = [(QueueItem | null)[], len: number, head: number, tail: number];
4
- /**
5
- * Check whether the semaphore queue is full.
6
- */
7
- export declare const full: (qu: Extend<Queue>) => boolean;
8
- export type Semaphore = [...Queue, remain: number];
9
- /**
10
- * Create a semaphore.
11
- *
12
- * @example
13
- * // maximum of 10 concurrent tasks and 200 waiting tasks.
14
- * const sem = semaphore.init(10, 200);
15
- */
16
- export declare const init: (permits: number, capacity: number) => Semaphore;
17
- /**
18
- * Acquire a permit.
19
- *
20
- * @example
21
- *
22
- * if (semaphore.full(sem)) {
23
- * // Internal queue is full
24
- * }
25
- *
26
- * await semaphore.acquire(sem);
27
- *
28
- * // Do something and then release the permit.
29
- * semaphore.release(sem);
30
- */
31
- export declare const acquire: (sem: Extend<Semaphore>) => Promise<void> | void;
32
- /**
33
- * Release a permit.
34
- *
35
- * @example
36
- * semaphore.release(sem);
37
- */
38
- export declare const release: (sem: Extend<Semaphore>) => void;
39
- export {};
package/semaphore.js DELETED
@@ -1 +0,0 @@
1
- import{loadedResolve,loadResolve}from"./utils.js";let push=(qu,item)=>{let tail=qu[3];qu[3]=tail+1===qu[1]?0:tail+1;qu[0][tail]=item};export let full=qu=>qu[2]===qu[3]&&qu[0][qu[2]]!==null;let pop=qu=>{let head=qu[2];qu[2]=head+1===qu[1]?0:head+1;let val=qu[0][head];qu[0][head]=null;return val};export let init=(permits,capacity)=>[new Array(capacity).fill(null),capacity,0,0,permits];export let acquire=sem=>{if(--sem[4]<0){let promise=new Promise(loadResolve);push(sem,loadedResolve);return promise}};export let release=sem=>{sem[4]++<0&&pop(sem)()};
package/signal.d.ts DELETED
@@ -1,41 +0,0 @@
1
- /**
2
- * Describe a signal
3
- */
4
- export type Signal = [interrupted: boolean, ...parents: Signal[]];
5
- /**
6
- * Create a signal
7
- */
8
- export declare const init: () => Signal;
9
- /**
10
- * Create a signal that aborts when any of the input signals abort
11
- * @param sigs
12
- */
13
- export declare const any: (signals: Signal[]) => Signal;
14
- /**
15
- * Create a signal that when interrupted will interrupt a group of other signals
16
- */
17
- export declare const group: (signals: Signal[]) => Signal;
18
- /**
19
- * Check whether the signal has been aborted
20
- * @param t
21
- */
22
- export declare const aborted: (t: Signal) => boolean;
23
- /**
24
- * Abort a signal
25
- * @param t
26
- */
27
- export declare const abort: (t: Signal) => void;
28
- /**
29
- * Abort a signal after a duration
30
- * @param t
31
- */
32
- export declare const abortAfter: (ms: number, t: Signal) => void;
33
- /**
34
- * Create a signal that aborts after ms
35
- * @param ms
36
- */
37
- export declare const timeout: (ms: number) => Signal;
38
- /**
39
- * Attach a signal to a `DisposableStack` or `AsyncDisposableStack`
40
- */
41
- export declare const adopt: (t: Signal, stack: DisposableStack | AsyncDisposableStack) => void;
package/signal.js DELETED
@@ -1 +0,0 @@
1
- export let init=()=>[false];export let any=signals=>{let sig=[false];for(let i=0;i<signals.length;i++)signals[i].push(sig);return sig};let _=[false];export let group=signals=>_.concat(signals);export let aborted=t=>t[0];export let abort=t=>{if(!t[0]){t[0]=true;if(t.length>1)for(let i=1;i<t.length;i++)abort(t[i])}};export let abortAfter=(ms,t)=>{setTimeout(()=>abort(t),ms)};export let timeout=ms=>{let sig=[false];abortAfter(ms,sig);return sig};export let adopt=(t,stack)=>{stack.adopt(t,abort)};