-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolyfills.js
More file actions
74 lines (64 loc) · 1.89 KB
/
polyfills.js
File metadata and controls
74 lines (64 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// polyfills.js
// MessageChannel polyfill with full Web API compatibility
if (typeof global.MessageChannel === 'undefined') {
class MessagePort {
constructor() {
this.onmessage = null;
this.onmessageerror = null;
this._listeners = new Map();
this._otherPort = null;
}
postMessage(message) {
if (this._otherPort) {
setTimeout(() => {
const event = { data: message, type: 'message' };
// Call onmessage handler if set
if (this._otherPort.onmessage) {
this._otherPort.onmessage(event);
}
// Call addEventListener listeners
const listeners = this._otherPort._listeners.get('message') || [];
listeners.forEach((listener) => {
try {
listener(event);
} catch (error) {
console.error('MessagePort listener error:', error);
}
});
}, 0);
}
}
addEventListener(type, listener) {
if (!this._listeners.has(type)) {
this._listeners.set(type, []);
}
this._listeners.get(type).push(listener);
}
removeEventListener(type, listener) {
if (this._listeners.has(type)) {
const listeners = this._listeners.get(type);
const index = listeners.indexOf(listener);
if (index > -1) {
listeners.splice(index, 1);
}
}
}
start() {
// MessagePort.start() - required by spec but no-op in this implementation
}
close() {
// MessagePort.close() - required by spec
this._otherPort = null;
this._listeners.clear();
}
}
global.MessageChannel = class MessageChannel {
constructor() {
this.port1 = new MessagePort();
this.port2 = new MessagePort();
// Connect the ports to each other
this.port1._otherPort = this.port2;
this.port2._otherPort = this.port1;
}
};
}