@thi.ng/fibers 1.0.87 → 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 CHANGED
@@ -406,7 +406,7 @@ For Node.js REPL:
406
406
  const fib = await import("@thi.ng/fibers");
407
407
  ```
408
408
 
409
- Package sizes (brotli'd, pre-treeshake): ESM: 2.44 KB
409
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.46 KB
410
410
 
411
411
  ## Dependencies
412
412
 
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, maxTime: number, opts?: Partial<FiberOpts>) => Fiber<void>;
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(function* () {
43
- const $fiber = fiber(body);
44
- while (true) {
45
- let t0 = now();
46
- do {
47
- if ($fiber.state > STATE_ACTIVE || $fiber.next() > STATE_ACTIVE)
48
- return;
49
- } while (timeDiff(t0) < maxTime);
50
- yield;
51
- }
52
- }, opts);
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.87",
3
+ "version": "1.1.0",
4
4
  "description": "Process hierarchies & operators for cooperative multitasking",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,18 +40,18 @@
40
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@thi.ng/api": "^8.12.26",
44
- "@thi.ng/arrays": "^2.14.23",
45
- "@thi.ng/buffers": "^1.0.45",
46
- "@thi.ng/checks": "^3.10.0",
47
- "@thi.ng/errors": "^2.6.15",
48
- "@thi.ng/idgen": "^2.2.100",
49
- "@thi.ng/logger": "^3.3.9",
50
- "@thi.ng/random": "^4.1.52",
51
- "@thi.ng/timestamp": "^1.1.45"
43
+ "@thi.ng/api": "^8.12.27",
44
+ "@thi.ng/arrays": "^2.14.24",
45
+ "@thi.ng/buffers": "^1.0.46",
46
+ "@thi.ng/checks": "^3.10.1",
47
+ "@thi.ng/errors": "^2.6.16",
48
+ "@thi.ng/idgen": "^2.2.101",
49
+ "@thi.ng/logger": "^3.3.10",
50
+ "@thi.ng/random": "^4.1.53",
51
+ "@thi.ng/timestamp": "^1.1.46"
52
52
  },
53
53
  "devDependencies": {
54
- "esbuild": "^0.28.0",
54
+ "esbuild": "^0.28.1",
55
55
  "typedoc": "^0.28.19",
56
56
  "typescript": "^6.0.3"
57
57
  },
@@ -116,5 +116,5 @@
116
116
  "status": "alpha",
117
117
  "year": 2023
118
118
  },
119
- "gitHead": "c47c56420bcae2e0477d252a497be44f08ca185a"
119
+ "gitHead": "bc535a90970f96564cae15f1732b5fb8562135c4"
120
120
  }