@fireproof/core-protocols-cloud 0.0.0-smoke-1b31059-1752074105
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/LICENSE.md +232 -0
- package/http-connection.d.ts +25 -0
- package/http-connection.js +150 -0
- package/http-connection.js.map +1 -0
- package/http-connection.ts +204 -0
- package/index.d.ts +4 -0
- package/index.js +5 -0
- package/index.js.map +1 -0
- package/index.ts +4 -0
- package/msg-raw-connection-base.d.ts +15 -0
- package/msg-raw-connection-base.js +24 -0
- package/msg-raw-connection-base.js.map +1 -0
- package/msg-raw-connection-base.ts +38 -0
- package/msger.d.ts +84 -0
- package/msger.js +365 -0
- package/msger.js.map +1 -0
- package/msger.ts +551 -0
- package/package.json +45 -0
- package/tsconfig.json +18 -0
- package/ws-connection.d.ts +39 -0
- package/ws-connection.js +201 -0
- package/ws-connection.js.map +1 -0
- package/ws-connection.ts +270 -0
package/ws-connection.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { exception2Result, Future, Result, top_uint8 } from "@adviser/cement";
|
|
2
|
+
import { MsgIsError, buildErrorMsg, } from "@fireproof/core-types-protocols-cloud";
|
|
3
|
+
import { MsgRawConnectionBase } from "./msg-raw-connection-base.js";
|
|
4
|
+
import { ensureLogger } from "@fireproof/core-runtime";
|
|
5
|
+
class WaitForTids {
|
|
6
|
+
waitForTids = new Map();
|
|
7
|
+
start(sthis, logger, waitFor) {
|
|
8
|
+
let timeout = undefined;
|
|
9
|
+
if (typeof waitFor.timeout === "number" && waitFor.timeout > 0) {
|
|
10
|
+
timeout = setTimeout(() => {
|
|
11
|
+
this.waitForTids.delete(waitFor.tid);
|
|
12
|
+
waitFor.future.resolve(buildErrorMsg({ logger, sthis }, {
|
|
13
|
+
tid: waitFor.tid,
|
|
14
|
+
}, logger.Error().Any({ tid: waitFor }).Msg("Timeout").AsError()));
|
|
15
|
+
}, waitFor.timeout);
|
|
16
|
+
}
|
|
17
|
+
this.waitForTids.set(waitFor.tid, {
|
|
18
|
+
opts: waitFor,
|
|
19
|
+
timeout,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
stop(tid) {
|
|
23
|
+
const item = this.waitForTids.get(tid);
|
|
24
|
+
if (!item) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (item.timeout) {
|
|
28
|
+
clearTimeout(item.timeout);
|
|
29
|
+
}
|
|
30
|
+
this.waitForTids.delete(tid);
|
|
31
|
+
}
|
|
32
|
+
resolve(msg) {
|
|
33
|
+
const item = this.waitForTids.get(msg.tid);
|
|
34
|
+
if (!item) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
if (item.opts.waitFor(msg)) {
|
|
38
|
+
if (item.timeout) {
|
|
39
|
+
clearTimeout(item.timeout);
|
|
40
|
+
}
|
|
41
|
+
item.opts.future.resolve(msg);
|
|
42
|
+
}
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const DefaultRoundTripTime = 1000;
|
|
47
|
+
export class WSConnection extends MsgRawConnectionBase {
|
|
48
|
+
logger;
|
|
49
|
+
msgP;
|
|
50
|
+
ws;
|
|
51
|
+
#onMsg = new Map();
|
|
52
|
+
#onClose = new Map();
|
|
53
|
+
waitForTid = new WaitForTids();
|
|
54
|
+
id;
|
|
55
|
+
isReady = false;
|
|
56
|
+
constructor(sthis, ws, msgP, exGestalt) {
|
|
57
|
+
super(sthis, exGestalt);
|
|
58
|
+
this.id = sthis.nextId().str;
|
|
59
|
+
this.logger = ensureLogger(sthis, "WSConnection");
|
|
60
|
+
this.msgP = msgP;
|
|
61
|
+
this.ws = ws;
|
|
62
|
+
}
|
|
63
|
+
async start() {
|
|
64
|
+
const onOpenFuture = new Future();
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
const err = this.logger.Error().Dur("timeout", this.msgP.timeout).Msg("Timeout").AsError();
|
|
67
|
+
this.toMsg(buildErrorMsg(this, {}, err));
|
|
68
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
69
|
+
}, this.msgP.timeout);
|
|
70
|
+
this.ws.onopen = () => {
|
|
71
|
+
this.isReady = true;
|
|
72
|
+
onOpenFuture.resolve(Result.Ok(undefined));
|
|
73
|
+
};
|
|
74
|
+
this.ws.onerror = (ierr) => {
|
|
75
|
+
const err = this.logger.Error().Err(ierr).Msg("WS Error").AsError();
|
|
76
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
77
|
+
const res = this.buildErrorMsg(this, {}, err);
|
|
78
|
+
this.toMsg(res);
|
|
79
|
+
};
|
|
80
|
+
this.ws.onmessage = (evt) => {
|
|
81
|
+
if (!this.isReady) {
|
|
82
|
+
this.toMsg(buildErrorMsg(this, {}, this.logger.Error().Msg("Received message before onOpen").AsError()));
|
|
83
|
+
}
|
|
84
|
+
this.#wsOnMessage(evt);
|
|
85
|
+
};
|
|
86
|
+
this.ws.onclose = () => {
|
|
87
|
+
this.isReady = false;
|
|
88
|
+
this.close().catch((ierr) => {
|
|
89
|
+
const err = this.logger.Error().Err(ierr).Msg("close error").AsError();
|
|
90
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
91
|
+
this.toMsg(buildErrorMsg(this, { tid: "internal" }, err));
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
const rOpen = await onOpenFuture.asPromise().finally(() => {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
});
|
|
97
|
+
if (rOpen.isErr()) {
|
|
98
|
+
return rOpen;
|
|
99
|
+
}
|
|
100
|
+
return Result.Ok(undefined);
|
|
101
|
+
}
|
|
102
|
+
#wsOnMessage = async (event) => {
|
|
103
|
+
const rMsg = await exception2Result(async () => {
|
|
104
|
+
const msg = this.msgP.ende.decode(await top_uint8(event.data));
|
|
105
|
+
return msg;
|
|
106
|
+
});
|
|
107
|
+
if (rMsg.isErr()) {
|
|
108
|
+
this.logger.Error().Err(rMsg).Any({ event }).Msg("Invalid message");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const msg = rMsg.Ok();
|
|
112
|
+
this.waitForTid.resolve(msg);
|
|
113
|
+
Array.from(this.#onMsg.values()).forEach((cb) => {
|
|
114
|
+
cb(msg);
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
async close() {
|
|
118
|
+
this.#onClose.forEach((fn) => fn());
|
|
119
|
+
this.#onClose.clear();
|
|
120
|
+
this.#onMsg.clear();
|
|
121
|
+
this.ws.close();
|
|
122
|
+
return Result.Ok(undefined);
|
|
123
|
+
}
|
|
124
|
+
toMsg(msg) {
|
|
125
|
+
this.#onMsg.forEach((fn) => fn(msg));
|
|
126
|
+
return msg;
|
|
127
|
+
}
|
|
128
|
+
send(msg) {
|
|
129
|
+
this.ws.send(this.msgP.ende.encode(msg));
|
|
130
|
+
return Promise.resolve(msg);
|
|
131
|
+
}
|
|
132
|
+
onMsg(fn) {
|
|
133
|
+
const key = this.sthis.nextId().str;
|
|
134
|
+
this.#onMsg.set(key, fn);
|
|
135
|
+
return () => this.#onMsg.delete(key);
|
|
136
|
+
}
|
|
137
|
+
onClose(fn) {
|
|
138
|
+
const key = this.sthis.nextId().str;
|
|
139
|
+
this.#onClose.set(key, fn);
|
|
140
|
+
return () => this.#onClose.delete(key);
|
|
141
|
+
}
|
|
142
|
+
activeBinds = new Map();
|
|
143
|
+
bind(req, opts) {
|
|
144
|
+
const state = {
|
|
145
|
+
id: this.sthis.nextId().str,
|
|
146
|
+
bind: {
|
|
147
|
+
msg: req,
|
|
148
|
+
opts,
|
|
149
|
+
},
|
|
150
|
+
controller: undefined,
|
|
151
|
+
};
|
|
152
|
+
this.activeBinds.set(state.id, state);
|
|
153
|
+
const ret = new ReadableStream({
|
|
154
|
+
cancel: () => {
|
|
155
|
+
this.activeBinds.delete(state.id);
|
|
156
|
+
},
|
|
157
|
+
start: (controller) => {
|
|
158
|
+
state.controller = controller;
|
|
159
|
+
this.onMsg((msg) => {
|
|
160
|
+
try {
|
|
161
|
+
if (MsgIsError(msg)) {
|
|
162
|
+
controller.enqueue(msg);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!opts.waitFor) {
|
|
166
|
+
controller.enqueue(msg);
|
|
167
|
+
}
|
|
168
|
+
else if (opts.waitFor(msg)) {
|
|
169
|
+
controller.enqueue(msg);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
this.logger.Error().Err(err).Any({ msg }).Msg("Error in onMsg callback[ignored]");
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
this.send(req);
|
|
177
|
+
const future = new Future();
|
|
178
|
+
this.waitForTid.start(this.sthis, this.logger, { tid: req.tid, future, waitFor: opts.waitFor });
|
|
179
|
+
future.asPromise().then((msg) => {
|
|
180
|
+
if (MsgIsError(msg)) {
|
|
181
|
+
controller.enqueue(msg);
|
|
182
|
+
controller.close();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
return ret;
|
|
188
|
+
}
|
|
189
|
+
async request(req, opts) {
|
|
190
|
+
const future = new Future();
|
|
191
|
+
this.waitForTid.start(this.sthis, this.logger, {
|
|
192
|
+
tid: req.tid,
|
|
193
|
+
future,
|
|
194
|
+
waitFor: opts.waitFor,
|
|
195
|
+
timeout: opts.timeout ?? DefaultRoundTripTime,
|
|
196
|
+
});
|
|
197
|
+
await this.send(req);
|
|
198
|
+
return future.asPromise();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=ws-connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-connection.js","sourceRoot":"","sources":["ws-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAU,MAAM,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACtF,OAAO,EAEL,UAAU,EACV,aAAa,GAMd,MAAM,uCAAuC,CAAC;AAE/C,OAAO,EAAE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AAC;oBACrB,CAAC;gBACH,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;QACH,OAAO,GAAG,CAAC;IACb,CAAC;IAED,KAAK,CAAC,OAAO,CAAuC,GAAM,EAAE,IAAiB;QAC3E,MAAM,MAAM,GAAG,IAAI,MAAM,EAAK,CAAC;QAC/B,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE;YAC7C,GAAG,EAAE,GAAG,CAAC,GAAG;YACZ,MAAM;YACN,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,OAAO,EAAE,IAAI,CAAC,OAAO,IAAI,oBAAoB;SAC9C,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC,SAAS,EAAE,CAAC;IAC5B,CAAC;CAMF"}
|
package/ws-connection.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { exception2Result, Future, Logger, Result, top_uint8 } from "@adviser/cement";
|
|
2
|
+
import {
|
|
3
|
+
MsgBase,
|
|
4
|
+
MsgIsError,
|
|
5
|
+
buildErrorMsg,
|
|
6
|
+
ReqOpen,
|
|
7
|
+
WaitForTid,
|
|
8
|
+
MsgWithError,
|
|
9
|
+
RequestOpts,
|
|
10
|
+
MsgRawConnection,
|
|
11
|
+
} from "@fireproof/core-types-protocols-cloud";
|
|
12
|
+
import { ActiveStream, ExchangedGestalt, MsgerParamsWithEnDe, OnMsgFn, UnReg } from "./msger.js";
|
|
13
|
+
import { MsgRawConnectionBase } from "./msg-raw-connection-base.js";
|
|
14
|
+
import { SuperThis } from "@fireproof/core-types-base";
|
|
15
|
+
import { ensureLogger } from "@fireproof/core-runtime";
|
|
16
|
+
|
|
17
|
+
export interface WSReqOpen {
|
|
18
|
+
readonly reqOpen: ReqOpen;
|
|
19
|
+
readonly ws: WebSocket; // this WS is opened with a specific URL-Param
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface WaitForTidItem {
|
|
23
|
+
readonly opts: WaitForTid;
|
|
24
|
+
readonly timeout?: ReturnType<typeof setTimeout>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class WaitForTids {
|
|
28
|
+
readonly waitForTids = new Map<string, WaitForTidItem>();
|
|
29
|
+
|
|
30
|
+
start(sthis: SuperThis, logger: Logger, waitFor: WaitForTid) {
|
|
31
|
+
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
|
32
|
+
if (typeof waitFor.timeout === "number" && waitFor.timeout > 0) {
|
|
33
|
+
timeout = setTimeout(() => {
|
|
34
|
+
this.waitForTids.delete(waitFor.tid);
|
|
35
|
+
waitFor.future.resolve(
|
|
36
|
+
buildErrorMsg(
|
|
37
|
+
{ logger, sthis },
|
|
38
|
+
{
|
|
39
|
+
tid: waitFor.tid,
|
|
40
|
+
} as MsgBase,
|
|
41
|
+
logger.Error().Any({ tid: waitFor }).Msg("Timeout").AsError(),
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
}, waitFor.timeout);
|
|
45
|
+
}
|
|
46
|
+
// console.log("waitForTids", waitFor.tid, waitFor.timeout);
|
|
47
|
+
this.waitForTids.set(waitFor.tid, {
|
|
48
|
+
opts: waitFor,
|
|
49
|
+
timeout,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
stop(tid: string) {
|
|
54
|
+
const item = this.waitForTids.get(tid);
|
|
55
|
+
if (!item) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (item.timeout) {
|
|
59
|
+
clearTimeout(item.timeout);
|
|
60
|
+
}
|
|
61
|
+
this.waitForTids.delete(tid);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
resolve(msg: MsgBase): WaitForTidItem | undefined {
|
|
65
|
+
const item = this.waitForTids.get(msg.tid);
|
|
66
|
+
if (!item) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
if (item.opts.waitFor(msg)) {
|
|
70
|
+
if (item.timeout) {
|
|
71
|
+
clearTimeout(item.timeout);
|
|
72
|
+
}
|
|
73
|
+
item.opts.future.resolve(msg);
|
|
74
|
+
}
|
|
75
|
+
return item;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const DefaultRoundTripTime = 1000;
|
|
80
|
+
|
|
81
|
+
export class WSConnection extends MsgRawConnectionBase implements MsgRawConnection {
|
|
82
|
+
readonly logger: Logger;
|
|
83
|
+
readonly msgP: MsgerParamsWithEnDe;
|
|
84
|
+
readonly ws: WebSocket;
|
|
85
|
+
// readonly baseURI: URI;
|
|
86
|
+
|
|
87
|
+
readonly #onMsg = new Map<string, OnMsgFn>();
|
|
88
|
+
readonly #onClose = new Map<string, UnReg>();
|
|
89
|
+
|
|
90
|
+
readonly waitForTid = new WaitForTids();
|
|
91
|
+
|
|
92
|
+
readonly id: string;
|
|
93
|
+
|
|
94
|
+
isReady = false;
|
|
95
|
+
|
|
96
|
+
constructor(sthis: SuperThis, ws: WebSocket, msgP: MsgerParamsWithEnDe, exGestalt: ExchangedGestalt) {
|
|
97
|
+
super(sthis, exGestalt);
|
|
98
|
+
this.id = sthis.nextId().str;
|
|
99
|
+
this.logger = ensureLogger(sthis, "WSConnection");
|
|
100
|
+
this.msgP = msgP;
|
|
101
|
+
this.ws = ws;
|
|
102
|
+
// this.wqs = { ...wsq };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async start(): Promise<Result<void>> {
|
|
106
|
+
const onOpenFuture: Future<Result<unknown>> = new Future<Result<unknown>>();
|
|
107
|
+
const timer = setTimeout(() => {
|
|
108
|
+
const err = this.logger.Error().Dur("timeout", this.msgP.timeout).Msg("Timeout").AsError();
|
|
109
|
+
this.toMsg(buildErrorMsg(this, {} as MsgBase, err));
|
|
110
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
111
|
+
}, this.msgP.timeout);
|
|
112
|
+
|
|
113
|
+
this.ws.onopen = () => {
|
|
114
|
+
this.isReady = true;
|
|
115
|
+
onOpenFuture.resolve(Result.Ok(undefined));
|
|
116
|
+
};
|
|
117
|
+
this.ws.onerror = (ierr) => {
|
|
118
|
+
// console.log("onerror", this.id, ierr);
|
|
119
|
+
const err = this.logger.Error().Err(ierr).Msg("WS Error").AsError();
|
|
120
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
121
|
+
const res = this.buildErrorMsg(this, {}, err);
|
|
122
|
+
this.toMsg(res);
|
|
123
|
+
};
|
|
124
|
+
this.ws.onmessage = (evt) => {
|
|
125
|
+
if (!this.isReady) {
|
|
126
|
+
this.toMsg(buildErrorMsg(this, {} as MsgBase, this.logger.Error().Msg("Received message before onOpen").AsError()));
|
|
127
|
+
}
|
|
128
|
+
this.#wsOnMessage(evt);
|
|
129
|
+
};
|
|
130
|
+
this.ws.onclose = () => {
|
|
131
|
+
this.isReady = false;
|
|
132
|
+
// console.log("onclose", this.id);
|
|
133
|
+
this.close().catch((ierr) => {
|
|
134
|
+
const err = this.logger.Error().Err(ierr).Msg("close error").AsError();
|
|
135
|
+
onOpenFuture.resolve(Result.Err(err));
|
|
136
|
+
this.toMsg(buildErrorMsg(this, { tid: "internal" } as MsgBase, err));
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
/* wait for onOpen */
|
|
140
|
+
const rOpen = await onOpenFuture.asPromise().finally(() => {
|
|
141
|
+
clearTimeout(timer);
|
|
142
|
+
});
|
|
143
|
+
if (rOpen.isErr()) {
|
|
144
|
+
return rOpen;
|
|
145
|
+
}
|
|
146
|
+
// const resOpen = await this.request(this.wqs.reqOpen, { waitFor: MsgIsResOpen });
|
|
147
|
+
// if (!MsgIsResOpen(resOpen)) {
|
|
148
|
+
// return Result.Err(this.logger.Error().Any("ErrMsg", resOpen).Msg("Invalid response").AsError());
|
|
149
|
+
// }
|
|
150
|
+
// this.wqs.resOpen = resOpen;
|
|
151
|
+
return Result.Ok(undefined);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
readonly #wsOnMessage = async (event: MessageEvent) => {
|
|
155
|
+
const rMsg = await exception2Result(async () => {
|
|
156
|
+
const msg = this.msgP.ende.decode(await top_uint8(event.data)) as MsgBase;
|
|
157
|
+
return msg;
|
|
158
|
+
});
|
|
159
|
+
if (rMsg.isErr()) {
|
|
160
|
+
this.logger.Error().Err(rMsg).Any({ event }).Msg("Invalid message");
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const msg = rMsg.Ok();
|
|
164
|
+
// console.log("wsOnMessage", this.id, msg);
|
|
165
|
+
this.waitForTid.resolve(msg);
|
|
166
|
+
// console.log("wsOnMessage", msg, this.#onMsg.size);
|
|
167
|
+
Array.from(this.#onMsg.values()).forEach((cb) => {
|
|
168
|
+
// console.log("cb-onmessage", this.id, msg, cb.toString());
|
|
169
|
+
cb(msg);
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
async close(): Promise<Result<void>> {
|
|
174
|
+
this.#onClose.forEach((fn) => fn());
|
|
175
|
+
this.#onClose.clear();
|
|
176
|
+
this.#onMsg.clear();
|
|
177
|
+
// console.lows.id, ion", this.id, new Error().stack);
|
|
178
|
+
this.ws.close();
|
|
179
|
+
return Result.Ok(undefined);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
toMsg<S extends MsgBase>(msg: MsgWithError<S>): MsgWithError<S> {
|
|
183
|
+
this.#onMsg.forEach((fn) => fn(msg));
|
|
184
|
+
return msg;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
send<Q extends MsgBase, S extends MsgBase>(msg: Q): Promise<S> {
|
|
188
|
+
// console.log("send", msg);
|
|
189
|
+
this.ws.send(this.msgP.ende.encode(msg));
|
|
190
|
+
return Promise.resolve(msg as unknown as S);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
onMsg<S extends MsgBase>(fn: OnMsgFn<S>): UnReg {
|
|
194
|
+
const key = this.sthis.nextId().str;
|
|
195
|
+
this.#onMsg.set(key, fn as OnMsgFn);
|
|
196
|
+
return () => this.#onMsg.delete(key);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
onClose(fn: UnReg): UnReg {
|
|
200
|
+
const key = this.sthis.nextId().str;
|
|
201
|
+
this.#onClose.set(key, fn);
|
|
202
|
+
return () => this.#onClose.delete(key);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
readonly activeBinds = new Map<string, ActiveStream>();
|
|
206
|
+
bind<Q extends MsgBase, S extends MsgBase>(req: Q, opts: RequestOpts): ReadableStream<MsgWithError<S>> {
|
|
207
|
+
const state: ActiveStream = {
|
|
208
|
+
id: this.sthis.nextId().str,
|
|
209
|
+
bind: {
|
|
210
|
+
msg: req,
|
|
211
|
+
opts,
|
|
212
|
+
},
|
|
213
|
+
// timeout: undefined,
|
|
214
|
+
controller: undefined,
|
|
215
|
+
} satisfies ActiveStream;
|
|
216
|
+
this.activeBinds.set(state.id, state);
|
|
217
|
+
const ret = new ReadableStream<MsgWithError<S>>({
|
|
218
|
+
cancel: () => {
|
|
219
|
+
// clearTimeout(state.timeout as number);
|
|
220
|
+
this.activeBinds.delete(state.id);
|
|
221
|
+
},
|
|
222
|
+
start: (controller) => {
|
|
223
|
+
state.controller = controller; // set controller in ActiveStream
|
|
224
|
+
this.onMsg<S>((msg) => {
|
|
225
|
+
try {
|
|
226
|
+
if (MsgIsError(msg)) {
|
|
227
|
+
controller.enqueue(msg);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!opts.waitFor) {
|
|
231
|
+
controller.enqueue(msg);
|
|
232
|
+
} else if (opts.waitFor(msg)) {
|
|
233
|
+
controller.enqueue(msg);
|
|
234
|
+
}
|
|
235
|
+
} catch (err) {
|
|
236
|
+
this.logger.Error().Err(err).Any({ msg }).Msg("Error in onMsg callback[ignored]");
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
this.send(req);
|
|
240
|
+
const future = new Future<S>();
|
|
241
|
+
this.waitForTid.start(this.sthis, this.logger, { tid: req.tid, future, waitFor: opts.waitFor });
|
|
242
|
+
future.asPromise().then((msg) => {
|
|
243
|
+
if (MsgIsError(msg)) {
|
|
244
|
+
// double err emitting
|
|
245
|
+
controller.enqueue(msg);
|
|
246
|
+
controller.close();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
return ret;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async request<Q extends MsgBase, S extends MsgBase>(req: Q, opts: RequestOpts): Promise<MsgWithError<S>> {
|
|
255
|
+
const future = new Future<S>();
|
|
256
|
+
this.waitForTid.start(this.sthis, this.logger, {
|
|
257
|
+
tid: req.tid,
|
|
258
|
+
future,
|
|
259
|
+
waitFor: opts.waitFor,
|
|
260
|
+
timeout: opts.timeout ?? DefaultRoundTripTime,
|
|
261
|
+
});
|
|
262
|
+
await this.send(req);
|
|
263
|
+
return future.asPromise();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// toOnMessage<T extends MsgBase>(msg: WithErrorMsg<T>): Result<WithErrorMsg<T>> {
|
|
267
|
+
// this.mec.msgFn?.(msg as unknown as MessageEvent<MsgBase>);
|
|
268
|
+
// return Result.Ok(msg);
|
|
269
|
+
// }
|
|
270
|
+
}
|