@kehto/paja 0.3.1 → 0.3.2

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.
@@ -1299,13 +1299,128 @@ function publishSignedEncrypted(context, windowId, id, signed, replyPe) {
1299
1299
  }
1300
1300
  }
1301
1301
  function handleRelayQuery(context, windowId, m) {
1302
+ const { hooks, serviceRegistry, eventBuffer } = context;
1302
1303
  const id = m.id ?? "";
1303
1304
  const filters = m.filters ?? [];
1304
- let count = 0;
1305
- for (const event of context.eventBuffer.getBufferedEvents()) {
1306
- if (matchesAnyFilter(event, filters)) count++;
1305
+ const seenIds = /* @__PURE__ */ new Set();
1306
+ const events = [];
1307
+ for (const event of eventBuffer.getBufferedEvents()) {
1308
+ if (matchesAnyFilter(event, filters) && !seenIds.has(event.id)) {
1309
+ seenIds.add(event.id);
1310
+ events.push(event);
1311
+ }
1312
+ }
1313
+ let settled = false;
1314
+ let fallbackTimer;
1315
+ function settle6() {
1316
+ if (settled) return;
1317
+ settled = true;
1318
+ clearTimeout(fallbackTimer);
1319
+ hooks.sendToNapplet(windowId, { type: "relay.query.result", id, events });
1307
1320
  }
1308
- context.hooks.sendToNapplet(windowId, { type: "relay.query.result", id, count });
1321
+ if (isShellKindQuery(filters)) {
1322
+ settle6();
1323
+ return;
1324
+ }
1325
+ const relayService = relayServiceFrom(context);
1326
+ const cacheService = !serviceRegistry["relay"] ? serviceRegistry["cache"] : void 0;
1327
+ if (relayService) {
1328
+ let onBackendEose2 = function() {
1329
+ openBackends--;
1330
+ if (openBackends <= 0) {
1331
+ const closeMsg = { type: "relay.close", id, subId };
1332
+ svc.handleMessage(windowId, closeMsg, () => {
1333
+ });
1334
+ if (cacheService) {
1335
+ cacheService.handleMessage(windowId, closeMsg, () => {
1336
+ });
1337
+ }
1338
+ settle6();
1339
+ }
1340
+ }, makeCollector2 = function() {
1341
+ return function collector(resp) {
1342
+ const r = resp;
1343
+ if (r.type === "relay.event" && r.event && !seenIds.has(r.event.id)) {
1344
+ seenIds.add(r.event.id);
1345
+ events.push(r.event);
1346
+ } else if (r.type === "relay.eose") {
1347
+ onBackendEose2();
1348
+ }
1349
+ };
1350
+ };
1351
+ var onBackendEose = onBackendEose2, makeCollector = makeCollector2;
1352
+ const svc = relayService;
1353
+ const subId = "__query__:" + id;
1354
+ const subscribeMsg = {
1355
+ type: "relay.subscribe",
1356
+ id,
1357
+ subId,
1358
+ filters
1359
+ };
1360
+ if (typeof m.relay === "string" && m.relay.length > 0) {
1361
+ subscribeMsg.relay = m.relay;
1362
+ }
1363
+ let openBackends = cacheService ? 2 : 1;
1364
+ fallbackTimer = setTimeout(() => {
1365
+ const closeMsg = { type: "relay.close", id, subId };
1366
+ svc.handleMessage(windowId, closeMsg, () => {
1367
+ });
1368
+ if (cacheService) {
1369
+ cacheService.handleMessage(windowId, closeMsg, () => {
1370
+ });
1371
+ }
1372
+ settle6();
1373
+ }, 15e3);
1374
+ svc.handleMessage(windowId, subscribeMsg, makeCollector2());
1375
+ if (cacheService) {
1376
+ cacheService.handleMessage(windowId, subscribeMsg, makeCollector2());
1377
+ }
1378
+ return;
1379
+ }
1380
+ const cache2 = hooks.cache;
1381
+ const pool = hooks.relayPool;
1382
+ if (cache2?.isAvailable()) {
1383
+ cache2.query(filters).then((cachedEvents) => {
1384
+ for (const event of cachedEvents) {
1385
+ if (!seenIds.has(event.id)) {
1386
+ seenIds.add(event.id);
1387
+ events.push(event);
1388
+ }
1389
+ }
1390
+ }).catch(() => {
1391
+ });
1392
+ }
1393
+ if (!pool?.isAvailable()) {
1394
+ settle6();
1395
+ return;
1396
+ }
1397
+ const relayHint = typeof m.relay === "string" && m.relay.length > 0 ? m.relay : void 0;
1398
+ const relayUrls = relayHint ? [relayHint] : pool.selectRelayTier(filters);
1399
+ let poolSubscription;
1400
+ fallbackTimer = setTimeout(() => {
1401
+ poolSubscription?.unsubscribe();
1402
+ settle6();
1403
+ }, 15e3);
1404
+ poolSubscription = pool.subscribe(filters, (item) => {
1405
+ if (item === "EOSE") {
1406
+ clearTimeout(fallbackTimer);
1407
+ poolSubscription?.unsubscribe();
1408
+ settle6();
1409
+ return;
1410
+ }
1411
+ const event = item;
1412
+ if (!seenIds.has(event.id)) {
1413
+ seenIds.add(event.id);
1414
+ events.push(event);
1415
+ }
1416
+ if (cache2?.isAvailable()) {
1417
+ try {
1418
+ cache2.store(event);
1419
+ } catch {
1420
+ return;
1421
+ }
1422
+ }
1423
+ }, relayUrls);
1309
1424
  }
1310
1425
  function createIdentityHandler(context) {
1311
1426
  return function handleIdentityMessage(windowId, msg) {
@@ -6117,12 +6232,20 @@ function unsupported2(resultType, id) {
6117
6232
  error: `${resultType.replace(".result", "")} unavailable`
6118
6233
  };
6119
6234
  }
6235
+ function createContext(windowId, send) {
6236
+ return {
6237
+ windowId,
6238
+ emit(event) {
6239
+ send({ type: "ble.event", event });
6240
+ }
6241
+ };
6242
+ }
6120
6243
  function createBleService(options = {}) {
6121
6244
  return {
6122
6245
  descriptor: BLE_DESCRIPTOR,
6123
6246
  handleMessage(windowId, message, send) {
6124
6247
  const id = message.id ?? "";
6125
- const context = { windowId };
6248
+ const context = createContext(windowId, send);
6126
6249
  if (message.type === "ble.open") {
6127
6250
  if (!options.open) {
6128
6251
  send(unsupported2("ble.open.result", id));
@@ -6260,7 +6383,7 @@ function unsupported3(resultType, id) {
6260
6383
  error: `${resultType.replace(".result", "")} unavailable`
6261
6384
  };
6262
6385
  }
6263
- function createContext(windowId, send) {
6386
+ function createContext2(windowId, send) {
6264
6387
  return {
6265
6388
  windowId,
6266
6389
  emit(event) {
@@ -6273,7 +6396,7 @@ function createWebrtcService(options = {}) {
6273
6396
  descriptor: WEBRTC_DESCRIPTOR,
6274
6397
  handleMessage(windowId, message, send) {
6275
6398
  const id = message.id ?? "";
6276
- const context = createContext(windowId, send);
6399
+ const context = createContext2(windowId, send);
6277
6400
  if (message.type === "webrtc.open") {
6278
6401
  if (!options.open) {
6279
6402
  send(unsupported3("webrtc.open.result", id));