@thi.ng/fibers 1.0.88 → 1.1.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 +1 -1
- package/api.d.ts +5 -2
- package/fiber.d.ts +1 -0
- package/fiber.js +6 -2
- package/ops.d.ts +5 -2
- package/ops.js +16 -11
- package/package.json +2 -2
package/README.md
CHANGED
package/api.d.ts
CHANGED
|
@@ -9,11 +9,14 @@ export interface FiberOpts {
|
|
|
9
9
|
*/
|
|
10
10
|
id: string;
|
|
11
11
|
/**
|
|
12
|
-
* ID generator instance for fiber ID (instead of {@link FiberOpts.id})
|
|
12
|
+
* ID generator instance for fiber ID (instead of {@link FiberOpts.id}). If
|
|
13
|
+
* not given, uses {@link DEFAULT_ID_GEN} (use {@link setDefaultIDGen} to
|
|
14
|
+
* configure).
|
|
13
15
|
*/
|
|
14
16
|
idgen: IIDGen<string>;
|
|
15
17
|
/**
|
|
16
|
-
* Logger instance.
|
|
18
|
+
* Logger instance. If not given, uses {@link DEFAULT_LOGGER} (use
|
|
19
|
+
* {@link setDefaultLogger} to configure).
|
|
17
20
|
*/
|
|
18
21
|
logger: ILogger;
|
|
19
22
|
/**
|
package/fiber.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Event as $Event, Fn, Fn0, IDeref, IID, IIDGen, INotify, Listener,
|
|
|
2
2
|
import type { ILogger } from "@thi.ng/logger";
|
|
3
3
|
import { type FiberEventType, type FiberFactory, type FiberOpts, type MaybeFiber, type State } from "./api.js";
|
|
4
4
|
export declare const setDefaultIDGen: (gen: IIDGen<string>) => IIDGen<string>;
|
|
5
|
+
export declare const setDefaultLogger: (logger: ILogger) => ILogger;
|
|
5
6
|
export declare class Fiber<T = any> implements IDeref<Maybe<T>>, IID<string>, INotify<FiberEventType> {
|
|
6
7
|
/**
|
|
7
8
|
* This fiber's user provided or generated ID.
|
package/fiber.js
CHANGED
|
@@ -24,7 +24,9 @@ import {
|
|
|
24
24
|
STATE_NEW
|
|
25
25
|
} from "./api.js";
|
|
26
26
|
let DEFAULT_ID_GEN = prefixed("fib-", monotonic());
|
|
27
|
+
let DEFAULT_LOGGER;
|
|
27
28
|
const setDefaultIDGen = (gen) => DEFAULT_ID_GEN = gen;
|
|
29
|
+
const setDefaultLogger = (logger) => DEFAULT_LOGGER = logger;
|
|
28
30
|
const NO_RESULT = { done: false, value: void 0 };
|
|
29
31
|
let Fiber = class {
|
|
30
32
|
/**
|
|
@@ -48,7 +50,7 @@ let Fiber = class {
|
|
|
48
50
|
constructor(gen, opts) {
|
|
49
51
|
if (opts) {
|
|
50
52
|
this.autoTerminate = !!opts.terminate;
|
|
51
|
-
this.logger = opts.logger;
|
|
53
|
+
this.logger = opts.logger ?? DEFAULT_LOGGER;
|
|
52
54
|
this.parent = opts.parent;
|
|
53
55
|
this.user = {
|
|
54
56
|
init: opts.init,
|
|
@@ -62,6 +64,7 @@ let Fiber = class {
|
|
|
62
64
|
}
|
|
63
65
|
} else {
|
|
64
66
|
this.idgen = DEFAULT_ID_GEN;
|
|
67
|
+
this.logger = DEFAULT_LOGGER;
|
|
65
68
|
}
|
|
66
69
|
if (this.idgen) this.id = this.idgen.next();
|
|
67
70
|
this.gen = isFunction(gen) ? gen(this) : gen;
|
|
@@ -397,5 +400,6 @@ const fiber = (fiber2, opts) => fiber2 != null && fiber2 instanceof Fiber ? fibe
|
|
|
397
400
|
export {
|
|
398
401
|
Fiber,
|
|
399
402
|
fiber,
|
|
400
|
-
setDefaultIDGen
|
|
403
|
+
setDefaultIDGen,
|
|
404
|
+
setDefaultLogger
|
|
401
405
|
};
|
package/ops.d.ts
CHANGED
|
@@ -81,13 +81,16 @@ export declare const withTimeout: (body: MaybeFiber, timeout: number, opts?: Par
|
|
|
81
81
|
/**
|
|
82
82
|
* Higher-order fiber which repeatedly executes given `fiber` until its
|
|
83
83
|
* completion, but does so in a time-sliced manner, such that the fiber never
|
|
84
|
-
* consumes more than `maxTime` milliseconds per update cycle.
|
|
84
|
+
* consumes more than `maxTime` milliseconds per update cycle. The returned
|
|
85
|
+
* fiber produces the same result (if any) as the wrapped fiber. If the wrapped
|
|
86
|
+
* fiber goes into an error state, the returned time-sliced fiber will do so
|
|
87
|
+
* too (with the same error).
|
|
85
88
|
*
|
|
86
89
|
* @param fiber
|
|
87
90
|
* @param maxTime
|
|
88
91
|
* @param opts
|
|
89
92
|
*/
|
|
90
|
-
export declare const timeSlice: (body: MaybeFiber
|
|
93
|
+
export declare const timeSlice: <T = any>(body: MaybeFiber<T>, maxTime: number, opts?: Partial<FiberOpts>) => Fiber<T>;
|
|
91
94
|
/**
|
|
92
95
|
* Similar to {@link timeSlice}, but for consuming the given iterable in a
|
|
93
96
|
* time-sliced manner. With each fiber update consumes & buffers values from
|
package/ops.js
CHANGED
|
@@ -39,17 +39,22 @@ const all = (fibers, opts) => fiber((ctx) => {
|
|
|
39
39
|
return ctx.join();
|
|
40
40
|
}, opts);
|
|
41
41
|
const withTimeout = (body, timeout, opts) => first([body, wait(timeout)], opts);
|
|
42
|
-
const timeSlice = (body, maxTime, opts) => fiber(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
42
|
+
const timeSlice = (body, maxTime, opts) => fiber(
|
|
43
|
+
function* () {
|
|
44
|
+
const $fiber = fiber(body);
|
|
45
|
+
while (true) {
|
|
46
|
+
let t0 = now();
|
|
47
|
+
do {
|
|
48
|
+
if ($fiber.next() > STATE_ACTIVE) {
|
|
49
|
+
if ($fiber.state === STATE_ERROR) throw $fiber.error;
|
|
50
|
+
return $fiber.value;
|
|
51
|
+
}
|
|
52
|
+
} while (timeDiff(t0) < maxTime);
|
|
53
|
+
yield;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
opts
|
|
57
|
+
);
|
|
53
58
|
const timeSliceIterable = (src, consume, maxTime, opts) => fiber(function* () {
|
|
54
59
|
const iter = src[Symbol.iterator]();
|
|
55
60
|
while (true) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/fibers",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Process hierarchies & operators for cooperative multitasking",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -116,5 +116,5 @@
|
|
|
116
116
|
"status": "alpha",
|
|
117
117
|
"year": 2023
|
|
118
118
|
},
|
|
119
|
-
"gitHead": "
|
|
119
|
+
"gitHead": "bc535a90970f96564cae15f1732b5fb8562135c4"
|
|
120
120
|
}
|