@absolutejs/sync 1.24.0 → 2.0.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
@@ -139,51 +139,54 @@ var sync = ({
139
139
  path = "/sync",
140
140
  resolveTopics = defaultResolveTopics,
141
141
  heartbeatMs = 25000
142
- }) => new Elysia({ name: "@absolutejs/sync" }).get(path, (context) => {
143
- const topics = resolveTopics({
144
- query: context.query,
145
- request: context.request
146
- });
147
- const encoder = new TextEncoder;
148
- const stream = new ReadableStream({
149
- start(controller) {
150
- const write = (chunk) => {
151
- try {
152
- controller.enqueue(encoder.encode(chunk));
153
- } catch {}
154
- };
155
- const send = (event) => {
156
- write(`data: ${JSON.stringify(event)}
142
+ }) => {
143
+ const app = new Elysia({ name: "@absolutejs/sync" }).get(path, (context) => {
144
+ const topics = resolveTopics({
145
+ query: context.query,
146
+ request: context.request
147
+ });
148
+ const encoder = new TextEncoder;
149
+ const stream = new ReadableStream({
150
+ start(controller) {
151
+ const write = (chunk) => {
152
+ try {
153
+ controller.enqueue(encoder.encode(chunk));
154
+ } catch {}
155
+ };
156
+ const send = (event) => {
157
+ write(`data: ${JSON.stringify(event)}
157
158
 
158
159
  `);
159
- };
160
- send({
161
- topic: SYNC_OPEN_TOPIC,
162
- at: Date.now(),
163
- payload: { topics }
164
- });
165
- const unsubscribe = topics.length > 0 ? hub.subscribe(topics, send) : () => {};
166
- const heartbeat = setInterval(() => write(`: ping
160
+ };
161
+ send({
162
+ topic: SYNC_OPEN_TOPIC,
163
+ at: Date.now(),
164
+ payload: { topics }
165
+ });
166
+ const unsubscribe = topics.length > 0 ? hub.subscribe(topics, send) : () => {};
167
+ const heartbeat = setInterval(() => write(`: ping
167
168
 
168
169
  `), heartbeatMs);
169
- context.request.signal.addEventListener("abort", () => {
170
- clearInterval(heartbeat);
171
- unsubscribe();
172
- try {
173
- controller.close();
174
- } catch {}
175
- }, { once: true });
176
- }
177
- });
178
- return new Response(stream, {
179
- headers: {
180
- "cache-control": "no-cache, no-transform",
181
- connection: "keep-alive",
182
- "content-type": "text/event-stream",
183
- "x-accel-buffering": "no"
184
- }
170
+ context.request.signal.addEventListener("abort", () => {
171
+ clearInterval(heartbeat);
172
+ unsubscribe();
173
+ try {
174
+ controller.close();
175
+ } catch {}
176
+ }, { once: true });
177
+ }
178
+ });
179
+ return new Response(stream, {
180
+ headers: {
181
+ "cache-control": "no-cache, no-transform",
182
+ connection: "keep-alive",
183
+ "content-type": "text/event-stream",
184
+ "x-accel-buffering": "no"
185
+ }
186
+ });
185
187
  });
186
- });
188
+ return app;
189
+ };
187
190
  // src/engine/socket.ts
188
191
  import { Elysia as Elysia2 } from "elysia";
189
192
 
@@ -1482,7 +1485,11 @@ var createSyncEngine = (options = {}) => {
1482
1485
  }
1483
1486
  const broadcast = (changes, originVersion) => {
1484
1487
  if (clusterBus !== undefined && changes.length > 0) {
1485
- clusterBus.publish({ changes, origin: instanceId, originVersion });
1488
+ clusterBus.publish({
1489
+ changes,
1490
+ origin: instanceId,
1491
+ originVersion
1492
+ });
1486
1493
  }
1487
1494
  };
1488
1495
  const subsFor = (collection) => {
@@ -2245,7 +2252,14 @@ var createSyncEngine = (options = {}) => {
2245
2252
  registerSearch: (collection) => {
2246
2253
  registry.set(collection.name, collection);
2247
2254
  },
2248
- subscribe: async ({ collection, params, ctx, onDiff, since, signal }) => {
2255
+ subscribe: async ({
2256
+ collection,
2257
+ params,
2258
+ ctx,
2259
+ onDiff,
2260
+ since,
2261
+ signal
2262
+ }) => {
2249
2263
  const subscribeSpan = tracer.startSpan("sync.subscribe", {
2250
2264
  attributes: {
2251
2265
  [ABS_ATTRS.engineId]: instanceId,
@@ -2274,7 +2288,10 @@ var createSyncEngine = (options = {}) => {
2274
2288
  releaseSubscriptionSlot(tenantSlot);
2275
2289
  innerUnsubscribe();
2276
2290
  };
2277
- const wrapped = { ...sub, unsubscribe: wrappedUnsubscribe };
2291
+ const wrapped = {
2292
+ ...sub,
2293
+ unsubscribe: wrappedUnsubscribe
2294
+ };
2278
2295
  linkAbortToUnsubscribe(signal, wrappedUnsubscribe);
2279
2296
  slotHandedOff = true;
2280
2297
  return wrapped;
@@ -2309,7 +2326,9 @@ var createSyncEngine = (options = {}) => {
2309
2326
  const scopedTable = tables.length === 1 ? tables[0] : undefined;
2310
2327
  const readRule = scopedTable !== undefined ? readRuleFor(scopedTable) : undefined;
2311
2328
  const rehydrate = async () => {
2312
- const raw = [...await definition.hydrate(params, ctx)];
2329
+ const raw = [
2330
+ ...await definition.hydrate(params, ctx)
2331
+ ];
2313
2332
  const rows = scopedTable !== undefined ? raw.map((row) => migrateRow(scopedTable, row)) : raw;
2314
2333
  return readRule ? rows.filter((row) => readRule(ctx, row)) : rows;
2315
2334
  };
@@ -2864,7 +2883,10 @@ var createSyncEngine = (options = {}) => {
2864
2883
  activeFences.add(handle);
2865
2884
  return handle;
2866
2885
  },
2867
- exportSnapshot: async ({ tables, ctx = {} } = {}) => {
2886
+ exportSnapshot: async ({
2887
+ tables,
2888
+ ctx = {}
2889
+ } = {}) => {
2868
2890
  const tableFilter = tables !== undefined ? new Set(tables) : undefined;
2869
2891
  const rows = {};
2870
2892
  for (const [table, reader] of readers) {
@@ -3445,5 +3467,5 @@ export {
3445
3467
  createPresenceHub
3446
3468
  };
3447
3469
 
3448
- //# debugId=07C83ADFBDD0094F64756E2164756E21
3470
+ //# debugId=62313B55CB7C7F6064756E2164756E21
3449
3471
  //# sourceMappingURL=index.js.map