iframe-pubsub 1.0.12 → 1.0.14
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/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -10
- package/dist/index.mjs +18 -10
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -27,6 +27,7 @@ declare class Client {
|
|
27
27
|
private callback?;
|
28
28
|
private pubsub;
|
29
29
|
private isIframe;
|
30
|
+
private boundHandleMessage;
|
30
31
|
/**
|
31
32
|
* Create a new client instance.
|
32
33
|
*
|
@@ -181,6 +182,7 @@ declare class PubSub {
|
|
181
182
|
private static instance;
|
182
183
|
private subscribers;
|
183
184
|
private mainCallback?;
|
185
|
+
private boundHandleMessage;
|
184
186
|
private constructor();
|
185
187
|
static getInstance(): PubSub;
|
186
188
|
onMessage(callback: MessageCallback): void;
|
package/dist/index.d.ts
CHANGED
@@ -27,6 +27,7 @@ declare class Client {
|
|
27
27
|
private callback?;
|
28
28
|
private pubsub;
|
29
29
|
private isIframe;
|
30
|
+
private boundHandleMessage;
|
30
31
|
/**
|
31
32
|
* Create a new client instance.
|
32
33
|
*
|
@@ -181,6 +182,7 @@ declare class PubSub {
|
|
181
182
|
private static instance;
|
182
183
|
private subscribers;
|
183
184
|
private mainCallback?;
|
185
|
+
private boundHandleMessage;
|
184
186
|
private constructor();
|
185
187
|
static getInstance(): PubSub;
|
186
188
|
onMessage(callback: MessageCallback): void;
|
package/dist/index.js
CHANGED
@@ -34,9 +34,13 @@ var _PubSub = class _PubSub {
|
|
34
34
|
constructor() {
|
35
35
|
__publicField(this, "subscribers");
|
36
36
|
__publicField(this, "mainCallback");
|
37
|
+
__publicField(this, "boundHandleMessage");
|
37
38
|
this.subscribers = /* @__PURE__ */ new Map();
|
38
|
-
this.
|
39
|
-
window.addEventListener("message", this.
|
39
|
+
this.boundHandleMessage = this.handleMessage.bind(this);
|
40
|
+
window.addEventListener("message", this.boundHandleMessage);
|
41
|
+
window.addEventListener("message", (event) => {
|
42
|
+
console.info("I am still live: ", event);
|
43
|
+
});
|
40
44
|
}
|
41
45
|
static getInstance() {
|
42
46
|
if (!_PubSub.instance) {
|
@@ -96,7 +100,8 @@ var _PubSub = class _PubSub {
|
|
96
100
|
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
97
101
|
}
|
98
102
|
}
|
99
|
-
|
103
|
+
handleMessage(event) {
|
104
|
+
console.log("Pubsub Received message:", event);
|
100
105
|
const data = event.data;
|
101
106
|
const source = event.source;
|
102
107
|
if (data?.type === "REGISTER") {
|
@@ -125,14 +130,14 @@ var _PubSub = class _PubSub {
|
|
125
130
|
if (!data || !data.from || !data.to) return;
|
126
131
|
const message = data;
|
127
132
|
if (this.mainCallback) {
|
128
|
-
|
133
|
+
this.mainCallback(message);
|
129
134
|
}
|
130
135
|
const subscriber = this.subscribers.get(message.to);
|
131
136
|
if (!subscriber) return;
|
132
137
|
if (subscriber.source) {
|
133
138
|
subscriber.source.postMessage(message, "*");
|
134
139
|
} else {
|
135
|
-
|
140
|
+
subscriber.callback(message);
|
136
141
|
}
|
137
142
|
}
|
138
143
|
/**
|
@@ -160,17 +165,19 @@ var Client = class {
|
|
160
165
|
__publicField(this, "callback");
|
161
166
|
__publicField(this, "pubsub");
|
162
167
|
__publicField(this, "isIframe");
|
168
|
+
__publicField(this, "boundHandleMessage");
|
163
169
|
this.pageId = pageId;
|
164
170
|
this.pubsub = PubSub.getInstance();
|
165
171
|
this.isIframe = window !== window.parent;
|
172
|
+
this.boundHandleMessage = this.handleMessage.bind(this);
|
166
173
|
if (this.isIframe) {
|
167
174
|
window.parent.postMessage({
|
168
175
|
type: "REGISTER",
|
169
176
|
pageId: this.pageId
|
170
177
|
}, "*");
|
171
|
-
window.addEventListener("message", this.
|
178
|
+
window.addEventListener("message", this.boundHandleMessage);
|
172
179
|
} else {
|
173
|
-
this.pubsub.register(pageId, this.
|
180
|
+
this.pubsub.register(pageId, this.boundHandleMessage);
|
174
181
|
}
|
175
182
|
}
|
176
183
|
/**
|
@@ -195,7 +202,7 @@ var Client = class {
|
|
195
202
|
if (this.isIframe) {
|
196
203
|
throw new Error("You are not allowed to clean up aichat from iframe.");
|
197
204
|
}
|
198
|
-
window.removeEventListener("message", this.
|
205
|
+
window.removeEventListener("message", this.boundHandleMessage);
|
199
206
|
return this.pubsub.unregister("aichat" /* AI_CHAT_CLIENT_ID */);
|
200
207
|
}
|
201
208
|
/**
|
@@ -290,7 +297,8 @@ var Client = class {
|
|
290
297
|
}
|
291
298
|
});
|
292
299
|
}
|
293
|
-
|
300
|
+
handleMessage(event) {
|
301
|
+
console.log("Client Received message:", event);
|
294
302
|
let message;
|
295
303
|
if (event.data) {
|
296
304
|
const evt = event;
|
@@ -301,7 +309,7 @@ var Client = class {
|
|
301
309
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
302
310
|
if (this.callback) {
|
303
311
|
try {
|
304
|
-
|
312
|
+
this.callback(message);
|
305
313
|
} catch (error) {
|
306
314
|
console.error(`Client ${this.pageId} failed to process message:`, error);
|
307
315
|
}
|
package/dist/index.mjs
CHANGED
@@ -7,9 +7,13 @@ var _PubSub = class _PubSub {
|
|
7
7
|
constructor() {
|
8
8
|
__publicField(this, "subscribers");
|
9
9
|
__publicField(this, "mainCallback");
|
10
|
+
__publicField(this, "boundHandleMessage");
|
10
11
|
this.subscribers = /* @__PURE__ */ new Map();
|
11
|
-
this.
|
12
|
-
window.addEventListener("message", this.
|
12
|
+
this.boundHandleMessage = this.handleMessage.bind(this);
|
13
|
+
window.addEventListener("message", this.boundHandleMessage);
|
14
|
+
window.addEventListener("message", (event) => {
|
15
|
+
console.info("I am still live: ", event);
|
16
|
+
});
|
13
17
|
}
|
14
18
|
static getInstance() {
|
15
19
|
if (!_PubSub.instance) {
|
@@ -69,7 +73,8 @@ var _PubSub = class _PubSub {
|
|
69
73
|
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
70
74
|
}
|
71
75
|
}
|
72
|
-
|
76
|
+
handleMessage(event) {
|
77
|
+
console.log("Pubsub Received message:", event);
|
73
78
|
const data = event.data;
|
74
79
|
const source = event.source;
|
75
80
|
if (data?.type === "REGISTER") {
|
@@ -98,14 +103,14 @@ var _PubSub = class _PubSub {
|
|
98
103
|
if (!data || !data.from || !data.to) return;
|
99
104
|
const message = data;
|
100
105
|
if (this.mainCallback) {
|
101
|
-
|
106
|
+
this.mainCallback(message);
|
102
107
|
}
|
103
108
|
const subscriber = this.subscribers.get(message.to);
|
104
109
|
if (!subscriber) return;
|
105
110
|
if (subscriber.source) {
|
106
111
|
subscriber.source.postMessage(message, "*");
|
107
112
|
} else {
|
108
|
-
|
113
|
+
subscriber.callback(message);
|
109
114
|
}
|
110
115
|
}
|
111
116
|
/**
|
@@ -133,17 +138,19 @@ var Client = class {
|
|
133
138
|
__publicField(this, "callback");
|
134
139
|
__publicField(this, "pubsub");
|
135
140
|
__publicField(this, "isIframe");
|
141
|
+
__publicField(this, "boundHandleMessage");
|
136
142
|
this.pageId = pageId;
|
137
143
|
this.pubsub = PubSub.getInstance();
|
138
144
|
this.isIframe = window !== window.parent;
|
145
|
+
this.boundHandleMessage = this.handleMessage.bind(this);
|
139
146
|
if (this.isIframe) {
|
140
147
|
window.parent.postMessage({
|
141
148
|
type: "REGISTER",
|
142
149
|
pageId: this.pageId
|
143
150
|
}, "*");
|
144
|
-
window.addEventListener("message", this.
|
151
|
+
window.addEventListener("message", this.boundHandleMessage);
|
145
152
|
} else {
|
146
|
-
this.pubsub.register(pageId, this.
|
153
|
+
this.pubsub.register(pageId, this.boundHandleMessage);
|
147
154
|
}
|
148
155
|
}
|
149
156
|
/**
|
@@ -168,7 +175,7 @@ var Client = class {
|
|
168
175
|
if (this.isIframe) {
|
169
176
|
throw new Error("You are not allowed to clean up aichat from iframe.");
|
170
177
|
}
|
171
|
-
window.removeEventListener("message", this.
|
178
|
+
window.removeEventListener("message", this.boundHandleMessage);
|
172
179
|
return this.pubsub.unregister("aichat" /* AI_CHAT_CLIENT_ID */);
|
173
180
|
}
|
174
181
|
/**
|
@@ -263,7 +270,8 @@ var Client = class {
|
|
263
270
|
}
|
264
271
|
});
|
265
272
|
}
|
266
|
-
|
273
|
+
handleMessage(event) {
|
274
|
+
console.log("Client Received message:", event);
|
267
275
|
let message;
|
268
276
|
if (event.data) {
|
269
277
|
const evt = event;
|
@@ -274,7 +282,7 @@ var Client = class {
|
|
274
282
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
275
283
|
if (this.callback) {
|
276
284
|
try {
|
277
|
-
|
285
|
+
this.callback(message);
|
278
286
|
} catch (error) {
|
279
287
|
console.error(`Client ${this.pageId} failed to process message:`, error);
|
280
288
|
}
|