postalworker-es 4.0.8 → 5.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/README.md +152 -35
- package/dist/PostalSharedWorker.js +305 -513
- package/dist/PostalSharedWorker.js.map +7 -0
- package/dist/PostalWorker.min.js +2 -171
- package/dist/PostalWorker.min.js.map +7 -0
- package/dist/compat.d.ts +53 -0
- package/dist/core.d.ts +104 -0
- package/dist/cross.d.ts +32 -0
- package/dist/emitter.d.ts +15 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +860 -0
- package/dist/index.js.map +7 -0
- package/dist/protocol.d.ts +125 -0
- package/dist/types.d.ts +77 -0
- package/dist/worker-script.d.ts +44 -0
- package/dist/worker-script.js +8 -0
- package/dist/worker-script.js.map +7 -0
- package/package.json +47 -71
- package/dist/PolyfillSharedWorker.js +0 -525
- package/dist/PostalWorker.es.js +0 -408
|
@@ -1,529 +1,321 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const LOAD = "LOAD";
|
|
15
|
-
const POBOX = "POBOX";
|
|
16
|
-
const POST = "POST";
|
|
17
|
-
const COLLECT = "COLLECT";
|
|
18
|
-
const CLOSEBOX = "CLOSEBOX";
|
|
19
|
-
const BOXCLOSED = "BOXCLOSED";
|
|
20
|
-
const PACKAGE = "PACKAGE";
|
|
21
|
-
const PACK = "PACK";
|
|
22
|
-
const DELIVERY = "DELIVERY";
|
|
23
|
-
|
|
24
|
-
var hasProp = Object.prototype.hasOwnProperty;
|
|
25
|
-
|
|
26
|
-
function throwsMessage(err) {
|
|
27
|
-
return '[Throws: ' + (err ? err.message : '?') + ']';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function safeGetValueFromPropertyOnObject(obj, property) {
|
|
31
|
-
if (hasProp.call(obj, property)) {
|
|
32
|
-
try {
|
|
33
|
-
return obj[property];
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
return throwsMessage(err);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return obj[property];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function ensureProperties(obj) {
|
|
44
|
-
var seen = [ ]; // store references to objects we have seen before
|
|
45
|
-
|
|
46
|
-
function visit(obj) {
|
|
47
|
-
if (obj === null || typeof obj !== 'object') {
|
|
48
|
-
return obj;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (seen.indexOf(obj) !== -1) {
|
|
52
|
-
return '[Circular]';
|
|
53
|
-
}
|
|
54
|
-
seen.push(obj);
|
|
55
|
-
|
|
56
|
-
if (typeof obj.toJSON === 'function') {
|
|
57
|
-
try {
|
|
58
|
-
var fResult = visit(obj.toJSON());
|
|
59
|
-
seen.pop();
|
|
60
|
-
return fResult;
|
|
61
|
-
} catch(err) {
|
|
62
|
-
return throwsMessage(err);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (Array.isArray(obj)) {
|
|
67
|
-
var aResult = obj.map(visit);
|
|
68
|
-
seen.pop();
|
|
69
|
-
return aResult;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
var result = Object.keys(obj).reduce(function(result, prop) {
|
|
73
|
-
// prevent faulty defined getter properties
|
|
74
|
-
result[prop] = visit(safeGetValueFromPropertyOnObject(obj, prop));
|
|
75
|
-
return result;
|
|
76
|
-
}, {});
|
|
77
|
-
seen.pop();
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return visit(obj);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var index = function(data, replacer, space) {
|
|
85
|
-
return JSON.stringify(ensureProperties(data), replacer, space);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
var ensureProperties_1 = ensureProperties;
|
|
89
|
-
|
|
90
|
-
index.ensureProperties = ensureProperties_1;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Postal Worker - Shared Worker Thread Engine
|
|
94
|
-
* @Description: Use web worker threading to fork threads and use postMessage to pass serialized data between threads and windows
|
|
95
|
-
* @Author: Russ Stratfull
|
|
96
|
-
* @Contributors: Sakshi Dheer, & Francois Wauquier
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
const _env = {
|
|
100
|
-
_id: "SharedWorker",
|
|
101
|
-
_members: {},
|
|
102
|
-
_stringify: index,
|
|
103
|
-
_events: new Map(),
|
|
104
|
-
_scripts: new Map(),
|
|
105
|
-
_channel: new BroadcastChannel(POSTAL_WORKER),
|
|
106
|
-
_subscriptions: new Map(),
|
|
107
|
-
_postBoxes: {},
|
|
108
|
-
_packages: {},
|
|
109
|
-
PostalWorker: {}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Load a library (script) into the worker
|
|
114
|
-
* @param {*} msg
|
|
115
|
-
*/
|
|
116
|
-
function _loadLibrary(msg) {
|
|
117
|
-
let scrpt = _env._scripts.get(msg.data);
|
|
118
|
-
// if script already exists, don't reload it
|
|
119
|
-
if (scrpt) {
|
|
120
|
-
// just add window id to it
|
|
121
|
-
if (!scrpt.includes(msg.id)) scrpt.push(msg.id);
|
|
122
|
-
} else {
|
|
123
|
-
try {
|
|
124
|
-
// Thank you chatGpt for this one
|
|
125
|
-
// Create a closure and importScripts in the closure
|
|
126
|
-
// this allows binding the imported script to the current scope
|
|
127
|
-
// and PostalWorker can be passed securely to it, a little like
|
|
128
|
-
// import in es modules
|
|
129
|
-
const closure = (() => {
|
|
130
|
-
importScripts(msg.data);
|
|
131
|
-
return () => {
|
|
132
|
-
return {
|
|
133
|
-
usePrivate: init
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
})();
|
|
137
|
-
const importedScript = closure();
|
|
138
|
-
// This is the magic
|
|
139
|
-
importedScript.usePrivate(PostalWorker);
|
|
140
|
-
|
|
141
|
-
_env._scripts.set(msg.data, [msg.id]);
|
|
142
|
-
} catch (e) {
|
|
143
|
-
console.error(e);
|
|
144
|
-
_env._channel.postMessage(
|
|
145
|
-
_env._stringify({
|
|
146
|
-
postal: true,
|
|
147
|
-
type: "ERROR",
|
|
148
|
-
data: `PostalWorkerWorker Error: Unable to load file ${msg.data}`
|
|
149
|
-
})
|
|
150
|
-
);
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
// src/emitter.ts
|
|
4
|
+
var Registry = class {
|
|
5
|
+
#handlers = /* @__PURE__ */ new Map();
|
|
6
|
+
add(key, handler) {
|
|
7
|
+
let set = this.#handlers.get(key);
|
|
8
|
+
if (!set) {
|
|
9
|
+
set = /* @__PURE__ */ new Set();
|
|
10
|
+
this.#handlers.set(key, set);
|
|
11
|
+
}
|
|
12
|
+
set.add(handler);
|
|
13
|
+
return () => this.remove(key, handler);
|
|
151
14
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
*
|
|
180
|
-
* @param {*} msg
|
|
181
|
-
* @param {Function} callback - optional callback
|
|
182
|
-
*/
|
|
183
|
-
function _registerPObox(msg, callback) {
|
|
184
|
-
const { id, address } = msg;
|
|
185
|
-
let addr = _env._postBoxes[address];
|
|
186
|
-
if (addr) {
|
|
187
|
-
if (!addr.members.includes(id)) addr.members.push(id);
|
|
188
|
-
if (!addr.handlers.length) addr.handlers.push(callback);
|
|
189
|
-
} else {
|
|
190
|
-
_env._postBoxes[address] = {
|
|
191
|
-
members: [id],
|
|
192
|
-
handlers: callback ? [callback] : [],
|
|
193
|
-
value: null
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
198
|
-
store[current[0]] = current[1].value;
|
|
199
|
-
return store;
|
|
200
|
-
}, {});
|
|
201
|
-
|
|
202
|
-
PostalWorker().fire(COLLECT, {
|
|
203
|
-
address: address,
|
|
204
|
-
value: _env._postBoxes[address].value,
|
|
205
|
-
box
|
|
206
|
-
});
|
|
207
|
-
// Is there a worker handler for this item?
|
|
208
|
-
for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
209
|
-
if (key === address && bx.handlers.length) {
|
|
210
|
-
bx.handlers.forEach(handler => {
|
|
211
|
-
if (handler) {
|
|
212
|
-
handler({ address, value: _env._postBoxes[address].value, box });
|
|
15
|
+
remove(key, handler) {
|
|
16
|
+
if (!handler) {
|
|
17
|
+
this.#handlers.delete(key);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const set = this.#handlers.get(key);
|
|
21
|
+
if (!set) return;
|
|
22
|
+
set.delete(handler);
|
|
23
|
+
if (set.size === 0) this.#handlers.delete(key);
|
|
24
|
+
}
|
|
25
|
+
has(key) {
|
|
26
|
+
return this.#handlers.has(key);
|
|
27
|
+
}
|
|
28
|
+
keys() {
|
|
29
|
+
return [...this.#handlers.keys()];
|
|
30
|
+
}
|
|
31
|
+
clear() {
|
|
32
|
+
this.#handlers.clear();
|
|
33
|
+
}
|
|
34
|
+
emit(key, ...args) {
|
|
35
|
+
const set = this.#handlers.get(key);
|
|
36
|
+
if (!set) return;
|
|
37
|
+
for (const handler of [...set]) {
|
|
38
|
+
try {
|
|
39
|
+
handler(...args);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
reportHandlerError(key, error);
|
|
213
42
|
}
|
|
214
|
-
}
|
|
43
|
+
}
|
|
215
44
|
}
|
|
45
|
+
};
|
|
46
|
+
function reportHandlerError(key, error) {
|
|
47
|
+
queueMicrotask(() => {
|
|
48
|
+
throw error instanceof Error ? Object.assign(error, { message: `PostalWorker handler for "${key}" threw: ${error.message}` }) : error;
|
|
49
|
+
});
|
|
216
50
|
}
|
|
217
|
-
}
|
|
218
51
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*/
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
52
|
+
// src/protocol.ts
|
|
53
|
+
var PROTOCOL = 5;
|
|
54
|
+
var WORKER_ID = "worker";
|
|
55
|
+
var CHANNEL_KINDS = /* @__PURE__ */ new Set([
|
|
56
|
+
"fire",
|
|
57
|
+
"hello",
|
|
58
|
+
"worker-ready",
|
|
59
|
+
"join",
|
|
60
|
+
"leave",
|
|
61
|
+
"box-open",
|
|
62
|
+
"box-post",
|
|
63
|
+
"box-close",
|
|
64
|
+
"box-update",
|
|
65
|
+
"box-closed",
|
|
66
|
+
"package-open",
|
|
67
|
+
"package-pack",
|
|
68
|
+
"package-update",
|
|
69
|
+
"load",
|
|
70
|
+
"error"
|
|
71
|
+
]);
|
|
72
|
+
function isEnvelope(data) {
|
|
73
|
+
if (typeof data !== "object" || data === null) return false;
|
|
74
|
+
const d = data;
|
|
75
|
+
return d["__postal"] === PROTOCOL && typeof d["from"] === "string" && typeof d["kind"] === "string";
|
|
240
76
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
function
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (!addr.members.length) {
|
|
254
|
-
delete _env._postBoxes[address];
|
|
255
|
-
}
|
|
256
|
-
const value = null;
|
|
257
|
-
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
258
|
-
store[current[0]] = current[1].value;
|
|
259
|
-
return store;
|
|
260
|
-
}, {});
|
|
261
|
-
PostalWorker().fire(BOXCLOSED, { id, address, value, box });
|
|
77
|
+
function isChannelMsg(data) {
|
|
78
|
+
return isEnvelope(data) && CHANNEL_KINDS.has(data.kind);
|
|
79
|
+
}
|
|
80
|
+
function envelope(from, body) {
|
|
81
|
+
return { __postal: PROTOCOL, from, ...body };
|
|
82
|
+
}
|
|
83
|
+
function reachesWorker(audience) {
|
|
84
|
+
if (audience === "all" || audience === "others" || audience === "worker") return true;
|
|
85
|
+
return audience.to === WORKER_ID;
|
|
86
|
+
}
|
|
87
|
+
function assertNever(x) {
|
|
88
|
+
throw new Error(`PostalWorker: unexpected value ${JSON.stringify(x)}`);
|
|
262
89
|
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
*
|
|
267
|
-
* @param {*} msg
|
|
268
|
-
* @param {*} callback
|
|
269
|
-
*/
|
|
270
|
-
function _preparePackage(msg, callback) {
|
|
271
|
-
const { id, address } = msg;
|
|
272
|
-
let pack = _env._packages[address];
|
|
273
|
-
if (pack) {
|
|
274
|
-
if (!pack.members.includes(id)) pack.members.push(id);
|
|
275
|
-
if (!pack.handlers.length) pack.handlers.push(callback);
|
|
276
|
-
} else
|
|
277
|
-
_env._packages[address] = {
|
|
278
|
-
members: [id],
|
|
279
|
-
handlers: callback ? [callback] : [],
|
|
280
|
-
content: null
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
if (_env._packages[address].content)
|
|
284
|
-
PostalWorker().fire(DELIVERY, {
|
|
285
|
-
address: address,
|
|
286
|
-
content: _env._packages[address].content
|
|
287
|
-
});
|
|
288
|
-
else
|
|
289
|
-
PostalWorker().fire(DELIVERY, {
|
|
290
|
-
address: address,
|
|
291
|
-
content: null
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
*
|
|
297
|
-
* @param {*} msg
|
|
298
|
-
*/
|
|
299
|
-
function _pack(msg) {
|
|
300
|
-
const { address, content } = msg;
|
|
301
|
-
let addr = _env._packages[address];
|
|
302
|
-
if (addr) addr.content = content;
|
|
303
|
-
|
|
304
|
-
PostalWorker().fire(DELIVERY, { address, content });
|
|
305
|
-
|
|
306
|
-
// for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
307
|
-
// if (key === address && bx.handlers.length) {
|
|
308
|
-
// bx.handlers.forEach(handler => {
|
|
309
|
-
// if (handler) {
|
|
310
|
-
// handler({ address, value: _env._postBoxes[address].value, box });
|
|
311
|
-
// }
|
|
312
|
-
// });
|
|
313
|
-
// }
|
|
314
|
-
// }
|
|
315
90
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
91
|
+
// src/worker/hub.ts
|
|
92
|
+
var Hub = class {
|
|
93
|
+
instance = randomId();
|
|
94
|
+
#channel;
|
|
95
|
+
#events = new Registry();
|
|
96
|
+
#boxes = /* @__PURE__ */ new Map();
|
|
97
|
+
#packages = /* @__PURE__ */ new Map();
|
|
98
|
+
#loaded = /* @__PURE__ */ new Set();
|
|
99
|
+
#members = /* @__PURE__ */ new Set();
|
|
100
|
+
#streams = /* @__PURE__ */ new Map();
|
|
101
|
+
api;
|
|
102
|
+
constructor(channelName) {
|
|
103
|
+
this.#channel = new BroadcastChannel(channelName);
|
|
104
|
+
this.#channel.addEventListener("message", (e) => this.#handle(e.data));
|
|
105
|
+
this.api = this.#createApi();
|
|
106
|
+
}
|
|
107
|
+
/** Tell every tab this worker instance is up. */
|
|
108
|
+
announce() {
|
|
109
|
+
this.#post({ kind: "worker-ready", instance: this.instance });
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
#handle(data) {
|
|
113
|
+
if (!isChannelMsg(data) || data.from === WORKER_ID) return;
|
|
114
|
+
const from = data.from;
|
|
115
|
+
switch (data.kind) {
|
|
116
|
+
case "hello":
|
|
117
|
+
this.#members.add(from);
|
|
118
|
+
this.announce();
|
|
119
|
+
return;
|
|
120
|
+
case "join":
|
|
121
|
+
this.#members.add(from);
|
|
122
|
+
return;
|
|
123
|
+
case "leave":
|
|
124
|
+
this.#members.delete(from);
|
|
125
|
+
for (const [address, box] of this.#boxes) {
|
|
126
|
+
box.members.delete(from);
|
|
127
|
+
this.#dropIfUnused(address, box);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
case "fire":
|
|
131
|
+
if (reachesWorker(data.audience)) this.#events.emit(data.msgClass, data.message, { from });
|
|
132
|
+
return;
|
|
133
|
+
case "box-open": {
|
|
134
|
+
this.#members.add(from);
|
|
135
|
+
const box = this.#box(data.address);
|
|
136
|
+
box.members.add(from);
|
|
137
|
+
if (data.seed && (box.value === null || box.value === void 0) && data.seed.value !== null) {
|
|
138
|
+
this.#setBox(data.address, box, data.seed.value);
|
|
139
|
+
}
|
|
140
|
+
this.#post({ kind: "box-update", address: data.address, value: box.value, box: this.#snapshot(), to: from });
|
|
141
|
+
return;
|
|
321
142
|
}
|
|
322
|
-
|
|
143
|
+
case "box-post":
|
|
144
|
+
this.#setBox(data.address, this.#box(data.address), data.value);
|
|
145
|
+
return;
|
|
146
|
+
case "box-close": {
|
|
147
|
+
const box = this.#boxes.get(data.address);
|
|
148
|
+
if (box) {
|
|
149
|
+
box.members.delete(from);
|
|
150
|
+
this.#dropIfUnused(data.address, box);
|
|
151
|
+
}
|
|
152
|
+
this.#post({ kind: "box-closed", address: data.address, box: this.#snapshot(), to: from });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
case "package-open": {
|
|
156
|
+
const pkg = this.#package(data.address);
|
|
157
|
+
this.#post({ kind: "package-update", address: data.address, content: pkg.content, to: from });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
case "package-pack":
|
|
161
|
+
this.#setPackage(data.address, data.content);
|
|
162
|
+
return;
|
|
163
|
+
case "load":
|
|
164
|
+
this.#load(data.url, from);
|
|
165
|
+
return;
|
|
166
|
+
// messages meant for tabs
|
|
167
|
+
case "worker-ready":
|
|
168
|
+
case "box-update":
|
|
169
|
+
case "box-closed":
|
|
170
|
+
case "package-update":
|
|
171
|
+
case "error":
|
|
172
|
+
return;
|
|
173
|
+
default:
|
|
174
|
+
assertNever(data);
|
|
175
|
+
}
|
|
323
176
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
* @param {*} msg
|
|
330
|
-
*/
|
|
331
|
-
function _processChannelMessage(msg) {
|
|
332
|
-
// console.info(msg);
|
|
333
|
-
if (!msg.postal) return;
|
|
334
|
-
// console.info(msg);
|
|
335
|
-
switch (msg.type) {
|
|
336
|
-
case FIRE:
|
|
337
|
-
if (_env._events.has(msg.data.msgClass)) {
|
|
338
|
-
_env._events.get(msg.data.msgClass)(msg.data.message, msg.id);
|
|
177
|
+
#box(address) {
|
|
178
|
+
let box = this.#boxes.get(address);
|
|
179
|
+
if (!box) {
|
|
180
|
+
box = { members: /* @__PURE__ */ new Set(), value: null, handlers: /* @__PURE__ */ new Set() };
|
|
181
|
+
this.#boxes.set(address, box);
|
|
339
182
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
un: msgClass => {
|
|
382
|
-
_env._events.delete(msgClass);
|
|
383
|
-
},
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
*
|
|
387
|
-
* @param msgClass
|
|
388
|
-
* @param msg
|
|
389
|
-
*/
|
|
390
|
-
fire: (msgClass, msg) => {
|
|
391
|
-
_env._channel.postMessage(
|
|
392
|
-
_env._stringify({
|
|
393
|
-
postal: true,
|
|
394
|
-
type: FIRE,
|
|
395
|
-
id: POSTAL_SHARED_WORKER,
|
|
396
|
-
data: {
|
|
397
|
-
msgClass: msgClass,
|
|
398
|
-
message: msg
|
|
183
|
+
return box;
|
|
184
|
+
}
|
|
185
|
+
#setBox(address, box, value) {
|
|
186
|
+
box.value = value;
|
|
187
|
+
const snapshot = this.#snapshot();
|
|
188
|
+
this.#post({ kind: "box-update", address, value, box: snapshot });
|
|
189
|
+
for (const h of [...box.handlers]) safeCall(() => h({ address, value, box: snapshot, closed: false }));
|
|
190
|
+
}
|
|
191
|
+
/** Forget a box nobody uses (no tab has it open and no worker handler). Values posted to unopened boxes are kept. */
|
|
192
|
+
#dropIfUnused(address, box) {
|
|
193
|
+
if (box.members.size === 0 && box.handlers.size === 0) this.#boxes.delete(address);
|
|
194
|
+
}
|
|
195
|
+
#snapshot() {
|
|
196
|
+
const out = {};
|
|
197
|
+
for (const [address, box] of this.#boxes) out[address] = box.value;
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
#package(address) {
|
|
201
|
+
let pkg = this.#packages.get(address);
|
|
202
|
+
if (!pkg) {
|
|
203
|
+
pkg = { content: null, handlers: /* @__PURE__ */ new Set() };
|
|
204
|
+
this.#packages.set(address, pkg);
|
|
205
|
+
}
|
|
206
|
+
return pkg;
|
|
207
|
+
}
|
|
208
|
+
#setPackage(address, content) {
|
|
209
|
+
const pkg = this.#package(address);
|
|
210
|
+
pkg.content = content;
|
|
211
|
+
this.#post({ kind: "package-update", address, content });
|
|
212
|
+
for (const h of [...pkg.handlers]) safeCall(() => h({ address, content }));
|
|
213
|
+
}
|
|
214
|
+
#load(url, from) {
|
|
215
|
+
if (this.#loaded.has(url)) return;
|
|
216
|
+
const scope2 = globalThis;
|
|
217
|
+
delete scope2.init;
|
|
218
|
+
try {
|
|
219
|
+
importScripts(url);
|
|
220
|
+
const init = scope2.init;
|
|
221
|
+
delete scope2.init;
|
|
222
|
+
if (typeof init !== "function") {
|
|
223
|
+
throw new Error(`${url} did not define self.init = (pw) => { \u2026 }`);
|
|
399
224
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
// message: msg,
|
|
409
|
-
// audience: audience
|
|
410
|
-
// },
|
|
411
|
-
// target
|
|
412
|
-
// );
|
|
413
|
-
},
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
*
|
|
417
|
-
* @param {*} library
|
|
418
|
-
* @returns
|
|
419
|
-
*/
|
|
420
|
-
load: library => _loadLibrary(library),
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Subscribe to Server Sent Events and create event source
|
|
424
|
-
* @param {*} endpoint
|
|
425
|
-
* @param {*} callback
|
|
426
|
-
* @param {*} errorCallback
|
|
427
|
-
*/
|
|
428
|
-
subscribe: (endpoint, callback, errorCallback) => {
|
|
429
|
-
if (!_env._subscriptions.get(endpoint)) {
|
|
430
|
-
_env._subscriptions.set(endpoint, new EventSource(endpoint));
|
|
431
|
-
_env._subscriptions.get(endpoint).onmessage = msg => {
|
|
432
|
-
callback(msg);
|
|
433
|
-
};
|
|
434
|
-
if (errorCallback) {
|
|
435
|
-
_env._subscriptions.get(endpoint).onerror = error => {
|
|
436
|
-
errorCallback(error);
|
|
437
|
-
};
|
|
225
|
+
this.#loaded.add(url);
|
|
226
|
+
init(this.api);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
this.#post({
|
|
229
|
+
kind: "error",
|
|
230
|
+
message: `PostalWorker: could not load ${url} into the worker: ${error instanceof Error ? error.message : String(error)}`,
|
|
231
|
+
to: from
|
|
232
|
+
});
|
|
438
233
|
}
|
|
439
234
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
{
|
|
445
|
-
id:
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
235
|
+
#post(body) {
|
|
236
|
+
this.#channel.postMessage(envelope(WORKER_ID, body));
|
|
237
|
+
}
|
|
238
|
+
#createApi() {
|
|
239
|
+
const api = {
|
|
240
|
+
id: WORKER_ID,
|
|
241
|
+
on: (msgClass, handler) => this.#events.add(msgClass, handler),
|
|
242
|
+
off: (msgClass, handler) => this.#events.remove(msgClass, handler),
|
|
243
|
+
fire: (msgClass, message, audience = "all") => {
|
|
244
|
+
if (audience === "all" || audience.to === WORKER_ID) {
|
|
245
|
+
queueMicrotask(() => this.#events.emit(msgClass, message, { from: WORKER_ID }));
|
|
246
|
+
}
|
|
247
|
+
if (typeof audience === "object" && audience.to === WORKER_ID) return;
|
|
248
|
+
this.#post({ kind: "fire", msgClass, message, audience });
|
|
249
|
+
},
|
|
250
|
+
postBox: (address, handler) => {
|
|
251
|
+
const box = this.#box(address);
|
|
252
|
+
const h = handler;
|
|
253
|
+
box.handlers.add(h);
|
|
254
|
+
queueMicrotask(() => safeCall(() => h({ address, value: box.value, box: this.#snapshot(), closed: false })));
|
|
255
|
+
return () => {
|
|
256
|
+
box.handlers.delete(h);
|
|
257
|
+
this.#dropIfUnused(address, box);
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
post: (address, value) => this.#setBox(address, this.#box(address), value),
|
|
261
|
+
closeBox: (address) => {
|
|
262
|
+
const box = this.#boxes.get(address);
|
|
263
|
+
if (!box) return;
|
|
264
|
+
const handlers = [...box.handlers];
|
|
265
|
+
box.handlers.clear();
|
|
266
|
+
this.#dropIfUnused(address, box);
|
|
267
|
+
const snapshot = this.#snapshot();
|
|
268
|
+
for (const h of handlers) safeCall(() => h({ address, value: null, box: snapshot, closed: true }));
|
|
269
|
+
},
|
|
270
|
+
package: (address, handler) => {
|
|
271
|
+
const pkg = this.#package(address);
|
|
272
|
+
const h = handler;
|
|
273
|
+
pkg.handlers.add(h);
|
|
274
|
+
queueMicrotask(() => safeCall(() => h({ address, content: pkg.content })));
|
|
275
|
+
return () => pkg.handlers.delete(h);
|
|
276
|
+
},
|
|
277
|
+
pack: (address, content) => this.#setPackage(address, content),
|
|
278
|
+
subscribe: (url, onMessage, onError) => {
|
|
279
|
+
let stream = this.#streams.get(url);
|
|
280
|
+
if (!stream) {
|
|
281
|
+
stream = new EventSource(url);
|
|
282
|
+
this.#streams.set(url, stream);
|
|
283
|
+
}
|
|
284
|
+
const s = stream;
|
|
285
|
+
const msg = (e) => onMessage(e);
|
|
286
|
+
s.addEventListener("message", msg);
|
|
287
|
+
const err = onError ? (e) => onError(e) : void 0;
|
|
288
|
+
if (err) s.addEventListener("error", err);
|
|
289
|
+
return () => {
|
|
290
|
+
s.removeEventListener("message", msg);
|
|
291
|
+
if (err) s.removeEventListener("error", err);
|
|
292
|
+
};
|
|
293
|
+
},
|
|
294
|
+
members: () => [...this.#members]
|
|
496
295
|
};
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
onconnect = () => {
|
|
513
|
-
// New messaging method using broadcast channel
|
|
514
|
-
if (!_env._channel.onmessage)
|
|
515
|
-
_env._channel.onmessage = event => {
|
|
516
|
-
const data = JSON.parse(event.data);
|
|
517
|
-
_processChannelMessage(data);
|
|
518
|
-
};
|
|
519
|
-
|
|
520
|
-
// Announce alive to broadcast channel
|
|
521
|
-
_env._channel.postMessage(
|
|
522
|
-
_env._stringify({
|
|
523
|
-
postal: true,
|
|
524
|
-
type: WORKERREGISTER
|
|
525
|
-
})
|
|
526
|
-
);
|
|
527
|
-
};
|
|
296
|
+
return Object.assign(() => api, api);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
function safeCall(fn) {
|
|
300
|
+
try {
|
|
301
|
+
fn();
|
|
302
|
+
} catch (error) {
|
|
303
|
+
queueMicrotask(() => {
|
|
304
|
+
throw error;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function randomId() {
|
|
309
|
+
return typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
310
|
+
}
|
|
528
311
|
|
|
529
|
-
|
|
312
|
+
// src/worker/index.ts
|
|
313
|
+
var scope = self;
|
|
314
|
+
var hub = new Hub(scope.name || "PostalWorker");
|
|
315
|
+
scope.onconnect = (e) => {
|
|
316
|
+
e.ports[0]?.start();
|
|
317
|
+
hub.announce();
|
|
318
|
+
};
|
|
319
|
+
hub.announce();
|
|
320
|
+
})();
|
|
321
|
+
//# sourceMappingURL=PostalSharedWorker.js.map
|