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 +9 -13
- package/package.json +1 -1
- package/signal.d.ts +12 -18
- package/signal.js +1 -1
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
|
-
###
|
115
|
-
A module to interrupt executions of
|
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('
|
124
|
+
console.log('Task 1 waiting: 1s');
|
125
125
|
await sleep(1000);
|
126
126
|
|
127
127
|
// Interruption point
|
128
|
-
if (signal.
|
128
|
+
if (signal.aborted(sig)) return;
|
129
129
|
|
130
130
|
const res = Math.random();
|
131
|
-
console.log('
|
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
|
-
|
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.
|
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('
|
153
|
+
logTime('Task 1 started');
|
156
154
|
|
157
155
|
// Interrupt the function after 500ms
|
158
|
-
await f1(signal.
|
159
|
-
|
160
|
-
logTime('Fiber 1 interrupted');
|
156
|
+
await f1(signal.timeout(500));
|
161
157
|
}
|
162
158
|
```
|
package/package.json
CHANGED
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
|
-
*
|
11
|
-
* @param
|
10
|
+
* Create a signal that aborts when any of the input signals abort
|
11
|
+
* @param sigs
|
12
12
|
*/
|
13
|
-
export declare const
|
13
|
+
export declare const any: (...sigs: Signal[]) => Signal;
|
14
14
|
/**
|
15
|
-
*
|
15
|
+
* Check whether the signal has been aborted
|
16
16
|
* @param t
|
17
17
|
*/
|
18
|
-
export declare const
|
18
|
+
export declare const aborted: (t: Signal) => boolean;
|
19
19
|
/**
|
20
|
-
*
|
20
|
+
* Abort a signal
|
21
21
|
* @param t
|
22
22
|
*/
|
23
|
-
export declare const
|
23
|
+
export declare const abort: (t: Signal) => void;
|
24
24
|
/**
|
25
|
-
*
|
25
|
+
* Abort a signal after a duration
|
26
26
|
* @param t
|
27
|
-
* @param signal
|
28
27
|
*/
|
29
|
-
export declare const
|
28
|
+
export declare const abortAfter: (ms: number, t: Signal) => Promise<void>;
|
30
29
|
/**
|
31
|
-
* Create a signal that
|
30
|
+
* Create a signal that aborts after ms
|
32
31
|
* @param ms
|
33
32
|
*/
|
34
|
-
export declare const
|
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
|
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};
|