@thi.ng/fibers 0.5.10 → 0.6.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-10-30T14:31:56Z
3
+ - **Last updated**: 2023-11-09T10:28:18Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.6.0) (2023-11-09)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add Fiber.promise() ([4861d95](https://github.com/thi-ng/umbrella/commit/4861d95))
17
+
18
+ #### ♻️ Refactoring
19
+
20
+ - update all tests (packages A-S) ([e3085e4](https://github.com/thi-ng/umbrella/commit/e3085e4))
21
+
12
22
  ### [0.5.5](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.5.5) (2023-10-18)
13
23
 
14
24
  #### 🩹 Bug fixes
package/README.md CHANGED
@@ -378,7 +378,7 @@ For Node.js REPL:
378
378
  const fibers = await import("@thi.ng/fibers");
379
379
  ```
380
380
 
381
- Package sizes (brotli'd, pre-treeshake): ESM: 2.40 KB
381
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.60 KB
382
382
 
383
383
  ## Dependencies
384
384
 
package/csp.js CHANGED
@@ -10,9 +10,11 @@ const STATE_CLOSED = 2;
10
10
  * (and ordering). By default uses a single value {@link fifo} buffer impl.
11
11
  */
12
12
  export class Channel {
13
+ opts;
14
+ buffer;
15
+ state = STATE_OPEN;
13
16
  constructor(buffer = 1, opts) {
14
17
  this.opts = opts;
15
- this.state = STATE_OPEN;
16
18
  this.buffer = isNumber(buffer) ? new FIFOBuffer(buffer) : buffer;
17
19
  }
18
20
  /**
@@ -157,9 +159,10 @@ export const channel = (buffer, opts) => new Channel(buffer, opts);
157
159
  * First-in, first-out ring buffer implementation for use with {@link Channel}.
158
160
  */
159
161
  export class FIFOBuffer {
162
+ buf;
163
+ rpos = 0;
164
+ wpos = 0;
160
165
  constructor(cap = 1) {
161
- this.rpos = 0;
162
- this.wpos = 0;
163
166
  assert(cap >= 1, `capacity must be >= 1`);
164
167
  this.buf = new Array(cap + 1);
165
168
  }
@@ -201,9 +204,10 @@ export class FIFOBuffer {
201
204
  */
202
205
  export const fifo = (cap) => new FIFOBuffer(cap);
203
206
  export class LIFOBuffer {
207
+ cap;
208
+ buf = [];
204
209
  constructor(cap = 1) {
205
210
  this.cap = cap;
206
- this.buf = [];
207
211
  assert(cap >= 1, `capacity must be >= 1`);
208
212
  }
209
213
  clear() {
package/fiber.d.ts CHANGED
@@ -20,12 +20,29 @@ export declare class Fiber<T = any> implements IDeref<T | undefined>, IID<string
20
20
  error?: Error;
21
21
  logger?: ILogger;
22
22
  user?: Partial<Pick<FiberOpts, "init" | "deinit" | "catch">>;
23
+ _promise?: Promise<T | undefined>;
23
24
  constructor(gen?: Nullable<FiberFactory<T> | Generator<unknown, T>>, opts?: Partial<FiberOpts>);
24
25
  /**
25
26
  * Co-routine which blocks whilst this fiber (incl. its children) is active.
26
27
  * Then return this fiber's value.
27
28
  */
28
29
  [Symbol.iterator](): Generator<undefined, T | undefined, unknown>;
30
+ /**
31
+ * Returns a promise which only resolves when the fiber is not active
32
+ * anymore. If there was an unhandled error during the fiber execution the
33
+ * promise will reject, else if the fiber (incl. children) completed on its
34
+ * own or was cancelled, the promise resolves with the fiber's final
35
+ * (possibly `undefined`) value.
36
+ *
37
+ * @remarks
38
+ * The promise assumes the fiber either already has been (or will be)
39
+ * scheduled & executed via other means. This promise only repeatedly checks
40
+ * for any state changes of this fiber (at a configurable
41
+ * frequency/interval), but does *NOT* trigger fiber execution!
42
+ *
43
+ * @param delay
44
+ */
45
+ promise(delay?: number): Promise<T | undefined>;
29
46
  /**
30
47
  * Returns this fiber's result value (if any). Only available if the fiber
31
48
  * completed successfully and produced a value (either by returning a value
package/fiber.js CHANGED
@@ -10,11 +10,25 @@ let DEFAULT_ID_GEN = prefixed("fib-", monotonic());
10
10
  export const setDefaultIDGen = (gen) => (DEFAULT_ID_GEN = gen);
11
11
  const NO_RESULT = { done: false, value: undefined };
12
12
  let Fiber = Fiber_1 = class Fiber {
13
+ /**
14
+ * This fiber's user provided or generated ID.
15
+ */
16
+ id;
17
+ /**
18
+ * This fiber's parent.
19
+ */
20
+ parent;
21
+ gen;
22
+ idgen;
23
+ state = STATE_NEW;
24
+ autoTerminate = false;
25
+ children = [];
26
+ value = undefined;
27
+ error;
28
+ logger;
29
+ user;
30
+ _promise;
13
31
  constructor(gen, opts) {
14
- this.state = STATE_NEW;
15
- this.autoTerminate = false;
16
- this.children = [];
17
- this.value = undefined;
18
32
  if (opts) {
19
33
  this.autoTerminate = !!opts.terminate;
20
34
  this.logger = opts.logger;
@@ -47,6 +61,34 @@ let Fiber = Fiber_1 = class Fiber {
47
61
  yield;
48
62
  return this.value;
49
63
  }
64
+ /**
65
+ * Returns a promise which only resolves when the fiber is not active
66
+ * anymore. If there was an unhandled error during the fiber execution the
67
+ * promise will reject, else if the fiber (incl. children) completed on its
68
+ * own or was cancelled, the promise resolves with the fiber's final
69
+ * (possibly `undefined`) value.
70
+ *
71
+ * @remarks
72
+ * The promise assumes the fiber either already has been (or will be)
73
+ * scheduled & executed via other means. This promise only repeatedly checks
74
+ * for any state changes of this fiber (at a configurable
75
+ * frequency/interval), but does *NOT* trigger fiber execution!
76
+ *
77
+ * @param delay
78
+ */
79
+ promise(delay = 1) {
80
+ return (this._promise ||
81
+ (this._promise = new Promise((resolve, reject) => {
82
+ const timerID = setInterval(() => {
83
+ if (this.state > STATE_ACTIVE) {
84
+ clearInterval(timerID);
85
+ this.state < STATE_ERROR
86
+ ? resolve(this.value)
87
+ : reject(this.error);
88
+ }
89
+ }, delay);
90
+ })));
91
+ }
50
92
  /**
51
93
  * Returns this fiber's result value (if any). Only available if the fiber
52
94
  * completed successfully and produced a value (either by returning a value
package/ops.js CHANGED
@@ -241,6 +241,7 @@ export const untilEvent = (target, type, opts) => {
241
241
  * Custom fiber implementation for {@link shuffle}.
242
242
  */
243
243
  export class Shuffle extends Fiber {
244
+ rnd;
244
245
  constructor(fibers, opts) {
245
246
  super((ctx) => ctx.join(), opts);
246
247
  this.rnd = opts?.rnd || SYSTEM;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/fibers",
3
- "version": "0.5.10",
3
+ "version": "0.6.1",
4
4
  "description": "Process hierarchies & operators for cooperative multitasking",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -28,27 +28,26 @@
28
28
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
29
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
30
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
31
- "doc:readme": "yarn doc:stats && tools:readme",
32
- "doc:stats": "tools:module-stats",
31
+ "doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
33
32
  "pub": "yarn npm publish --access public",
34
- "test": "testament test"
33
+ "test": "bun test"
35
34
  },
36
35
  "dependencies": {
37
- "@thi.ng/api": "^8.9.6",
38
- "@thi.ng/arrays": "^2.7.2",
39
- "@thi.ng/bench": "^3.4.8",
40
- "@thi.ng/checks": "^3.4.6",
41
- "@thi.ng/errors": "^2.4.0",
42
- "@thi.ng/idgen": "^2.2.9",
43
- "@thi.ng/logger": "^1.4.22",
44
- "@thi.ng/random": "^3.6.12"
36
+ "@thi.ng/api": "^8.9.8",
37
+ "@thi.ng/arrays": "^2.7.4",
38
+ "@thi.ng/bench": "^3.4.10",
39
+ "@thi.ng/checks": "^3.4.8",
40
+ "@thi.ng/errors": "^2.4.2",
41
+ "@thi.ng/idgen": "^2.2.11",
42
+ "@thi.ng/logger": "^1.4.24",
43
+ "@thi.ng/random": "^3.6.14"
45
44
  },
46
45
  "devDependencies": {
47
- "@microsoft/api-extractor": "^7.38.0",
48
- "@thi.ng/testament": "^0.3.24",
46
+ "@microsoft/api-extractor": "^7.38.2",
47
+ "@thi.ng/testament": "^0.4.1",
49
48
  "rimraf": "^5.0.5",
50
49
  "tools": "^0.0.1",
51
- "typedoc": "^0.25.2",
50
+ "typedoc": "^0.25.3",
52
51
  "typescript": "^5.2.2"
53
52
  },
54
53
  "keywords": [
@@ -108,5 +107,5 @@
108
107
  "status": "alpha",
109
108
  "year": 2023
110
109
  },
111
- "gitHead": "336bd1bf95825b3c318a3ab49c54451c94aaa883\n"
110
+ "gitHead": "669a3151e4302480244fe3e60eff5e732ea5b7a7\n"
112
111
  }