@syncular/client 0.77.3 → 0.78.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/client.js CHANGED
@@ -168,6 +168,8 @@ export class SyncClient {
168
168
  #transportEnabled;
169
169
  #transportEpoch = 0;
170
170
  #registrationEpoch = 0;
171
+ /** The units of each window base's latest `setWindow` (§4.8). */
172
+ #windowTargets = new Map();
171
173
  #now;
172
174
  #outcomeRetentionMaxEntries;
173
175
  #retainFailedCommits;
@@ -336,8 +338,7 @@ export class SyncClient {
336
338
  ? 'unsupported'
337
339
  : 'disconnected';
338
340
  this.#encryption = config.encryption;
339
- this.#securityLifecycle =
340
- config.securityPreflight === true ? 'preflight' : 'active';
341
+ this.#securityLifecycle = config.securityPreflight ? 'preflight' : 'active';
341
342
  this.#now = config.now ?? Date.now;
342
343
  const outcomeRetentionMaxEntries = config.limits?.outcomeRetentionMaxEntries ?? 1_000;
343
344
  if (!Number.isSafeInteger(outcomeRetentionMaxEntries) ||
@@ -2082,43 +2083,44 @@ export class SyncClient {
2082
2083
  async setWindowCommand(base, units) {
2083
2084
  this.#requireActive();
2084
2085
  let changed = false;
2085
- // Serialize the whole edit: a shrink yields between eviction chunks, so
2086
- // without the chain a delta apply or another setWindow could interleave.
2087
- await this.#serialize(async () => {
2088
- const baseKey = windowBaseKey(base);
2089
- const wanted = new Set(units);
2090
- const live = loadWindowUnits(this.#db, baseKey);
2091
- const held = new Set(live.map((entry) => entry.value));
2092
- for (const value of wanted) {
2093
- if (held.has(value))
2094
- continue;
2095
- this.#applyBatch((batch) => {
2096
- const { id, changed: registered } = this.#holdUnit(windowUnitScopes(base, value), false);
2097
- this.#db.exec(`INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`, [baseKey, value, id]);
2098
- if (registered)
2099
- this.#needsPull = true;
2100
- batch.window(baseKey, value);
2101
- batch.status();
2102
- });
2103
- changed = true;
2104
- this.#registrationEpoch += 1;
2105
- }
2106
- for (const { value, unitId: id } of live) {
2107
- if (wanted.has(value))
2108
- continue;
2109
- await this.#releaseWindowUnit(baseKey, value, id);
2110
- changed = true;
2111
- this.#registrationEpoch += 1;
2112
- }
2113
- });
2114
- const effects = {
2115
- sync: this.#transportEnabled && changed
2116
- ? { kind: 'interactive' }
2117
- : { kind: 'none' },
2086
+ const baseKey = windowBaseKey(base);
2087
+ const wanted = new Set(units);
2088
+ const live = loadWindowUnits(this.#db, baseKey);
2089
+ // §7.8: a widen registers like `subscribe` and never waits for a
2090
+ // running round.
2091
+ for (const value of wanted) {
2092
+ if (live.some((entry) => entry.value === value))
2093
+ continue;
2094
+ this.#applyBatch((batch) => {
2095
+ const { id, changed: registered } = this.#holdUnit(windowUnitScopes(base, value), false);
2096
+ this.#db.exec(`INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`, [baseKey, value, id]);
2097
+ this.#needsPull ||= registered;
2098
+ batch.window(baseKey, value);
2099
+ batch.status();
2100
+ });
2101
+ changed = true;
2102
+ this.#registrationEpoch += 1;
2103
+ }
2104
+ this.#windowTargets.set(baseKey, wanted);
2105
+ // A shrink yields between eviction chunks, so it runs on the chain; a
2106
+ // later change of the base supersedes a release that has not run.
2107
+ const leaving = live.filter((entry) => !wanted.has(entry.value));
2108
+ if (leaving.length > 0)
2109
+ await this.#serialize(async () => {
2110
+ for (const { value, unitId: id } of leaving) {
2111
+ if (this.#windowTargets.get(baseKey) !== wanted)
2112
+ return;
2113
+ await this.#releaseWindowUnit(baseKey, value, id);
2114
+ changed = true;
2115
+ this.#registrationEpoch += 1;
2116
+ }
2117
+ });
2118
+ const sync = {
2119
+ kind: this.#transportEnabled && changed ? 'interactive' : 'none',
2118
2120
  };
2119
- if (effects.sync.kind === 'interactive')
2120
- this.#emitSyncIntent(effects.sync);
2121
- return { value: undefined, effects };
2121
+ if (sync.kind !== 'none')
2122
+ this.#emitSyncIntent(sync);
2123
+ return { value: undefined, effects: { sync } };
2122
2124
  }
