@thi.ng/csp 2.1.80 → 2.1.82
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 +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/buffer.js +59 -54
- package/channel.js +485 -485
- package/mult.js +95 -95
- package/package.json +12 -10
- package/pubsub.js +84 -81
package/mult.js
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
1
|
import { DCons } from "@thi.ng/dcons/dcons";
|
|
2
2
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
3
3
|
import { Channel } from "./channel.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
src = args[0];
|
|
22
|
-
}
|
|
23
|
-
break;
|
|
24
|
-
case 0:
|
|
25
|
-
id = "mult" + Mult.nextID++;
|
|
26
|
-
break;
|
|
27
|
-
default:
|
|
28
|
-
illegalArity(args.length);
|
|
4
|
+
class Mult {
|
|
5
|
+
static nextID = 0;
|
|
6
|
+
src;
|
|
7
|
+
taps;
|
|
8
|
+
tapID = 0;
|
|
9
|
+
constructor(...args) {
|
|
10
|
+
let id, src;
|
|
11
|
+
switch (args.length) {
|
|
12
|
+
case 2:
|
|
13
|
+
id = args[0];
|
|
14
|
+
src = args[1];
|
|
15
|
+
break;
|
|
16
|
+
case 1:
|
|
17
|
+
if (typeof args[0] === "string") {
|
|
18
|
+
id = args[0];
|
|
19
|
+
} else {
|
|
20
|
+
src = args[0];
|
|
29
21
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.taps = new DCons();
|
|
37
|
-
this.process();
|
|
22
|
+
break;
|
|
23
|
+
case 0:
|
|
24
|
+
id = "mult" + Mult.nextID++;
|
|
25
|
+
break;
|
|
26
|
+
default:
|
|
27
|
+
illegalArity(args.length);
|
|
38
28
|
}
|
|
39
|
-
|
|
40
|
-
|
|
29
|
+
if (src instanceof Channel) {
|
|
30
|
+
this.src = src;
|
|
31
|
+
} else {
|
|
32
|
+
this.src = new Channel(id, src);
|
|
41
33
|
}
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
this.taps = new DCons();
|
|
35
|
+
this.process();
|
|
36
|
+
}
|
|
37
|
+
get id() {
|
|
38
|
+
return this.src && this.src.id;
|
|
39
|
+
}
|
|
40
|
+
set id(id) {
|
|
41
|
+
this.src && (this.src.id = id);
|
|
42
|
+
}
|
|
43
|
+
channel() {
|
|
44
|
+
return this.src;
|
|
45
|
+
}
|
|
46
|
+
write(val) {
|
|
47
|
+
if (this.src) {
|
|
48
|
+
return this.src.write(val);
|
|
44
49
|
}
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
return Promise.resolve(false);
|
|
51
|
+
}
|
|
52
|
+
close(flush = false) {
|
|
53
|
+
return this.src ? this.src.close(flush) : void 0;
|
|
54
|
+
}
|
|
55
|
+
tap(ch) {
|
|
56
|
+
if (this.taps) {
|
|
57
|
+
if (!(ch instanceof Channel)) {
|
|
58
|
+
ch = new Channel(this.src.id + "-tap" + this.tapID++, ch);
|
|
59
|
+
} else if (this.taps.find(ch)) {
|
|
60
|
+
return ch;
|
|
61
|
+
}
|
|
62
|
+
this.taps.push(ch);
|
|
63
|
+
return ch;
|
|
47
64
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
65
|
+
}
|
|
66
|
+
untap(ch) {
|
|
67
|
+
if (this.taps) {
|
|
68
|
+
const t = this.taps.find(ch);
|
|
69
|
+
if (t) {
|
|
70
|
+
this.taps.remove(t);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
53
73
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this.taps.push(ch);
|
|
66
|
-
return ch;
|
|
67
|
-
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
untapAll(close = true) {
|
|
77
|
+
if (this.taps) {
|
|
78
|
+
let tap = this.taps.head;
|
|
79
|
+
while (tap) {
|
|
80
|
+
close && tap.value.close();
|
|
81
|
+
this.taps.remove(tap);
|
|
82
|
+
tap = tap.next;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
68
85
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
async process() {
|
|
89
|
+
let x;
|
|
90
|
+
while ((x = null, x = await this.src.read()) !== void 0) {
|
|
91
|
+
let t = this.taps.head;
|
|
92
|
+
while (t) {
|
|
93
|
+
if (!await t.value.write(x)) {
|
|
94
|
+
this.taps.remove(t);
|
|
76
95
|
}
|
|
77
|
-
|
|
96
|
+
t = t.next;
|
|
97
|
+
}
|
|
78
98
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
let tap = this.taps.head;
|
|
82
|
-
while (tap) {
|
|
83
|
-
close && tap.value.close();
|
|
84
|
-
this.taps.remove(tap);
|
|
85
|
-
tap = tap.next;
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
async process() {
|
|
92
|
-
let x;
|
|
93
|
-
while (((x = null), (x = await this.src.read())) !== undefined) {
|
|
94
|
-
let t = this.taps.head;
|
|
95
|
-
while (t) {
|
|
96
|
-
if (!(await t.value.write(x))) {
|
|
97
|
-
this.taps.remove(t);
|
|
98
|
-
}
|
|
99
|
-
t = t.next;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
for (let t of this.taps) {
|
|
103
|
-
await t.close();
|
|
104
|
-
}
|
|
105
|
-
delete this.src;
|
|
106
|
-
delete this.taps;
|
|
107
|
-
delete this.tapID;
|
|
99
|
+
for (let t of this.taps) {
|
|
100
|
+
await t.close();
|
|
108
101
|
}
|
|
102
|
+
delete this.src;
|
|
103
|
+
delete this.taps;
|
|
104
|
+
delete this.tapID;
|
|
105
|
+
}
|
|
109
106
|
}
|
|
107
|
+
export {
|
|
108
|
+
Mult
|
|
109
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/csp",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.82",
|
|
4
4
|
"description": "ES6 promise based CSP primitives & operations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -37,16 +39,16 @@
|
|
|
37
39
|
"testnode": "tsc -p test && node build/test/node.js"
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"@thi.ng/api": "^8.9.
|
|
41
|
-
"@thi.ng/arrays": "^2.7.
|
|
42
|
-
"@thi.ng/checks": "^3.4.
|
|
43
|
-
"@thi.ng/dcons": "^3.2.
|
|
44
|
-
"@thi.ng/errors": "^2.4.
|
|
45
|
-
"@thi.ng/transducers": "^8.8.
|
|
42
|
+
"@thi.ng/api": "^8.9.12",
|
|
43
|
+
"@thi.ng/arrays": "^2.7.8",
|
|
44
|
+
"@thi.ng/checks": "^3.4.12",
|
|
45
|
+
"@thi.ng/dcons": "^3.2.77",
|
|
46
|
+
"@thi.ng/errors": "^2.4.6",
|
|
47
|
+
"@thi.ng/transducers": "^8.8.15"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@microsoft/api-extractor": "^7.38.3",
|
|
49
|
-
"
|
|
51
|
+
"esbuild": "^0.19.8",
|
|
50
52
|
"rimraf": "^5.0.5",
|
|
51
53
|
"tools": "^0.0.1",
|
|
52
54
|
"typedoc": "^0.25.4",
|
|
@@ -102,5 +104,5 @@
|
|
|
102
104
|
"status": "deprecated",
|
|
103
105
|
"year": 2016
|
|
104
106
|
},
|
|
105
|
-
"gitHead": "
|
|
107
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
106
108
|
}
|
package/pubsub.js
CHANGED
|
@@ -1,92 +1,95 @@
|
|
|
1
1
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
2
2
|
import { Channel } from "./channel.js";
|
|
3
3
|
import { Mult } from "./mult.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
this.topics = {};
|
|
23
|
-
this.process();
|
|
4
|
+
class PubSub {
|
|
5
|
+
static NEXT_ID = 0;
|
|
6
|
+
src;
|
|
7
|
+
fn;
|
|
8
|
+
topics;
|
|
9
|
+
constructor(...args) {
|
|
10
|
+
switch (args.length) {
|
|
11
|
+
case 2:
|
|
12
|
+
this.src = args[0];
|
|
13
|
+
this.fn = args[1];
|
|
14
|
+
break;
|
|
15
|
+
case 1:
|
|
16
|
+
this.src = new Channel("pubsub" + PubSub.NEXT_ID++);
|
|
17
|
+
this.fn = args[0];
|
|
18
|
+
break;
|
|
19
|
+
default:
|
|
20
|
+
illegalArity(args.length);
|
|
24
21
|
}
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
this.topics = {};
|
|
23
|
+
this.process();
|
|
24
|
+
}
|
|
25
|
+
get id() {
|
|
26
|
+
return this.src && this.src.id;
|
|
27
|
+
}
|
|
28
|
+
set id(id) {
|
|
29
|
+
this.src && (this.src.id = id);
|
|
30
|
+
}
|
|
31
|
+
channel() {
|
|
32
|
+
return this.src;
|
|
33
|
+
}
|
|
34
|
+
write(val) {
|
|
35
|
+
if (this.src) {
|
|
36
|
+
return this.src.write(val);
|
|
27
37
|
}
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
return Promise.resolve(false);
|
|
39
|
+
}
|
|
40
|
+
close(flush = false) {
|
|
41
|
+
return this.src ? this.src.close(flush) : void 0;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new topic subscription channel and returns it.
|
|
45
|
+
* Each topic is managed by its own {@link Mult} and can have arbitrary
|
|
46
|
+
* number of subscribers. If the optional transducer is given, it will
|
|
47
|
+
* only be applied to the new subscription channel.
|
|
48
|
+
*
|
|
49
|
+
* The special "*" topic can be used to subscribe to all messages and
|
|
50
|
+
* acts as multiplexed pass-through of the source channel.
|
|
51
|
+
*
|
|
52
|
+
* @param id - topic id
|
|
53
|
+
* @param tx - transducer for new subscription
|
|
54
|
+
*/
|
|
55
|
+
sub(id, tx) {
|
|
56
|
+
let topic = this.topics[id];
|
|
57
|
+
if (!topic) {
|
|
58
|
+
this.topics[id] = topic = new Mult(this.src.id + "-" + id);
|
|
30
59
|
}
|
|
31
|
-
|
|
32
|
-
|
|
60
|
+
return topic.tap(tx);
|
|
61
|
+
}
|
|
62
|
+
unsub(id, ch) {
|
|
63
|
+
let topic = this.topics[id];
|
|
64
|
+
if (topic) {
|
|
65
|
+
return topic.untap(ch);
|
|
33
66
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
unsubAll(id, close = true) {
|
|
70
|
+
let topic = this.topics[id];
|
|
71
|
+
if (topic) {
|
|
72
|
+
return topic.untapAll(close);
|
|
39
73
|
}
|
|
40
|
-
|
|
41
|
-
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
async process() {
|
|
77
|
+
let x;
|
|
78
|
+
while ((x = null, x = await this.src.read()) !== void 0) {
|
|
79
|
+
const id = await this.fn(x);
|
|
80
|
+
let topic = this.topics[id];
|
|
81
|
+
topic && await topic.write(x);
|
|
82
|
+
topic = this.topics["*"];
|
|
83
|
+
topic && await topic.write(x);
|
|
42
84
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* Each topic is managed by its own {@link Mult} and can have arbitrary
|
|
46
|
-
* number of subscribers. If the optional transducer is given, it will
|
|
47
|
-
* only be applied to the new subscription channel.
|
|
48
|
-
*
|
|
49
|
-
* The special "*" topic can be used to subscribe to all messages and
|
|
50
|
-
* acts as multiplexed pass-through of the source channel.
|
|
51
|
-
*
|
|
52
|
-
* @param id - topic id
|
|
53
|
-
* @param tx - transducer for new subscription
|
|
54
|
-
*/
|
|
55
|
-
sub(id, tx) {
|
|
56
|
-
let topic = this.topics[id];
|
|
57
|
-
if (!topic) {
|
|
58
|
-
this.topics[id] = topic = new Mult(this.src.id + "-" + id);
|
|
59
|
-
}
|
|
60
|
-
return topic.tap(tx);
|
|
61
|
-
}
|
|
62
|
-
unsub(id, ch) {
|
|
63
|
-
let topic = this.topics[id];
|
|
64
|
-
if (topic) {
|
|
65
|
-
return topic.untap(ch);
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
unsubAll(id, close = true) {
|
|
70
|
-
let topic = this.topics[id];
|
|
71
|
-
if (topic) {
|
|
72
|
-
return topic.untapAll(close);
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
async process() {
|
|
77
|
-
let x;
|
|
78
|
-
while (((x = null), (x = await this.src.read())) !== undefined) {
|
|
79
|
-
const id = await this.fn(x);
|
|
80
|
-
let topic = this.topics[id];
|
|
81
|
-
topic && (await topic.write(x));
|
|
82
|
-
topic = this.topics["*"];
|
|
83
|
-
topic && (await topic.write(x));
|
|
84
|
-
}
|
|
85
|
-
for (let id of Object.keys(this.topics)) {
|
|
86
|
-
this.topics[id].close();
|
|
87
|
-
}
|
|
88
|
-
delete this.src;
|
|
89
|
-
delete this.topics;
|
|
90
|
-
delete this.fn;
|
|
85
|
+
for (let id of Object.keys(this.topics)) {
|
|
86
|
+
this.topics[id].close();
|
|
91
87
|
}
|
|
88
|
+
delete this.src;
|
|
89
|
+
delete this.topics;
|
|
90
|
+
delete this.fn;
|
|
91
|
+
}
|
|
92
92
|
}
|
|
93
|
+
export {
|
|
94
|
+
PubSub
|
|
95
|
+
};
|