@rocicorp/zero 0.22.2025070600 → 0.22.2025070701

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.
@@ -3209,7 +3209,6 @@ function applyChange(parentEntry, change, schema, relationship, format, withIDs
3209
3209
  switch (change.type) {
3210
3210
  case "add": {
3211
3211
  let newEntry;
3212
- let rc = 1;
3213
3212
  if (singular) {
3214
3213
  const oldEntry = parentEntry[relationship];
3215
3214
  if (oldEntry !== void 0) {
@@ -3217,42 +3216,40 @@ function applyChange(parentEntry, change, schema, relationship, format, withIDs
3217
3216
  schema.compareRows(oldEntry, change.node.row) === 0,
3218
3217
  `Singular relationship '${relationship}' should not have multiple rows. You may need to declare this relationship with the \`many\` helper instead of the \`one\` helper in your schema.`
3219
3218
  );
3220
- rc = oldEntry[refCountSymbol] + 1;
3219
+ oldEntry[refCountSymbol]++;
3220
+ } else {
3221
+ newEntry = makeNewMetaEntry(change.node.row, schema, withIDs, 1);
3222
+ parentEntry[relationship] = newEntry;
3221
3223
  }
3222
- newEntry = makeNewEntryWithRefCount(
3223
- change.node.row,
3224
- schema,
3225
- withIDs,
3226
- rc
3227
- );
3228
- parentEntry[relationship] = newEntry;
3229
3224
  } else {
3230
- newEntry = makeNewEntryAndInsert(
3225
+ newEntry = add(
3231
3226
  change.node.row,
3232
3227
  getChildEntryList(parentEntry, relationship),
3233
3228
  schema,
3234
3229
  withIDs
3235
3230
  );
3236
3231
  }
3237
- for (const [relationship2, children] of Object.entries(
3238
- change.node.relationships
3239
- )) {
3240
- const childSchema = must(schema.relationships[relationship2]);
3241
- const childFormat = childFormats[relationship2];
3242
- if (childFormat === void 0) {
3243
- continue;
3244
- }
3245
- const newView = childFormat.singular ? void 0 : [];
3246
- newEntry[relationship2] = newView;
3247
- for (const node of children()) {
3248
- applyChange(
3249
- newEntry,
3250
- { type: "add", node },
3251
- childSchema,
3252
- relationship2,
3253
- childFormat,
3254
- withIDs
3255
- );
3232
+ if (newEntry) {
3233
+ for (const [relationship2, children] of Object.entries(
3234
+ change.node.relationships
3235
+ )) {
3236
+ const childSchema = must(schema.relationships[relationship2]);
3237
+ const childFormat = childFormats[relationship2];
3238
+ if (childFormat === void 0) {
3239
+ continue;
3240
+ }
3241
+ const newView = childFormat.singular ? void 0 : [];
3242
+ newEntry[relationship2] = newView;
3243
+ for (const node of children()) {
3244
+ applyChange(
3245
+ newEntry,
3246
+ { type: "add", node },
3247
+ childSchema,
3248
+ relationship2,
3249
+ childFormat,
3250
+ withIDs
3251
+ );
3252
+ }
3256
3253
  }
3257
3254
  }
3258
3255
  break;
@@ -3309,50 +3306,54 @@ function applyChange(parentEntry, change, schema, relationship, format, withIDs
3309
3306
  case "edit": {
3310
3307
  if (singular) {
3311
3308
  const existing = parentEntry[relationship];
3312
- assertRCEntry(existing);
3313
- const rc = existing[refCountSymbol];
3314
- const newEntry = {
3315
- ...existing,
3316
- ...change.node.row,
3317
- [refCountSymbol]: rc
3318
- };
3319
- existing[refCountSymbol] = 0;
3320
- parentEntry[relationship] = newEntry;
3309
+ assertMetaEntry(existing);
3310
+ applyEdit(existing, change, schema, withIDs);
3321
3311
  } else {
3322
3312
  const view = getChildEntryList(parentEntry, relationship);
3323
- if (schema.compareRows(change.oldNode.row, change.node.row) === 0) {
3324
- const { pos, found } = binarySearch3(
3313
+ if (schema.compareRows(change.oldNode.row, change.node.row) !== 0) {
3314
+ const { pos: oldPos, found: oldFound } = binarySearch3(
3325
3315
  view,
3326
3316
  change.oldNode.row,
3327
3317
  schema.compareRows
3328
3318
  );
3329
- assert(found, "node does not exist");
3330
- const oldEntry = view[pos];
3331
- const rc = oldEntry[refCountSymbol];
3332
- oldEntry[refCountSymbol] = 0;
3333
- const newEntry = makeEntryPreserveRelationships(
3319
+ assert(oldFound, "old node does not exist");
3320
+ const oldEntry = view[oldPos];
3321
+ const { pos, found } = binarySearch3(
3322
+ view,
3334
3323
  change.node.row,
3335
- oldEntry,
3336
- format.relationships,
3337
- schema,
3338
- withIDs,
3339
- rc
3324
+ schema.compareRows
3340
3325
  );
3341
- view[pos] = newEntry;
3326
+ if (oldEntry[refCountSymbol] === 1 && (pos === oldPos || pos - 1 === oldPos)) {
3327
+ applyEdit(oldEntry, change, schema, withIDs);
3328
+ } else {
3329
+ oldEntry[refCountSymbol]--;
3330
+ let adjustedPos = pos;
3331
+ if (oldEntry[refCountSymbol] === 0) {
3332
+ view.splice(oldPos, 1);
3333
+ adjustedPos = oldPos < pos ? pos - 1 : pos;
3334
+ }
3335
+ let entryToEdit;
3336
+ if (found) {
3337
+ entryToEdit = view[adjustedPos];
3338
+ } else {
3339
+ view.splice(adjustedPos, 0, oldEntry);
3340
+ entryToEdit = oldEntry;
3341
+ if (oldEntry[refCountSymbol] > 0) {
3342
+ const oldEntryCopy = { ...oldEntry };
3343
+ view[oldPos] = oldEntryCopy;
3344
+ }
3345
+ }
3346
+ entryToEdit[refCountSymbol]++;
3347
+ applyEdit(entryToEdit, change, schema, withIDs);
3348
+ }
3342
3349
  } else {
3343
- const oldEntry = removeAndUpdateRefCount(
3350
+ const { pos, found } = binarySearch3(
3344
3351
  view,
3345
3352
  change.oldNode.row,
3346
3353
  schema.compareRows
3347
3354
  );
3348
- insertAndSetRefCount(
3349
- view,
3350
- change.node.row,
3351
- oldEntry,
3352
- format.relationships,
3353
- schema,
3354
- withIDs
3355
- );
3355
+ assert(found, "node does not exist");
3356
+ applyEdit(view[pos], change, schema, withIDs);
3356
3357
  }
3357
3358
  }
3358
3359
  break;
@@ -3361,39 +3362,21 @@ function applyChange(parentEntry, change, schema, relationship, format, withIDs
3361
3362
  unreachable(change);
3362
3363
  }
3363
3364
  }
3364
- function makeNewEntryAndInsert(newRow, view, schema, withIDs) {
3365
- const { pos, found } = binarySearch3(view, newRow, schema.compareRows);
3366
- let deleteCount = 0;
3367
- let rc = 1;
3368
- if (found) {
3369
- deleteCount = 1;
3370
- rc = view[pos][refCountSymbol];
3371
- view[pos][refCountSymbol] = rc - 1;
3372
- rc++;
3365
+ function applyEdit(existing, change, schema, withIDs) {
3366
+ Object.assign(existing, change.node.row);
3367
+ if (withIDs) {
3368
+ existing[idSymbol] = makeID(change.node.row, schema);
3373
3369
  }
3374
- const newEntry = makeNewEntryWithRefCount(newRow, schema, withIDs, rc);
3375
- view.splice(pos, deleteCount, newEntry);
3376
- return newEntry;
3377
3370
  }
3378
- function insertAndSetRefCount(view, newRow, oldEntry, relationships, schema, withIDs) {
3379
- const { pos, found } = binarySearch3(view, newRow, schema.compareRows);
3380
- let deleteCount = 0;
3381
- let rc = 1;
3371
+ function add(row, view, schema, withIDs) {
3372
+ const { pos, found } = binarySearch3(view, row, schema.compareRows);
3382
3373
  if (found) {
3383
- deleteCount = 1;
3384
- const oldEntry2 = view[pos];
3385
- rc = oldEntry2[refCountSymbol] + 1;
3386
- oldEntry2[refCountSymbol] = 0;
3387
- }
3388
- const newEntry = makeEntryPreserveRelationships(
3389
- newRow,
3390
- oldEntry,
3391
- relationships,
3392
- schema,
3393
- withIDs,
3394
- rc
3395
- );
3396
- view.splice(pos, deleteCount, newEntry);
3374
+ view[pos][refCountSymbol]++;
3375
+ return void 0;
3376
+ }
3377
+ const newEntry = makeNewMetaEntry(row, schema, withIDs, 1);
3378
+ view.splice(pos, 0, newEntry);
3379
+ return newEntry;
3397
3380
  }
3398
3381
  function removeAndUpdateRefCount(view, row, compareRows) {
3399
3382
  const { pos, found } = binarySearch3(view, row, compareRows);
@@ -3422,20 +3405,12 @@ function binarySearch3(view, target, comparator) {
3422
3405
  }
3423
3406
  return { pos: low, found: false };
3424
3407
  }
3425
- function makeEntryPreserveRelationships(newRow, oldEntry, relationships, schema, withIDs, rc) {
3426
- const entry = makeNewEntryWithRefCount(newRow, schema, withIDs, rc);
3427
- for (const relationship in relationships) {
3428
- assert(!(relationship in newRow), "Relationship already exists");
3429
- entry[relationship] = oldEntry[relationship];
3430
- }
3431
- return entry;
3432
- }
3433
3408
  function getChildEntryList(parentEntry, relationship) {
3434
3409
  const view = parentEntry[relationship];
3435
3410
  assertArray(view);
3436
3411
  return view;
3437
3412
  }
3438
- function assertRCEntry(v2) {
3413
+ function assertMetaEntry(v2) {
3439
3414
  assertNumber(v2[refCountSymbol]);
3440
3415
  }
3441
3416
  function getSingularEntry(parentEntry, relationship) {
@@ -3443,9 +3418,11 @@ function getSingularEntry(parentEntry, relationship) {
3443
3418
  assertNumber(e[refCountSymbol]);
3444
3419
  return e;
3445
3420
  }
3446
- function makeNewEntryWithRefCount(row, schema, withIDs, rc) {
3447
- const id = withIDs ? makeID(row, schema) : "";
3448
- return { ...row, [refCountSymbol]: rc, [idSymbol]: id };
3421
+ function makeNewMetaEntry(row, schema, withIDs, rc) {
3422
+ if (withIDs) {
3423
+ return { ...row, [refCountSymbol]: rc, [idSymbol]: makeID(row, schema) };
3424
+ }
3425
+ return { ...row, [refCountSymbol]: rc };
3449
3426
  }
3450
3427
  function makeID(row, schema) {
3451
3428
  if (schema.primaryKey.length === 1) {
@@ -7088,4 +7065,4 @@ export {
7088
7065
  toPrimaryKeyString,
7089
7066
  sourceNameFromKey
7090
7067
  };
7091
- //# sourceMappingURL=chunk-U52OBQ2U.js.map
7068
+ //# sourceMappingURL=chunk-4XUYLHLN.js.map