@mgamadeus/ddd-agents 0.5.5 → 0.5.7

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/ui.js CHANGED
@@ -1175,7 +1175,74 @@ const createAgentWindowAdapter = (options) => {
1175
1175
  shouldStepAsideBefore: (requestType) => stepAsideFor.includes(requestType)
1176
1176
  };
1177
1177
  };
1178
+ const ACCOUNT_REALTIME_POLL_INTERVAL_MS = 5e3;
1179
+ const ACCOUNT_REALTIME_PING_REREAD_DELAY_MS = 400;
1180
+ const identityOf = (message) => `${message.messageType}\0${message.messageKey}\0${message.payloadJson}`;
1181
+ const createAccountRealtimePoll = (options) => {
1182
+ const intervalMs = options.intervalMs ?? ACCOUNT_REALTIME_POLL_INTERVAL_MS;
1183
+ const rereadDelayMs = options.rereadDelayMs ?? ACCOUNT_REALTIME_PING_REREAD_DELAY_MS;
1184
+ let knownIdentities = /* @__PURE__ */ new Set();
1185
+ let stopped = false;
1186
+ let timer = null;
1187
+ const acceptArrivedMessage = (message) => {
1188
+ const identity = identityOf(message);
1189
+ if (knownIdentities.has(identity)) return false;
1190
+ knownIdentities.add(identity);
1191
+ options.onMessage(message);
1192
+ return true;
1193
+ };
1194
+ const readNow = async () => {
1195
+ const read = await options.readAccountRealtime();
1196
+ const identitiesOfRead = new Set(read.messages.map(identityOf));
1197
+ let arrivedCount = 0;
1198
+ for (const message of read.messages) {
1199
+ if (!knownIdentities.has(identityOf(message))) {
1200
+ arrivedCount += 1;
1201
+ options.onMessage(message);
1202
+ }
1203
+ }
1204
+ knownIdentities = identitiesOfRead;
1205
+ options.onListenerCredential?.(read.listenerCredential);
1206
+ return arrivedCount;
1207
+ };
1208
+ const schedule = () => {
1209
+ if (stopped) return;
1210
+ timer = setTimeout(tick, intervalMs);
1211
+ };
1212
+ const readIgnoringFailure = async () => {
1213
+ try {
1214
+ return await readNow();
1215
+ } catch (error) {
1216
+ options.onReadFailed?.(error);
1217
+ return 0;
1218
+ }
1219
+ };
1220
+ const tick = async () => {
1221
+ timer = null;
1222
+ if (stopped) return;
1223
+ if (!(options.isTransportBound?.() ?? false)) await readIgnoringFailure();
1224
+ schedule();
1225
+ };
1226
+ const readAfterPing = async () => {
1227
+ if (await readIgnoringFailure() > 0) return true;
1228
+ await new Promise((resolve) => setTimeout(resolve, rereadDelayMs));
1229
+ return await readIgnoringFailure() > 0;
1230
+ };
1231
+ void readIgnoringFailure().then(schedule);
1232
+ return {
1233
+ readNow,
1234
+ acceptArrivedMessage,
1235
+ readAfterPing,
1236
+ stop: () => {
1237
+ stopped = true;
1238
+ if (timer !== null) clearTimeout(timer);
1239
+ timer = null;
1240
+ }
1241
+ };
1242
+ };
1178
1243
  export {
1244
+ ACCOUNT_REALTIME_PING_REREAD_DELAY_MS,
1245
+ ACCOUNT_REALTIME_POLL_INTERVAL_MS,
1179
1246
  AGENT_ANCHOR_ATTRIBUTE,
1180
1247
  AGENT_ANCHOR_GROUP_ATTRIBUTE,
1181
1248
  AGENT_ATTRIBUTES,
@@ -1219,6 +1286,7 @@ export {
1219
1286
  computePictureDimensions,
1220
1287
  computeRetryScale,
1221
1288
  convertPixelsToGrey,
1289
+ createAccountRealtimePoll,
1222
1290
  createAgentUIClientId,
1223
1291
  createAgentWindowAdapter,
1224
1292
  createDomScreenPathGuide,