iframe-pubsub 1.0.11 → 1.0.13
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 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +11 -7
- package/dist/index.mjs +11 -7
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -42,7 +42,7 @@ declare class Client {
|
|
42
42
|
*
|
43
43
|
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
44
44
|
*/
|
45
|
-
cleanAIChat():
|
45
|
+
cleanAIChat(): boolean;
|
46
46
|
/**
|
47
47
|
* Listen for messages from the parent page with a callback.
|
48
48
|
*
|
@@ -197,7 +197,7 @@ declare class PubSub {
|
|
197
197
|
*
|
198
198
|
* @param pageId The ID of the page or component to unregister from.
|
199
199
|
*/
|
200
|
-
unregister(pageId: string):
|
200
|
+
unregister(pageId: string): boolean;
|
201
201
|
sendMessage(message: IMessage): void;
|
202
202
|
/**
|
203
203
|
* Try to send a message to a client with retry logic.
|
package/dist/index.d.ts
CHANGED
@@ -42,7 +42,7 @@ declare class Client {
|
|
42
42
|
*
|
43
43
|
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
44
44
|
*/
|
45
|
-
cleanAIChat():
|
45
|
+
cleanAIChat(): boolean;
|
46
46
|
/**
|
47
47
|
* Listen for messages from the parent page with a callback.
|
48
48
|
*
|
@@ -197,7 +197,7 @@ declare class PubSub {
|
|
197
197
|
*
|
198
198
|
* @param pageId The ID of the page or component to unregister from.
|
199
199
|
*/
|
200
|
-
unregister(pageId: string):
|
200
|
+
unregister(pageId: string): boolean;
|
201
201
|
sendMessage(message: IMessage): void;
|
202
202
|
/**
|
203
203
|
* Try to send a message to a client with retry logic.
|
package/dist/index.js
CHANGED
@@ -63,7 +63,7 @@ var _PubSub = class _PubSub {
|
|
63
63
|
* @param pageId The ID of the page or component to unregister from.
|
64
64
|
*/
|
65
65
|
unregister(pageId) {
|
66
|
-
this.subscribers.delete(pageId);
|
66
|
+
return this.subscribers.delete(pageId);
|
67
67
|
}
|
68
68
|
sendMessage(message) {
|
69
69
|
if (this.mainCallback) {
|
@@ -96,7 +96,8 @@ var _PubSub = class _PubSub {
|
|
96
96
|
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
97
97
|
}
|
98
98
|
}
|
99
|
-
|
99
|
+
handleMessage(event) {
|
100
|
+
console.log("Pubsub Received message:", event);
|
100
101
|
const data = event.data;
|
101
102
|
const source = event.source;
|
102
103
|
if (data?.type === "REGISTER") {
|
@@ -125,14 +126,14 @@ var _PubSub = class _PubSub {
|
|
125
126
|
if (!data || !data.from || !data.to) return;
|
126
127
|
const message = data;
|
127
128
|
if (this.mainCallback) {
|
128
|
-
|
129
|
+
this.mainCallback(message);
|
129
130
|
}
|
130
131
|
const subscriber = this.subscribers.get(message.to);
|
131
132
|
if (!subscriber) return;
|
132
133
|
if (subscriber.source) {
|
133
134
|
subscriber.source.postMessage(message, "*");
|
134
135
|
} else {
|
135
|
-
|
136
|
+
subscriber.callback(message);
|
136
137
|
}
|
137
138
|
}
|
138
139
|
/**
|
@@ -195,7 +196,8 @@ var Client = class {
|
|
195
196
|
if (this.isIframe) {
|
196
197
|
throw new Error("You are not allowed to clean up aichat from iframe.");
|
197
198
|
}
|
198
|
-
this.
|
199
|
+
window.removeEventListener("message", this.handleMessage.bind(this));
|
200
|
+
return this.pubsub.unregister("aichat" /* AI_CHAT_CLIENT_ID */);
|
199
201
|
}
|
200
202
|
/**
|
201
203
|
* Listen for messages from the parent page with a callback.
|
@@ -203,6 +205,7 @@ var Client = class {
|
|
203
205
|
* @param callback The callback function to register.
|
204
206
|
*/
|
205
207
|
onMessage(callback) {
|
208
|
+
console.info("Register new onMessage callback for", this.pageId, callback);
|
206
209
|
this.callback = callback;
|
207
210
|
}
|
208
211
|
/**
|
@@ -288,7 +291,8 @@ var Client = class {
|
|
288
291
|
}
|
289
292
|
});
|
290
293
|
}
|
291
|
-
|
294
|
+
handleMessage(event) {
|
295
|
+
console.log("Client Received message:", event);
|
292
296
|
let message;
|
293
297
|
if (event.data) {
|
294
298
|
const evt = event;
|
@@ -299,7 +303,7 @@ var Client = class {
|
|
299
303
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
300
304
|
if (this.callback) {
|
301
305
|
try {
|
302
|
-
|
306
|
+
this.callback(message);
|
303
307
|
} catch (error) {
|
304
308
|
console.error(`Client ${this.pageId} failed to process message:`, error);
|
305
309
|
}
|
package/dist/index.mjs
CHANGED
@@ -36,7 +36,7 @@ var _PubSub = class _PubSub {
|
|
36
36
|
* @param pageId The ID of the page or component to unregister from.
|
37
37
|
*/
|
38
38
|
unregister(pageId) {
|
39
|
-
this.subscribers.delete(pageId);
|
39
|
+
return this.subscribers.delete(pageId);
|
40
40
|
}
|
41
41
|
sendMessage(message) {
|
42
42
|
if (this.mainCallback) {
|
@@ -69,7 +69,8 @@ var _PubSub = class _PubSub {
|
|
69
69
|
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
70
70
|
}
|
71
71
|
}
|
72
|
-
|
72
|
+
handleMessage(event) {
|
73
|
+
console.log("Pubsub Received message:", event);
|
73
74
|
const data = event.data;
|
74
75
|
const source = event.source;
|
75
76
|
if (data?.type === "REGISTER") {
|
@@ -98,14 +99,14 @@ var _PubSub = class _PubSub {
|
|
98
99
|
if (!data || !data.from || !data.to) return;
|
99
100
|
const message = data;
|
100
101
|
if (this.mainCallback) {
|
101
|
-
|
102
|
+
this.mainCallback(message);
|
102
103
|
}
|
103
104
|
const subscriber = this.subscribers.get(message.to);
|
104
105
|
if (!subscriber) return;
|
105
106
|
if (subscriber.source) {
|
106
107
|
subscriber.source.postMessage(message, "*");
|
107
108
|
} else {
|
108
|
-
|
109
|
+
subscriber.callback(message);
|
109
110
|
}
|
110
111
|
}
|
111
112
|
/**
|
@@ -168,7 +169,8 @@ var Client = class {
|
|
168
169
|
if (this.isIframe) {
|
169
170
|
throw new Error("You are not allowed to clean up aichat from iframe.");
|
170
171
|
}
|
171
|
-
this.
|
172
|
+
window.removeEventListener("message", this.handleMessage.bind(this));
|
173
|
+
return this.pubsub.unregister("aichat" /* AI_CHAT_CLIENT_ID */);
|
172
174
|
}
|
173
175
|
/**
|
174
176
|
* Listen for messages from the parent page with a callback.
|
@@ -176,6 +178,7 @@ var Client = class {
|
|
176
178
|
* @param callback The callback function to register.
|
177
179
|
*/
|
178
180
|
onMessage(callback) {
|
181
|
+
console.info("Register new onMessage callback for", this.pageId, callback);
|
179
182
|
this.callback = callback;
|
180
183
|
}
|
181
184
|
/**
|
@@ -261,7 +264,8 @@ var Client = class {
|
|
261
264
|
}
|
262
265
|
});
|
263
266
|
}
|
264
|
-
|
267
|
+
handleMessage(event) {
|
268
|
+
console.log("Client Received message:", event);
|
265
269
|
let message;
|
266
270
|
if (event.data) {
|
267
271
|
const evt = event;
|
@@ -272,7 +276,7 @@ var Client = class {
|
|
272
276
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
273
277
|
if (this.callback) {
|
274
278
|
try {
|
275
|
-
|
279
|
+
this.callback(message);
|
276
280
|
} catch (error) {
|
277
281
|
console.error(`Client ${this.pageId} failed to process message:`, error);
|
278
282
|
}
|