@thi.ng/fibers 0.6.36 → 1.0.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**: 2024-04-08T14:59:29Z
3
+ - **Last updated**: 2024-04-20T14:42:45Z
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,23 @@ 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
+ ### [1.0.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@1.0.1) (2024-04-20)
13
+
14
+ #### ♻️ Refactoring
15
+
16
+ - update type usage ([9df7cb0](https://github.com/thi-ng/umbrella/commit/9df7cb0))
17
+
18
+ # [1.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@1.0.0) (2024-04-11)
19
+
20
+ #### 🛑 Breaking changes
21
+
22
+ - remove/migrate CSP buffer types, update readme ([d4a1d23](https://github.com/thi-ng/umbrella/commit/d4a1d23))
23
+ - BREAKING CHANGE: remove obsolete CSP buffer types & impls, re-use from [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/main/packages/buffers) (see [55ba21b50f](https://github.com/thi-ng/umbrella/commit/55ba21b50f))
24
+ - remove IReadBuffer & IReadWriteBuffer interfaces
25
+ - update deps (add [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/main/packages/buffers))
26
+ - update docs
27
+ - update readme
28
+
12
29
  ## [0.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.6.0) (2023-11-09)
13
30
 
14
31
  #### 🚀 Features
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 191 standalone projects, maintained as part
10
+ > This is one of 192 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -203,8 +203,9 @@ values are being read, essentially a memory management issue).
203
203
 
204
204
  #### Buffering behaviors
205
205
 
206
- The following channel buffer types/behaviors are included, all accepting a max.
207
- capacity and all implementing the
206
+ The following channel buffer types/behaviors are included (from the
207
+ [thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/develop/packages/buffers) package), all accepting a max. capacity
208
+ and all implementing the
208
209
  [IReadWriteBuffer](https://docs.thi.ng/umbrella/fibers/interfaces/IReadWriteBuffer.html)
209
210
  interface required by the channel:
210
211
 
@@ -214,17 +215,18 @@ interface required by the channel:
214
215
  channel reads are non-blocking whilst there're more buffered values available.
215
216
  Reads will only block if the buffer is empty.
216
217
  - [`lifo`](https://docs.thi.ng/umbrella/fibers/functions/lifo.html): Last in,
217
- first out. Read/write behavior is mostly the same as with `fifo`, with the
218
- important difference, that (as the name indicates), the last value written
219
- will be the first value read (i.e. stack behavior).
218
+ first out. Write behavior is the same as with `fifo`, reads are in reverse
219
+ order (as the name indicates), i.e. the last value written will be the first
220
+ value read (i.e. stack behavior).
220
221
  - [`sliding`](https://docs.thi.ng/umbrella/fibers/functions/sliding.html):
221
- Sliding window ring buffer. Writes to the channel are **never** blocking! Once
222
- the buffer's capacity is reached, a new write will first expunge the oldest
223
- buffered value (similar to LRU cache behavior). Read behavior is the same as
224
- for `fifo`.
222
+ Sliding window ring buffer. Writes to the channel are **never** blocking!
223
+ Whilst the buffer is at full capacity, new writes will first expunge the
224
+ oldest buffered value (similar to [LRU
225
+ cache](https://github.com/thi-ng/umbrella/blob/develop/packages/cache/README.md#lru)
226
+ behavior). Read behavior is the same as for `fifo`.
225
227
  - [`dropping`](https://docs.thi.ng/umbrella/fibers/functions/dropping.html):
226
228
  Dropping value ring buffer. Writes to the channel are **never** blocking!
227
- Whilst the buffer's capacity is reached, new writes will be silently ignored.
229
+ Whilst the buffer is at full capacity, new writes will be silently ignored.
228
230
  Read behavior is the same as for `fifo`.
229
231
 
230
232
  #### Channels
@@ -384,10 +386,10 @@ import * as fib from "@thi.ng/fibers";
384
386
  Browser ESM import:
385
387
 
386
388
  ```html
387
- <script type="module" src="https://cdn.skypack.dev/@thi.ng/fibers"></script>
389
+ <script type="module" src="https://esm.run/@thi.ng/fibers"></script>
388
390
  ```
389
391
 
390
- [Skypack documentation](https://docs.skypack.dev/)
392
+ [JSDelivr documentation](https://www.jsdelivr.com/)
391
393
 
392
394
  For Node.js REPL:
393
395
 
@@ -395,13 +397,14 @@ For Node.js REPL:
395
397
  const fib = await import("@thi.ng/fibers");
396
398
  ```
397
399
 
398
- Package sizes (brotli'd, pre-treeshake): ESM: 2.64 KB
400
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.45 KB
399
401
 
400
402
  ## Dependencies
401
403
 
402
404
  - [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
403
405
  - [@thi.ng/arrays](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays)
404
406
  - [@thi.ng/bench](https://github.com/thi-ng/umbrella/tree/develop/packages/bench)
407
+ - [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/develop/packages/buffers)
405
408
  - [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
406
409
  - [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
407
410
  - [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
package/api.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Fn2, IClear, IIDGen } from "@thi.ng/api";
1
+ import type { Fn, Fn2, IIDGen } from "@thi.ng/api";
2
2
  import type { ILogger } from "@thi.ng/logger";
3
3
  import type { Fiber } from "./fiber.js";
4
4
  export type FiberFactory<T = any> = (f: Fiber<T>) => Generator<unknown, T, unknown>;
@@ -68,27 +68,4 @@ export declare const EVENT_FIBER_CANCELED = "fiber-canceled";
68
68
  */
69
69
  export declare const EVENT_FIBER_ERROR = "fiber-error";
70
70
  export type FiberEventType = typeof EVENT_FIBER_DONE | typeof EVENT_FIBER_CANCELED | typeof EVENT_FIBER_ERROR | "*";
71
- export interface IReadBuffer<T> {
72
- /**
73
- * Returns true iff the buffer has at least one value available for reading.
74
- */
75
- readable(): boolean;
76
- /**
77
- * Unguarded read operation. Assumes the caller checked
78
- * {@link IReadBuffer.readable} immediately before. Returns next value from
79
- * buffer.
80
- */
81
- read(): T;
82
- }
83
- export interface IReadWriteBuffer<T> extends IReadBuffer<T>, IClear {
84
- /**
85
- * Returns true iff the buffer has at least one slot available for writing.
86
- */
87
- writable(): boolean;
88
- /**
89
- * Unguarded write operation. Assumes the caller checked
90
- * {@link IReadWriteBuffer.writable} immediately before.
91
- */
92
- write(x: T): void;
93
- }
94
71
  //# sourceMappingURL=api.d.ts.map
package/csp.d.ts CHANGED
@@ -1,4 +1,10 @@
1
- import type { FiberOpts, IReadWriteBuffer } from "./api.js";
1
+ import type { Maybe } from "@thi.ng/api";
2
+ import type { IReadWriteBuffer } from "@thi.ng/buffers";
3
+ import { DroppingBuffer } from "@thi.ng/buffers/dropping";
4
+ import { FIFOBuffer } from "@thi.ng/buffers/fifo";
5
+ import { LIFOBuffer } from "@thi.ng/buffers/lifo";
6
+ import { SlidingBuffer } from "@thi.ng/buffers/sliding";
7
+ import type { FiberOpts } from "./api.js";
2
8
  import { Fiber } from "./fiber.js";
3
9
  declare const STATE_OPEN = 0;
4
10
  declare const STATE_CLOSING = 1;
@@ -6,8 +12,9 @@ declare const STATE_CLOSED = 2;
6
12
  type CSPState = typeof STATE_OPEN | typeof STATE_CLOSING | typeof STATE_CLOSED;
7
13
  /**
8
14
  * Fiber-based CSP channel implementation, supporting any
9
- * {@link IReadWriteBuffer} implementation to customize read/write behaviors
10
- * (and ordering). By default uses a single value {@link fifo} buffer impl.
15
+ * [IReadWriteBuffer](https://docs.thi.ng/umbrella/buffers/interfaces/IReadWriteBuffer.html)
16
+ * implementation to customize read/write behaviors (and ordering). By default
17
+ * uses a single value {@link fifo} buffer impl.
11
18
  */
12
19
  export declare class Channel<T> {
13
20
  opts?: Partial<FiberOpts> | undefined;
@@ -29,7 +36,7 @@ export declare class Channel<T> {
29
36
  * const val = yield* chan.read();
30
37
  * ```
31
38
  */
32
- read(): Fiber<T | undefined>;
39
+ read(): Fiber<Maybe<T>>;
33
40
  /**
34
41
  * Returns a new fiber which attempts to write the given `value` to the
35
42
  * channel and "blocks" until channel is writable (which depends on the
@@ -105,20 +112,6 @@ export declare class Channel<T> {
105
112
  * @param opts
106
113
  */
107
114
  export declare const channel: <T>(buffer?: IReadWriteBuffer<T> | number, opts?: Partial<FiberOpts>) => Channel<T>;
108
- /**
109
- * First-in, first-out ring buffer implementation for use with {@link Channel}.
110
- */
111
- export declare class FIFOBuffer<T> implements IReadWriteBuffer<T> {
112
- protected buf: (T | undefined)[];
113
- protected rpos: number;
114
- protected wpos: number;
115
- constructor(cap?: number);
116
- clear(): void;
117
- readable(): boolean;
118
- read(): NonNullable<T>;
119
- writable(): boolean;
120
- write(x: T): void;
121
- }
122
115
  /**
123
116
  * Returns a {@link FIFOBuffer} ring buffer with given capacity for use with
124
117
  * {@link channel}.
@@ -134,57 +127,41 @@ export declare class FIFOBuffer<T> implements IReadWriteBuffer<T> {
134
127
  * @param cap
135
128
  */
136
129
  export declare const fifo: <T>(cap: number) => FIFOBuffer<T>;
137
- export declare class LIFOBuffer<T> implements IReadWriteBuffer<T> {
138
- protected cap: number;
139
- protected buf: T[];
140
- constructor(cap?: number);
141
- clear(): void;
142
- readable(): boolean;
143
- read(): NonNullable<T>;
144
- writable(): boolean;
145
- write(x: T): void;
146
- }
147
130
  /**
148
131
  * Returns a {@link LIFOBuffer} with given capacity for use with
149
132
  * {@link channel}.
150
133
  *
151
134
  * @remarks
152
- * Read/write behavior is mostly the same as with {@link fifo}, with the
153
- * important difference, that (as the name indicates), the last value written
154
- * will be the first value read (i.e. stack behavior).
135
+ * Write behavior is the same as with {@link fifo}, reads are in reverse order
136
+ * (as the name indicates), i.e. the last value written will be the first value
137
+ * read (i.e. stack behavior).
155
138
  *
156
139
  * @param cap
157
140
  */
158
141
  export declare const lifo: <T>(cap: number) => LIFOBuffer<T>;
159
- export declare class SlidingBuffer<T> extends FIFOBuffer<T> {
160
- writable(): boolean;
161
- write(x: T): void;
162
- }
163
142
  /**
164
143
  * Returns a {@link SlidingBuffer} with given capacity for use with
165
144
  * {@link channel}.
166
145
  *
167
146
  * @remarks
168
- * With this implementation, writes to the channel are **never** blocking! Once
169
- * the buffer's capacity is reached, a new write will first expunge the oldest
170
- * buffered value. Read behavior is the same as for {@link fifo}.
147
+ * With this implementation, writes to the channel are **never** blocking!
148
+ * Whilst the buffer is at full capacity, new writes will first expunge the
149
+ * oldest buffered value (similar to [LRU
150
+ * cache](https://github.com/thi-ng/umbrella/blob/develop/packages/cache/README.md#lru)
151
+ * behavior). Read behavior is the same as for {@link fifo}.
171
152
  *
172
153
  * Also see {@link dropping} for alternative behavior.
173
154
  *
174
155
  * @param cap
175
156
  */
176
157
  export declare const sliding: <T>(cap: number) => SlidingBuffer<T>;
177
- export declare class DroppingBuffer<T> extends FIFOBuffer<T> {
178
- writable(): boolean;
179
- write(x: T): void;
180
- }
181
158
  /**
182
159
  * Returns a {@link DroppingBuffer} with given capacity for use with
183
160
  * {@link channel}.
184
161
  *
185
162
  * @remarks
186
163
  * With this implementation, writes to the channel are **never** blocking!
187
- * Whilst the buffer's capacity is reached, new writes will be silently ignored.
164
+ * Whilst the buffer is at full capacity, new writes will be silently ignored.
188
165
  * Read behavior is the same as for {@link fifo}.
189
166
  *
190
167
  * Also see {@link sliding} for alternative behavior.
package/csp.js CHANGED
@@ -1,5 +1,8 @@
1
- import { isNumber } from "@thi.ng/checks";
2
- import { assert } from "@thi.ng/errors/assert";
1
+ import { DroppingBuffer } from "@thi.ng/buffers/dropping";
2
+ import { FIFOBuffer } from "@thi.ng/buffers/fifo";
3
+ import { LIFOBuffer } from "@thi.ng/buffers/lifo";
4
+ import { SlidingBuffer } from "@thi.ng/buffers/sliding";
5
+ import { isNumber } from "@thi.ng/checks/is-number";
3
6
  import { Fiber, fiber } from "./fiber.js";
4
7
  const STATE_OPEN = 0;
5
8
  const STATE_CLOSING = 1;
@@ -119,90 +122,12 @@ class Channel {
119
122
  }
120
123
  }
121
124
  const channel = (buffer, opts) => new Channel(buffer, opts);
122
- class FIFOBuffer {
123
- buf;
124
- rpos = 0;
125
- wpos = 0;
126
- constructor(cap = 1) {
127
- assert(cap >= 1, `capacity must be >= 1`);
128
- this.buf = new Array(cap + 1);
129
- }
130
- clear() {
131
- this.buf.fill(void 0);
132
- }
133
- readable() {
134
- return this.rpos !== this.wpos;
135
- }
136
- read() {
137
- const { buf, rpos } = this;
138
- const val = buf[rpos];
139
- buf[rpos] = void 0;
140
- this.rpos = (rpos + 1) % buf.length;
141
- return val;
142
- }
143
- writable() {
144
- return (this.wpos + 1) % this.buf.length !== this.rpos;
145
- }
146
- write(x) {
147
- const { buf, wpos } = this;
148
- buf[wpos] = x;
149
- this.wpos = (wpos + 1) % buf.length;
150
- }
151
- }
152
125
  const fifo = (cap) => new FIFOBuffer(cap);
153
- class LIFOBuffer {
154
- constructor(cap = 1) {
155
- this.cap = cap;
156
- assert(cap >= 1, `capacity must be >= 1`);
157
- }
158
- buf = [];
159
- clear() {
160
- this.buf.length = 0;
161
- }
162
- readable() {
163
- return this.buf.length > 0;
164
- }
165
- read() {
166
- return this.buf.pop();
167
- }
168
- writable() {
169
- return this.buf.length < this.cap;
170
- }
171
- write(x) {
172
- this.buf.push(x);
173
- }
174
- }
175
126
  const lifo = (cap) => new LIFOBuffer(cap);
176
- class SlidingBuffer extends FIFOBuffer {
177
- writable() {
178
- return true;
179
- }
180
- write(x) {
181
- if (!super.writable()) {
182
- const { buf, rpos } = this;
183
- buf[rpos] = void 0;
184
- this.rpos = (rpos + 1) % buf.length;
185
- }
186
- super.write(x);
187
- }
188
- }
189
127
  const sliding = (cap) => new SlidingBuffer(cap);
190
- class DroppingBuffer extends FIFOBuffer {
191
- writable() {
192
- return true;
193
- }
194
- write(x) {
195
- if (super.writable())
196
- super.write(x);
197
- }
198
- }
199
128
  const dropping = (cap) => new DroppingBuffer(cap);
200
129
  export {
201
130
  Channel,
202
- DroppingBuffer,
203
- FIFOBuffer,
204
- LIFOBuffer,
205
- SlidingBuffer,
206
131
  channel,
207
132
  dropping,
208
133
  fifo,
package/fiber.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { Event as $Event, Fn, Fn0, IDeref, IID, IIDGen, INotify, Listener, Nullable } from "@thi.ng/api";
1
+ import type { Event as $Event, Fn, Fn0, IDeref, IID, IIDGen, INotify, Listener, Maybe, Nullable } from "@thi.ng/api";
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 class Fiber<T = any> implements IDeref<T | undefined>, IID<string>, INotify<FiberEventType> {
5
+ export declare class Fiber<T = any> implements IDeref<Maybe<T>>, IID<string>, INotify<FiberEventType> {
6
6
  /**
7
7
  * This fiber's user provided or generated ID.
8
8
  */
@@ -20,7 +20,7 @@ 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
+ _promise?: Promise<Maybe<T>>;
24
24
  constructor(gen?: Nullable<FiberFactory<T> | Generator<unknown, T>>, opts?: Partial<FiberOpts>);
25
25
  /**
26
26
  * Co-routine which blocks whilst this fiber (incl. its children) is active.
@@ -42,7 +42,7 @@ export declare class Fiber<T = any> implements IDeref<T | undefined>, IID<string
42
42
  *
43
43
  * @param delay
44
44
  */
45
- promise(delay?: number): Promise<T | undefined>;
45
+ promise(delay?: number): Promise<Maybe<T>>;
46
46
  /**
47
47
  * Returns this fiber's result value (if any). Only available if the fiber
48
48
  * completed successfully and produced a value (either by returning a value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/fibers",
3
- "version": "0.6.36",
3
+ "version": "1.0.1",
4
4
  "description": "Process hierarchies & operators for cooperative multitasking",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,14 +36,15 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.10.0",
40
- "@thi.ng/arrays": "^2.9.2",
41
- "@thi.ng/bench": "^3.5.2",
42
- "@thi.ng/checks": "^3.6.0",
43
- "@thi.ng/errors": "^2.5.3",
44
- "@thi.ng/idgen": "^2.2.37",
45
- "@thi.ng/logger": "^3.0.8",
46
- "@thi.ng/random": "^3.7.2"
39
+ "@thi.ng/api": "^8.11.0",
40
+ "@thi.ng/arrays": "^2.9.4",
41
+ "@thi.ng/bench": "^3.5.4",
42
+ "@thi.ng/buffers": "^0.1.1",
43
+ "@thi.ng/checks": "^3.6.2",
44
+ "@thi.ng/errors": "^2.5.5",
45
+ "@thi.ng/idgen": "^2.2.39",
46
+ "@thi.ng/logger": "^3.0.10",
47
+ "@thi.ng/random": "^3.7.4"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@microsoft/api-extractor": "^7.43.0",
@@ -109,5 +110,5 @@
109
110
  "status": "alpha",
110
111
  "year": 2023
111
112
  },
112
- "gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
113
+ "gitHead": "8339d05ecc857e529c7325a9839c0063b89e728d\n"
113
114
  }