@verdant-web/store 3.5.2 → 3.6.1

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/src/sync/Sync.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  ServerSyncEndpointProviderConfig,
17
17
  } from './ServerSyncEndpointProvider.js';
18
18
  import { WebSocketSync } from './WebSocketSync.js';
19
+ import { Context } from '../context.js';
19
20
 
20
21
  type SyncEvents = {
21
22
  onlineChange: (isOnline: boolean) => void;
@@ -59,6 +60,7 @@ export interface Sync<Presence = any, Profile = any>
59
60
  ignoreIncoming(): void;
60
61
  destroy(): void;
61
62
  reconnect(): void;
63
+ syncOnce(): Promise<void>;
62
64
  readonly isConnected: boolean;
63
65
  readonly status: 'active' | 'paused';
64
66
  readonly mode: SyncTransportMode;
@@ -113,6 +115,8 @@ export class NoSync<Presence = any, Profile = any>
113
115
  retry: false,
114
116
  };
115
117
  };
118
+
119
+ syncOnce: () => Promise<void> = async () => {};
116
120
  }
117
121
 
118
122
  export type SyncTransportMode = 'realtime' | 'pull';
@@ -210,11 +214,11 @@ export class ServerSync<Presence = any, Profile = any>
210
214
  }: ServerSyncOptions<Profile, Presence>,
211
215
  {
212
216
  meta,
213
- log,
217
+ ctx,
214
218
  onData,
215
219
  }: {
216
220
  meta: Metadata;
217
- log?: (...args: any[]) => void;
221
+ ctx: Context;
218
222
  onData: (data: {
219
223
  operations: Operation[];
220
224
  baselines?: DocumentBaseline[];
@@ -225,7 +229,7 @@ export class ServerSync<Presence = any, Profile = any>
225
229
  super();
226
230
  this.meta = meta;
227
231
  this.onData = onData;
228
- this.log = log || (() => {});
232
+ this.log = ctx.log;
229
233
  this.presence = new PresenceManager({
230
234
  initialPresence,
231
235
  defaultProfile,
@@ -257,7 +261,7 @@ export class ServerSync<Presence = any, Profile = any>
257
261
  log: this.log,
258
262
  });
259
263
  if (useBroadcastChannel && 'BroadcastChannel' in window) {
260
- this.broadcastChannel = new BroadcastChannel('verdant');
264
+ this.broadcastChannel = new BroadcastChannel(`verdant-${ctx.namespace}`);
261
265
  this.broadcastChannel.addEventListener(
262
266
  'message',
263
267
  this.handleBroadcastChannelMessage,
@@ -493,6 +497,15 @@ export class ServerSync<Presence = any, Profile = any>
493
497
  return this.activeSync.reconnect();
494
498
  };
495
499
 
500
+ /**
501
+ * Runs one isolated sync cycle over HTTP. This is useful for
502
+ * syncing via a periodic background job in a service worker,
503
+ * which keeps a client up to date while the app isn't open.
504
+ */
505
+ public syncOnce = () => {
506
+ return this.pushPullSync.syncOnce();
507
+ };
508
+
496
509
  public get isConnected(): boolean {
497
510
  return this.activeSync.isConnected;
498
511
  }