@thi.ng/fibers 0.4.0 → 0.5.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/CHANGELOG.md +7 -1
- package/README.md +10 -7
- package/ops.d.ts +35 -1
- package/ops.js +49 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-09-
|
|
3
|
+
- **Last updated**: 2023-09-17T15:40:02Z
|
|
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,12 @@ 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.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.5.0) (2023-09-17)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add asPromise() fiber-to-promise conversion ([29fc86f](https://github.com/thi-ng/umbrella/commit/29fc86f))
|
|
17
|
+
|
|
12
18
|
## [0.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.4.0) (2023-09-06)
|
|
13
19
|
|
|
14
20
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -114,6 +114,7 @@ app.fork(function* () {
|
|
|
114
114
|
The following operators act as basic composition helpers to construct more elaborate fiber setups:
|
|
115
115
|
|
|
116
116
|
- [`all`](https://docs.thi.ng/umbrella/fibers/functions/all.html): wait for all given fibers to complete
|
|
117
|
+
- [`asPromise`](https://docs.thi.ng/umbrella/fibers/functions/asPromise.html): wrap fiber as promise for use in `async` contexts
|
|
117
118
|
- [`first`](https://docs.thi.ng/umbrella/fibers/functions/first.html): wait for one of the given fibers to complete
|
|
118
119
|
- [`fork`](https://docs.thi.ng/umbrella/fibers/classes/Fiber.html#fork): create & attach a new child process
|
|
119
120
|
- [`forkAll`](https://docs.thi.ng/umbrella/fibers/classes/Fiber.html#forkAll): create & attach multiple child processes
|
|
@@ -121,6 +122,7 @@ The following operators act as basic composition helpers to construct more elabo
|
|
|
121
122
|
- [`sequence`](https://docs.thi.ng/umbrella/fibers/functions/sequence.html): execute fibers in sequence
|
|
122
123
|
- [`shuffle`](https://docs.thi.ng/umbrella/fibers/functions/shuffle-1.html): execute fibers in constantly randomized order
|
|
123
124
|
- [`timeSlice`](https://docs.thi.ng/umbrella/fibers/functions/timeSlice.html): execute fiber in batches of N milliseconds
|
|
125
|
+
- [`timeSliceIterable`](https://docs.thi.ng/umbrella/fibers/functions/timeSliceIterable.html): consume iterable in batches of N milliseconds
|
|
124
126
|
- [`until`](https://docs.thi.ng/umbrella/fibers/functions/until.html): wait until predicate is truthy
|
|
125
127
|
- [`untilEvent`](https://docs.thi.ng/umbrella/fibers/functions/untilEvent.html): wait until event occurs
|
|
126
128
|
- [`untilPromise`](https://docs.thi.ng/umbrella/fibers/functions/untilPromise.html): wait until promise resolves/rejects
|
|
@@ -376,7 +378,7 @@ For Node.js REPL:
|
|
|
376
378
|
const fibers = await import("@thi.ng/fibers");
|
|
377
379
|
```
|
|
378
380
|
|
|
379
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 2.
|
|
381
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.41 KB
|
|
380
382
|
|
|
381
383
|
## Dependencies
|
|
382
384
|
|
|
@@ -397,12 +399,13 @@ directory are using this package.
|
|
|
397
399
|
|
|
398
400
|
A selection:
|
|
399
401
|
|
|
400
|
-
| Screenshot | Description
|
|
401
|
-
|
|
402
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/ascii-raymarch.jpg" width="240"/> | ASCII art raymarching with thi.ng/shader-ast & thi.ng/text-canvas
|
|
403
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/fiber-basics.png" width="240"/> | Fiber-based cooperative multitasking basics
|
|
404
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/ifs-fractal.jpg" width="240"/> | Barnsley fern IFS fractal renderer
|
|
405
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/
|
|
402
|
+
| Screenshot | Description | Live demo | Source |
|
|
403
|
+
|:----------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------|:-----------------------------------------------------|:----------------------------------------------------------------------------------|
|
|
404
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/ascii-raymarch.jpg" width="240"/> | ASCII art raymarching with thi.ng/shader-ast & thi.ng/text-canvas | [Demo](https://demo.thi.ng/umbrella/ascii-raymarch/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/ascii-raymarch) |
|
|
405
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/fiber-basics.png" width="240"/> | Fiber-based cooperative multitasking basics | [Demo](https://demo.thi.ng/umbrella/fiber-basics/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/fiber-basics) |
|
|
406
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/ifs-fractal.jpg" width="240"/> | Barnsley fern IFS fractal renderer | [Demo](https://demo.thi.ng/umbrella/ifs-fractal/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/ifs-fractal) |
|
|
407
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/mastodon-feed.jpg" width="240"/> | Mastodon API feed reader with support for different media types, fullscreen media modal, HTML rewriting | [Demo](https://demo.thi.ng/umbrella/mastodon-feed/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/mastodon-feed) |
|
|
408
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/render-audio.png" width="240"/> | Generative audio synth offline renderer and WAV file export | [Demo](https://demo.thi.ng/umbrella/render-audio/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/render-audio) |
|
|
406
409
|
|
|
407
410
|
## API
|
|
408
411
|
|
package/ops.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export declare const sequence: (fibers: Iterable<MaybeFiber>, opts?: Partial<Fib
|
|
|
31
31
|
* Also see {@link withTimeout}, {@link all}.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
|
-
* ```
|
|
34
|
+
* ```ts
|
|
35
35
|
* // wait until mouse click for max 5 seconds
|
|
36
36
|
* const res = yield* first([
|
|
37
37
|
* untilEvent(window, "click", { id: "click" }),
|
|
@@ -202,4 +202,38 @@ export declare class Shuffle extends Fiber {
|
|
|
202
202
|
export declare const shuffle: (fibers: Iterable<MaybeFiber>, opts?: Partial<FiberOpts & {
|
|
203
203
|
rnd: IRandom;
|
|
204
204
|
}>) => Shuffle;
|
|
205
|
+
/**
|
|
206
|
+
* Helper function to await fiber-based task completion from within an `async`
|
|
207
|
+
* context. Wraps given `task` in a fiber with custom {@link FiberOpts.deinit}
|
|
208
|
+
* and {@link FiberOpts.catch} handlers and executes it. Returns a promise which
|
|
209
|
+
* resolves with that fiber's final value once its complete. If there is an
|
|
210
|
+
* error during fiber execution the promise will be rejected with that original
|
|
211
|
+
* error.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* If `opts` are given, their {@link FiberOpts.deinit} and
|
|
215
|
+
* {@link FiberOpts.catch} handlers will be augmented with resolving/rejecting
|
|
216
|
+
* the promise. I.e. The promise will NOT be rejected if there's a user-provided
|
|
217
|
+
* `catch` handler indicating the error has been dealt with.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* (async () => {
|
|
222
|
+
* // create & spawn task/fiber
|
|
223
|
+
* const task = asPromise(function*() {
|
|
224
|
+
* for(let i = 0; i< 3; i++) {
|
|
225
|
+
* console.log("working...", i);
|
|
226
|
+
* yield* wait(1000);
|
|
227
|
+
* }
|
|
228
|
+
* return 42;
|
|
229
|
+
* });
|
|
230
|
+
* // now wait for task to complete
|
|
231
|
+
* const result = await task;
|
|
232
|
+
* console.log("final result", result);
|
|
233
|
+
* })()
|
|
234
|
+
* ```
|
|
235
|
+
*
|
|
236
|
+
* @param task
|
|
237
|
+
*/
|
|
238
|
+
export declare const asPromise: <T>(task: MaybeFiber<T>, opts?: Partial<FiberOpts>) => Promise<T>;
|
|
205
239
|
//# sourceMappingURL=ops.d.ts.map
|
package/ops.js
CHANGED
|
@@ -52,7 +52,7 @@ export const sequence = (fibers, opts) => fiber(function* (ctx) {
|
|
|
52
52
|
* Also see {@link withTimeout}, {@link all}.
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
|
-
* ```
|
|
55
|
+
* ```ts
|
|
56
56
|
* // wait until mouse click for max 5 seconds
|
|
57
57
|
* const res = yield* first([
|
|
58
58
|
* untilEvent(window, "click", { id: "click" }),
|
|
@@ -291,3 +291,51 @@ export class Shuffle extends Fiber {
|
|
|
291
291
|
* @param opts
|
|
292
292
|
*/
|
|
293
293
|
export const shuffle = (fibers, opts) => new Shuffle(fibers, opts);
|
|
294
|
+
/**
|
|
295
|
+
* Helper function to await fiber-based task completion from within an `async`
|
|
296
|
+
* context. Wraps given `task` in a fiber with custom {@link FiberOpts.deinit}
|
|
297
|
+
* and {@link FiberOpts.catch} handlers and executes it. Returns a promise which
|
|
298
|
+
* resolves with that fiber's final value once its complete. If there is an
|
|
299
|
+
* error during fiber execution the promise will be rejected with that original
|
|
300
|
+
* error.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* If `opts` are given, their {@link FiberOpts.deinit} and
|
|
304
|
+
* {@link FiberOpts.catch} handlers will be augmented with resolving/rejecting
|
|
305
|
+
* the promise. I.e. The promise will NOT be rejected if there's a user-provided
|
|
306
|
+
* `catch` handler indicating the error has been dealt with.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* (async () => {
|
|
311
|
+
* // create & spawn task/fiber
|
|
312
|
+
* const task = asPromise(function*() {
|
|
313
|
+
* for(let i = 0; i< 3; i++) {
|
|
314
|
+
* console.log("working...", i);
|
|
315
|
+
* yield* wait(1000);
|
|
316
|
+
* }
|
|
317
|
+
* return 42;
|
|
318
|
+
* });
|
|
319
|
+
* // now wait for task to complete
|
|
320
|
+
* const result = await task;
|
|
321
|
+
* console.log("final result", result);
|
|
322
|
+
* })()
|
|
323
|
+
* ```
|
|
324
|
+
*
|
|
325
|
+
* @param task
|
|
326
|
+
*/
|
|
327
|
+
export const asPromise = (task, opts) => new Promise((resolve, reject) => {
|
|
328
|
+
fiber(task, {
|
|
329
|
+
...opts,
|
|
330
|
+
deinit: (ctx) => {
|
|
331
|
+
opts?.deinit && opts.deinit(ctx);
|
|
332
|
+
ctx.state < STATE_ERROR && resolve(ctx.deref());
|
|
333
|
+
},
|
|
334
|
+
catch: (ctx, e) => {
|
|
335
|
+
if (opts?.catch && opts.catch(ctx, e))
|
|
336
|
+
return true;
|
|
337
|
+
reject(e);
|
|
338
|
+
return false;
|
|
339
|
+
},
|
|
340
|
+
}).run();
|
|
341
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/fibers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Process hierarchies & operators for cooperative multitasking",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@thi.ng/api": "^8.9.5",
|
|
38
|
-
"@thi.ng/arrays": "^2.5.
|
|
38
|
+
"@thi.ng/arrays": "^2.5.23",
|
|
39
39
|
"@thi.ng/bench": "^3.4.6",
|
|
40
40
|
"@thi.ng/checks": "^3.4.5",
|
|
41
41
|
"@thi.ng/errors": "^2.3.5",
|
|
@@ -108,5 +108,5 @@
|
|
|
108
108
|
"status": "alpha",
|
|
109
109
|
"year": 2023
|
|
110
110
|
},
|
|
111
|
-
"gitHead": "
|
|
111
|
+
"gitHead": "3be3edbcb76c6855ee8b0c667fe4e152615652c9\n"
|
|
112
112
|
}
|