@mtcute/web 0.25.0 → 0.25.2
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/package.json +2 -2
- package/worker.cjs +47 -42
- package/worker.d.cts +2 -1
- package/worker.d.ts +2 -1
- package/worker.js +47 -42
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mtcute/web",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.25.
|
|
4
|
+
"version": "0.25.2",
|
|
5
5
|
"description": "Meta-package for the web platform",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@mtcute/core": "^0.25.
|
|
8
|
+
"@mtcute/core": "^0.25.2",
|
|
9
9
|
"@mtcute/wasm": "^0.24.3",
|
|
10
10
|
"@fuman/net": "0.0.15"
|
|
11
11
|
},
|
package/worker.cjs
CHANGED
|
@@ -7,57 +7,61 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
|
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
8
|
const worker_js = require("@mtcute/core/worker.js");
|
|
9
9
|
const platform$1 = require("./platform.cjs");
|
|
10
|
-
let
|
|
10
|
+
let _broadcast;
|
|
11
|
+
const _sharedHandlers = /* @__PURE__ */ new Set();
|
|
11
12
|
class TelegramWorker extends worker_js.TelegramWorker {
|
|
12
13
|
registerWorker(handler) {
|
|
13
|
-
if (_registered) {
|
|
14
|
-
throw new Error("TelegramWorker must be created only once");
|
|
15
|
-
}
|
|
16
|
-
_registered = true;
|
|
17
14
|
if (typeof SharedWorkerGlobalScope !== "undefined" && self instanceof SharedWorkerGlobalScope) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
port
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
self.onconnect = (event) => {
|
|
25
|
-
const port = event.ports[0];
|
|
26
|
-
connections.push(port);
|
|
27
|
-
const respond = port.postMessage.bind(port);
|
|
28
|
-
const onClose = () => {
|
|
29
|
-
port.close();
|
|
30
|
-
const idx = connections.indexOf(port);
|
|
31
|
-
if (idx >= 0) {
|
|
32
|
-
connections.splice(connections.indexOf(port), 1);
|
|
15
|
+
if (!_broadcast) {
|
|
16
|
+
const connections = [];
|
|
17
|
+
const broadcast = (message) => {
|
|
18
|
+
for (const port of connections) {
|
|
19
|
+
port.postMessage(message);
|
|
33
20
|
}
|
|
34
21
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
self.onconnect = (event) => {
|
|
23
|
+
const port = event.ports[0];
|
|
24
|
+
connections.push(port);
|
|
25
|
+
const respond = port.postMessage.bind(port);
|
|
26
|
+
const onClose = () => {
|
|
27
|
+
port.close();
|
|
28
|
+
const idx = connections.indexOf(port);
|
|
29
|
+
if (idx >= 0) {
|
|
30
|
+
connections.splice(connections.indexOf(port), 1);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const onTimeout = () => {
|
|
34
|
+
console.warn("some connection timed out!");
|
|
35
|
+
respond({ __type__: "timeout" });
|
|
43
36
|
onClose();
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
37
|
+
};
|
|
38
|
+
let timeout = setTimeout(onTimeout, 6e4);
|
|
39
|
+
port.addEventListener("message", (message) => {
|
|
40
|
+
if (message.data.__type__ === "close") {
|
|
41
|
+
onClose();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (message.data.__type__ === "ping") {
|
|
45
|
+
clearTimeout(timeout);
|
|
46
|
+
timeout = setTimeout(onTimeout, 6e4);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
for (const handler2 of _sharedHandlers) {
|
|
50
|
+
handler2(message.data, respond);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
port.start();
|
|
54
|
+
};
|
|
55
|
+
_broadcast = broadcast;
|
|
56
|
+
}
|
|
57
|
+
_sharedHandlers.add(handler);
|
|
58
|
+
return [_broadcast, () => _sharedHandlers.delete(handler)];
|
|
56
59
|
}
|
|
57
60
|
if (typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) {
|
|
58
61
|
const respond = self.postMessage.bind(self);
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
const messageHandler = (message) => handler(message.data, respond);
|
|
63
|
+
self.addEventListener("message", messageHandler);
|
|
64
|
+
return [respond, () => self.removeEventListener("message", messageHandler)];
|
|
61
65
|
}
|
|
62
66
|
throw new Error("TelegramWorker must be created from a worker");
|
|
63
67
|
}
|
|
@@ -67,6 +71,7 @@ class TelegramWorkerPort extends worker_js.TelegramWorkerPort {
|
|
|
67
71
|
constructor(options) {
|
|
68
72
|
super({
|
|
69
73
|
worker: options.worker,
|
|
74
|
+
workerId: options.workerId,
|
|
70
75
|
platform
|
|
71
76
|
});
|
|
72
77
|
}
|
package/worker.d.cts
CHANGED
|
@@ -2,9 +2,10 @@ import { ClientMessageHandler, RespondFn, SendFn, SomeWorker, TelegramWorkerOpti
|
|
|
2
2
|
export type { TelegramWorkerOptions, WorkerCustomMethods };
|
|
3
3
|
export interface TelegramWorkerPortOptions {
|
|
4
4
|
worker: SomeWorker;
|
|
5
|
+
workerId?: string;
|
|
5
6
|
}
|
|
6
7
|
export declare class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorkerBase<T> {
|
|
7
|
-
registerWorker(handler: WorkerMessageHandler): RespondFn;
|
|
8
|
+
registerWorker(handler: WorkerMessageHandler): [RespondFn, VoidFunction];
|
|
8
9
|
}
|
|
9
10
|
export declare class TelegramWorkerPort<T extends WorkerCustomMethods> extends TelegramWorkerPortBase<T> {
|
|
10
11
|
constructor(options: TelegramWorkerPortOptions);
|
package/worker.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ import { ClientMessageHandler, RespondFn, SendFn, SomeWorker, TelegramWorkerOpti
|
|
|
2
2
|
export type { TelegramWorkerOptions, WorkerCustomMethods };
|
|
3
3
|
export interface TelegramWorkerPortOptions {
|
|
4
4
|
worker: SomeWorker;
|
|
5
|
+
workerId?: string;
|
|
5
6
|
}
|
|
6
7
|
export declare class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorkerBase<T> {
|
|
7
|
-
registerWorker(handler: WorkerMessageHandler): RespondFn;
|
|
8
|
+
registerWorker(handler: WorkerMessageHandler): [RespondFn, VoidFunction];
|
|
8
9
|
}
|
|
9
10
|
export declare class TelegramWorkerPort<T extends WorkerCustomMethods> extends TelegramWorkerPortBase<T> {
|
|
10
11
|
constructor(options: TelegramWorkerPortOptions);
|
package/worker.js
CHANGED
|
@@ -1,56 +1,60 @@
|
|
|
1
1
|
import { TelegramWorker as TelegramWorker$1, TelegramWorkerPort as TelegramWorkerPort$1 } from "@mtcute/core/worker.js";
|
|
2
2
|
import { WebPlatform } from "./platform.js";
|
|
3
|
-
let
|
|
3
|
+
let _broadcast;
|
|
4
|
+
const _sharedHandlers = /* @__PURE__ */ new Set();
|
|
4
5
|
class TelegramWorker extends TelegramWorker$1 {
|
|
5
6
|
registerWorker(handler) {
|
|
6
|
-
if (_registered) {
|
|
7
|
-
throw new Error("TelegramWorker must be created only once");
|
|
8
|
-
}
|
|
9
|
-
_registered = true;
|
|
10
7
|
if (typeof SharedWorkerGlobalScope !== "undefined" && self instanceof SharedWorkerGlobalScope) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
port
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
self.onconnect = (event) => {
|
|
18
|
-
const port = event.ports[0];
|
|
19
|
-
connections.push(port);
|
|
20
|
-
const respond = port.postMessage.bind(port);
|
|
21
|
-
const onClose = () => {
|
|
22
|
-
port.close();
|
|
23
|
-
const idx = connections.indexOf(port);
|
|
24
|
-
if (idx >= 0) {
|
|
25
|
-
connections.splice(connections.indexOf(port), 1);
|
|
8
|
+
if (!_broadcast) {
|
|
9
|
+
const connections = [];
|
|
10
|
+
const broadcast = (message) => {
|
|
11
|
+
for (const port of connections) {
|
|
12
|
+
port.postMessage(message);
|
|
26
13
|
}
|
|
27
14
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
15
|
+
self.onconnect = (event) => {
|
|
16
|
+
const port = event.ports[0];
|
|
17
|
+
connections.push(port);
|
|
18
|
+
const respond = port.postMessage.bind(port);
|
|
19
|
+
const onClose = () => {
|
|
20
|
+
port.close();
|
|
21
|
+
const idx = connections.indexOf(port);
|
|
22
|
+
if (idx >= 0) {
|
|
23
|
+
connections.splice(connections.indexOf(port), 1);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const onTimeout = () => {
|
|
27
|
+
console.warn("some connection timed out!");
|
|
28
|
+
respond({ __type__: "timeout" });
|
|
36
29
|
onClose();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
};
|
|
31
|
+
let timeout = setTimeout(onTimeout, 6e4);
|
|
32
|
+
port.addEventListener("message", (message) => {
|
|
33
|
+
if (message.data.__type__ === "close") {
|
|
34
|
+
onClose();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (message.data.__type__ === "ping") {
|
|
38
|
+
clearTimeout(timeout);
|
|
39
|
+
timeout = setTimeout(onTimeout, 6e4);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
for (const handler2 of _sharedHandlers) {
|
|
43
|
+
handler2(message.data, respond);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
port.start();
|
|
47
|
+
};
|
|
48
|
+
_broadcast = broadcast;
|
|
49
|
+
}
|
|
50
|
+
_sharedHandlers.add(handler);
|
|
51
|
+
return [_broadcast, () => _sharedHandlers.delete(handler)];
|
|
49
52
|
}
|
|
50
53
|
if (typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) {
|
|
51
54
|
const respond = self.postMessage.bind(self);
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
const messageHandler = (message) => handler(message.data, respond);
|
|
56
|
+
self.addEventListener("message", messageHandler);
|
|
57
|
+
return [respond, () => self.removeEventListener("message", messageHandler)];
|
|
54
58
|
}
|
|
55
59
|
throw new Error("TelegramWorker must be created from a worker");
|
|
56
60
|
}
|
|
@@ -60,6 +64,7 @@ class TelegramWorkerPort extends TelegramWorkerPort$1 {
|
|
|
60
64
|
constructor(options) {
|
|
61
65
|
super({
|
|
62
66
|
worker: options.worker,
|
|
67
|
+
workerId: options.workerId,
|
|
63
68
|
platform
|
|
64
69
|
});
|
|
65
70
|
}
|