@asyra/props-manager 0.5.3 → 0.6.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +104 -0
  2. package/README.md +9 -5
  3. package/dist/components/base.d.ts.map +1 -1
  4. package/dist/components/base.js.map +1 -1
  5. package/dist/factories/create-property.d.ts.map +1 -1
  6. package/dist/factories/create-property.js.map +1 -1
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/manager/component-accessor.d.ts +1 -0
  11. package/dist/manager/component-accessor.d.ts.map +1 -1
  12. package/dist/manager/component-accessor.js +3 -0
  13. package/dist/manager/component-accessor.js.map +1 -1
  14. package/dist/manager/property-intents.d.ts +75 -0
  15. package/dist/manager/property-intents.d.ts.map +1 -0
  16. package/dist/manager/property-intents.js +681 -0
  17. package/dist/manager/property-intents.js.map +1 -0
  18. package/dist/manager/props-manager.d.ts +24 -2
  19. package/dist/manager/props-manager.d.ts.map +1 -1
  20. package/dist/manager/props-manager.js +566 -50
  21. package/dist/manager/props-manager.js.map +1 -1
  22. package/dist/manager/subscribes.d.ts.map +1 -1
  23. package/dist/manager/subscribes.js +19 -0
  24. package/dist/manager/subscribes.js.map +1 -1
  25. package/dist/registries/declarative-property-type.d.ts.map +1 -1
  26. package/dist/registries/declarative-property-type.js +119 -23
  27. package/dist/registries/declarative-property-type.js.map +1 -1
  28. package/dist/registries/property-component.d.ts.map +1 -1
  29. package/dist/registries/property-component.js.map +1 -1
  30. package/dist/registries/property-definition-value.d.ts.map +1 -1
  31. package/dist/registries/property-definition-value.js.map +1 -1
  32. package/dist/registries/property-definition.d.ts.map +1 -1
  33. package/dist/registries/property-definition.js +2 -1
  34. package/dist/registries/property-definition.js.map +1 -1
  35. package/dist/registries/property-inheritance.d.ts +9 -0
  36. package/dist/registries/property-inheritance.d.ts.map +1 -0
  37. package/dist/registries/property-inheritance.js +164 -0
  38. package/dist/registries/property-inheritance.js.map +1 -0
  39. package/dist/registries/property-registration.d.ts.map +1 -1
  40. package/dist/registries/property-registration.js.map +1 -1
  41. package/dist/registries/property-schema.d.ts.map +1 -1
  42. package/dist/registries/property-schema.js.map +1 -1
  43. package/dist/registries/property-value-kind.d.ts +2 -1
  44. package/dist/registries/property-value-kind.d.ts.map +1 -1
  45. package/dist/registries/property-value-kind.js +20 -0
  46. package/dist/registries/property-value-kind.js.map +1 -1
  47. package/dist/registries/state.d.ts.map +1 -1
  48. package/dist/registries/state.js.map +1 -1
  49. package/package.json +6 -6
