@thi.ng/csp 2.1.116 → 3.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 +35 -1
- package/README.md +50 -183
- 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 +23 -0
- package/ops.js +117 -0
- package/package.json +16 -13
- package/pubsub.d.ts +14 -20
- 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-25T19:44:55Z
|
|
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,46 @@ 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.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@3.0.0) (2024-04-25)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- complete rewrite of Channel, Mult, PubSub, remove deps, add new operators ([2cf7431](https://github.com/thi-ng/umbrella/commit/2cf7431))
|
|
17
|
+
- BREAKING CHANGES: complete rewrite of Channel, Mult, PubSub, remove deps, add new operators
|
|
18
|
+
- remove/replace types
|
|
19
|
+
- remove buffer impls (now using [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/main/packages/buffers))
|
|
20
|
+
- remove transducer handling (now done externally, e.g. via [@thi.ng/transducers-async](https://github.com/thi-ng/umbrella/tree/main/packages/transducers-async))
|
|
21
|
+
- replace Channel, Mult, PubSub
|
|
22
|
+
- remove obsolete dependencies
|
|
23
|
+
- add/update channel operators:
|
|
24
|
+
- broadcast()
|
|
25
|
+
- concat()
|
|
26
|
+
- consume() / consumeWith()
|
|
27
|
+
- drain()
|
|
28
|
+
- fromAsyncIterable()
|
|
29
|
+
- merge()
|
|
30
|
+
- pipe()
|
|
31
|
+
- select()
|
|
32
|
+
- timeout()
|
|
33
|
+
- add/update tests, remove old/obsolete ones
|
|
34
|
+
|
|
35
|
+
#### 🚀 Features
|
|
36
|
+
|
|
37
|
+
- update consume() to accept opt. limit ([164d1dd](https://github.com/thi-ng/umbrella/commit/164d1dd))
|
|
38
|
+
- add write queue, update tests ([f201401](https://github.com/thi-ng/umbrella/commit/f201401))
|
|
39
|
+
|
|
12
40
|
### [2.1.115](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.115) (2024-04-20)
|
|
13
41
|
|
|
14
42
|
#### ♻️ Refactoring
|
|
15
43
|
|
|
16
44
|
- update type usage ([749b24c](https://github.com/thi-ng/umbrella/commit/749b24c))
|
|
17
45
|
|
|
46
|
+
### [2.1.114](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.114) (2024-04-11)
|
|
47
|
+
|
|
48
|
+
#### 🚀 Features
|
|
49
|
+
|
|
50
|
+
- add initial new Channel impl & related ops/tests ([edddf61](https://github.com/thi-ng/umbrella/commit/edddf61))
|
|
51
|
+
|
|
18
52
|
### [2.1.113](https://github.com/thi-ng/umbrella/tree/@thi.ng/csp@2.1.113) (2024-04-08)
|
|
19
53
|
|
|
20
54
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -20,47 +20,46 @@
|
|
|
20
20
|
- [Installation](#installation)
|
|
21
21
|
- [Dependencies](#dependencies)
|
|
22
22
|
- [API](#api)
|
|
23
|
-
- [File loading & word frequency analysis](#file-loading--word-frequency-analysis)
|
|
24
|
-
- [Channel merging](#channel-merging)
|
|
25
23
|
- [PubSub](#pubsub)
|
|
26
24
|
- [Authors](#authors)
|
|
27
25
|
- [License](#license)
|
|
28
26
|
|
|
29
27
|
## About
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- `Channel` with/without buffering and/or
|
|
34
|
-
[transducers](https://github.com/thi-ng/umbrella/blob/develop/packages/transducers)
|
|
35
|
-
- optional channel IDs
|
|
36
|
-
- choice of buffer behaviors (fixed, sliding, dropping)
|
|
37
|
-
- channel selection
|
|
38
|
-
- channel merging (many-to-one, serial or parallel)
|
|
39
|
-
- channel piping (w/ transducers)
|
|
40
|
-
- timeouts / sleeping / throttling / delaying
|
|
41
|
-
- prepopulated channel ctors (iterators, ranges, promise, constants etc.)
|
|
42
|
-
- `Mult` for channel multiplexing (one-to-many splitting)
|
|
43
|
-
- individual transducers per tap
|
|
44
|
-
- dynamic add/removal of taps
|
|
45
|
-
- `PubSub` for topic subscriptions
|
|
46
|
-
- each topic implemented as `Mult`
|
|
47
|
-
- wildcard topic for processing fallthrough messages
|
|
29
|
+
Primitives & operators for Communicating Sequential Processes based on async/await and async iterables.
|
|
48
30
|
|
|
49
|
-
|
|
31
|
+
This package was temporarily deprecated (throughout most of 2023), but meanwhile
|
|
32
|
+
has been reanimated in the form of a **complete rewrite**, using a new, more
|
|
33
|
+
simple and more modern approach afforded by contemporary ES language features
|
|
34
|
+
(and widespread support for them).
|
|
50
35
|
|
|
51
|
-
**
|
|
36
|
+
**This new/current implementation is in most cases NOT compatible with earlier
|
|
37
|
+
versions**.
|
|
52
38
|
|
|
53
|
-
|
|
39
|
+
Provided are:
|
|
54
40
|
|
|
55
|
-
|
|
56
|
-
|
|
41
|
+
- [CSP `Channel`
|
|
42
|
+
primitive](https://docs.thi.ng/umbrella/csp/classes/Channel.html) supporting a
|
|
43
|
+
choice of buffer behaviors (fifo, sliding, dropping, see
|
|
44
|
+
[thi.ng/buffers](https://github.com/thi-ng/umbrella/blob/develop/packages/buffers)
|
|
45
|
+
for options)
|
|
46
|
+
- Composable channel operators (see list below)
|
|
47
|
+
- [`Mult`](https://docs.thi.ng/umbrella/csp/classes/Mult.html) for channel
|
|
48
|
+
multiplexing (one-to-many splitting) and dynamic add/removal of subscribers
|
|
49
|
+
- [`PubSub`](https://docs.thi.ng/umbrella/csp/classes/PubSub.html) for
|
|
50
|
+
topic-based subscriptions, each topic implemented as `Mult`
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
## Status
|
|
53
|
+
|
|
54
|
+
**BETA** - possibly breaking changes forthcoming
|
|
55
|
+
|
|
56
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bcsp%5D+in%3Atitle)
|
|
60
57
|
|
|
61
58
|
## Related packages
|
|
62
59
|
|
|
60
|
+
- [@thi.ng/fibers](https://github.com/thi-ng/umbrella/tree/develop/packages/fibers) - Process hierarchies & operators for cooperative multitasking
|
|
63
61
|
- [@thi.ng/rstream](https://github.com/thi-ng/umbrella/tree/develop/packages/rstream) - Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
|
|
62
|
+
- [@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
63
|
|
|
65
64
|
## Installation
|
|
66
65
|
|
|
@@ -88,183 +87,51 @@ For Node.js REPL:
|
|
|
88
87
|
const csp = await import("@thi.ng/csp");
|
|
89
88
|
```
|
|
90
89
|
|
|
91
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
90
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.77 KB
|
|
92
91
|
|
|
93
92
|
## Dependencies
|
|
94
93
|
|
|
95
94
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
96
|
-
- [@thi.ng/
|
|
95
|
+
- [@thi.ng/buffers](https://github.com/thi-ng/umbrella/tree/develop/packages/buffers)
|
|
97
96
|
- [@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
97
|
- [@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
98
|
|
|
102
99
|
## API
|
|
103
100
|
|
|
104
101
|
[Generated API docs](https://docs.thi.ng/umbrella/csp/)
|
|
105
102
|
|
|
106
|
-
### File loading & word frequency analysis
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
import { Channel } from "@thi.ng/csp";
|
|
110
|
-
import * as tx from "@thi.ng/transducers";
|
|
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
|
-
```
|
|
190
|
-
|
|
191
|
-
### Channel merging
|
|
192
|
-
|
|
193
|
-
```ts
|
|
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
|
-
```
|
|
234
|
-
|
|
235
103
|
### PubSub
|
|
236
104
|
|
|
237
|
-
```ts
|
|
238
|
-
import {
|
|
239
|
-
import { map } from "@thi.ng/transducers";
|
|
105
|
+
```ts tangle:export/readme-pubsub.ts
|
|
106
|
+
import { channel, consumeWith, pubsub } from "@thi.ng/csp";
|
|
240
107
|
|
|
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
|
-
);
|
|
108
|
+
// input channel (optional)
|
|
109
|
+
const src = channel<string>({ id: "users" });
|
|
247
110
|
|
|
248
|
-
//
|
|
111
|
+
// publisher with a topic function
|
|
112
|
+
// (topic here is the first character of each received string)
|
|
113
|
+
const pub = pubsub<string>(src, (x) => x[0]);
|
|
114
|
+
|
|
115
|
+
// create topic subscriptions (channel & debug consumer)
|
|
249
116
|
// under the hood each topic is a Mult (multiplexed channel)
|
|
250
|
-
//
|
|
117
|
+
// subscription channels are automatically named:
|
|
251
118
|
// `<src-id>-<topic>-tap<tapid>` (see below)
|
|
252
119
|
for (let i of "abc") {
|
|
253
|
-
pub.
|
|
120
|
+
consumeWith(pub.subscribeTopic(i), (x, ch) => console.log(ch.id, x));
|
|
254
121
|
}
|
|
255
122
|
|
|
256
|
-
// start processing
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
// users-a-tap0
|
|
261
|
-
// users-b-
|
|
262
|
-
// users-b-
|
|
263
|
-
// users-c-
|
|
264
|
-
// users-a-tap0
|
|
265
|
-
|
|
266
|
-
//
|
|
267
|
-
|
|
123
|
+
// start processing
|
|
124
|
+
for (let x of ["alice", "bert", "bella", "charlie", "arthur"]) {
|
|
125
|
+
await src.write(x);
|
|
126
|
+
}
|
|
127
|
+
// users-a-tap0 alice
|
|
128
|
+
// users-b-tap1 bert
|
|
129
|
+
// users-b-tap1 bella
|
|
130
|
+
// users-c-tap2 charlie
|
|
131
|
+
// users-a-tap0 arthur
|
|
132
|
+
|
|
133
|
+
// pubsubs & mults are closed recursively once we close the input channel
|
|
134
|
+
src.close();
|
|
268
135
|
```
|
|
269
136
|
|
|
270
137
|
## 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
|