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