@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-only-when-changed.3 → 0.24.14-alpha.jp-update-cr-links-only-when-changed.4

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.
@@ -92,7 +92,6 @@ type CustomTypeFieldIdChangedMeta = NonNullable<
92
92
  NonNullable<CustomTypeUpdateHookData["updateMeta"]>["fieldIdChanged"]
93
93
  >;
94
94
 
95
- type CrCustomTypes = readonly CrCustomType[];
96
95
  type CrCustomType =
97
96
  | string
98
97
  | { id: string; fields?: readonly CrCustomTypeNestedCr[] };
@@ -188,151 +187,6 @@ export class CustomTypesManager extends BaseManager {
188
187
  };
189
188
  }
190
189
 
191
- private updateCRCustomType(
192
- args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
193
- ): CrCustomType {
194
- const [previousCustomTypeId, previousFieldId] = args.previousPath;
195
- const [newCustomTypeId, newFieldId] = args.newPath;
196
-
197
- if (!previousCustomTypeId || !newCustomTypeId) {
198
- throw new Error(
199
- "Could not find a customtype id in previousPath and/or newPath, which should not be possible.",
200
- );
201
- }
202
-
203
- if (!previousFieldId || !newFieldId) {
204
- throw new Error(
205
- "Could not find a field id in previousPath and/or newPath, which should not be possible.",
206
- );
207
- }
208
-
209
- const customType = shallowCloneIfObject(args.customType);
210
-
211
- if (typeof customType === "string" || !customType.fields) {
212
- return customType;
213
- }
214
-
215
- const matchedCustomTypeId = customType.id === previousCustomTypeId;
216
-
217
- const newFields = customType.fields.map((fieldArg) => {
218
- const customTypeField = shallowCloneIfObject(fieldArg);
219
-
220
- if (typeof customTypeField === "string") {
221
- if (
222
- matchedCustomTypeId &&
223
- customTypeField === previousFieldId &&
224
- customTypeField !== newFieldId
225
- ) {
226
- // We have reached a field id that matches the id that was renamed,
227
- // so we update it new one. The field is a string, so return the new
228
- // id.
229
- return newFieldId;
230
- }
231
-
232
- return customTypeField;
233
- }
234
-
235
- if (
236
- matchedCustomTypeId &&
237
- customTypeField.id === previousFieldId &&
238
- customTypeField.id !== newFieldId
239
- ) {
240
- // We have reached a field id that matches the id that was renamed,
241
- // so we update it new one.
242
- // Since field is not a string, we don't exit, as we might have
243
- // something to update further down in customtypes.
244
- customTypeField.id = newFieldId;
245
- }
246
-
247
- return {
248
- ...customTypeField,
249
- customtypes: customTypeField.customtypes.map((customTypeArg) => {
250
- const nestedCustomType = shallowCloneIfObject(customTypeArg);
251
-
252
- if (
253
- typeof nestedCustomType === "string" ||
254
- !nestedCustomType.fields ||
255
- // Since we are on the last level, if we don't start matching right
256
- // at the custom type id, we can return exit early because it's not
257
- // a match.
258
- nestedCustomType.id !== previousCustomTypeId
259
- ) {
260
- return nestedCustomType;
261
- }
262
-
263
- return {
264
- ...nestedCustomType,
265
- fields: nestedCustomType.fields.map((fieldArg) => {
266
- const nestedCustomTypeField = shallowCloneIfObject(fieldArg);
267
-
268
- if (
269
- nestedCustomTypeField === previousFieldId &&
270
- nestedCustomTypeField !== newFieldId
271
- ) {
272
- // Matches the previous id, so we update it and return because
273
- // it's the last level.
274
- return newFieldId;
275
- }
276
-
277
- return nestedCustomTypeField;
278
- }),
279
- };
280
- }),
281
- };
282
- });
283
-
284
- return { ...customType, fields: newFields };
285
- }
286
-
287
- /**
288
- * Map over the custom types of a Content Relationship Link and update the API
289
- * IDs that were changed during the custom type update.
290
- */
291
- private updateCRCustomTypes(
292
- args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
293
- ): CrCustomTypes {
294
- const { customTypes, ...updateMeta } = args;
295
-
296
- return customTypes.map((customType) => {
297
- return this.updateCRCustomType({ customType, ...updateMeta });
298
- });
299
- }
300
-
301
- /**
302
- * Update the Content Relationship API IDs of a single field. The change is
303
- * determined by the `previousPath` and `newPath` properties.
304
- */
305
- private updateFieldContentRelationships<
306
- T extends UID | NestableWidget | Group | NestedGroup,
307
- >(
308
- args: { field: T } & CustomTypeFieldIdChangedMeta,
309
- ): { field: T; changed: boolean } {
310
- const { field, ...updateMeta } = args;
311
- if (
312
- field.type !== "Link" ||
313
- field.config?.select !== "document" ||
314
- !field.config?.customtypes
315
- ) {
316
- // not a content relationship field
317
- return { field, changed: false };
318
- }
319
-
320
- const newField = {
321
- ...field,
322
- config: {
323
- ...field.config,
324
- customtypes: this.updateCRCustomTypes({
325
- ...updateMeta,
326
- customTypes: field.config.customtypes,
327
- }),
328
- },
329
- };
330
-
331
- const changed = JSON.stringify(field) !== JSON.stringify(newField);
332
-
333
- return { field: newField, changed };
334
- }
335
-
336
190
  /**
337
191
  * Update the Content Relationship API IDs for all existing custom types and
338
192
  * slices. The change is determined by properties inside the `updateMeta`
@@ -357,34 +211,20 @@ export class CustomTypesManager extends BaseManager {
357
211
  // Find existing content relationships that link to the renamed field id in
358
212
  // any custom type and update them to use the new one.
359
213
  const customTypes = await this.readAllCustomTypes();
360
- for (const customType of customTypes.models) {
361
- // Keep track of whether the model has changed to avoid calling the
362
- // update hook if nothing has changed
363
- let customTypeHasChanged = false;
364
-
365
- const updatedCustomTypeModel = traverseCustomType({
366
- customType: customType.model,
367
- onField: ({ field }) => {
368
- const update = this.updateFieldContentRelationships({
369
- field,
370
- previousPath,
371
- newPath,
372
- });
373
-
374
- customTypeHasChanged ||= update.changed;
375
-
376
- return update.field;
377
- },
378
- });
379
214
 
380
- if (customTypeHasChanged) {
381
- crUpdates.push(
382
- this.sliceMachinePluginRunner.callHook("custom-type:update", {
383
- model: updatedCustomTypeModel,
215
+ updateCustomTypeContentRelationships({
216
+ models: customTypes.models,
217
+ onUpdate: (model) => {
218
+ pushIfDefined(
219
+ crUpdates,
220
+ this.sliceMachinePluginRunner?.callHook("custom-type:update", {
221
+ model,
384
222
  }),
385
223
  );
386
- }
387
- }
224
+ },
225
+ previousPath,
226
+ newPath,
227
+ });
388
228
 
389
229
  // Find existing slice with content relationships that link to the renamed
390
230
  // field id in all libraries and update them to use the new one.
@@ -395,36 +235,20 @@ export class CustomTypesManager extends BaseManager {
395
235
  libraryID: library.libraryID,
396
236
  });
397
237
 
398
- for (const slice of slices.models) {
399
- // Keep track of whether the model has changed to avoid calling the
400
- // update hook if nothing has changed
401
- let sliceHasChanged = false;
402
-
403
- const updatedSliceModel = traverseSharedSlice({
404
- path: ["."],
405
- slice: slice.model,
406
- onField: ({ field }) => {
407
- const update = this.updateFieldContentRelationships({
408
- field,
409
- previousPath,
410
- newPath,
411
- });
412
-
413
- sliceHasChanged ||= update.changed;
414
-
415
- return update.field;
416
- },
417
- });
418
-
419
- if (sliceHasChanged) {
420
- crUpdates.push(
421
- this.sliceMachinePluginRunner.callHook("slice:update", {
238
+ updateSharedSliceContentRelationships({
239
+ models: slices.models,
240
+ onUpdate: (model) => {
241
+ pushIfDefined(
242
+ crUpdates,
243
+ this.sliceMachinePluginRunner?.callHook("slice:update", {
422
244
  libraryID: library.libraryID,
423
- model: updatedSliceModel,
245
+ model,
424
246
  }),
425
247
  );
426
- }
427
- }
248
+ },
249
+ previousPath,
250
+ newPath,
251
+ });
428
252
  }
429
253
 
430
254
  // Process all the Content Relationship updates at once.
@@ -670,6 +494,206 @@ const InferSliceResponse = z.object({
670
494
  langSmithUrl: z.string().url().optional(),
671
495
  });
672
496
 
497
+ function updateCRCustomType(
498
+ args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
499
+ ): CrCustomType {
500
+ const [previousCustomTypeId, previousFieldId] = args.previousPath;
501
+ const [newCustomTypeId, newFieldId] = args.newPath;
502
+
503
+ if (!previousCustomTypeId || !newCustomTypeId) {
504
+ throw new Error(
505
+ "Could not find a customtype id in previousPath and/or newPath, which should not be possible.",
506
+ );
507
+ }
508
+
509
+ if (!previousFieldId || !newFieldId) {
510
+ throw new Error(
511
+ "Could not find a field id in previousPath and/or newPath, which should not be possible.",
512
+ );
513
+ }
514
+
515
+ const customType = shallowCloneIfObject(args.customType);
516
+
517
+ if (typeof customType === "string" || !customType.fields) {
518
+ return customType;
519
+ }
520
+
521
+ const matchedCustomTypeId = customType.id === previousCustomTypeId;
522
+
523
+ const newFields = customType.fields.map((fieldArg) => {
524
+ const customTypeField = shallowCloneIfObject(fieldArg);
525
+
526
+ if (typeof customTypeField === "string") {
527
+ if (
528
+ matchedCustomTypeId &&
529
+ customTypeField === previousFieldId &&
530
+ customTypeField !== newFieldId
531
+ ) {
532
+ // We have reached a field id that matches the id that was renamed,
533
+ // so we update it new one. The field is a string, so return the new
534
+ // id.
535
+ return newFieldId;
536
+ }
537
+
538
+ return customTypeField;
539
+ }
540
+
541
+ if (
542
+ matchedCustomTypeId &&
543
+ customTypeField.id === previousFieldId &&
544
+ customTypeField.id !== newFieldId
545
+ ) {
546
+ // We have reached a field id that matches the id that was renamed,
547
+ // so we update it new one.
548
+ // Since field is not a string, we don't exit, as we might have
549
+ // something to update further down in customtypes.
550
+ customTypeField.id = newFieldId;
551
+ }
552
+
553
+ return {
554
+ ...customTypeField,
555
+ customtypes: customTypeField.customtypes.map((customTypeArg) => {
556
+ const nestedCustomType = shallowCloneIfObject(customTypeArg);
557
+
558
+ if (
559
+ typeof nestedCustomType === "string" ||
560
+ !nestedCustomType.fields ||
561
+ // Since we are on the last level, if we don't start matching right
562
+ // at the custom type id, we can return exit early because it's not
563
+ // a match.
564
+ nestedCustomType.id !== previousCustomTypeId
565
+ ) {
566
+ return nestedCustomType;
567
+ }
568
+
569
+ return {
570
+ ...nestedCustomType,
571
+ fields: nestedCustomType.fields.map((fieldArg) => {
572
+ const nestedCustomTypeField = shallowCloneIfObject(fieldArg);
573
+
574
+ if (
575
+ nestedCustomTypeField === previousFieldId &&
576
+ nestedCustomTypeField !== newFieldId
577
+ ) {
578
+ // Matches the previous id, so we update it and return because
579
+ // it's the last level.
580
+ return newFieldId;
581
+ }
582
+
583
+ return nestedCustomTypeField;
584
+ }),
585
+ };
586
+ }),
587
+ };
588
+ });
589
+
590
+ return { ...customType, fields: newFields };
591
+ }
592
+
593
+ /**
594
+ * Update the Content Relationship API IDs of a single field. The change is
595
+ * determined by the `previousPath` and `newPath` properties.
596
+ */
597
+ function updateFieldContentRelationships<
598
+ T extends UID | NestableWidget | Group | NestedGroup,
599
+ >(
600
+ args: { field: T } & CustomTypeFieldIdChangedMeta,
601
+ ): { field: T; changed: boolean } {
602
+ const { field, ...updateMeta } = args;
603
+ if (
604
+ field.type !== "Link" ||
605
+ field.config?.select !== "document" ||
606
+ !field.config?.customtypes
607
+ ) {
608
+ // not a content relationship field
609
+ return { field, changed: false };
610
+ }
611
+
612
+ const newCustomTypes = field.config.customtypes.map((customType) => {
613
+ return updateCRCustomType({ customType, ...updateMeta });
614
+ });
615
+
616
+ return {
617
+ field: {
618
+ ...field,
619
+ config: { ...field.config, customtypes: newCustomTypes },
620
+ },
621
+ // the size and complexity of a field is small, so JSON.stringify is fine
622
+ changed:
623
+ JSON.stringify(field.config.customtypes) !==
624
+ JSON.stringify(newCustomTypes),
625
+ };
626
+ }
627
+
628
+ export function updateCustomTypeContentRelationships(
629
+ args: {
630
+ models: { model: CustomType }[];
631
+ onUpdate: (model: CustomType) => void;
632
+ } & CustomTypeFieldIdChangedMeta,
633
+ ): void {
634
+ const { models, previousPath, newPath, onUpdate } = args;
635
+
636
+ for (const customType of models) {
637
+ // Keep track of whether the model has changed to avoid calling the
638
+ // update hook if nothing has changed
639
+ let changed = false;
640
+
641
+ const updatedCustomTypeModel = traverseCustomType({
642
+ customType: customType.model,
643
+ onField: ({ field }) => {
644
+ const update = updateFieldContentRelationships({
645
+ field,
646
+ previousPath,
647
+ newPath,
648
+ });
649
+
650
+ changed ||= update.changed;
651
+
652
+ return update.field;
653
+ },
654
+ });
655
+
656
+ if (changed) {
657
+ onUpdate(updatedCustomTypeModel);
658
+ }
659
+ }
660
+ }
661
+
662
+ export function updateSharedSliceContentRelationships(
663
+ args: {
664
+ models: { model: SharedSlice }[];
665
+ onUpdate: (model: SharedSlice) => void;
666
+ } & CustomTypeFieldIdChangedMeta,
667
+ ): void {
668
+ const { models, previousPath, newPath, onUpdate } = args;
669
+
670
+ for (const slice of models) {
671
+ // Keep track of whether the model has changed to avoid calling the
672
+ // update hook if nothing has changed
673
+ let changed = false;
674
+
675
+ const updatedSliceModel = traverseSharedSlice({
676
+ path: ["."],
677
+ slice: slice.model,
678
+ onField: ({ field }) => {
679
+ const update = updateFieldContentRelationships({
680
+ field,
681
+ previousPath,
682
+ newPath,
683
+ });
684
+
685
+ changed ||= update.changed;
686
+
687
+ return update.field;
688
+ },
689
+ });
690
+
691
+ if (changed) {
692
+ onUpdate(updatedSliceModel);
693
+ }
694
+ }
695
+ }
696
+
673
697
  function shallowCloneIfObject<T>(value: T): T {
674
698
  if (typeof value === "object") {
675
699
  return { ...value };
@@ -677,3 +701,9 @@ function shallowCloneIfObject<T>(value: T): T {
677
701
 
678
702
  return value;
679
703
  }
704
+
705
+ function pushIfDefined<T>(array: T[], value: T | undefined) {
706
+ if (value) {
707
+ array.push(value);
708
+ }
709
+ }