2123
2125
  /**
2124
2126
  * The completeness oracle (§4.8): which values of a base are windowed-in
@@ -3226,7 +3228,7 @@ export class SyncClient {
3226
3228
  this.#roundFailureCode = unseen;
3227
3229
  // §7.8: a round the drive deadline aborted ends with the deadline's
3228
3230
  // error, whichever transport call observed the abort.
3229
- const error = signal?.aborted === true ? signal.reason : caught;
3231
+ const error = signal?.aborted ? signal.reason : caught;
3230
3232
  // §7.3.5: a request-level lease code stops-and-surfaces — record it
3231
3233
  // in leaseState (no local-data purge, §7.3.4) and re-throw. Not a
3232
3234
  // silent retry: the app drives recovery to a live resolver. The
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/client",
3
- "version": "0.77.3",
3
+ "version": "0.78.0",
4
4
  "description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -121,11 +121,11 @@
121
121
  },
122
122
  "dependencies": {
123
123
  "@sqlite.org/sqlite-wasm": "^3.53.0-build1",
124
- "@syncular/core": "0.77.3"
124
+ "@syncular/core": "0.78.0"
125
125
  },
126
126
  "devDependencies": {
127
127
  "@electric-sql/pglite": "^0.5.4",
128
- "@syncular/server": "0.77.3",
128
+ "@syncular/server": "0.78.0",
129
129
  "playwright": "1.63.0",
130
130
  "vite": "8.3.4"
131
131
  }
package/src/client.ts CHANGED
@@ -127,7 +127,6 @@ import {
127
127
  ChangeAccumulator,
128
128
  ChangeEmitter,
129
129
  type ClientChangeListener,
130
- type CommandEffects,
131
130
  type CommandResult,
132
131
  InvalidationEmitter,
133
132
  type InvalidationListener,
@@ -957,6 +956,8 @@ export class SyncClient {
957
956
  #transportEnabled: boolean;
958
957
  #transportEpoch = 0;
959
958
  #registrationEpoch = 0;
959
+ /** The units of each window base's latest `setWindow` (§4.8). */
960
+ readonly #windowTargets = new Map<string, ReadonlySet<string>>();
960
961
  readonly #now: () => number;
961
962
  readonly #outcomeRetentionMaxEntries: number;
962
963
  readonly #retainFailedCommits: boolean;
