@absolutejs/sync 2.0.0 → 2.1.0

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.js CHANGED
@@ -496,40 +496,59 @@ var syncSocket = ({
496
496
  };
497
497
  return new Elysia2({ name: "@absolutejs/sync/socket" }).ws(path, {
498
498
  async open(ws) {
499
- const ctx = resolveContext ? await resolveContext(ws.data) : {};
500
499
  const bunWs = ws;
501
500
  const tracked = {
502
- connection: createSyncConnection({
503
- engine,
504
- ctx,
505
- presence,
506
- serializer,
507
- send: (frame) => {
508
- const payload = serializer.encodeServer(frame);
509
- const ret = bunWs.send(typeof payload === "string" ? payload : payload);
510
- const buffered = bunWs.getBufferedAmount?.() ?? 0;
511
- const overBuffer = buffered > threshold;
512
- const backpressure = ret === -1;
513
- if ((overBuffer || backpressure) && !tracked.slowSignaled) {
514
- tracked.slowSignaled = true;
515
- fireSlow({
516
- bufferedAmount: buffered,
517
- reason: backpressure ? "send-backpressure" : "buffer-threshold",
518
- stats: tracked.connection.stats(),
519
- wsId: bunWs.id
520
- });
521
- if (closeOnSlow)
522
- bunWs.close?.();
523
- }
524
- return ret;
525
- }
526
- }),
501
+ connection: null,
502
+ pending: [],
527
503
  slowSignaled: false
528
504
  };
529
505
  connections.set(bunWs.id, tracked);
506
+ const ctx = resolveContext ? await resolveContext(ws.data) : {};
507
+ const connection = createSyncConnection({
508
+ engine,
509
+ ctx,
510
+ presence,
511
+ serializer,
512
+ send: (frame) => {
513
+ const payload = serializer.encodeServer(frame);
514
+ const ret = bunWs.send(typeof payload === "string" ? payload : payload);
515
+ const buffered2 = bunWs.getBufferedAmount?.() ?? 0;
516
+ const overBuffer = buffered2 > threshold;
517
+ const backpressure = ret === -1;
518
+ if ((overBuffer || backpressure) && !tracked.slowSignaled) {
519
+ tracked.slowSignaled = true;
520
+ fireSlow({
521
+ bufferedAmount: buffered2,
522
+ reason: backpressure ? "send-backpressure" : "buffer-threshold",
523
+ stats: connection.stats(),
524
+ wsId: bunWs.id
525
+ });
526
+ if (closeOnSlow)
527
+ bunWs.close?.();
528
+ }
529
+ return ret;
530
+ }
531
+ });
532
+ if (!connections.has(bunWs.id)) {
533
+ connection.close();
534
+ return;
535
+ }
536
+ tracked.connection = connection;
537
+ const buffered = tracked.pending;
538
+ tracked.pending = [];
539
+ for (const frame of buffered) {
540
+ await connection.handle(frame);
541
+ }
530
542
  },
531
543
  async message(ws, message) {
532
- await connections.get(ws.id)?.connection.handle(message);
544
+ const tracked = connections.get(ws.id);
545
+ if (!tracked)
546
+ return;
547
+ if (tracked.connection) {
548
+ await tracked.connection.handle(message);
549
+ } else {
550
+ tracked.pending.push(message);
551
+ }
533
552
  },
534
553
  drain(ws) {
535
554
  const tracked = connections.get(ws.id);
@@ -539,7 +558,7 @@ var syncSocket = ({
539
558
  close(ws) {
540
559
  const tracked = connections.get(ws.id);
541
560
  if (tracked) {
542
- tracked.connection.close();
561
+ tracked.connection?.close();
543
562
  connections.delete(ws.id);
544
563
  }
545
564
  }
@@ -1356,6 +1375,9 @@ var createSyncEngine = (options = {}) => {
1356
1375
  let mutationsFailed = 0;
1357
1376
  let mutationsRetried = 0;
1358
1377
  let mutationsInFlight = 0;
1378
+ let connectedSources = 0;
1379
+ let sourceChangesReceived = 0;
1380
+ let sourceLastChangeAt = null;
1359
1381
  const mutationWaiters = [];
1360
1382
  let mutationsQueued = 0;
1361
1383
  const activeFences = new Set;
@@ -2409,8 +2431,18 @@ var createSyncEngine = (options = {}) => {
2409
2431
  },
2410
2432
  applyChange: (table, change) => applyChange(table, change),
2411
2433
  connectSource: async (source) => {
2412
- await source.start((table, change) => applyChange(table, change));
2434
+ await source.start((table, change) => {
2435
+ sourceChangesReceived += 1;
2436
+ sourceLastChangeAt = Date.now();
2437
+ return applyChange(table, change);
2438
+ });
2439
+ connectedSources += 1;
2440
+ let stopped = false;
2413
2441
  return async () => {
2442
+ if (!stopped) {
2443
+ stopped = true;
2444
+ connectedSources -= 1;
2445
+ }
2414
2446
  await source.stop();
2415
2447
  };
2416
2448
  },
@@ -2971,6 +3003,12 @@ var createSyncEngine = (options = {}) => {
2971
3003
  schedules: {
2972
3004
  registered: schedules.size
2973
3005
  },
3006
+ source: {
3007
+ changesReceived: sourceChangesReceived,
3008
+ connected: connectedSources,
3009
+ lastChangeAgeMs: sourceLastChangeAt === null ? null : now - sourceLastChangeAt,
3010
+ lastChangeAt: sourceLastChangeAt
3011
+ },
2974
3012
  subscriptions: {
2975
3013
  byCollection,
2976
3014
  byTenant: Object.fromEntries(subscriptionsByTenant),
@@ -3467,5 +3505,5 @@ export {
3467
3505
  createPresenceHub
3468
3506
  };
3469
3507
 
3470
- //# debugId=62313B55CB7C7F6064756E2164756E21
3508
+ //# debugId=B94BDA3C56450BBB64756E2164756E21
3471
3509
  //# sourceMappingURL=index.js.map