iframe-pubsub 1.0.9 → 1.0.11
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 +30 -7
- package/dist/index.d.ts +30 -7
- package/dist/index.js +103 -76
- package/dist/index.mjs +103 -76
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -37,6 +37,12 @@ declare class Client {
|
|
37
37
|
* Unregister the client from the pubsub.
|
38
38
|
*/
|
39
39
|
unregister(): void;
|
40
|
+
/**
|
41
|
+
* Clean up the aichat registration if the iframe is removed.
|
42
|
+
*
|
43
|
+
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
44
|
+
*/
|
45
|
+
cleanAIChat(): void;
|
40
46
|
/**
|
41
47
|
* Listen for messages from the parent page with a callback.
|
42
48
|
*
|
@@ -50,6 +56,25 @@ declare class Client {
|
|
50
56
|
* @param payload The payload of the message.
|
51
57
|
*/
|
52
58
|
sendMessage(to: string, payload: any): void;
|
59
|
+
/**
|
60
|
+
* Check if a client with the given ID exists in the PubSub system.
|
61
|
+
*
|
62
|
+
* @param clientId The ID of the client to check.
|
63
|
+
* @param maxRetries Maximum number of retries. Default is 3.
|
64
|
+
* @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
|
65
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
66
|
+
*/
|
67
|
+
checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
|
68
|
+
/**
|
69
|
+
* Check if a client with the given ID exists in the PubSub system with retry mechanism.
|
70
|
+
* Will retry up to 3 times with 1 second delay between retries.
|
71
|
+
*
|
72
|
+
* @param clientId The ID of the client to check.
|
73
|
+
* @param retryCount The current retry count.
|
74
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
75
|
+
* @private
|
76
|
+
*/
|
77
|
+
private checkClientExistsWithRetry;
|
53
78
|
private handleMessage;
|
54
79
|
}
|
55
80
|
|
@@ -173,25 +198,23 @@ declare class PubSub {
|
|
173
198
|
* @param pageId The ID of the page or component to unregister from.
|
174
199
|
*/
|
175
200
|
unregister(pageId: string): void;
|
176
|
-
sendMessage(message: IMessage):
|
201
|
+
sendMessage(message: IMessage): void;
|
177
202
|
/**
|
178
203
|
* Try to send a message to a client with retry logic.
|
179
204
|
* Will retry up to 3 times with 1 second delay between retries.
|
180
|
-
* If client exists, will check if it's still available with PING/PONG.
|
181
205
|
*
|
182
206
|
* @param message The message to send.
|
183
207
|
* @param retryCount The current retry count.
|
184
208
|
*/
|
185
209
|
private trySendMessageWithRetry;
|
210
|
+
private handleMessage;
|
186
211
|
/**
|
187
|
-
* Check if a client
|
212
|
+
* Check if a client with the given ID exists in the PubSub system.
|
188
213
|
*
|
189
214
|
* @param clientId The ID of the client to check.
|
190
|
-
* @
|
191
|
-
* @returns A Promise that resolves to true if the client is pingable, false otherwise.
|
215
|
+
* @returns True if the client exists, false otherwise.
|
192
216
|
*/
|
193
|
-
|
194
|
-
private handleMessage;
|
217
|
+
isClientExists(clientId: string): boolean;
|
195
218
|
}
|
196
219
|
|
197
220
|
export { AIChatClient, AIChatNameEnum, Client, type IMessage, type IRegistrationMessage, type MessageCallback, PubSub };
|
package/dist/index.d.ts
CHANGED
@@ -37,6 +37,12 @@ declare class Client {
|
|
37
37
|
* Unregister the client from the pubsub.
|
38
38
|
*/
|
39
39
|
unregister(): void;
|
40
|
+
/**
|
41
|
+
* Clean up the aichat registration if the iframe is removed.
|
42
|
+
*
|
43
|
+
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
44
|
+
*/
|
45
|
+
cleanAIChat(): void;
|
40
46
|
/**
|
41
47
|
* Listen for messages from the parent page with a callback.
|
42
48
|
*
|
@@ -50,6 +56,25 @@ declare class Client {
|
|
50
56
|
* @param payload The payload of the message.
|
51
57
|
*/
|
52
58
|
sendMessage(to: string, payload: any): void;
|
59
|
+
/**
|
60
|
+
* Check if a client with the given ID exists in the PubSub system.
|
61
|
+
*
|
62
|
+
* @param clientId The ID of the client to check.
|
63
|
+
* @param maxRetries Maximum number of retries. Default is 3.
|
64
|
+
* @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
|
65
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
66
|
+
*/
|
67
|
+
checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
|
68
|
+
/**
|
69
|
+
* Check if a client with the given ID exists in the PubSub system with retry mechanism.
|
70
|
+
* Will retry up to 3 times with 1 second delay between retries.
|
71
|
+
*
|
72
|
+
* @param clientId The ID of the client to check.
|
73
|
+
* @param retryCount The current retry count.
|
74
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
75
|
+
* @private
|
76
|
+
*/
|
77
|
+
private checkClientExistsWithRetry;
|
53
78
|
private handleMessage;
|
54
79
|
}
|
55
80
|
|
@@ -173,25 +198,23 @@ declare class PubSub {
|
|
173
198
|
* @param pageId The ID of the page or component to unregister from.
|
174
199
|
*/
|
175
200
|
unregister(pageId: string): void;
|
176
|
-
sendMessage(message: IMessage):
|
201
|
+
sendMessage(message: IMessage): void;
|
177
202
|
/**
|
178
203
|
* Try to send a message to a client with retry logic.
|
179
204
|
* Will retry up to 3 times with 1 second delay between retries.
|
180
|
-
* If client exists, will check if it's still available with PING/PONG.
|
181
205
|
*
|
182
206
|
* @param message The message to send.
|
183
207
|
* @param retryCount The current retry count.
|
184
208
|
*/
|
185
209
|
private trySendMessageWithRetry;
|
210
|
+
private handleMessage;
|
186
211
|
/**
|
187
|
-
* Check if a client
|
212
|
+
* Check if a client with the given ID exists in the PubSub system.
|
188
213
|
*
|
189
214
|
* @param clientId The ID of the client to check.
|
190
|
-
* @
|
191
|
-
* @returns A Promise that resolves to true if the client is pingable, false otherwise.
|
215
|
+
* @returns True if the client exists, false otherwise.
|
192
216
|
*/
|
193
|
-
|
194
|
-
private handleMessage;
|
217
|
+
isClientExists(clientId: string): boolean;
|
195
218
|
}
|
196
219
|
|
197
220
|
export { AIChatClient, AIChatNameEnum, Client, type IMessage, type IRegistrationMessage, type MessageCallback, PubSub };
|
package/dist/index.js
CHANGED
@@ -69,83 +69,33 @@ var _PubSub = class _PubSub {
|
|
69
69
|
if (this.mainCallback) {
|
70
70
|
this.mainCallback(message);
|
71
71
|
}
|
72
|
-
|
72
|
+
this.trySendMessageWithRetry(message, 0);
|
73
73
|
}
|
74
74
|
/**
|
75
75
|
* Try to send a message to a client with retry logic.
|
76
76
|
* Will retry up to 3 times with 1 second delay between retries.
|
77
|
-
*
|
78
|
-
*
|
77
|
+
*
|
79
78
|
* @param message The message to send.
|
80
79
|
* @param retryCount The current retry count.
|
81
80
|
*/
|
82
|
-
|
81
|
+
trySendMessageWithRetry(message, retryCount) {
|
83
82
|
const subscriber = this.subscribers.get(message.to);
|
84
83
|
if (subscriber) {
|
85
84
|
if (subscriber.source) {
|
86
|
-
|
87
|
-
const isPingable = await this.checkClientPingable(
|
88
|
-
message.to,
|
89
|
-
subscriber.source
|
90
|
-
);
|
91
|
-
if (isPingable) {
|
92
|
-
subscriber.source.postMessage(message, "*");
|
93
|
-
return;
|
94
|
-
} else {
|
95
|
-
this.subscribers.delete(message.to);
|
96
|
-
}
|
97
|
-
} catch (error) {
|
98
|
-
console.warn(`Failed to ping client ${message.to}:`, error);
|
99
|
-
this.subscribers.delete(message.to);
|
100
|
-
}
|
85
|
+
subscriber.source.postMessage(message, "*");
|
101
86
|
} else {
|
102
87
|
subscriber.callback(message);
|
103
|
-
return;
|
104
88
|
}
|
89
|
+
return;
|
105
90
|
}
|
106
|
-
if (retryCount <
|
107
|
-
|
108
|
-
|
91
|
+
if (retryCount < 10) {
|
92
|
+
setTimeout(() => {
|
93
|
+
this.trySendMessageWithRetry(message, retryCount + 1);
|
94
|
+
}, 1e3);
|
109
95
|
} else {
|
110
|
-
console.warn(
|
111
|
-
`Failed to send message to client ${message.to} after 3 retries`
|
112
|
-
);
|
96
|
+
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
113
97
|
}
|
114
98
|
}
|
115
|
-
/**
|
116
|
-
* Check if a client is pingable (still available).
|
117
|
-
*
|
118
|
-
* @param clientId The ID of the client to check.
|
119
|
-
* @param source The window object of the client.
|
120
|
-
* @returns A Promise that resolves to true if the client is pingable, false otherwise.
|
121
|
-
*/
|
122
|
-
checkClientPingable(clientId, source) {
|
123
|
-
return new Promise((resolve) => {
|
124
|
-
const pingId = `ping-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
125
|
-
const messageHandler = (event) => {
|
126
|
-
const data = event.data;
|
127
|
-
if (data && data.from === clientId && data.to === "pubsub" && data.payload?.type === "PONG" && data.payload.pingId === pingId) {
|
128
|
-
window.removeEventListener("message", messageHandler);
|
129
|
-
clearTimeout(timeoutId);
|
130
|
-
resolve(true);
|
131
|
-
}
|
132
|
-
};
|
133
|
-
window.addEventListener("message", messageHandler);
|
134
|
-
const pingMessage = {
|
135
|
-
from: "pubsub",
|
136
|
-
to: clientId,
|
137
|
-
payload: {
|
138
|
-
type: "PING",
|
139
|
-
pingId
|
140
|
-
}
|
141
|
-
};
|
142
|
-
source.postMessage(pingMessage, "*");
|
143
|
-
const timeoutId = setTimeout(() => {
|
144
|
-
window.removeEventListener("message", messageHandler);
|
145
|
-
resolve(false);
|
146
|
-
}, 1e3);
|
147
|
-
});
|
148
|
-
}
|
149
99
|
async handleMessage(event) {
|
150
100
|
const data = event.data;
|
151
101
|
const source = event.source;
|
@@ -164,6 +114,14 @@ var _PubSub = class _PubSub {
|
|
164
114
|
this.subscribers.delete(unregistration.pageId);
|
165
115
|
return;
|
166
116
|
}
|
117
|
+
if (data?.type === "CLIENT_EXISTS_CHECK") {
|
118
|
+
source.postMessage({
|
119
|
+
type: "CLIENT_EXISTS_RESPONSE",
|
120
|
+
requestId: data.requestId,
|
121
|
+
exists: this.subscribers.has(data.clientId)
|
122
|
+
}, "*");
|
123
|
+
return;
|
124
|
+
}
|
167
125
|
if (!data || !data.from || !data.to) return;
|
168
126
|
const message = data;
|
169
127
|
if (this.mainCallback) {
|
@@ -177,6 +135,15 @@ var _PubSub = class _PubSub {
|
|
177
135
|
await subscriber.callback(message);
|
178
136
|
}
|
179
137
|
}
|
138
|
+
/**
|
139
|
+
* Check if a client with the given ID exists in the PubSub system.
|
140
|
+
*
|
141
|
+
* @param clientId The ID of the client to check.
|
142
|
+
* @returns True if the client exists, false otherwise.
|
143
|
+
*/
|
144
|
+
isClientExists(clientId) {
|
145
|
+
return this.subscribers.has(clientId);
|
146
|
+
}
|
180
147
|
};
|
181
148
|
__publicField(_PubSub, "instance");
|
182
149
|
var PubSub = _PubSub;
|
@@ -219,6 +186,17 @@ var Client = class {
|
|
219
186
|
this.pubsub.unregister(this.pageId);
|
220
187
|
}
|
221
188
|
}
|
189
|
+
/**
|
190
|
+
* Clean up the aichat registration if the iframe is removed.
|
191
|
+
*
|
192
|
+
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
193
|
+
*/
|
194
|
+
cleanAIChat() {
|
195
|
+
if (this.isIframe) {
|
196
|
+
throw new Error("You are not allowed to clean up aichat from iframe.");
|
197
|
+
}
|
198
|
+
this.pubsub.unregister("aichat");
|
199
|
+
}
|
222
200
|
/**
|
223
201
|
* Listen for messages from the parent page with a callback.
|
224
202
|
*
|
@@ -245,6 +223,71 @@ var Client = class {
|
|
245
223
|
this.pubsub.sendMessage(message);
|
246
224
|
}
|
247
225
|
}
|
226
|
+
/**
|
227
|
+
* Check if a client with the given ID exists in the PubSub system.
|
228
|
+
*
|
229
|
+
* @param clientId The ID of the client to check.
|
230
|
+
* @param maxRetries Maximum number of retries. Default is 3.
|
231
|
+
* @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
|
232
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
233
|
+
*/
|
234
|
+
checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
|
235
|
+
return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
|
236
|
+
}
|
237
|
+
/**
|
238
|
+
* Check if a client with the given ID exists in the PubSub system with retry mechanism.
|
239
|
+
* Will retry up to 3 times with 1 second delay between retries.
|
240
|
+
*
|
241
|
+
* @param clientId The ID of the client to check.
|
242
|
+
* @param retryCount The current retry count.
|
243
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
244
|
+
* @private
|
245
|
+
*/
|
246
|
+
checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
|
247
|
+
return new Promise((resolve) => {
|
248
|
+
if (this.isIframe) {
|
249
|
+
const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
250
|
+
const messageHandler = (event) => {
|
251
|
+
const data = event.data;
|
252
|
+
if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
|
253
|
+
window.removeEventListener("message", messageHandler);
|
254
|
+
clearTimeout(removeHandlerTimeout);
|
255
|
+
if (data.exists) {
|
256
|
+
resolve(true);
|
257
|
+
} else if (retryCount < maxRetries - 1) {
|
258
|
+
setTimeout(() => {
|
259
|
+
this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
|
260
|
+
}, retryInterval);
|
261
|
+
} else {
|
262
|
+
resolve(false);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
};
|
266
|
+
window.addEventListener("message", messageHandler);
|
267
|
+
window.parent.postMessage({
|
268
|
+
type: "CLIENT_EXISTS_CHECK",
|
269
|
+
clientId,
|
270
|
+
requestId,
|
271
|
+
from: this.pageId
|
272
|
+
}, "*");
|
273
|
+
const removeHandlerTimeout = setTimeout(() => {
|
274
|
+
window.removeEventListener("message", messageHandler);
|
275
|
+
resolve(false);
|
276
|
+
}, retryInterval + 1e3);
|
277
|
+
} else {
|
278
|
+
const exists = this.pubsub.isClientExists(clientId);
|
279
|
+
if (exists) {
|
280
|
+
resolve(true);
|
281
|
+
} else if (retryCount < maxRetries - 1) {
|
282
|
+
setTimeout(() => {
|
283
|
+
this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
|
284
|
+
}, retryInterval);
|
285
|
+
} else {
|
286
|
+
resolve(false);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
});
|
290
|
+
}
|
248
291
|
async handleMessage(event) {
|
249
292
|
let message;
|
250
293
|
if (event.data) {
|
@@ -254,22 +297,6 @@ var Client = class {
|
|
254
297
|
message = event;
|
255
298
|
}
|
256
299
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
257
|
-
if (message.payload?.type === "PING") {
|
258
|
-
const pongMsg = {
|
259
|
-
from: this.pageId,
|
260
|
-
to: message.from,
|
261
|
-
payload: {
|
262
|
-
type: "PONG",
|
263
|
-
pingId: message.payload.pingId
|
264
|
-
}
|
265
|
-
};
|
266
|
-
if (this.isIframe) {
|
267
|
-
window.parent.postMessage(pongMsg, "*");
|
268
|
-
} else {
|
269
|
-
this.pubsub.sendMessage(pongMsg);
|
270
|
-
}
|
271
|
-
return;
|
272
|
-
}
|
273
300
|
if (this.callback) {
|
274
301
|
try {
|
275
302
|
await this.callback(message);
|
package/dist/index.mjs
CHANGED
@@ -42,83 +42,33 @@ var _PubSub = class _PubSub {
|
|
42
42
|
if (this.mainCallback) {
|
43
43
|
this.mainCallback(message);
|
44
44
|
}
|
45
|
-
|
45
|
+
this.trySendMessageWithRetry(message, 0);
|
46
46
|
}
|
47
47
|
/**
|
48
48
|
* Try to send a message to a client with retry logic.
|
49
49
|
* Will retry up to 3 times with 1 second delay between retries.
|
50
|
-
*
|
51
|
-
*
|
50
|
+
*
|
52
51
|
* @param message The message to send.
|
53
52
|
* @param retryCount The current retry count.
|
54
53
|
*/
|
55
|
-
|
54
|
+
trySendMessageWithRetry(message, retryCount) {
|
56
55
|
const subscriber = this.subscribers.get(message.to);
|
57
56
|
if (subscriber) {
|
58
57
|
if (subscriber.source) {
|
59
|
-
|
60
|
-
const isPingable = await this.checkClientPingable(
|
61
|
-
message.to,
|
62
|
-
subscriber.source
|
63
|
-
);
|
64
|
-
if (isPingable) {
|
65
|
-
subscriber.source.postMessage(message, "*");
|
66
|
-
return;
|
67
|
-
} else {
|
68
|
-
this.subscribers.delete(message.to);
|
69
|
-
}
|
70
|
-
} catch (error) {
|
71
|
-
console.warn(`Failed to ping client ${message.to}:`, error);
|
72
|
-
this.subscribers.delete(message.to);
|
73
|
-
}
|
58
|
+
subscriber.source.postMessage(message, "*");
|
74
59
|
} else {
|
75
60
|
subscriber.callback(message);
|
76
|
-
return;
|
77
61
|
}
|
62
|
+
return;
|
78
63
|
}
|
79
|
-
if (retryCount <
|
80
|
-
|
81
|
-
|
64
|
+
if (retryCount < 10) {
|
65
|
+
setTimeout(() => {
|
66
|
+
this.trySendMessageWithRetry(message, retryCount + 1);
|
67
|
+
}, 1e3);
|
82
68
|
} else {
|
83
|
-
console.warn(
|
84
|
-
`Failed to send message to client ${message.to} after 3 retries`
|
85
|
-
);
|
69
|
+
console.warn(`Failed to send message to client ${message.to} after 10 retries`);
|
86
70
|
}
|
87
71
|
}
|
88
|
-
/**
|
89
|
-
* Check if a client is pingable (still available).
|
90
|
-
*
|
91
|
-
* @param clientId The ID of the client to check.
|
92
|
-
* @param source The window object of the client.
|
93
|
-
* @returns A Promise that resolves to true if the client is pingable, false otherwise.
|
94
|
-
*/
|
95
|
-
checkClientPingable(clientId, source) {
|
96
|
-
return new Promise((resolve) => {
|
97
|
-
const pingId = `ping-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
98
|
-
const messageHandler = (event) => {
|
99
|
-
const data = event.data;
|
100
|
-
if (data && data.from === clientId && data.to === "pubsub" && data.payload?.type === "PONG" && data.payload.pingId === pingId) {
|
101
|
-
window.removeEventListener("message", messageHandler);
|
102
|
-
clearTimeout(timeoutId);
|
103
|
-
resolve(true);
|
104
|
-
}
|
105
|
-
};
|
106
|
-
window.addEventListener("message", messageHandler);
|
107
|
-
const pingMessage = {
|
108
|
-
from: "pubsub",
|
109
|
-
to: clientId,
|
110
|
-
payload: {
|
111
|
-
type: "PING",
|
112
|
-
pingId
|
113
|
-
}
|
114
|
-
};
|
115
|
-
source.postMessage(pingMessage, "*");
|
116
|
-
const timeoutId = setTimeout(() => {
|
117
|
-
window.removeEventListener("message", messageHandler);
|
118
|
-
resolve(false);
|
119
|
-
}, 1e3);
|
120
|
-
});
|
121
|
-
}
|
122
72
|
async handleMessage(event) {
|
123
73
|
const data = event.data;
|
124
74
|
const source = event.source;
|
@@ -137,6 +87,14 @@ var _PubSub = class _PubSub {
|
|
137
87
|
this.subscribers.delete(unregistration.pageId);
|
138
88
|
return;
|
139
89
|
}
|
90
|
+
if (data?.type === "CLIENT_EXISTS_CHECK") {
|
91
|
+
source.postMessage({
|
92
|
+
type: "CLIENT_EXISTS_RESPONSE",
|
93
|
+
requestId: data.requestId,
|
94
|
+
exists: this.subscribers.has(data.clientId)
|
95
|
+
}, "*");
|
96
|
+
return;
|
97
|
+
}
|
140
98
|
if (!data || !data.from || !data.to) return;
|
141
99
|
const message = data;
|
142
100
|
if (this.mainCallback) {
|
@@ -150,6 +108,15 @@ var _PubSub = class _PubSub {
|
|
150
108
|
await subscriber.callback(message);
|
151
109
|
}
|
152
110
|
}
|
111
|
+
/**
|
112
|
+
* Check if a client with the given ID exists in the PubSub system.
|
113
|
+
*
|
114
|
+
* @param clientId The ID of the client to check.
|
115
|
+
* @returns True if the client exists, false otherwise.
|
116
|
+
*/
|
117
|
+
isClientExists(clientId) {
|
118
|
+
return this.subscribers.has(clientId);
|
119
|
+
}
|
153
120
|
};
|
154
121
|
__publicField(_PubSub, "instance");
|
155
122
|
var PubSub = _PubSub;
|
@@ -192,6 +159,17 @@ var Client = class {
|
|
192
159
|
this.pubsub.unregister(this.pageId);
|
193
160
|
}
|
194
161
|
}
|
162
|
+
/**
|
163
|
+
* Clean up the aichat registration if the iframe is removed.
|
164
|
+
*
|
165
|
+
* Note: aichat itself does not know the iframe is removed then we have to clean up from parent
|
166
|
+
*/
|
167
|
+
cleanAIChat() {
|
168
|
+
if (this.isIframe) {
|
169
|
+
throw new Error("You are not allowed to clean up aichat from iframe.");
|
170
|
+
}
|
171
|
+
this.pubsub.unregister("aichat");
|
172
|
+
}
|
195
173
|
/**
|
196
174
|
* Listen for messages from the parent page with a callback.
|
197
175
|
*
|
@@ -218,6 +196,71 @@ var Client = class {
|
|
218
196
|
this.pubsub.sendMessage(message);
|
219
197
|
}
|
220
198
|
}
|
199
|
+
/**
|
200
|
+
* Check if a client with the given ID exists in the PubSub system.
|
201
|
+
*
|
202
|
+
* @param clientId The ID of the client to check.
|
203
|
+
* @param maxRetries Maximum number of retries. Default is 3.
|
204
|
+
* @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
|
205
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
206
|
+
*/
|
207
|
+
checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
|
208
|
+
return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
|
209
|
+
}
|
210
|
+
/**
|
211
|
+
* Check if a client with the given ID exists in the PubSub system with retry mechanism.
|
212
|
+
* Will retry up to 3 times with 1 second delay between retries.
|
213
|
+
*
|
214
|
+
* @param clientId The ID of the client to check.
|
215
|
+
* @param retryCount The current retry count.
|
216
|
+
* @returns A Promise that resolves to true if the client exists, false otherwise.
|
217
|
+
* @private
|
218
|
+
*/
|
219
|
+
checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
|
220
|
+
return new Promise((resolve) => {
|
221
|
+
if (this.isIframe) {
|
222
|
+
const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
223
|
+
const messageHandler = (event) => {
|
224
|
+
const data = event.data;
|
225
|
+
if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
|
226
|
+
window.removeEventListener("message", messageHandler);
|
227
|
+
clearTimeout(removeHandlerTimeout);
|
228
|
+
if (data.exists) {
|
229
|
+
resolve(true);
|
230
|
+
} else if (retryCount < maxRetries - 1) {
|
231
|
+
setTimeout(() => {
|
232
|
+
this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
|
233
|
+
}, retryInterval);
|
234
|
+
} else {
|
235
|
+
resolve(false);
|
236
|
+
}
|
237
|
+
}
|
238
|
+
};
|
239
|
+
window.addEventListener("message", messageHandler);
|
240
|
+
window.parent.postMessage({
|
241
|
+
type: "CLIENT_EXISTS_CHECK",
|
242
|
+
clientId,
|
243
|
+
requestId,
|
244
|
+
from: this.pageId
|
245
|
+
}, "*");
|
246
|
+
const removeHandlerTimeout = setTimeout(() => {
|
247
|
+
window.removeEventListener("message", messageHandler);
|
248
|
+
resolve(false);
|
249
|
+
}, retryInterval + 1e3);
|
250
|
+
} else {
|
251
|
+
const exists = this.pubsub.isClientExists(clientId);
|
252
|
+
if (exists) {
|
253
|
+
resolve(true);
|
254
|
+
} else if (retryCount < maxRetries - 1) {
|
255
|
+
setTimeout(() => {
|
256
|
+
this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
|
257
|
+
}, retryInterval);
|
258
|
+
} else {
|
259
|
+
resolve(false);
|
260
|
+
}
|
261
|
+
}
|
262
|
+
});
|
263
|
+
}
|
221
264
|
async handleMessage(event) {
|
222
265
|
let message;
|
223
266
|
if (event.data) {
|
@@ -227,22 +270,6 @@ var Client = class {
|
|
227
270
|
message = event;
|
228
271
|
}
|
229
272
|
if (!message || !message.from || !message.to || message.to !== this.pageId) return;
|
230
|
-
if (message.payload?.type === "PING") {
|
231
|
-
const pongMsg = {
|
232
|
-
from: this.pageId,
|
233
|
-
to: message.from,
|
234
|
-
payload: {
|
235
|
-
type: "PONG",
|
236
|
-
pingId: message.payload.pingId
|
237
|
-
}
|
238
|
-
};
|
239
|
-
if (this.isIframe) {
|
240
|
-
window.parent.postMessage(pongMsg, "*");
|
241
|
-
} else {
|
242
|
-
this.pubsub.sendMessage(pongMsg);
|
243
|
-
}
|
244
|
-
return;
|
245
|
-
}
|
246
273
|
if (this.callback) {
|
247
274
|
try {
|
248
275
|
await this.callback(message);
|