@@ -1151,8 +1152,7 @@ export class SyncClient {
1151
1152
  ? 'unsupported'
1152
1153
  : 'disconnected';
1153
1154
  this.#encryption = config.encryption;
1154
- this.#securityLifecycle =
1155
- config.securityPreflight === true ? 'preflight' : 'active';
1155
+ this.#securityLifecycle = config.securityPreflight ? 'preflight' : 'active';
1156
1156
  this.#now = config.now ?? Date.now;
1157
1157
  const outcomeRetentionMaxEntries =
1158
1158
  config.limits?.outcomeRetentionMaxEntries ?? 1_000;
@@ -3242,46 +3242,47 @@ export class SyncClient {
3242
3242
  ): Promise<CommandResult<void>> {
3243
3243
  this.#requireActive();
3244
3244
  let changed = false;
3245
- // Serialize the whole edit: a shrink yields between eviction chunks, so
3246
- // without the chain a delta apply or another setWindow could interleave.
3247
- await this.#serialize(async () => {
3248
- const baseKey = windowBaseKey(base);
3249
- const wanted = new Set(units);
3250
- const live = loadWindowUnits(this.#db, baseKey);
3251
- const held = new Set(live.map((entry) => entry.value));
3252
- for (const value of wanted) {
3253
- if (held.has(value)) continue;
3254
- this.#applyBatch((batch) => {
3255
- const { id, changed: registered } = this.#holdUnit(
3256
- windowUnitScopes(base, value),
3257
- false,
3258
- );
3259
- this.#db.exec(
3260
- `INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`,
3261
- [baseKey, value, id],
3262
- );
3263
- if (registered) this.#needsPull = true;
3264
- batch.window(baseKey, value);
3265
- batch.status();
3266
- });
3267
- changed = true;
3268
- this.#registrationEpoch += 1;
3269
- }
3270
- for (const { value, unitId: id } of live) {
3271
- if (wanted.has(value)) continue;
3272
- await this.#releaseWindowUnit(baseKey, value, id);
3273
- changed = true;
3274
- this.#registrationEpoch += 1;
3275
- }
3276
- });
3277
- const effects: CommandEffects = {
3278
- sync:
3279
- this.#transportEnabled && changed
3280
- ? { kind: 'interactive' }
3281
- : { kind: 'none' },
3245
+ const baseKey = windowBaseKey(base);
3246
+ const wanted = new Set(units);
3247
+ const live = loadWindowUnits(this.#db, baseKey);
3248
+ // §7.8: a widen registers like `subscribe` and never waits for a
3249
+ // running round.
3250
+ for (const value of wanted) {
3251
+ if (live.some((entry) => entry.value === value)) continue;
3252
+ this.#applyBatch((batch) => {
3253
+ const { id, changed: registered } = this.#holdUnit(
3254
+ windowUnitScopes(base, value),
3255
+ false,
3256
+ );
3257
+ this.#db.exec(
3258
+ `INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`,
3259
+ [baseKey, value, id],
3260
+ );
3261
+ this.#needsPull ||= registered;
3262
+ batch.window(baseKey, value);
3263
+ batch.status();
3264
+ });
3265
+ changed = true;
3266
+ this.#registrationEpoch += 1;
3267
+ }
3268
+ this.#windowTargets.set(baseKey, wanted);
3269
+ // A shrink yields between eviction chunks, so it runs on the chain; a
3270
+ // later change of the base supersedes a release that has not run.
3271
+ const leaving = live.filter((entry) => !wanted.has(entry.value));
3272
+ if (leaving.length > 0)
3273
+ await this.#serialize(async () => {
3274
+ for (const { value, unitId: id } of leaving) {
3275
+ if (this.#windowTargets.get(baseKey) !== wanted) return;
3276
+ await this.#releaseWindowUnit(baseKey, value, id);
3277
+ changed = true;
3278
+ this.#registrationEpoch += 1;
3279
+ }
3280
+ });
3281
+ const sync: SyncIntent = {
3282
+ kind: this.#transportEnabled && changed ? 'interactive' : 'none',
3282
3283
  };
3283
- if (effects.sync.kind === 'interactive') this.#emitSyncIntent(effects.sync);
3284
- return { value: undefined, effects };
3284
+ if (sync.kind !== 'none') this.#emitSyncIntent(sync);
3285
+ return { value: undefined, effects: { sync } };
3285
3286
  }
3286
3287
 
3287
3288
  /**
@@ -4673,7 +4674,7 @@ export class SyncClient {
4673
4674
  if (unseen !== undefined) this.#roundFailureCode = unseen;
4674
4675
  // §7.8: a round the drive deadline aborted ends with the deadline's
4675
4676
  // error, whichever transport call observed the abort.
4676
- const error: unknown = signal?.aborted === true ? signal.reason : caught;
4677
+ const error: unknown = signal?.aborted ? signal.reason : caught;
4677
4678
  // §7.3.5: a request-level lease code stops-and-surfaces — record it
4678
4679
  // in leaseState (no local-data purge, §7.3.4) and re-throw. Not a
4679
4680
  // silent retry: the app drives recovery to a live resolver. The