ciorent 0.7.1 → 0.8.0

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.
package/README.md CHANGED
@@ -111,8 +111,8 @@ const task = async (id: number) => {
111
111
  for (let i = 1; i <= 5; i++) task(i);
112
112
  ```
113
113
 
114
- ### Fibers
115
- A module to interrupt executions of async functions.
114
+ ### Signal
115
+ A module to interrupt executions of functions.
116
116
  ```ts
117
117
  import { signal, sleep } from 'ciorent';
118
118
 
@@ -121,14 +121,14 @@ const logTime = (label: string) =>
121
121
 
122
122
  const f1 = async (sig: signal.Signal) => {
123
123
  // Wait for a promise
124
- console.log('Fiber 1 waiting: 1s');
124
+ console.log('Task 1 waiting: 1s');
125
125
  await sleep(1000);
126
126
 
127
127
  // Interruption point
128
- if (signal.interrupted(sig)) return;
128
+ if (signal.aborted(sig)) return;
129
129
 
130
130
  const res = Math.random();
131
- console.log('Fiber 1 result:', res);
131
+ console.log('Task 1 result:', res);
132
132
 
133
133
  return res;
134
134
  };
@@ -136,27 +136,23 @@ const f1 = async (sig: signal.Signal) => {
136
136
  {
137
137
  console.log('------------------------');
138
138
 
139
- console.log('Fiber 1 started');
139
+ logTime('Task 1 started');
140
140
  const sig = signal.init();
141
141
  const promise = f1(sig);
142
142
 
143
143
  // Interrupt the signal
144
- signal.interrupt(sig);
144
+ signal.abort(sig);
145
145
 
146
146
  // Execution will be stopped on the last interruption point
147
147
  await promise;
148
-
149
- console.log('Fiber 1 interrupted');
150
148
  }
151
149
 
152
150
  {
153
151
  console.log('------------------------');
154
152
 
155
- logTime('Fiber 1 started');
153
+ logTime('Task 1 started');
156
154
 
157
155
  // Interrupt the function after 500ms
158
- await f1(signal.duration(500));
159
-
160
- logTime('Fiber 1 interrupted');
156
+ await f1(signal.timeout(500));
161
157
  }
162
158
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "repository": {
6
6
  "type": "git",
package/signal.d.ts CHANGED
@@ -1,39 +1,33 @@
1
1
  /**
2
2
  * Describe a signal
3
3
  */
4
- export type Signal = [interrupted: boolean];
4
+ export type Signal = [interrupted: boolean, ...parents: Signal[]];
5
5
  /**
6
6
  * Create a signal
7
7
  */
8
8
  export declare const init: () => Signal;
9
9
  /**
10
- * Check whether the signal has been interrupted
11
- * @param t
10
+ * Create a signal that aborts when any of the input signals abort
11
+ * @param sigs
12
12
  */
13
- export declare const interrupted: (t: Signal) => boolean;
13
+ export declare const any: (...sigs: Signal[]) => Signal;
14
14
  /**
15
- * Interrupt a signal
15
+ * Check whether the signal has been aborted
16
16
  * @param t
17
17
  */
18
- export declare const interrupt: (t: Signal) => void;
18
+ export declare const aborted: (t: Signal) => boolean;
19
19
  /**
20
- * Interrupt a signal after a duration
20
+ * Abort a signal
21
21
  * @param t
22
22
  */
23
- export declare const timeout: (t: Signal, ms: number) => Promise<void>;
23
+ export declare const abort: (t: Signal) => void;
24
24
  /**
25
- * Link a signal to an `AbortSignal`
25
+ * Abort a signal after a duration
26
26
  * @param t
27
- * @param signal
28
27
  */
29
- export declare const link: (t: Signal, signal: AbortSignal) => void;
28
+ export declare const abortAfter: (ms: number, t: Signal) => Promise<void>;
30
29
  /**
31
- * Create a signal that interrupts after ms
30
+ * Create a signal that aborts after ms
32
31
  * @param ms
33
32
  */
34
- export declare const duration: (ms: number) => Signal;
35
- /**
36
- * Create a signal that aborts when the abort signal aborts
37
- * @param signal
38
- */
39
- export declare const bind: (signal: AbortSignal) => Signal;
33
+ export declare const timeout: (ms: number) => Signal;
package/signal.js CHANGED
@@ -1 +1 @@
1
- import{sleep}from".";export let init=()=>[false];export let interrupted=e=>e[0];export let interrupt=e=>{e[0]=true};export let timeout=async(o,s)=>{await sleep(s);interrupt(o)};export let link=(e,o)=>{o.addEventListener(`abort`,()=>interrupt(e))};export let duration=e=>{let o=[false];timeout(o,e);return o};export let bind=e=>{let o=[false];link(o,e);return o};
1
+ import{sleep}from".";export let init=()=>[false];export let any=(...e)=>{let a=[false];for(let o=0;o<e.length;o++)e[o].push(a);return a};export let aborted=e=>e[0];export let abort=e=>{if(!e[0]){e[0]=true;if(e.length>1)for(let a=1;a<e.length;a++)abort(e[a])}};export let abortAfter=async(a,o)=>{await sleep(a);abort(o)};export let timeout=e=>{let a=[false];abortAfter(e,a);return a};