@thi.ng/csp 2.1.116 → 3.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/CHANGELOG.md +42 -1
- package/README.md +77 -182
- package/api.d.ts +12 -20
- package/channel.d.ts +37 -145
- package/channel.js +131 -480
- package/idgen.d.ts +3 -0
- package/idgen.js +5 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/mult.d.ts +12 -20
- package/mult.js +42 -80
- package/ops.d.ts +24 -0
- package/ops.js +126 -0
- package/package.json +16 -13
- package/pubsub.d.ts +15 -21
- package/pubsub.js +32 -49
- package/buffer.d.ts +0 -24
- package/buffer.js +0 -67
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-04-
|
|
3
|
+
- **Last updated**: 2024-04-26T13:32:20Z
|
|
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,12 +9,53 @@ 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
|
+
## [3.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@3.1.0) (2024-04-26)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add opt. generics for PubSub.subscribe()/unsubscribe() ([b4e9d20](https://github.com/thi-ng/umbrella/commit/b4e9d20))
|
|
17
|
+
- add into() to feed (async) iterables into a channel ([c7c1f6d](https://github.com/thi-ng/umbrella/commit/c7c1f6d))
|
|
18
|
+
|
|
19
|
+
# [3.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@3.0.0) (2024-04-25)
|
|
20
|
+
|
|
21
|
+
#### 🛑 Breaking changes
|
|
22
|
+
|
|
23
|
+
- complete rewrite of Channel, Mult, PubSub, remove deps, add new operators ([2cf7431](https://github.com/thi-ng/umbrella/commit/2cf7431))
|
|
24
|
+
- BREAKING CHANGES: complete rewrite of Channel, Mult, PubSub, remove deps, add new operators
|
|
25
|
+
- remove/replace types
|
|
26
|
+
- remove buffer impls (now using [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/main/packages/buffers))
|
|
27
|
+
- remove transducer handling (now done externally, e.g. via [@thi.ng/transducers-async](https://github.com/thi-ng/umbrella/tree/main/packages/transducers-async))
|
|
28
|
+
- replace Channel, Mult, PubSub
|
|
29
|
+
- remove obsolete dependencies
|
|
30
|
+
- add/update channel operators:
|
|
31
|
+
- broadcast()
|
|
32
|
+
- concat()
|
|
33
|
+
- consume() / consumeWith()
|
|
34
|
+
- drain()
|
|
35
|
+
- fromAsyncIterable()
|
|
36
|
+
- merge()
|
|
37
|
+
- pipe()
|
|
38
|
+
- select()
|
|
39
|
+
- timeout()
|
|
40
|
+
- add/update tests, remove old/obsolete ones
|
|
41
|
+
|
|
42
|
+
#### 🚀 Features
|
|
43
|
+
|
|
44
|
+
- update consume() to accept opt. limit ([164d1dd](https://github.com/thi-ng/umbrella/commit/164d1dd))
|
|
45
|
+
- add write queue, update tests ([f201401](https://github.com/thi-ng/umbrella/commit/f201401))
|
|
46
|
+
|
|
12
47
|
### [2.1.115](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.115) (2024-04-20)
|
|
13
48
|
|
|
14
49
|
#### ♻️ Refactoring
|
|
15
50
|
|
|
16
51
|
- update type usage ([749b24c](https://github.com/thi-ng/umbrella/commit/749b24c))
|
|
17
52
|
|
|
53
|
+
### [2.1.114](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.114) (2024-04-11)
|
|
54
|
+
|
|
55
|
+
#### 🚀 Features
|
|
56
|
+
|
|
57
|
+
- add initial new Channel impl & related ops/tests ([edddf61](https://github.com/thi-ng/umbrella/commit/edddf61))
|
|
58
|
+
|
|
18
59
|
### [2.1.113](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.113) (2024-04-08)
|
|
19
60
|
|
|
20
61
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -15,52 +15,70 @@
|
|
|
15
15
|
> GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
|
|
16
16
|
|
|
17
17
|
- [About](#about)
|
|
18
|
+
- [Channel operators](#channel-operators)
|
|
18
19
|
- [Status](#status)
|
|
19
20
|
- [Related packages](#related-packages)
|
|
20
21
|
- [Installation](#installation)
|
|
21
22
|
- [Dependencies](#dependencies)
|
|
23
|
+
- [Usage examples](#usage-examples)
|
|
22
24
|
- [API](#api)
|
|
23
|
-
- [File loading & word frequency analysis](#file-loading--word-frequency-analysis)
|
|
24
|
-
- [Channel merging](#channel-merging)
|
|
25
25
|
- [PubSub](#pubsub)
|
|
26
26
|
- [Authors](#authors)
|
|
27
27
|
- [License](#license)
|
|
28
28
|
|
|
29
29
|
## About
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
|
|
31
|
+
Primitives & operators for Communicating Sequential Processes based on async/await and async iterables.
|
|
32
|
+
|
|
33
|
+
This package was temporarily deprecated (throughout most of 2023), but meanwhile
|
|
34
|
+
has been reanimated in the form of a **complete rewrite**, using a new, more
|
|
35
|
+
simple and more modern approach afforded by contemporary ES language features
|
|
36
|
+
(and widespread support for them).
|
|
37
|
+
|
|
38
|
+
**This new/current implementation is in most cases NOT compatible with earlier
|
|
39
|
+
versions**.
|
|
40
|
+
|
|
41
|
+
Provided are:
|
|
42
|
+
|
|
43
|
+
- [CSP `Channel`
|
|
44
|
+
primitive](https://docs.thi.ng/umbrella/csp/classes/Channel.html) supporting a
|
|
45
|
+
choice of buffer behaviors (fifo, sliding, dropping, see
|
|
46
|
+
[thi.ng/buffers](https://github.com/thi-ng/umbrella/blob/develop/packages/buffers)
|
|
47
|
+
for options)
|
|
48
|
+
- Composable channel operators (see list below)
|
|
49
|
+
- [`Mult`](https://docs.thi.ng/umbrella/csp/classes/Mult.html) for channel
|
|
50
|
+
multiplexing (one-to-many splitting) and dynamic add/removal of subscribers
|
|
51
|
+
- [`PubSub`](https://docs.thi.ng/umbrella/csp/classes/PubSub.html) for
|
|
52
|
+
topic-based subscriptions, each topic implemented as `Mult`
|
|
53
|
+
|
|
54
|
+
### Channel operators
|
|
55
|
+
|
|
56
|
+
- [`broadcast()`](https://docs.thi.ng/umbrella/csp/functions/broadcast.html)
|
|
57
|
+
- [`channel()`](https://docs.thi.ng/umbrella/csp/functions/channel.html)
|
|
58
|
+
- [`concat()`](https://docs.thi.ng/umbrella/csp/functions/concat.html)
|
|
59
|
+
- [`consume()`](https://docs.thi.ng/umbrella/csp/functions/consume.html)
|
|
60
|
+
- [`consumeWith()`](https://docs.thi.ng/umbrella/csp/functions/consumeWith.html)
|
|
61
|
+
- [`drain()`](https://docs.thi.ng/umbrella/csp/functions/drain.html)
|
|
62
|
+
- [`fromAsyncIterable()`](https://docs.thi.ng/umbrella/csp/functions/fromAsyncIterable.html)
|
|
63
|
+
- [`into()`](https://docs.thi.ng/umbrella/csp/functions/into.html)
|
|
64
|
+
- [`merge()`](https://docs.thi.ng/umbrella/csp/functions/merge.html)
|
|
65
|
+
- [`mult()`](https://docs.thi.ng/umbrella/csp/functions/mult.html)
|
|
66
|
+
- [`pipe()`](https://docs.thi.ng/umbrella/csp/functions/pipe.html)
|
|
67
|
+
- [`pubsub()`](https://docs.thi.ng/umbrella/csp/functions/pubsub.html)
|
|
68
|
+
- [`select()`](https://docs.thi.ng/umbrella/csp/functions/select.html)
|
|
69
|
+
- [`timeout()`](https://docs.thi.ng/umbrella/csp/functions/timeout.html)
|
|
48
70
|
|
|
49
71
|
## Status
|
|
50
72
|
|
|
51
|
-
**
|
|
73
|
+
**BETA** - possibly breaking changes forthcoming
|
|
52
74
|
|
|
53
75
|
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bcsp%5D+in%3Atitle)
|
|
54
76
|
|
|
55
|
-
This package is deprecated. Please see the following actively maintained
|
|
56
|
-
alternatives providing similar functionality:
|
|
57
|
-
|
|
58
|
-
- [@thi.ng/fibers](https://github.com/thi-ng/umbrella/tree/develop/packages/fibers)
|
|
59
|
-
- [@thi.ng/rstream](https://github.com/thi-ng/umbrella/tree/develop/packages/rstream)
|
|
60
|
-
|
|
61
77
|
## Related packages
|
|
62
78
|
|
|
79
|
+
- [@thi.ng/fibers](https://github.com/thi-ng/umbrella/tree/develop/packages/fibers) - Process hierarchies & operators for cooperative multitasking
|
|
63
80
|
- [@thi.ng/rstream](https://github.com/thi-ng/umbrella/tree/develop/packages/rstream) - Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
|
|
81
|
+
- [@thi.ng/transducers-async](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers-async) - Async versions of various highly composable transducers, reducers and iterators
|
|
64
82
|
|
|
65
83
|
## Installation
|
|
66
84
|
|
|
@@ -88,183 +106,60 @@ For Node.js REPL:
|
|
|
88
106
|
const csp = await import("@thi.ng/csp");
|
|
89
107
|
```
|
|
90
108
|
|
|
91
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
109
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.79 KB
|
|
92
110
|
|
|
93
111
|
## Dependencies
|
|
94
112
|
|
|
95
113
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
96
|
-
- [@thi.ng/
|
|
114
|
+
- [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/develop/packages/buffers)
|
|
97
115
|
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
98
|
-
- [@thi.ng/dcons](https://github.com/thi-ng/umbrella/tree/develop/packages/dcons)
|
|
99
116
|
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
100
|
-
- [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers)
|
|
101
|
-
|
|
102
|
-
## API
|
|
103
117
|
|
|
104
|
-
|
|
118
|
+
## Usage examples
|
|
105
119
|
|
|
106
|
-
|
|
120
|
+
One project in this repo's
|
|
121
|
+
[/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
|
|
122
|
+
directory is using this package:
|
|
107
123
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// compose transducer to split source file into words
|
|
113
|
-
// and filter out short strings
|
|
114
|
-
const proc: tx.Transducer<string, string> = tx.comp(
|
|
115
|
-
tx.mapcat((src: string) => src.toLowerCase().split(/[^\w]+/g)),
|
|
116
|
-
tx.filter((w: string) => w.length > 1)
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
// define a channel which receives file paths
|
|
120
|
-
// and resolves them with their contents
|
|
121
|
-
const paths = new Channel<any>(
|
|
122
|
-
tx.map((path: string) =>
|
|
123
|
-
new Promise<string>(
|
|
124
|
-
resolve => fs.readFile(path, (_, data) => resolve(data.toString()))
|
|
125
|
-
)
|
|
126
|
-
)
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
// define multiplexed output channel
|
|
130
|
-
// items in this channel will have this form: `[word, count]`
|
|
131
|
-
const results = new Mult("results");
|
|
132
|
-
|
|
133
|
-
// tap result channel and sum word counts
|
|
134
|
-
const counter = results
|
|
135
|
-
.tap(tx.map(x => x[1]))
|
|
136
|
-
.reduce(tx.add());
|
|
137
|
-
|
|
138
|
-
// 2nd output channel with streaming sort transducer
|
|
139
|
-
// (using a sliding window size of 500 items) and dropping
|
|
140
|
-
// words with < 20 occurrences
|
|
141
|
-
const sorted = results.tap(
|
|
142
|
-
tx.comp(
|
|
143
|
-
tx.streamSort(500, x => x[1]),
|
|
144
|
-
tx.dropWhile(x => x[1] < 20)
|
|
145
|
-
)
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
// define workflow:
|
|
149
|
-
// pipe source files into a new channel and
|
|
150
|
-
// reduce this channel using `frequencies` reducer
|
|
151
|
-
// finally stream the result map (word frequencies)
|
|
152
|
-
// into the `sorted` channel
|
|
153
|
-
// (`freqs` is a JS Map and is iterable)
|
|
154
|
-
paths.pipe(proc)
|
|
155
|
-
.reduce(tx.frequencies())
|
|
156
|
-
.then(freqs => results.channel().into(freqs));
|
|
157
|
-
|
|
158
|
-
// start tracing sorted outputs and
|
|
159
|
-
// wait for all to finish
|
|
160
|
-
Promise
|
|
161
|
-
.all([sorted.consume(), counter])
|
|
162
|
-
.then(([_, num]) => console.log("total words:", num));
|
|
163
|
-
|
|
164
|
-
// no real work has been executed thus far (only scheduled via promises)
|
|
165
|
-
// now kick off entire process by writing file paths into the 1st channel
|
|
166
|
-
paths.into(["src/channel.ts", "src/mult.ts", "src/pubsub.ts"]);
|
|
167
|
-
|
|
168
|
-
// results-tap1 : [ 'let', 20 ]
|
|
169
|
-
// results-tap1 : [ 'topic', 20 ]
|
|
170
|
-
// results-tap1 : [ 'chan', 20 ]
|
|
171
|
-
// results-tap1 : [ 'number', 22 ]
|
|
172
|
-
// results-tap1 : [ 'buf', 23 ]
|
|
173
|
-
// results-tap1 : [ 'length', 23 ]
|
|
174
|
-
// results-tap1 : [ 'tx', 25 ]
|
|
175
|
-
// results-tap1 : [ 'state', 25 ]
|
|
176
|
-
// results-tap1 : [ 'from', 27 ]
|
|
177
|
-
// results-tap1 : [ 'close', 28 ]
|
|
178
|
-
// results-tap1 : [ 'new', 33 ]
|
|
179
|
-
// results-tap1 : [ 'args', 34 ]
|
|
180
|
-
// results-tap1 : [ 'id', 36 ]
|
|
181
|
-
// results-tap1 : [ 'any', 36 ]
|
|
182
|
-
// results-tap1 : [ 'src', 38 ]
|
|
183
|
-
// results-tap1 : [ 'if', 40 ]
|
|
184
|
-
// results-tap1 : [ 'return', 47 ]
|
|
185
|
-
// results-tap1 : [ 'channel', 57 ]
|
|
186
|
-
// results-tap1 : [ 'this', 120 ]
|
|
187
|
-
// results-tap1 done
|
|
188
|
-
// total words: 1607
|
|
189
|
-
```
|
|
124
|
+
| Screenshot | Description | Live demo | Source |
|
|
125
|
+
|:---------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------|:----------------------------------------------|:---------------------------------------------------------------------------|
|
|
126
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/csp-bus.png" width="240"/> | CSP channel-based event handling, async transducers & reactive UI components | [Demo](https://demo.thi.ng/umbrella/csp-bus/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/csp-bus) |
|
|
190
127
|
|
|
191
|
-
|
|
128
|
+
## API
|
|
192
129
|
|
|
193
|
-
|
|
194
|
-
import { Channel } from "@thi.ng/csp";
|
|
195
|
-
import { push } from "@thi.ng/transducers";
|
|
196
|
-
|
|
197
|
-
Channel.merge([
|
|
198
|
-
Channel.range(0, 3),
|
|
199
|
-
Channel.range(10, 15),
|
|
200
|
-
Channel.range(100, 110)
|
|
201
|
-
]).reduce(push()).then(console.log);
|
|
202
|
-
|
|
203
|
-
// [ 0, 100, 101, 102, 103, 1, 2, 104, 105, 10, 11, 12, 13, 106, 14, 107, 108, 109 ]
|
|
204
|
-
|
|
205
|
-
// emit tuples of values read from all inputs
|
|
206
|
-
// preserves ordering of all inputs, but
|
|
207
|
-
// throughput controlled by slowest input
|
|
208
|
-
// by default stops & closes when any of the inputs closes
|
|
209
|
-
Channel.mergeTuples([
|
|
210
|
-
Channel.from([1, 2, 3]),
|
|
211
|
-
Channel.from([10, 20, 30, 40]),
|
|
212
|
-
Channel.from([100, 200, 300, 400, 500])
|
|
213
|
-
], null, false).consume();
|
|
214
|
-
|
|
215
|
-
// chan-3 : [ 1, 10, 100 ]
|
|
216
|
-
// chan-3 : [ 2, 20, 200 ]
|
|
217
|
-
// chan-3 : [ 3, 30, 300 ]
|
|
218
|
-
// chan-3 done
|
|
219
|
-
|
|
220
|
-
// same as above, however continues until all inputs are closed
|
|
221
|
-
Channel.mergeTuples([
|
|
222
|
-
Channel.from([1, 2, 3]),
|
|
223
|
-
Channel.from([10, 20, 30, 40]),
|
|
224
|
-
Channel.from([100, 200, 300, 400, 500])
|
|
225
|
-
], null, false).consume();
|
|
226
|
-
|
|
227
|
-
// chan-3 : [ 1, 10, 100 ]
|
|
228
|
-
// chan-3 : [ 2, 20, 200 ]
|
|
229
|
-
// chan-3 : [ 3, 30, 300 ]
|
|
230
|
-
// chan-3 : [ undefined, 40, 400 ]
|
|
231
|
-
// chan-3 : [ undefined, undefined, 500 ]
|
|
232
|
-
// chan-3 done
|
|
233
|
-
```
|
|
130
|
+
[Generated API docs](https://docs.thi.ng/umbrella/csp/)
|
|
234
131
|
|
|
235
132
|
### PubSub
|
|
236
133
|
|
|
237
|
-
```ts
|
|
238
|
-
import {
|
|
239
|
-
import { map } from "@thi.ng/transducers";
|
|
134
|
+
```ts tangle:export/readme-pubsub.ts
|
|
135
|
+
import { channel, consumeWith, into, pubsub } from "@thi.ng/csp";
|
|
240
136
|
|
|
241
|
-
//
|
|
242
|
-
|
|
243
|
-
const pub = new PubSub(
|
|
244
|
-
new Channel<any>("users", map((x: string) => ({ type: x.charAt(0), val: x }))),
|
|
245
|
-
(x) => x.type
|
|
246
|
-
);
|
|
137
|
+
// input channel (optional)
|
|
138
|
+
const src = channel<string>({ id: "users" });
|
|
247
139
|
|
|
248
|
-
//
|
|
140
|
+
// publisher with a topic function
|
|
141
|
+
// (topic here is the first character of each received string)
|
|
142
|
+
const pub = pubsub<string>(src, (x) => x[0]);
|
|
143
|
+
|
|
144
|
+
// create topic subscriptions (channel & debug consumer)
|
|
249
145
|
// under the hood each topic is a Mult (multiplexed channel)
|
|
250
|
-
//
|
|
146
|
+
// subscription channels are automatically named:
|
|
251
147
|
// `<src-id>-<topic>-tap<tapid>` (see below)
|
|
252
148
|
for (let i of "abc") {
|
|
253
|
-
pub.
|
|
149
|
+
consumeWith(pub.subscribeTopic(i), (x, ch) => console.log(ch.id, x));
|
|
254
150
|
}
|
|
255
151
|
|
|
256
|
-
// start processing
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
// users-
|
|
261
|
-
// users-b-
|
|
262
|
-
// users-
|
|
263
|
-
// users-
|
|
264
|
-
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
// users-a-tap0 done
|
|
152
|
+
// start processing by feeding an iterable of names
|
|
153
|
+
await into(src, ["alice", "bert", "bella", "charlie", "arthur"]);
|
|
154
|
+
|
|
155
|
+
// users-a-tap0 alice
|
|
156
|
+
// users-b-tap1 bert
|
|
157
|
+
// users-b-tap1 bella
|
|
158
|
+
// users-c-tap2 charlie
|
|
159
|
+
// users-a-tap0 arthur
|
|
160
|
+
|
|
161
|
+
// pubsubs & mults are closed recursively once we close the input channel
|
|
162
|
+
src.close();
|
|
268
163
|
```
|
|
269
164
|
|
|
270
165
|
## Authors
|
package/api.d.ts
CHANGED
|
@@ -1,27 +1,19 @@
|
|
|
1
|
-
import type { Fn,
|
|
2
|
-
import type {
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { Fn, Maybe } from "@thi.ng/api";
|
|
2
|
+
import type { IReadWriteBuffer } from "@thi.ng/buffers";
|
|
3
|
+
export type ChannelValue<T> = [T, Fn<boolean, void>];
|
|
4
|
+
export type ChannelBuffer<T> = IReadWriteBuffer<ChannelValue<T>>;
|
|
5
|
+
export interface IClosable {
|
|
6
|
+
close(): void;
|
|
7
|
+
closed(): boolean;
|
|
6
8
|
}
|
|
7
|
-
export interface
|
|
8
|
-
isEmpty(): boolean;
|
|
9
|
-
isFull(): boolean;
|
|
10
|
-
drop(): Maybe<ChannelItem<T>>;
|
|
11
|
-
push(x: ChannelItem<T>): boolean;
|
|
12
|
-
}
|
|
13
|
-
export interface IChannel<T> extends IID<string> {
|
|
14
|
-
channel(): Channel<T>;
|
|
15
|
-
close(flush?: boolean): Maybe<Promise<void>>;
|
|
16
|
-
}
|
|
17
|
-
export interface IReadableChannel<T> extends IChannel<T> {
|
|
9
|
+
export interface IReadable<T> {
|
|
18
10
|
read(): Promise<Maybe<T>>;
|
|
19
11
|
}
|
|
20
|
-
export interface
|
|
21
|
-
write(val:
|
|
12
|
+
export interface IWriteable<T> {
|
|
13
|
+
write(val: T): Promise<boolean>;
|
|
14
|
+
writable(): boolean;
|
|
22
15
|
}
|
|
23
|
-
export interface
|
|
16
|
+
export interface IChannel<T> extends IReadable<T>, IWriteable<T>, IClosable {
|
|
24
17
|
}
|
|
25
18
|
export type TopicFn<T> = Fn<T, string>;
|
|
26
|
-
export type ErrorHandler = (e: Error, chan: Channel<any>, val?: any) => void;
|
|
27
19
|
//# sourceMappingURL=api.d.ts.map
|
package/channel.d.ts
CHANGED
|
@@ -1,154 +1,46 @@
|
|
|
1
|
-
import type { Fn,
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
CLOSED = 1,
|
|
8
|
-
DONE = 2
|
|
1
|
+
import type { Fn, Maybe } from "@thi.ng/api";
|
|
2
|
+
import type { ChannelBuffer, ChannelValue, IChannel } from "./api.js";
|
|
3
|
+
export declare const MAX_READS = 1024;
|
|
4
|
+
export declare const MAX_QUEUE = 1024;
|
|
5
|
+
export interface ChannelOpts {
|
|
6
|
+
id: string;
|
|
9
7
|
}
|
|
10
|
-
export declare
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* @param delay - time in ms
|
|
29
|
-
*/
|
|
30
|
-
static sleep(delay: number): Promise<any>;
|
|
31
|
-
/**
|
|
32
|
-
* Creates new channel with single value from given promise, then closes
|
|
33
|
-
* automatically iff promise has been resolved.
|
|
34
|
-
*
|
|
35
|
-
* @param p - promise
|
|
36
|
-
*/
|
|
37
|
-
static fromPromise<T>(p: Promise<T>): Channel<T>;
|
|
38
|
-
static from<T>(src: Iterable<any>): Channel<T>;
|
|
39
|
-
static from<T>(src: Iterable<any>, close: boolean): Channel<T>;
|
|
40
|
-
static from<T>(src: Iterable<any>, tx: Transducer<any, T>): Channel<T>;
|
|
41
|
-
static from<T>(src: Iterable<any>, tx: Transducer<any, T>, close: boolean): Channel<T>;
|
|
8
|
+
export declare function channel<T>(opts?: Partial<ChannelOpts>): Channel<T>;
|
|
9
|
+
export declare function channel<T>(buf: ChannelBuffer<T> | number, opts?: Partial<ChannelOpts>): Channel<T>;
|
|
10
|
+
export declare class Channel<T> implements IChannel<T> {
|
|
11
|
+
id: string;
|
|
12
|
+
writes: ChannelBuffer<T>;
|
|
13
|
+
queue: ChannelValue<T>[];
|
|
14
|
+
reads: Fn<Maybe<T>, void>[];
|
|
15
|
+
races: Fn<Channel<T>, void>[];
|
|
16
|
+
protected state: number;
|
|
17
|
+
constructor(opts?: Partial<ChannelOpts>);
|
|
18
|
+
constructor(buf: ChannelBuffer<T> | number, opts?: Partial<ChannelOpts>);
|
|
19
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
20
|
+
read(): Promise<Maybe<T>>;
|
|
21
|
+
poll(): Maybe<T>;
|
|
22
|
+
write(msg: T): Promise<boolean>;
|
|
23
|
+
offer(msg: T): boolean;
|
|
24
|
+
race(): Promise<Channel<T>>;
|
|
25
|
+
close(): void;
|
|
42
26
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* shuffled for each read attempt.
|
|
47
|
-
*
|
|
48
|
-
* @param chans - source channels
|
|
27
|
+
* Returns true if the channel is principally readable (i.e. not yet
|
|
28
|
+
* closed), however there might not be any values available yet and reads
|
|
29
|
+
* might block.
|
|
49
30
|
*/
|
|
50
|
-
|
|
31
|
+
readable(): boolean;
|
|
51
32
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* default).
|
|
56
|
-
*
|
|
57
|
-
* @remarks
|
|
58
|
-
* If `named` is true, the merged channel will have tuples of:
|
|
59
|
-
* `[src-id, val]` If false (default), only received values will be
|
|
60
|
-
* forwarded.
|
|
61
|
-
*
|
|
62
|
-
* @param chans - source channels
|
|
63
|
-
* @param out - result channel
|
|
64
|
-
* @param close - true, if result closes
|
|
65
|
-
* @param named - true, to emit labeled tuples
|
|
33
|
+
* Returns true if the channel is principally writable (i.e. not closing or
|
|
34
|
+
* closed), however depending on buffer behavior the channel might not yet
|
|
35
|
+
* accept new values and writes might block.
|
|
66
36
|
*/
|
|
67
|
-
|
|
37
|
+
writable(): boolean;
|
|
68
38
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* guarantees about ordering of the output.
|
|
72
|
-
*
|
|
73
|
-
* @remarks
|
|
74
|
-
* The output channel of this function will collect values from all
|
|
75
|
-
* channels and a new tuple is emitted only once a new value has
|
|
76
|
-
* been read from ALL channels. Therefore the overall throughput is
|
|
77
|
-
* dictated by the slowest of the inputs.
|
|
78
|
-
*
|
|
79
|
-
* Once any of the inputs closes, the process is terminated and the
|
|
80
|
-
* output channel is closed too (by default).
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```ts tangle:../export/merge-tuples.ts
|
|
84
|
-
* import { Channel } from "@thi.ng/csp";
|
|
85
|
-
*
|
|
86
|
-
* Channel.mergeTuples([
|
|
87
|
-
* Channel.from([1, 2, 3]),
|
|
88
|
-
* Channel.from([10, 20, 30]),
|
|
89
|
-
* Channel.from([100, 200, 300])
|
|
90
|
-
* ]).consume();
|
|
91
|
-
*
|
|
92
|
-
* // chan-0 : [ 1, 10, 100 ]
|
|
93
|
-
* // chan-0 : [ 2, 20, 200 ]
|
|
94
|
-
* // chan-0 : [ 3, 30, 300 ]
|
|
95
|
-
* // chan-0 done
|
|
96
|
-
*
|
|
97
|
-
* Channel.mergeTuples([
|
|
98
|
-
* Channel.from([1, 2, 3]),
|
|
99
|
-
* Channel.from([10, 20, 30]),
|
|
100
|
-
* Channel.from([100, 200, 300])
|
|
101
|
-
* ], null, false).consume();
|
|
102
|
-
* ```
|
|
103
|
-
*
|
|
104
|
-
* @param chans - source channels
|
|
105
|
-
* @param out - result channel
|
|
106
|
-
* @param closeOnFirst - true, if result closes when first input is done
|
|
107
|
-
* @param closeOutput - true, if result closes when all inputs are done
|
|
39
|
+
* Returns true if the channel is fully closed and no further reads or
|
|
40
|
+
* writes are possible.
|
|
108
41
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
static SCHEDULE: Fn2<FnAny<void>, number, void>;
|
|
113
|
-
private static RFN;
|
|
114
|
-
id: string;
|
|
115
|
-
onerror: ErrorHandler;
|
|
116
|
-
protected state: State;
|
|
117
|
-
protected buf: IBuffer<T>;
|
|
118
|
-
protected tx: Reducer<T, DCons<T>>;
|
|
119
|
-
protected writes: DCons<ChannelItem<T>>;
|
|
120
|
-
protected reads: DCons<Fn<T, void>>;
|
|
121
|
-
protected txbuf: DCons<T>;
|
|
122
|
-
protected isBusy: boolean;
|
|
123
|
-
constructor();
|
|
124
|
-
constructor(id: string);
|
|
125
|
-
constructor(buf: number | IBuffer<T>);
|
|
126
|
-
constructor(tx: Transducer<any, T>);
|
|
127
|
-
constructor(tx: Transducer<any, T>, err: ErrorHandler);
|
|
128
|
-
constructor(id: string, buf: number | IBuffer<T>);
|
|
129
|
-
constructor(id: string, tx: Transducer<any, T>);
|
|
130
|
-
constructor(id: string, tx: Transducer<any, T>, err: ErrorHandler);
|
|
131
|
-
constructor(id: string, buf: number | IBuffer<T>, tx: Transducer<any, T>);
|
|
132
|
-
constructor(id: string, buf: number | IBuffer<T>, tx: Transducer<any, T>, err: ErrorHandler);
|
|
133
|
-
channel(): this;
|
|
134
|
-
write(value: any): Promise<boolean>;
|
|
135
|
-
read(): Promise<Maybe<T>>;
|
|
136
|
-
tryRead(timeout?: number): Promise<unknown>;
|
|
137
|
-
close(flush?: boolean): Promise<void> | undefined;
|
|
138
|
-
isClosed(): boolean;
|
|
139
|
-
isReadable(): boolean;
|
|
140
|
-
consume(fn?: Fn<T, any>): Promise<void>;
|
|
141
|
-
produce(fn: Fn0<T>, close?: boolean): Promise<void>;
|
|
142
|
-
consumeWhileReadable(fn?: Fn<T, any>): Promise<void>;
|
|
143
|
-
reduce<R>(rfn: Reducer<T, R>, acc?: R): Promise<R>;
|
|
144
|
-
transduce<A, B>(tx: Transducer<T, A>, rfn: Reducer<A, B>, acc?: B): Promise<B>;
|
|
145
|
-
into(src: Iterable<T>, close?: boolean): Promise<void>;
|
|
146
|
-
pipe<R>(dest: Channel<R> | Transducer<T, R>, close?: boolean): Channel<R>;
|
|
147
|
-
split<A, B>(pred: Predicate<T>, truthy?: Channel<A>, falsey?: Channel<B>, close?: boolean): (Channel<A> | Channel<B>)[];
|
|
148
|
-
concat(chans: Iterable<Channel<T>>, close?: boolean): Promise<void>;
|
|
149
|
-
release(): void;
|
|
150
|
-
protected process(): Promise<void>;
|
|
151
|
-
protected flush(): void;
|
|
42
|
+
closed(): boolean;
|
|
43
|
+
protected deliver(): void;
|
|
44
|
+
protected updateQueue(): void;
|
|
152
45
|
}
|
|
153
|
-
export {};
|
|
154
46
|
//# sourceMappingURL=channel.d.ts.map
|