@thi.ng/fibers 0.6.35 → 1.0.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 +12 -1
- package/README.md +23 -14
- package/api.d.ts +1 -24
- package/csp.d.ts +18 -42
- package/csp.js +5 -80
- package/package.json +11 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
3
|
+
- **Last updated**: 2024-04-11T12:32:44Z
|
|
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,17 @@ 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.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@1.0.0) (2024-04-11)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- remove/migrate CSP buffer types, update readme ([d4a1d23](https://github.com/thi-ng/umbrella/commit/d4a1d23))
|
|
17
|
+
- 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))
|
|
18
|
+
- remove IReadBuffer & IReadWriteBuffer interfaces
|
|
19
|
+
- update deps (add [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/main/packages/buffers))
|
|
20
|
+
- update docs
|
|
21
|
+
- update readme
|
|
22
|
+
|
|
12
23
|
## [0.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/fibers@0.6.0) (2023-11-09)
|
|
13
24
|
|
|
14
25
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
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
|
|
207
|
-
|
|
206
|
+
The following channel buffer types/behaviors are included (from the
|
|
207
|
+
[thi.ng/buffers](https://thi.ng/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.
|
|
218
|
-
|
|
219
|
-
|
|
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!
|
|
222
|
-
the buffer
|
|
223
|
-
buffered value (similar to LRU
|
|
224
|
-
|
|
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
|
|
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
|
|
@@ -375,7 +377,13 @@ if (res !== undefined) {
|
|
|
375
377
|
yarn add @thi.ng/fibers
|
|
376
378
|
```
|
|
377
379
|
|
|
378
|
-
|
|
380
|
+
ESM import:
|
|
381
|
+
|
|
382
|
+
```ts
|
|
383
|
+
import * as fib from "@thi.ng/fibers";
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Browser ESM import:
|
|
379
387
|
|
|
380
388
|
```html
|
|
381
389
|
<script type="module" src="https://cdn.skypack.dev/@thi.ng/fibers"></script>
|
|
@@ -386,16 +394,17 @@ ES module import:
|
|
|
386
394
|
For Node.js REPL:
|
|
387
395
|
|
|
388
396
|
```js
|
|
389
|
-
const
|
|
397
|
+
const fib = await import("@thi.ng/fibers");
|
|
390
398
|
```
|
|
391
399
|
|
|
392
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 2.
|
|
400
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.45 KB
|
|
393
401
|
|
|
394
402
|
## Dependencies
|
|
395
403
|
|
|
396
404
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
397
405
|
- [@thi.ng/arrays](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays)
|
|
398
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)
|
|
399
408
|
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
400
409
|
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
401
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,
|
|
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,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IReadWriteBuffer } from "@thi.ng/buffers";
|
|
2
|
+
import { DroppingBuffer } from "@thi.ng/buffers/dropping";
|
|
3
|
+
import { FIFOBuffer } from "@thi.ng/buffers/fifo";
|
|
4
|
+
import { LIFOBuffer } from "@thi.ng/buffers/lifo";
|
|
5
|
+
import { SlidingBuffer } from "@thi.ng/buffers/sliding";
|
|
6
|
+
import type { FiberOpts } from "./api.js";
|
|
2
7
|
import { Fiber } from "./fiber.js";
|
|
3
8
|
declare const STATE_OPEN = 0;
|
|
4
9
|
declare const STATE_CLOSING = 1;
|
|
@@ -6,8 +11,9 @@ declare const STATE_CLOSED = 2;
|
|
|
6
11
|
type CSPState = typeof STATE_OPEN | typeof STATE_CLOSING | typeof STATE_CLOSED;
|
|
7
12
|
/**
|
|
8
13
|
* Fiber-based CSP channel implementation, supporting any
|
|
9
|
-
*
|
|
10
|
-
* (and ordering). By default
|
|
14
|
+
* [IReadWriteBuffer](https://docs.thi.ng/umbrella/buffers/interfaces/IReadWriteBuffer.html)
|
|
15
|
+
* implementation to customize read/write behaviors (and ordering). By default
|
|
16
|
+
* uses a single value {@link fifo} buffer impl.
|
|
11
17
|
*/
|
|
12
18
|
export declare class Channel<T> {
|
|
13
19
|
opts?: Partial<FiberOpts> | undefined;
|
|
@@ -105,20 +111,6 @@ export declare class Channel<T> {
|
|
|
105
111
|
* @param opts
|
|
106
112
|
*/
|
|
107
113
|
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
114
|
/**
|
|
123
115
|
* Returns a {@link FIFOBuffer} ring buffer with given capacity for use with
|
|
124
116
|
* {@link channel}.
|
|
@@ -134,57 +126,41 @@ export declare class FIFOBuffer<T> implements IReadWriteBuffer<T> {
|
|
|
134
126
|
* @param cap
|
|
135
127
|
*/
|
|
136
128
|
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
129
|
/**
|
|
148
130
|
* Returns a {@link LIFOBuffer} with given capacity for use with
|
|
149
131
|
* {@link channel}.
|
|
150
132
|
*
|
|
151
133
|
* @remarks
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
134
|
+
* Write behavior is the same as with {@link fifo}, reads are in reverse order
|
|
135
|
+
* (as the name indicates), i.e. the last value written will be the first value
|
|
136
|
+
* read (i.e. stack behavior).
|
|
155
137
|
*
|
|
156
138
|
* @param cap
|
|
157
139
|
*/
|
|
158
140
|
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
141
|
/**
|
|
164
142
|
* Returns a {@link SlidingBuffer} with given capacity for use with
|
|
165
143
|
* {@link channel}.
|
|
166
144
|
*
|
|
167
145
|
* @remarks
|
|
168
|
-
* With this implementation, writes to the channel are **never** blocking!
|
|
169
|
-
* the buffer
|
|
170
|
-
* buffered value
|
|
146
|
+
* With this implementation, writes to the channel are **never** blocking!
|
|
147
|
+
* Whilst the buffer is at full capacity, new writes will first expunge the
|
|
148
|
+
* oldest buffered value (similar to [LRU
|
|
149
|
+
* cache](https://github.com/thi-ng/umbrella/blob/develop/packages/cache/README.md#lru)
|
|
150
|
+
* behavior). Read behavior is the same as for {@link fifo}.
|
|
171
151
|
*
|
|
172
152
|
* Also see {@link dropping} for alternative behavior.
|
|
173
153
|
*
|
|
174
154
|
* @param cap
|
|
175
155
|
*/
|
|
176
156
|
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
157
|
/**
|
|
182
158
|
* Returns a {@link DroppingBuffer} with given capacity for use with
|
|
183
159
|
* {@link channel}.
|
|
184
160
|
*
|
|
185
161
|
* @remarks
|
|
186
162
|
* With this implementation, writes to the channel are **never** blocking!
|
|
187
|
-
* Whilst the buffer
|
|
163
|
+
* Whilst the buffer is at full capacity, new writes will be silently ignored.
|
|
188
164
|
* Read behavior is the same as for {@link fifo}.
|
|
189
165
|
*
|
|
190
166
|
* Also see {@link sliding} for alternative behavior.
|
package/csp.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/fibers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
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.
|
|
40
|
-
"@thi.ng/arrays": "^2.9.
|
|
41
|
-
"@thi.ng/bench": "^3.5.
|
|
42
|
-
"@thi.ng/
|
|
43
|
-
"@thi.ng/
|
|
44
|
-
"@thi.ng/
|
|
45
|
-
"@thi.ng/
|
|
46
|
-
"@thi.ng/
|
|
39
|
+
"@thi.ng/api": "^8.10.1",
|
|
40
|
+
"@thi.ng/arrays": "^2.9.3",
|
|
41
|
+
"@thi.ng/bench": "^3.5.3",
|
|
42
|
+
"@thi.ng/buffers": "^0.1.0",
|
|
43
|
+
"@thi.ng/checks": "^3.6.1",
|
|
44
|
+
"@thi.ng/errors": "^2.5.4",
|
|
45
|
+
"@thi.ng/idgen": "^2.2.38",
|
|
46
|
+
"@thi.ng/logger": "^3.0.9",
|
|
47
|
+
"@thi.ng/random": "^3.7.3"
|
|
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": "
|
|
113
|
+
"gitHead": "18a0c063a7b33d790e5bc2486c106f45f663ac28\n"
|
|
113
114
|
}
|