@@ -0,0 +1,681 @@
1
+ import { id, IDTypes, isRecord } from '@asyra/utils';
2
+ import * as lodashModule from 'lodash';
3
+ import { getPropertyComponentConfigDefinition, resolvePropertyComponentConfigRoles } from '../registries/property-component.js';
4
+ import { isRuntimePropertyFieldValueValid } from '../registries/property-value-kind.js';
5
+ import elementPropertyRegistry from '../registries/property-definition.js';
6
+ import { getPropertySchema } from '../registries/property-schema.js';
7
+ import { readPropertyInheritanceDependencies } from '../registries/property-inheritance.js';
8
+ const isEqual = (lodashModule.default ?? lodashModule).isEqual;
9
+ const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
10
+ const fail = (message) => {
11
+ throw new Error(`[PropsManager] ${message}`);
12
+ };
13
+ const requireCapability = (raw) => {
14
+ const config = getPropertyComponentConfigDefinition(raw.type);
15
+ const schema = getPropertySchema(raw.type);
16
+ if (!config || !schema) {
17
+ return fail(`Property "${raw.id}" has no declarative intention capability`);
18
+ }
19
+ return { config, schema };
20
+ };
21
+ const createDraft = (manager) => {
22
+ const originals = new Map();
23
+ const drafts = new Map();
24
+ const created = new Map();
25
+ const read = (propertyId) => {
26
+ const known = drafts.get(propertyId);
27
+ if (known) {
28
+ return known;
29
+ }
30
+ const property = manager.getPropertyById(propertyId);
31
+ if (!property) {
32
+ return fail(`Missing property "${propertyId}"`);
33
+ }
34
+ const raw = structuredClone(property.save());
35
+ requireCapability(raw);
36
+ originals.set(propertyId, structuredClone(raw));
37
+ drafts.set(propertyId, raw);
38
+ return raw;
39
+ };
40
+ const fields = (raw) => {
41
+ const { config } = requireCapability(raw);
42
+ const basis = raw.inheritance ? read(raw.inheritance.sourceId) : raw;
43
+ const result = {};
44
+ for (const key of resolvePropertyComponentConfigRoles(config).persistKeys) {
45
+ const value = raw.inheritance && own(raw.inheritance.overrides, key)
46
+ ? raw.inheritance.overrides[key]
47
+ : basis[key];
48
+ if (value !== undefined) {
49
+ result[key] = structuredClone(value);
50
+ }
51
+ }
52
+ const children = config.children;
53
+ if (raw.inheritance && children && Array.isArray(result[children.key])) {
54
+ result[children.key] = result[children.key].map((childId) => raw.inheritance?.childBindings[childId] ?? childId);
55
+ }
56
+ return result;
57
+ };
58
+ const allocate = (raw, requestedId) => {
59
+ if (requestedId !== undefined &&
60
+ (!requestedId ||
61
+ manager.getPropertyById(requestedId) ||
62
+ manager.getRestoreComponentById(requestedId) ||
63
+ drafts.has(requestedId))) {
64
+ return fail('Unavailable explicit child identity');
65
+ }
66
+ let propertyId = requestedId ?? id(IDTypes.PROPS);
67
+ while (manager.getPropertyById(propertyId) ||
68
+ manager.getRestoreComponentById(propertyId) ||
69
+ drafts.has(propertyId)) {
70
+ propertyId = id(IDTypes.PROPS);
71
+ }
72
+ const value = { ...raw, id: propertyId };
73
+ created.set(propertyId, value);
74
+ drafts.set(propertyId, value);
75
+ return value;
76
+ };
77
+ const inherit = (raw, isolate = false) => {
78
+ if (raw.inheritance) {
79
+ return isolate
80
+ ? allocate({
81
+ type: raw.type,
82
+ inheritance: structuredClone(raw.inheritance)
83
+ })
84
+ : raw;
85
+ }
86
+ raw.sharedSource = true;
87
+ return allocate({
88
+ type: raw.type,
89
+ inheritance: { sourceId: raw.id, overrides: {}, childBindings: {} }
90
+ });
91
+ };
92
+ const locate = (target) => {
93
+ if (!isRecord(target) ||
94
+ !isRecord(target.root) ||
95
+ !Array.isArray(target.path)) {
96
+ fail('Invalid property target');
97
+ }
98
+ if (typeof target.sharedRoot !== 'boolean' ||
99
+ [
100
+ target.root.componentId,
101
+ target.root.ownerElementId,
102
+ target.root.ownerElementType,
103
+ target.root.ownerPropertyName
104
+ ].some((value) => typeof value !== 'string' || !value)) {
105
+ fail('Invalid canonical owner target');
106
+ }
107
+ let raw = read(target.root.componentId);
108
+ const definition = elementPropertyRegistry.getForComponent(target.root.ownerElementType, target.root.ownerPropertyName);
109
+ if (!definition || definition.type !== raw.type) {
110
+ fail('Target does not match the registered owner property');
111
+ }
112
+ const chain = [];
113
+ for (const step of target.path) {
114
+ const relation = requireCapability(raw).config.children;
115
+ if (!relation ||
116
+ relation.key !== step.key ||
117
+ typeof step.childId !== 'string') {
118
+ return fail('Invalid registered child path');
119
+ }
120
+ const basis = raw.inheritance ? read(raw.inheritance.sourceId) : raw;
121
+ const childIds = raw.inheritance && own(raw.inheritance.overrides, step.key)
122
+ ? raw.inheritance.overrides[step.key]
123
+ : basis[step.key];
124
+ if (!Array.isArray(childIds) || !childIds.includes(step.childId)) {
125
+ fail(`Missing logical child "${step.childId}"`);
126
+ }
127
+ chain.push({ parent: raw, key: step.key, logicalId: step.childId });
128
+ raw = read(raw.inheritance?.childBindings[step.childId] ?? step.childId);
129
+ if (raw.type !== relation.childType) {
130
+ fail('Wrong registered child type');
131
+ }
132
+ }
133
+ if ((target.expectedPropertyId !== undefined &&
134
+ target.expectedPropertyId !== raw.id) ||
135
+ (target.expectedSourceId !== undefined &&
136
+ target.expectedSourceId !== raw.inheritance?.sourceId)) {
137
+ fail('Stale retained property target');
138
+ }
139
+ return { raw, chain };
140
+ };
141
+ return { originals, drafts, created, read, fields, allocate, inherit, locate };
142
+ };
143
+ export const readPropertyTargets = (manager, targets) => {
144
+ const draft = createDraft(manager);
145
+ return targets.map((target) => {
146
+ const { raw } = draft.locate(target);
147
+ let mode = 'independent';
148
+ if (raw.sharedSource) {
149
+ mode = 'source';
150
+ }
151
+ if (raw.inheritance) {
152
+ mode = 'source-backed';
153
+ }
154
+ const relation = requireCapability(raw).config.children;
155
+ const basis = raw.inheritance ? draft.read(raw.inheritance.sourceId) : raw;
156
+ let members = [];
157
+ if (relation) {
158
+ members = basis[relation.key];
159
+ if (raw.inheritance && own(raw.inheritance.overrides, relation.key)) {
160
+ members = raw.inheritance.overrides[relation.key];
161
+ }
162
+ }
163
+ const children = relation && Array.isArray(members)
164
+ ? members.map((childId) => ({
165
+ key: relation.key,
166
+ childId,
167
+ propertyId: raw.inheritance?.childBindings[childId] ?? childId
168
+ }))
169
+ : [];
170
+ return {
171
+ target: structuredClone(target),
172
+ propertyId: raw.id,
173
+ type: raw.type,
174
+ mode,
175
+ ...(raw.inheritance ? { sourceId: raw.inheritance.sourceId } : {}),
176
+ overrides: structuredClone(raw.inheritance?.overrides ?? {}),
177
+ effective: draft.fields(raw),
178
+ children
179
+ };
180
+ });
181
+ };
182
+ export const preparePropertyIntentBatch = (manager, intents, options, hasOtherOwners, directOperations = []) => {
183
+ const draft = createDraft(manager);
184
+ const roots = new Map();
185
+ const writes = new Map();
186
+ const identity = (target) => JSON.stringify([target.root.ownerElementId, target.root.ownerPropertyName]);
187
+ // Resolve every original target before allocating or changing the transient draft.
188
+ const kindsByTarget = new Map();
189
+ intents.forEach((intent) => {
190
+ draft.locate(intent.target);
191
+ const key = JSON.stringify([identity(intent.target), intent.target.path]);
192
+ const kinds = kindsByTarget.get(key) ?? new Set();
193
+ kinds.add(intent.kind);
194
+ if (kinds.size > 1 && (kinds.has('detach') || kinds.has('share'))) {
195
+ fail('Conflicting identity and field intentions');
196
+ }
197
+ kindsByTarget.set(key, kinds);
198
+ });
199
+ const isolated = (target, leafBinding) => {
200
+ const key = identity(target);
201
+ let root = roots.get(key);
202
+ if (!root) {
203
+ const before = draft.read(target.root.componentId);
204
+ root = { target, before, after: before };
205
+ roots.set(key, root);
206
+ }
207
+ else if (root.target.root.componentId !== target.root.componentId) {
208
+ fail('Conflicting owner root identities');
209
+ }
210
+ if (target.path.length ||
211
+ (leafBinding && (target.sharedRoot || root.after.sharedSource))) {
212
+ root.after = draft.inherit(root.after, target.sharedRoot && root.after === root.before);
213
+ }
214
+ let current = root.after;
215
+ let parent;
216
+ let logicalId;
217
+ for (let i = 0; i < target.path.length; i++) {
218
+ const step = target.path[i];
219
+ parent = current;
220
+ logicalId = step.childId;
221
+ const child = draft.read(current.inheritance?.childBindings[step.childId] ?? step.childId);
222
+ const mustBind = i < target.path.length - 1 || leafBinding;
223
+ const ownedChild = current.inheritance?.childBindings[step.childId] === child.id;
224
+ const sharedChild = hasOtherOwners(child.id, current.id);
225
+ if (!mustBind) {
226
+ current = child;
227
+ }
228
+ else if (ownedChild &&
229
+ !sharedChild &&
230
+ !child.sharedSource &&
231
+ !child.inheritance &&
232
+ i === target.path.length - 1) {
233
+ current = child;
234
+ }
235
+ else {
236
+ current = draft.inherit(child, !ownedChild || sharedChild);
237
+ }
238
+ if (current !== child) {
239
+ if (!parent.inheritance) {
240
+ return fail('Local child edit requires an isolated parent');
241
+ }
242
+ parent.inheritance.childBindings[step.childId] = current.id;
243
+ }
244
+ }
245
+ return { root, raw: current, parent, logicalId };
246
+ };
247
+ const pending = [...intents];
248
+ for (const intent of pending) {
249
+ const targetKey = JSON.stringify([
250
+ identity(intent.target),
251
+ intent.target.path
252
+ ]);
253
+ const registerWrite = (key, value) => {
254
+ const token = JSON.stringify([targetKey, key]);
255
+ if (writes.has(token) && !isEqual(writes.get(token), value)) {
256
+ fail('Conflicting local property writes');
257
+ }
258
+ writes.set(token, value);
259
+ };
260
+ if (intent.kind === 'records') {
261
+ const currentTarget = {
262
+ ...intent.target,
263
+ root: {
264
+ ...intent.target.root,
265
+ componentId: roots.get(identity(intent.target))?.after.id ??
266
+ intent.target.root.componentId
267
+ },
268
+ expectedPropertyId: undefined,
269
+ expectedSourceId: undefined
270
+ };
271
+ const current = draft.locate(currentTarget).raw;
272
+ const relation = requireCapability(current).config.children;
273
+ if (!relation ||
274
+ relation.key !== intent.key ||
275
+ relation.collection !== 'array-or-record') {
276
+ fail('Invalid registered record relationship');
277
+ }
278
+ if (!relation) {
279
+ return fail('Missing registered record relationship');
280
+ }
281
+ const set = intent.set ?? {}, remove = intent.remove ?? [];
282
+ if (!isRecord(set) ||
283
+ !Array.isArray(remove) ||
284
+ remove.some((id) => typeof id !== 'string' || !id) ||
285
+ new Set(remove).size !== remove.length) {
286
+ fail('Invalid record patch');
287
+ }
288
+ const basis = current.inheritance
289
+ ? draft.read(current.inheritance.sourceId)
290
+ : current;
291
+ const membership = current.inheritance && own(current.inheritance.overrides, relation.key)
292
+ ? current.inheritance.overrides[relation.key]
293
+ : basis[relation.key];
294
+ if (!Array.isArray(membership) ||
295
+ membership.some((id) => typeof id !== 'string')) {
296
+ fail('Invalid canonical record membership');
297
+ }
298
+ const members = [...membership];
299
+ const logical = (id) => {
300
+ const matches = members.filter((member) => member === id || current.inheritance?.childBindings[member] === id);
301
+ if (matches.length > 1) {
302
+ fail('Ambiguous record identity');
303
+ }
304
+ return matches[0];
305
+ };
306
+ const removed = new Set(remove.map((id) => logical(id) ?? fail(`Missing local record "${id}"`)));
307
+ const next = members.filter((id) => !removed.has(id));
308
+ const fieldIntents = [];
309
+ for (const [recordId, recordValues] of Object.entries(set)) {
310
+ if (!recordId || !isRecord(recordValues) || remove.includes(recordId)) {
311
+ fail('Invalid or conflicting record patch');
312
+ }
313
+ const mapped = relation.toChildData
314
+ ? relation.toChildData(recordValues, recordId)
315
+ : recordValues;
316
+ if (!isRecord(mapped) ||
317
+ (mapped.id !== undefined && mapped.id !== recordId) ||
318
+ (mapped.type !== undefined && mapped.type !== relation.childType)) {
319
+ return fail('Invalid mapped record identity');
320
+ }
321
+ const values = Object.fromEntries(Object.entries(mapped).filter(([key]) => key !== 'id' && key !== 'type'));
322
+ const member = logical(recordId);
323
+ if (member) {
324
+ if (removed.has(member)) {
325
+ fail('Conflicting local record operations');
326
+ }
327
+ fieldIntents.push({
328
+ kind: 'override',
329
+ target: {
330
+ ...intent.target,
331
+ path: [
332
+ ...intent.target.path,
333
+ { key: relation.key, childId: member }
334
+ ],
335
+ expectedPropertyId: undefined,
336
+ expectedSourceId: undefined
337
+ },
338
+ values
339
+ });
340
+ }
341
+ else {
342
+ const config = getPropertyComponentConfigDefinition(relation.childType);
343
+ if (!config || !getPropertySchema(relation.childType)) {
344
+ fail('New local record requires registered declarative capability');
345
+ }
346
+ if (!config) {
347
+ return fail('Missing child capability');
348
+ }
349
+ const roles = resolvePropertyComponentConfigRoles(config);
350
+ const defaults = Object.fromEntries(roles.persistKeys
351
+ .filter((key) => roles.defaults[key] !== undefined)
352
+ .map((key) => [key, structuredClone(roles.defaults[key])]));
353
+ const child = draft.allocate({
354
+ ...defaults,
355
+ ...structuredClone(values),
356
+ type: relation.childType
357
+ }, recordId);
358
+ const { schema } = requireCapability(child);
359
+ for (const [key, value] of Object.entries(values)) {
360
+ const field = schema.fields.find((field) => field.key === key);
361
+ if (!field || !isRuntimePropertyFieldValueValid(field, value)) {
362
+ fail(`Invalid new record field "${key}"`);
363
+ }
364
+ }
365
+ next.push(child.id);
366
+ }
367
+ }
368
+ if (!isEqual(next, members)) {
369
+ registerWrite(relation.key, { kind: 'membership', value: next });
370
+ const raw = isolated(intent.target, true).raw;
371
+ if (raw.inheritance) {
372
+ raw.inheritance.overrides[relation.key] = next;
373
+ for (const childId of removed) {
374
+ Reflect.deleteProperty(raw.inheritance.childBindings, childId);
375
+ }
376
+ }
377
+ else {
378
+ raw[relation.key] = next;
379
+ }
380
+ }
381
+ pending.push(...fieldIntents);
382
+ }
383
+ else if (intent.kind === 'override') {
384
+ if (!isRecord(intent.values)) {
385
+ fail('Invalid local values');
386
+ }
387
+ if (Object.keys(intent.values).length === 0) {
388
+ continue;
389
+ }
390
+ const original = draft.locate({
391
+ ...intent.target,
392
+ root: {
393
+ ...intent.target.root,
394
+ componentId: roots.get(identity(intent.target))?.after.id ??
395
+ intent.target.root.componentId
396
+ },
397
+ expectedPropertyId: undefined,
398
+ expectedSourceId: undefined
399
+ }).raw;
400
+ const { schema, config } = requireCapability(original);
401
+ const values = structuredClone(intent.values);
402
+ const relation = config.children;
403
+ if (relation &&
404
+ Array.isArray(values[relation.key]) &&
405
+ original.inheritance) {
406
+ const links = original.inheritance.childBindings;
407
+ values[relation.key] = values[relation.key].map((value) => {
408
+ const logical = Object.keys(links).filter((key) => links[key] === value);
409
+ if (logical.length > 1) {
410
+ fail('Ambiguous local membership identity');
411
+ }
412
+ return logical[0] ?? value;
413
+ });
414
+ }
415
+ for (const [key, value] of Object.entries(values)) {
416
+ const field = schema.fields.find((field) => field.key === key);
417
+ if (!field || !isRuntimePropertyFieldValueValid(field, value)) {
418
+ fail(`Unknown or invalid property field "${key}"`);
419
+ }
420
+ registerWrite(key, { kind: intent.kind, value });
421
+ }
422
+ const { raw } = isolated(intent.target, true);
423
+ for (const [key, value] of Object.entries(values)) {
424
+ if (raw.inheritance) {
425
+ raw.inheritance.overrides[key] = value;
426
+ if (key === relation?.key && Array.isArray(value)) {
427
+ for (const childId of Object.keys(raw.inheritance.childBindings)) {
428
+ if (!value.includes(childId)) {
429
+ Reflect.deleteProperty(raw.inheritance.childBindings, childId);
430
+ }
431
+ }
432
+ }
433
+ }
434
+ else {
435
+ raw[key] = value;
436
+ }
437
+ }
438
+ }
439
+ else if (intent.kind === 'reset') {
440
+ const { raw } = draft.locate({
441
+ ...intent.target,
442
+ root: {
443
+ ...intent.target.root,
444
+ componentId: roots.get(identity(intent.target))?.after.id ??
445
+ intent.target.root.componentId
446
+ },
447
+ expectedPropertyId: undefined,
448
+ expectedSourceId: undefined
449
+ });
450
+ const { schema } = requireCapability(raw);
451
+ for (const key of intent.keys) {
452
+ if (!schema.fields.some((field) => field.key === key)) {
453
+ fail(`Unknown override field "${key}"`);
454
+ }
455
+ registerWrite(key, { kind: intent.kind });
456
+ }
457
+ if (raw.inheritance &&
458
+ intent.keys.some((key) => own(raw.inheritance?.overrides ?? {}, key))) {
459
+ const isolatedRaw = isolated(intent.target, true).raw;
460
+ for (const key of intent.keys) {
461
+ if (isolatedRaw.inheritance) {
462
+ Reflect.deleteProperty(isolatedRaw.inheritance.overrides, key);
463
+ }
464
+ }
465
+ }
466
+ }
467
+ else if (intent.kind === 'detach' || intent.kind === 'share') {
468
+ registerWrite('$binding', intent);
469
+ const located = isolated(intent.target, false);
470
+ let replacement;
471
+ if (intent.kind === 'detach') {
472
+ if (!located.parent &&
473
+ !located.raw.inheritance &&
474
+ !located.raw.sharedSource &&
475
+ !intent.target.sharedRoot) {
476
+ continue;
477
+ }
478
+ replacement = draft.allocate({
479
+ type: located.raw.type,
480
+ ...draft.fields(located.raw)
481
+ });
482
+ }
483
+ else {
484
+ if (located.raw.inheritance &&
485
+ (Object.keys(located.raw.inheritance.overrides).length > 0 ||
486
+ Object.keys(located.raw.inheritance.childBindings).length > 0)) {
487
+ fail('Reset or detach local overrides before sharing another source');
488
+ }
489
+ const source = draft.read(intent.sourceId);
490
+ if (source.inheritance || source.type !== located.raw.type) {
491
+ fail('Shared source must be independent and match the target type');
492
+ }
493
+ replacement = draft.inherit(source);
494
+ }
495
+ if (located.parent && located.logicalId) {
496
+ if (!located.parent.inheritance) {
497
+ return fail('Local child edit requires an isolated parent');
498
+ }
499
+ located.parent.inheritance.childBindings[located.logicalId] =
500
+ replacement.id;
501
+ }
502
+ else {
503
+ located.root.after = replacement;
504
+ }
505
+ }
506
+ else {
507
+ fail('Unknown property intention');
508
+ }
509
+ }
510
+ const operations = [...directOperations];
511
+ for (const [propertyId, original] of draft.originals) {
512
+ const current = draft.read(propertyId);
513
+ const changes = [];
514
+ if (original.sharedSource !== current.sharedSource) {
515
+ changes.push({
516
+ layer: 'sharedSource',
517
+ key: 'sharedSource',
518
+ state: current.sharedSource
519
+ ? { present: true, value: true }
520
+ : { present: false }
521
+ });
522
+ }
523
+ if (original.inheritance && current.inheritance) {
524
+ for (const layer of ['overrides', 'childBindings']) {
525
+ for (const key of new Set([
526
+ ...Object.keys(original.inheritance[layer]),
527
+ ...Object.keys(current.inheritance[layer])
528
+ ])) {
529
+ if (own(original.inheritance[layer], key) ===
530
+ own(current.inheritance[layer], key) &&
531
+ isEqual(original.inheritance[layer][key], current.inheritance[layer][key])) {
532
+ continue;
533
+ }
534
+ changes.push({
535
+ layer,
536
+ key,
537
+ state: own(current.inheritance[layer], key)
538
+ ? { present: true, value: current.inheritance[layer][key] }
539
+ : { present: false }
540
+ });
541
+ }
542
+ }
543
+ }
544
+ if (changes.length) {
545
+ operations.push({ kind: 'layers', propertyId, changes });
546
+ }
547
+ if (!current.inheritance) {
548
+ const values = Object.fromEntries(Object.entries(current).filter(([key, value]) => !['id', 'type', 'sharedSource'].includes(key) &&
549
+ !isEqual(value, original[key])));
550
+ if (Object.keys(values).length) {
551
+ operations.push({ kind: 'values', propertyId, values });
552
+ }
553
+ }
554
+ }
555
+ const rootBindings = [...roots.values()]
556
+ .filter((root) => root.before.id !== root.after.id)
557
+ .map(({ target, before, after }) => ({
558
+ elementId: target.root.ownerElementId,
559
+ propertyName: target.root.ownerPropertyName,
560
+ propertyType: before.type,
561
+ before: before.id,
562
+ after: after.id
563
+ }));
564
+ if (draft.created.size) {
565
+ operations.push({
566
+ kind: 'create-exact-property-graph',
567
+ ownerRelations: [...roots.values()].map(({ target, after }) => ({
568
+ ...target.root,
569
+ componentId: after.id
570
+ })),
571
+ components: [...draft.created.values()]
572
+ });
573
+ }
574
+ const retainedExistingPropertyIds = new Set();
575
+ for (const raw of draft.created.values()) {
576
+ const relation = requireCapability(raw).config.children;
577
+ const dependencies = readPropertyInheritanceDependencies(raw, relation?.key) ??
578
+ (relation && Array.isArray(raw[relation.key])
579
+ ? raw[relation.key]
580
+ : []);
581
+ for (const propertyId of dependencies) {
582
+ if (!draft.created.has(propertyId)) {
583
+ retainedExistingPropertyIds.add(propertyId);
584
+ }
585
+ }
586
+ }
587
+ const properties = manager.preparePropertyMutationBatch({
588
+ operations,
589
+ options
590
+ });
591
+ return {
592
+ properties,
593
+ rootBindings,
594
+ releasedRootPropertyIds: rootBindings
595
+ .filter((binding) => !draft.read(binding.before).sharedSource)
596
+ .map((binding) => binding.before),
597
+ retainedExistingPropertyIds: [...retainedExistingPropertyIds],
598
+ createdPropertyIds: [...draft.created.keys()]
599
+ };
600
+ };
601
+ export const prepareSharedPropertySourceBatch = (manager, updates, options) => {
602
+ const draft = createDraft(manager);
603
+ const valuesById = new Map();
604
+ for (const update of updates) {
605
+ const source = draft.read(update.propertyId);
606
+ if (source.inheritance || !source.sharedSource) {
607
+ fail('Explicit source mutation requires an independent shared source');
608
+ }
609
+ const values = valuesById.get(source.id) ?? {};
610
+ if (!isRecord(update.values)) {
611
+ fail('Invalid shared source values');
612
+ }
613
+ const { schema } = requireCapability(source);
614
+ for (const [key, value] of Object.entries(update.values)) {
615
+ const field = schema.fields.find((field) => field.key === key);
616
+ if (!field || !isRuntimePropertyFieldValueValid(field, value)) {
617
+ fail(`Unknown or invalid source field "${key}"`);
618
+ }
619
+ if (own(values, key) && !isEqual(values[key], value)) {
620
+ fail('Conflicting shared source writes');
621
+ }
622
+ values[key] = structuredClone(value);
623
+ }
624
+ valuesById.set(source.id, values);
625
+ }
626
+ return manager.preparePropertyMutationBatch({
627
+ operations: [...valuesById]
628
+ .filter(([, values]) => Object.keys(values).length > 0)
629
+ .map(([propertyId, values]) => ({ kind: 'values', propertyId, values })),
630
+ options
631
+ });
632
+ };
633
+ export const prepareOwnerPropertyMutations = (manager, updates, options, hasOtherOwners) => {
634
+ const intents = [];
635
+ const direct = [];
636
+ for (const { target, mutation } of updates) {
637
+ if (target.path.length !== 0 ||
638
+ target.root.componentId !== mutation.propertyId) {
639
+ fail('Owner mutation requires its exact root target');
640
+ }
641
+ const component = manager.getPropertyById(mutation.propertyId);
642
+ if (!component) {
643
+ return fail('Missing owner property');
644
+ }
645
+ const raw = component.save();
646
+ let isolated = target.sharedRoot || !!raw.inheritance || raw.sharedSource === true;
647
+ if (!isolated && mutation.kind === 'records') {
648
+ const relation = getPropertyComponentConfigDefinition(raw.type)?.children;
649
+ const members = relation && Array.isArray(raw[relation.key])
650
+ ? raw[relation.key]
651
+ : [];
652
+ isolated = Object.keys(mutation.set ?? {}).some((childId) => {
653
+ if (!members.includes(childId)) {
654
+ return false;
655
+ }
656
+ const child = manager.getPropertyById(childId)?.save();
657
+ return (!!child?.inheritance ||
658
+ child?.sharedSource === true ||
659
+ hasOtherOwners(childId, raw.id));
660
+ });
661
+ }
662
+ if (!isolated) {
663
+ direct.push(mutation);
664
+ continue;
665
+ }
666
+ if (mutation.values && Object.keys(mutation.values).length) {
667
+ intents.push({ kind: 'override', target, values: mutation.values });
668
+ }
669
+ if (mutation.kind === 'records') {
670
+ intents.push({
671
+ kind: 'records',
672
+ target,
673
+ key: mutation.key,
674
+ set: mutation.set,
675
+ remove: mutation.remove
676
+ });
677
+ }
678
+ }
679
+ return preparePropertyIntentBatch(manager, intents, options, hasOtherOwners, direct);
680
+ };
681
+ //# sourceMappingURL=property-intents.js.map