@asyra/render 0.2.5 → 0.5.1
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/LICENSE +21 -0
- package/README.md +17 -1
- package/dist/canvas-pipeline-debugger/index.d.ts +4 -0
- package/dist/canvas-pipeline-debugger/index.d.ts.map +1 -0
- package/dist/canvas-pipeline-debugger/index.js +3 -0
- package/dist/canvas-pipeline-debugger/index.js.map +1 -0
- package/dist/canvas-pipeline-debugger/overlay.d.ts +4 -0
- package/dist/canvas-pipeline-debugger/overlay.d.ts.map +1 -0
- package/dist/canvas-pipeline-debugger/overlay.js +90 -0
- package/dist/canvas-pipeline-debugger/overlay.js.map +1 -0
- package/dist/canvas-pipeline-debugger/trace-projector.d.ts +8 -0
- package/dist/canvas-pipeline-debugger/trace-projector.d.ts.map +1 -0
- package/dist/canvas-pipeline-debugger/trace-projector.js +329 -0
- package/dist/canvas-pipeline-debugger/trace-projector.js.map +1 -0
- package/dist/canvas-pipeline-debugger/types.d.ts +77 -0
- package/dist/canvas-pipeline-debugger/types.d.ts.map +1 -0
- package/dist/canvas-pipeline-debugger/types.js.map +1 -0
- package/dist/diagnostics/canvas-pipeline.d.ts +84 -0
- package/dist/diagnostics/canvas-pipeline.d.ts.map +1 -0
- package/dist/diagnostics/canvas-pipeline.js +197 -0
- package/dist/diagnostics/canvas-pipeline.js.map +1 -0
- package/dist/diagnostics/freeze-evidence.d.ts +2 -0
- package/dist/diagnostics/freeze-evidence.d.ts.map +1 -0
- package/dist/diagnostics/freeze-evidence.js +9 -0
- package/dist/diagnostics/freeze-evidence.js.map +1 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +19 -0
- package/dist/errors.js.map +1 -0
- package/dist/fills/even-odd-fill.d.ts +38 -0
- package/dist/fills/even-odd-fill.d.ts.map +1 -0
- package/dist/fills/even-odd-fill.js +369 -0
- package/dist/fills/even-odd-fill.js.map +1 -0
- package/dist/fills/gradient-fill.d.ts +27 -0
- package/dist/fills/gradient-fill.d.ts.map +1 -0
- package/dist/fills/gradient-fill.js +8 -0
- package/dist/fills/gradient-fill.js.map +1 -0
- package/dist/handlers/element-selection.d.ts +15 -2
- package/dist/handlers/element-selection.d.ts.map +1 -1
- package/dist/handlers/element-selection.js +19 -4
- package/dist/handlers/element-selection.js.map +1 -1
- package/dist/handlers/index.d.ts +1 -1
- package/dist/handlers/index.d.ts.map +1 -1
- package/dist/handlers/index.js +1 -1
- package/dist/handlers/index.js.map +1 -1
- package/dist/index.d.ts +23 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -3
- package/dist/index.js.map +1 -1
- package/dist/interaction/engine-interaction-bridge.d.ts +9 -0
- package/dist/interaction/engine-interaction-bridge.d.ts.map +1 -0
- package/dist/interaction/engine-interaction-bridge.js +22 -0
- package/dist/interaction/engine-interaction-bridge.js.map +1 -0
- package/dist/interaction/interaction-bridge.d.ts +26 -0
- package/dist/interaction/interaction-bridge.d.ts.map +1 -0
- package/dist/interaction/interaction-bridge.js +238 -0
- package/dist/interaction/interaction-bridge.js.map +1 -0
- package/dist/interaction/target-helpers.d.ts +31 -0
- package/dist/interaction/target-helpers.d.ts.map +1 -0
- package/dist/interaction/target-helpers.js +99 -0
- package/dist/interaction/target-helpers.js.map +1 -0
- package/dist/layers/overlay-layer.d.ts +30 -0
- package/dist/layers/overlay-layer.d.ts.map +1 -0
- package/dist/layers/overlay-layer.js +161 -0
- package/dist/layers/overlay-layer.js.map +1 -0
- package/dist/layers/scene/index.d.ts +2 -0
- package/dist/layers/scene/index.d.ts.map +1 -0
- package/dist/layers/scene/index.js +2 -0
- package/dist/layers/scene/index.js.map +1 -0
- package/dist/{render-layer → layers/scene}/render-layer.d.ts +15 -15
- package/dist/layers/scene/render-layer.d.ts.map +1 -0
- package/dist/layers/scene/render-layer.js +347 -0
- package/dist/layers/scene/render-layer.js.map +1 -0
- package/dist/layers/viewport/index.d.ts +2 -0
- package/dist/layers/viewport/index.d.ts.map +1 -0
- package/dist/layers/viewport/index.js +2 -0
- package/dist/layers/viewport/index.js.map +1 -0
- package/dist/{viewport-layer → layers/viewport}/viewport-layer.d.ts +23 -12
- package/dist/layers/viewport/viewport-layer.d.ts.map +1 -0
- package/dist/{viewport-layer → layers/viewport}/viewport-layer.js +31 -10
- package/dist/layers/viewport/viewport-layer.js.map +1 -0
- package/dist/pixi-renderer.d.ts +6 -0
- package/dist/pixi-renderer.d.ts.map +1 -0
- package/dist/pixi-renderer.js +6 -0
- package/dist/pixi-renderer.js.map +1 -0
- package/dist/projections/mesh-projection.d.ts +32 -0
- package/dist/projections/mesh-projection.d.ts.map +1 -0
- package/dist/projections/mesh-projection.js +192 -0
- package/dist/projections/mesh-projection.js.map +1 -0
- package/dist/registries/interaction-target.d.ts +31 -0
- package/dist/registries/interaction-target.d.ts.map +1 -0
- package/dist/registries/interaction-target.js +126 -0
- package/dist/registries/interaction-target.js.map +1 -0
- package/dist/registries/render-interaction-handler.d.ts +12 -0
- package/dist/registries/render-interaction-handler.d.ts.map +1 -0
- package/dist/registries/render-interaction-handler.js +81 -0
- package/dist/registries/render-interaction-handler.js.map +1 -0
- package/dist/registries/render-layer.d.ts +14 -0
- package/dist/registries/render-layer.d.ts.map +1 -0
- package/dist/registries/render-layer.js +44 -0
- package/dist/registries/render-layer.js.map +1 -0
- package/dist/registries/render-strategy.d.ts +12 -0
- package/dist/registries/render-strategy.d.ts.map +1 -0
- package/dist/registries/render-strategy.js +29 -0
- package/dist/registries/render-strategy.js.map +1 -0
- package/dist/render.d.ts +105 -39
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +662 -68
- package/dist/render.js.map +1 -1
- package/dist/renderer.d.ts +33 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +88 -0
- package/dist/renderer.js.map +1 -0
- package/dist/stores/scene-tree.d.ts +58 -7
- package/dist/stores/scene-tree.d.ts.map +1 -1
- package/dist/stores/scene-tree.js +1270 -28
- package/dist/stores/scene-tree.js.map +1 -1
- package/dist/stores/selection.d.ts +3 -2
- package/dist/stores/selection.d.ts.map +1 -1
- package/dist/stores/selection.js +9 -4
- package/dist/stores/selection.js.map +1 -1
- package/dist/strategies/default-strategy.d.ts +4 -0
- package/dist/strategies/default-strategy.d.ts.map +1 -0
- package/dist/strategies/default-strategy.js +15 -0
- package/dist/strategies/default-strategy.js.map +1 -0
- package/dist/types/render-interaction.d.ts +25 -0
- package/dist/types/render-interaction.d.ts.map +1 -0
- package/dist/types/render-interaction.js +2 -0
- package/dist/types/render-interaction.js.map +1 -0
- package/dist/types/render-layer.d.ts +8 -0
- package/dist/types/render-layer.d.ts.map +1 -0
- package/dist/types/render-layer.js +2 -0
- package/dist/types/render-layer.js.map +1 -0
- package/dist/types/render-object.d.ts +194 -0
- package/dist/types/render-object.d.ts.map +1 -0
- package/dist/types/render-object.js +889 -0
- package/dist/types/render-object.js.map +1 -0
- package/dist/types/render-strategy.compatibility.d.ts +2 -0
- package/dist/types/render-strategy.compatibility.d.ts.map +1 -0
- package/dist/types/render-strategy.compatibility.js +2 -0
- package/dist/types/render-strategy.compatibility.js.map +1 -0
- package/dist/types/render-strategy.d.ts +35 -0
- package/dist/types/render-strategy.d.ts.map +1 -0
- package/dist/types/render-strategy.js +2 -0
- package/dist/types/render-strategy.js.map +1 -0
- package/dist/types/renderer.d.ts +86 -0
- package/dist/types/renderer.d.ts.map +1 -0
- package/dist/types/renderer.js +2 -0
- package/dist/types/renderer.js.map +1 -0
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +33 -8
- package/.turbo/turbo-test$colon$local.log +0 -48
- package/CHANGELOG.md +0 -58
- package/dist/__tests__/render.test.d.ts +0 -2
- package/dist/__tests__/render.test.d.ts.map +0 -1
- package/dist/__tests__/render.test.js +0 -148
- package/dist/__tests__/render.test.js.map +0 -1
- package/dist/render-layer/element-interaction-handler.d.ts +0 -8
- package/dist/render-layer/element-interaction-handler.d.ts.map +0 -1
- package/dist/render-layer/element-interaction-handler.js +0 -20
- package/dist/render-layer/element-interaction-handler.js.map +0 -1
- package/dist/render-layer/index.d.ts +0 -2
- package/dist/render-layer/index.d.ts.map +0 -1
- package/dist/render-layer/index.js +0 -2
- package/dist/render-layer/index.js.map +0 -1
- package/dist/render-layer/render-layer.d.ts.map +0 -1
- package/dist/render-layer/render-layer.js +0 -189
- package/dist/render-layer/render-layer.js.map +0 -1
- package/dist/selection-layer/index.d.ts +0 -3
- package/dist/selection-layer/index.d.ts.map +0 -1
- package/dist/selection-layer/index.js +0 -3
- package/dist/selection-layer/index.js.map +0 -1
- package/dist/selection-layer/selection-layer.d.ts +0 -29
- package/dist/selection-layer/selection-layer.d.ts.map +0 -1
- package/dist/selection-layer/selection-layer.js +0 -63
- package/dist/selection-layer/selection-layer.js.map +0 -1
- package/dist/selection-layer/utils.d.ts +0 -13
- package/dist/selection-layer/utils.d.ts.map +0 -1
- package/dist/selection-layer/utils.js +0 -27
- package/dist/selection-layer/utils.js.map +0 -1
- package/dist/subscribes/index.d.ts +0 -2
- package/dist/subscribes/index.d.ts.map +0 -1
- package/dist/subscribes/index.js +0 -11
- package/dist/subscribes/index.js.map +0 -1
- package/dist/subscribes/scene-tree.d.ts +0 -4
- package/dist/subscribes/scene-tree.d.ts.map +0 -1
- package/dist/subscribes/scene-tree.js +0 -56
- package/dist/subscribes/scene-tree.js.map +0 -1
- package/dist/subscribes/selection.d.ts +0 -3
- package/dist/subscribes/selection.d.ts.map +0 -1
- package/dist/subscribes/selection.js +0 -31
- package/dist/subscribes/selection.js.map +0 -1
- package/dist/subscribes/system.d.ts +0 -2
- package/dist/subscribes/system.d.ts.map +0 -1
- package/dist/subscribes/system.js +0 -15
- package/dist/subscribes/system.js.map +0 -1
- package/dist/subscribes/viewport.d.ts +0 -2
- package/dist/subscribes/viewport.d.ts.map +0 -1
- package/dist/subscribes/viewport.js +0 -36
- package/dist/subscribes/viewport.js.map +0 -1
- package/dist/viewport-layer/index.d.ts +0 -4
- package/dist/viewport-layer/index.d.ts.map +0 -1
- package/dist/viewport-layer/index.js +0 -4
- package/dist/viewport-layer/index.js.map +0 -1
- package/dist/viewport-layer/types.d.ts +0 -17
- package/dist/viewport-layer/types.d.ts.map +0 -1
- package/dist/viewport-layer/types.js.map +0 -1
- package/dist/viewport-layer/utils.d.ts +0 -8
- package/dist/viewport-layer/utils.d.ts.map +0 -1
- package/dist/viewport-layer/utils.js +0 -49
- package/dist/viewport-layer/utils.js.map +0 -1
- package/dist/viewport-layer/viewport-layer.d.ts.map +0 -1
- package/dist/viewport-layer/viewport-layer.js.map +0 -1
- package/src/__tests__/render.test.ts +0 -209
- package/src/handlers/element-selection.ts +0 -7
- package/src/handlers/index.ts +0 -1
- package/src/index.ts +0 -7
- package/src/render-layer/element-interaction-handler.ts +0 -31
- package/src/render-layer/index.ts +0 -1
- package/src/render-layer/render-layer.ts +0 -238
- package/src/render.ts +0 -161
- package/src/selection-layer/index.ts +0 -2
- package/src/selection-layer/selection-layer.ts +0 -90
- package/src/selection-layer/utils.ts +0 -37
- package/src/stores/scene-tree.ts +0 -79
- package/src/stores/selection.ts +0 -40
- package/src/subscribes/index.ts +0 -11
- package/src/subscribes/scene-tree.ts +0 -72
- package/src/subscribes/selection.ts +0 -40
- package/src/subscribes/system.ts +0 -18
- package/src/subscribes/viewport.ts +0 -54
- package/src/types.ts +0 -15
- package/src/viewport-layer/index.ts +0 -3
- package/src/viewport-layer/types.ts +0 -18
- package/src/viewport-layer/utils.ts +0 -55
- package/src/viewport-layer/viewport-layer.ts +0 -172
- package/test-global.d.ts +0 -11
- package/test-setup.ts +0 -28
- package/tsconfig.json +0 -22
- package/vitest.config.ts +0 -8
- /package/dist/{viewport-layer → canvas-pipeline-debugger}/types.js +0 -0
|
@@ -1,56 +1,1298 @@
|
|
|
1
|
-
import { EntityTypes } from '@asyra/utils';
|
|
1
|
+
import { EntityTypes, SCENE_TREE_ACTIONS, emitDiagnosticCounter, isRecord, measureBrowserDragPhase } from '@asyra/utils';
|
|
2
2
|
import sceneTree from '@asyra/scene-tree';
|
|
3
|
-
import render from '../render';
|
|
3
|
+
import render from '../render.js';
|
|
4
|
+
import renderStrategyRegistry from '../registries/render-strategy.js';
|
|
5
|
+
const DIRECT_RENDER_PROPERTY_KEYS = new Set(['x', 'y', 'rotation']);
|
|
6
|
+
const SCENE_TREE_PENDING_RENDER_LAYER = 'render-scene-tree-pending-updates';
|
|
7
|
+
const hasOwn = (record, key) => Object.prototype.hasOwnProperty.call(record, key);
|
|
8
|
+
const getEnumerableOwnKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
9
|
+
const isArrayIndexKey = (key) => {
|
|
10
|
+
if (typeof key !== 'string' || key === '') {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const index = Number(key);
|
|
14
|
+
return (Number.isInteger(index) &&
|
|
15
|
+
index >= 0 &&
|
|
16
|
+
index < 2 ** 32 - 1 &&
|
|
17
|
+
String(index) === key);
|
|
18
|
+
};
|
|
19
|
+
const cloneArrayWithEnumerableProperties = (value, entries = value.slice()) => {
|
|
20
|
+
const clone = entries;
|
|
21
|
+
getEnumerableOwnKeys(value).forEach((key) => {
|
|
22
|
+
if (isArrayIndexKey(key)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
26
|
+
if (descriptor) {
|
|
27
|
+
Object.defineProperty(clone, key, descriptor);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return clone;
|
|
31
|
+
};
|
|
32
|
+
const setOwn = (record, key, value) => {
|
|
33
|
+
Object.defineProperty(record, key, {
|
|
34
|
+
configurable: true,
|
|
35
|
+
enumerable: true,
|
|
36
|
+
value,
|
|
37
|
+
writable: true
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const isSceneTreeDataOwner = (value) => value === 'raw' || value === 'computed';
|
|
41
|
+
const composeCompleteRenderData = (elementId, rawDataSnapshot, computedDataSnapshot) => {
|
|
42
|
+
const renderDataSnapshot = {
|
|
43
|
+
...rawDataSnapshot,
|
|
44
|
+
...computedDataSnapshot
|
|
45
|
+
};
|
|
46
|
+
if (renderDataSnapshot.id !== elementId ||
|
|
47
|
+
typeof renderDataSnapshot.type !== 'string' ||
|
|
48
|
+
renderDataSnapshot.type.length === 0 ||
|
|
49
|
+
renderDataSnapshot.type === EntityTypes.WORKSPACE) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
return renderDataSnapshot;
|
|
53
|
+
};
|
|
54
|
+
const getEffectiveValue = (rawDataSnapshot, computedDataSnapshot, key) => hasOwn(computedDataSnapshot, key)
|
|
55
|
+
? computedDataSnapshot[key]
|
|
56
|
+
: rawDataSnapshot[key];
|
|
57
|
+
const rememberComparedPair = (left, right, comparedPairs) => {
|
|
58
|
+
const mappedRight = comparedPairs.leftToRight.get(left);
|
|
59
|
+
const mappedLeft = comparedPairs.rightToLeft.get(right);
|
|
60
|
+
if (mappedRight !== undefined || mappedLeft !== undefined) {
|
|
61
|
+
return mappedRight === right && mappedLeft === left ? 'equal' : 'mismatch';
|
|
62
|
+
}
|
|
63
|
+
comparedPairs.leftToRight.set(left, right);
|
|
64
|
+
comparedPairs.rightToLeft.set(right, left);
|
|
65
|
+
return 'new';
|
|
66
|
+
};
|
|
67
|
+
const isDataEqual = (left, right, comparedPairs = {
|
|
68
|
+
leftToRight: new WeakMap(),
|
|
69
|
+
rightToLeft: new WeakMap()
|
|
70
|
+
}) => {
|
|
71
|
+
if (Object.is(left, right)) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
75
|
+
if (!Array.isArray(left) ||
|
|
76
|
+
!Array.isArray(right) ||
|
|
77
|
+
left.length !== right.length) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const pairStatus = rememberComparedPair(left, right, comparedPairs);
|
|
81
|
+
if (pairStatus !== 'new') {
|
|
82
|
+
return pairStatus === 'equal';
|
|
83
|
+
}
|
|
84
|
+
const leftKeys = getEnumerableOwnKeys(left);
|
|
85
|
+
const rightKeys = getEnumerableOwnKeys(right);
|
|
86
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const leftRecord = left;
|
|
90
|
+
const rightRecord = right;
|
|
91
|
+
return leftKeys.every((key) => hasOwn(rightRecord, key) &&
|
|
92
|
+
isDataEqual(leftRecord[key], rightRecord[key], comparedPairs));
|
|
93
|
+
}
|
|
94
|
+
if (!isRecord(left) || !isRecord(right)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const leftKeys = Object.keys(left);
|
|
98
|
+
const rightKeys = Object.keys(right);
|
|
99
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
const pairStatus = rememberComparedPair(left, right, comparedPairs);
|
|
103
|
+
if (pairStatus !== 'new') {
|
|
104
|
+
return pairStatus === 'equal';
|
|
105
|
+
}
|
|
106
|
+
return leftKeys.every((key) => hasOwn(right, key) && isDataEqual(left[key], right[key], comparedPairs));
|
|
107
|
+
};
|
|
108
|
+
class ComputedDataMirror {
|
|
109
|
+
constructor() {
|
|
110
|
+
this.entries = new Map();
|
|
111
|
+
this.childIdsBySnapshot = new WeakMap();
|
|
112
|
+
}
|
|
113
|
+
clear() {
|
|
114
|
+
const clearedEntryCount = this.entries.size;
|
|
115
|
+
this.entries.clear();
|
|
116
|
+
return clearedEntryCount;
|
|
117
|
+
}
|
|
118
|
+
get size() {
|
|
119
|
+
return this.entries.size;
|
|
120
|
+
}
|
|
121
|
+
get elementIds() {
|
|
122
|
+
return [...this.entries.keys()];
|
|
123
|
+
}
|
|
124
|
+
getChildIdSet(children) {
|
|
125
|
+
const cached = this.childIdsBySnapshot.get(children);
|
|
126
|
+
if (cached) {
|
|
127
|
+
emitDiagnosticCounter('computed-mirror-child-id-cache-hit');
|
|
128
|
+
return cached;
|
|
129
|
+
}
|
|
130
|
+
const childIds = new Set();
|
|
131
|
+
for (const candidate of children) {
|
|
132
|
+
if (typeof candidate !== 'string' || childIds.has(candidate)) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
childIds.add(candidate);
|
|
136
|
+
}
|
|
137
|
+
this.childIdsBySnapshot.set(children, childIds);
|
|
138
|
+
emitDiagnosticCounter('computed-mirror-child-id-cache-miss');
|
|
139
|
+
return childIds;
|
|
140
|
+
}
|
|
141
|
+
delete(elementId) {
|
|
142
|
+
this.entries.delete(elementId);
|
|
143
|
+
emitDiagnosticCounter('computed-mirror-invalidate');
|
|
144
|
+
}
|
|
145
|
+
installSeed(elementId, rawData, computedData, reason) {
|
|
146
|
+
if (!isRecord(rawData) || !isRecord(computedData)) {
|
|
147
|
+
throw new Error('Render snapshot parts must be records');
|
|
148
|
+
}
|
|
149
|
+
const rawDataSnapshot = { ...rawData };
|
|
150
|
+
const computedDataSnapshot = {
|
|
151
|
+
...computedData
|
|
152
|
+
};
|
|
153
|
+
const renderDataSnapshot = composeCompleteRenderData(elementId, rawDataSnapshot, computedDataSnapshot);
|
|
154
|
+
if (!renderDataSnapshot) {
|
|
155
|
+
throw new Error('Render snapshot identity is incomplete');
|
|
156
|
+
}
|
|
157
|
+
const entry = {
|
|
158
|
+
rawDataSnapshot,
|
|
159
|
+
computedDataSnapshot,
|
|
160
|
+
renderDataSnapshot
|
|
161
|
+
};
|
|
162
|
+
this.entries.set(elementId, entry);
|
|
163
|
+
emitDiagnosticCounter('computed-mirror-seed');
|
|
164
|
+
emitDiagnosticCounter(`computed-mirror-seed-${reason}`);
|
|
165
|
+
return entry;
|
|
166
|
+
}
|
|
167
|
+
seed(elementId, reason) {
|
|
168
|
+
const element = sceneTree.getElementById(elementId);
|
|
169
|
+
if (!element) {
|
|
170
|
+
this.delete(elementId);
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
const { rawData, computedData } = measureBrowserDragPhase('render-scene-tree:mirror-seed', () => ({
|
|
175
|
+
rawData: element.save(),
|
|
176
|
+
computedData: element.getAllComputedData()
|
|
177
|
+
}));
|
|
178
|
+
return this.installSeed(elementId, rawData, computedData, reason);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
this.delete(elementId);
|
|
182
|
+
emitDiagnosticCounter('computed-mirror-seed-failed');
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
seedFromRawData(elementId, rawData) {
|
|
187
|
+
const element = sceneTree.getElementById(elementId);
|
|
188
|
+
if (!element) {
|
|
189
|
+
this.delete(elementId);
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
return this.installSeed(elementId, rawData, element.getAllComputedData(), 'add');
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
this.delete(elementId);
|
|
197
|
+
emitDiagnosticCounter('computed-mirror-seed-failed');
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
get(elementId) {
|
|
202
|
+
const entry = this.entries.get(elementId);
|
|
203
|
+
if (entry) {
|
|
204
|
+
emitDiagnosticCounter('computed-mirror-hit');
|
|
205
|
+
return entry;
|
|
206
|
+
}
|
|
207
|
+
emitDiagnosticCounter('computed-mirror-cache-miss');
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
matchesElementParent(elementId, parentId) {
|
|
211
|
+
return this.entries.get(elementId)?.rawDataSnapshot.parentId === parentId;
|
|
212
|
+
}
|
|
213
|
+
matchesParentChild(parentId, childId, index) {
|
|
214
|
+
const children = this.entries.get(parentId)?.rawDataSnapshot.children;
|
|
215
|
+
return Array.isArray(children) && children[index] === childId;
|
|
216
|
+
}
|
|
217
|
+
installSnapshot(elementId, rawDataSnapshot, computedDataSnapshot) {
|
|
218
|
+
const renderDataSnapshot = composeCompleteRenderData(elementId, rawDataSnapshot, computedDataSnapshot);
|
|
219
|
+
if (!renderDataSnapshot) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
const nextEntry = {
|
|
223
|
+
rawDataSnapshot,
|
|
224
|
+
computedDataSnapshot,
|
|
225
|
+
renderDataSnapshot
|
|
226
|
+
};
|
|
227
|
+
this.entries.set(elementId, nextEntry);
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
applyTopLevelChange(elementId, owner, key, before, after) {
|
|
231
|
+
if (!isSceneTreeDataOwner(owner)) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
const entry = this.get(elementId);
|
|
235
|
+
if (!entry) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
const rawDataSnapshot = { ...entry.rawDataSnapshot };
|
|
239
|
+
const computedDataSnapshot = { ...entry.computedDataSnapshot };
|
|
240
|
+
const ownerSnapshot = owner === 'raw' ? rawDataSnapshot : computedDataSnapshot;
|
|
241
|
+
if (!hasOwn(ownerSnapshot, key) ||
|
|
242
|
+
!isDataEqual(ownerSnapshot[key], before)) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const effectiveBefore = getEffectiveValue(rawDataSnapshot, computedDataSnapshot, key);
|
|
246
|
+
setOwn(ownerSnapshot, key, after);
|
|
247
|
+
const effectiveAfter = getEffectiveValue(rawDataSnapshot, computedDataSnapshot, key);
|
|
248
|
+
if (!this.installSnapshot(elementId, rawDataSnapshot, computedDataSnapshot)) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
emitDiagnosticCounter('computed-mirror-staged-change-count');
|
|
252
|
+
return {
|
|
253
|
+
effectiveChanges: isDataEqual(effectiveBefore, effectiveAfter)
|
|
254
|
+
? []
|
|
255
|
+
: [
|
|
256
|
+
{
|
|
257
|
+
key,
|
|
258
|
+
before: effectiveBefore,
|
|
259
|
+
after: effectiveAfter
|
|
260
|
+
}
|
|
261
|
+
]
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
applyTopLevelChanges(elementId, changes) {
|
|
265
|
+
const entry = this.get(elementId);
|
|
266
|
+
if (!entry) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
const rawDataSnapshot = { ...entry.rawDataSnapshot };
|
|
270
|
+
const computedDataSnapshot = { ...entry.computedDataSnapshot };
|
|
271
|
+
const effectiveChanges = [];
|
|
272
|
+
for (const { owner, key, before, after } of changes) {
|
|
273
|
+
if (!isSceneTreeDataOwner(owner)) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
const ownerSnapshot = owner === 'raw' ? rawDataSnapshot : computedDataSnapshot;
|
|
277
|
+
if (!hasOwn(ownerSnapshot, key) ||
|
|
278
|
+
!isDataEqual(ownerSnapshot[key], before)) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
const effectiveBefore = getEffectiveValue(rawDataSnapshot, computedDataSnapshot, key);
|
|
282
|
+
setOwn(ownerSnapshot, key, after);
|
|
283
|
+
const effectiveAfter = getEffectiveValue(rawDataSnapshot, computedDataSnapshot, key);
|
|
284
|
+
if (!isDataEqual(effectiveBefore, effectiveAfter)) {
|
|
285
|
+
effectiveChanges.push({
|
|
286
|
+
key,
|
|
287
|
+
before: effectiveBefore,
|
|
288
|
+
after: effectiveAfter
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (!this.installSnapshot(elementId, rawDataSnapshot, computedDataSnapshot)) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
emitDiagnosticCounter('computed-mirror-staged-change-count', changes.length);
|
|
296
|
+
emitDiagnosticCounter('computed-mirror-batch-apply-count');
|
|
297
|
+
return { effectiveChanges };
|
|
298
|
+
}
|
|
299
|
+
applyComputedPatch(elementId, patch) {
|
|
300
|
+
const entry = this.get(elementId);
|
|
301
|
+
if (!entry) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
const computedDataSnapshot = { ...entry.computedDataSnapshot };
|
|
305
|
+
let changeCount = 0;
|
|
306
|
+
for (const [key, change] of Object.entries(patch.values ?? {})) {
|
|
307
|
+
if (!hasOwn(computedDataSnapshot, key) ||
|
|
308
|
+
!isDataEqual(computedDataSnapshot[key], change.before)) {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
setOwn(computedDataSnapshot, key, change.after);
|
|
312
|
+
changeCount += 1;
|
|
313
|
+
}
|
|
314
|
+
for (const [key, recordPatch] of Object.entries(patch.records ?? {})) {
|
|
315
|
+
const currentRecord = computedDataSnapshot[key];
|
|
316
|
+
if (!hasOwn(computedDataSnapshot, key) || !isRecord(currentRecord)) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
let nextRecord = {
|
|
320
|
+
...currentRecord
|
|
321
|
+
};
|
|
322
|
+
for (const [recordId, change] of Object.entries(recordPatch.set ?? {})) {
|
|
323
|
+
const recordExists = hasOwn(nextRecord, recordId);
|
|
324
|
+
const hasOwnBefore = Object.prototype.hasOwnProperty.call(change, 'before');
|
|
325
|
+
if ((hasOwnBefore &&
|
|
326
|
+
(!recordExists ||
|
|
327
|
+
!isDataEqual(nextRecord[recordId], change.before))) ||
|
|
328
|
+
(!hasOwnBefore && recordExists)) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
setOwn(nextRecord, recordId, change.after);
|
|
332
|
+
changeCount += 1;
|
|
333
|
+
}
|
|
334
|
+
for (const [recordId, change] of Object.entries(recordPatch.remove ?? {})) {
|
|
335
|
+
if (!hasOwn(nextRecord, recordId) ||
|
|
336
|
+
!isDataEqual(nextRecord[recordId], change.before)) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
const { [recordId]: _removed, ...retainedRecord } = nextRecord;
|
|
340
|
+
nextRecord = retainedRecord;
|
|
341
|
+
changeCount += 1;
|
|
342
|
+
}
|
|
343
|
+
setOwn(computedDataSnapshot, key, nextRecord);
|
|
344
|
+
}
|
|
345
|
+
if (!this.installSnapshot(elementId, entry.rawDataSnapshot, computedDataSnapshot)) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
emitDiagnosticCounter('computed-mirror-staged-change-count', changeCount);
|
|
349
|
+
emitDiagnosticCounter('computed-mirror-patch-apply-count');
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
applyChildMembershipChange(elementId, childId, index, action) {
|
|
353
|
+
const entry = this.get(elementId);
|
|
354
|
+
const currentChildren = entry?.rawDataSnapshot.children;
|
|
355
|
+
if (!entry ||
|
|
356
|
+
!Array.isArray(currentChildren) ||
|
|
357
|
+
index === undefined ||
|
|
358
|
+
!Number.isInteger(index) ||
|
|
359
|
+
index < 0) {
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
const nextChildren = cloneArrayWithEnumerableProperties(currentChildren);
|
|
363
|
+
if (action === 'add') {
|
|
364
|
+
if (index > currentChildren.length || currentChildren.includes(childId)) {
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
nextChildren.splice(index, 0, childId);
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
if (index >= currentChildren.length ||
|
|
371
|
+
currentChildren[index] !== childId) {
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
nextChildren.splice(index, 1);
|
|
375
|
+
}
|
|
376
|
+
return this.applyTopLevelChange(elementId, 'raw', 'children', currentChildren, nextChildren);
|
|
377
|
+
}
|
|
378
|
+
applyChildAdditionBatch(elementId, additions) {
|
|
379
|
+
const entry = this.get(elementId);
|
|
380
|
+
const currentChildren = entry?.rawDataSnapshot.children;
|
|
381
|
+
if (!entry || !Array.isArray(currentChildren) || additions.length === 0) {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
const currentChildIdSet = this.getChildIdSet(currentChildren);
|
|
385
|
+
if (!currentChildIdSet) {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
const insertedIds = new Set();
|
|
389
|
+
const insertionByFinalIndex = new Map();
|
|
390
|
+
const finalLength = currentChildren.length + additions.length;
|
|
391
|
+
const appendIds = new Array(additions.length);
|
|
392
|
+
let appendCount = 0;
|
|
393
|
+
for (const { data, index } of additions) {
|
|
394
|
+
const childId = data.id;
|
|
395
|
+
if (typeof childId !== 'string' ||
|
|
396
|
+
childId.length === 0 ||
|
|
397
|
+
insertedIds.has(childId) ||
|
|
398
|
+
currentChildIdSet.has(childId) ||
|
|
399
|
+
!Number.isInteger(index) ||
|
|
400
|
+
index < 0 ||
|
|
401
|
+
index >= finalLength ||
|
|
402
|
+
insertionByFinalIndex.has(index)) {
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
insertedIds.add(childId);
|
|
406
|
+
insertionByFinalIndex.set(index, childId);
|
|
407
|
+
const appendIndex = index - currentChildren.length;
|
|
408
|
+
if (appendIndex >= 0 && appendIndex < additions.length) {
|
|
409
|
+
appendIds[appendIndex] = childId;
|
|
410
|
+
appendCount += 1;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
let finalChildren;
|
|
414
|
+
if (appendCount === additions.length) {
|
|
415
|
+
finalChildren = currentChildren.concat(appendIds);
|
|
416
|
+
emitDiagnosticCounter('computed-mirror-child-add-batch-append');
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
let currentIndex = 0;
|
|
420
|
+
finalChildren = [];
|
|
421
|
+
for (let index = 0; index < finalLength; index += 1) {
|
|
422
|
+
const insertedId = insertionByFinalIndex.get(index);
|
|
423
|
+
if (insertedId) {
|
|
424
|
+
finalChildren.push(insertedId);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
const retainedId = currentChildren[currentIndex];
|
|
428
|
+
if (retainedId === undefined) {
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
finalChildren.push(retainedId);
|
|
432
|
+
currentIndex += 1;
|
|
433
|
+
}
|
|
434
|
+
if (currentIndex !== currentChildren.length) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const rawDataSnapshot = { ...entry.rawDataSnapshot };
|
|
439
|
+
setOwn(rawDataSnapshot, 'children', finalChildren);
|
|
440
|
+
if (!this.installSnapshot(elementId, rawDataSnapshot, entry.computedDataSnapshot)) {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
this.childIdsBySnapshot.delete(currentChildren);
|
|
444
|
+
insertedIds.forEach((childId) => currentChildIdSet.add(childId));
|
|
445
|
+
this.childIdsBySnapshot.set(finalChildren, currentChildIdSet);
|
|
446
|
+
emitDiagnosticCounter('computed-mirror-staged-change-count');
|
|
447
|
+
emitDiagnosticCounter('computed-mirror-child-add-batch-apply');
|
|
448
|
+
return {
|
|
449
|
+
effectiveChanges: hasOwn(entry.computedDataSnapshot, 'children')
|
|
450
|
+
? []
|
|
451
|
+
: [
|
|
452
|
+
{
|
|
453
|
+
key: 'children',
|
|
454
|
+
before: currentChildren,
|
|
455
|
+
after: finalChildren
|
|
456
|
+
}
|
|
457
|
+
]
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
applyChildRemovalBatch(elementId, removals) {
|
|
461
|
+
const entry = this.get(elementId);
|
|
462
|
+
const currentChildren = entry?.rawDataSnapshot.children;
|
|
463
|
+
if (!entry ||
|
|
464
|
+
!Array.isArray(currentChildren) ||
|
|
465
|
+
removals.length === 0 ||
|
|
466
|
+
currentChildren.some((candidate) => typeof candidate !== 'string')) {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
const removalIndexes = new Set();
|
|
470
|
+
const removalIds = new Set();
|
|
471
|
+
for (const { data, index } of removals) {
|
|
472
|
+
if (typeof data.id !== 'string' ||
|
|
473
|
+
data.id.length === 0 ||
|
|
474
|
+
removalIds.has(data.id) ||
|
|
475
|
+
!Number.isInteger(index) ||
|
|
476
|
+
index < 0 ||
|
|
477
|
+
index >= currentChildren.length ||
|
|
478
|
+
currentChildren[index] !== data.id) {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
removalIds.add(data.id);
|
|
482
|
+
removalIndexes.add(index);
|
|
483
|
+
}
|
|
484
|
+
const retainedChildren = currentChildren.filter((_childId, index) => !removalIndexes.has(index));
|
|
485
|
+
const nextChildren = cloneArrayWithEnumerableProperties(currentChildren, retainedChildren);
|
|
486
|
+
const applyResult = this.applyTopLevelChange(elementId, 'raw', 'children', currentChildren, nextChildren);
|
|
487
|
+
if (applyResult) {
|
|
488
|
+
emitDiagnosticCounter('computed-mirror-child-remove-batch-apply');
|
|
489
|
+
}
|
|
490
|
+
return applyResult;
|
|
491
|
+
}
|
|
492
|
+
composeRenderData(elementId) {
|
|
493
|
+
const entry = this.get(elementId);
|
|
494
|
+
if (!entry) {
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
return entry.renderDataSnapshot;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
4
500
|
class RenderSceneTree {
|
|
5
501
|
constructor() {
|
|
6
|
-
this.
|
|
502
|
+
this.computedDataMirror = new ComputedDataMirror();
|
|
503
|
+
this.projectedElementIds = new Set();
|
|
504
|
+
this.pendingElementUpdates = new Set();
|
|
505
|
+
this.pendingFrameFlush = false;
|
|
506
|
+
this.pendingFlush = false;
|
|
507
|
+
this.frameAlignedFlush = false;
|
|
508
|
+
this.flushingPendingChanges = false;
|
|
509
|
+
this.frameAlignedFlush = installPendingRenderLayer(this);
|
|
510
|
+
}
|
|
511
|
+
isDirectRenderProperty(elementId, key) {
|
|
512
|
+
if (key === 'visible' || DIRECT_RENDER_PROPERTY_KEYS.has(key)) {
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
const type = this.computedDataMirror.get(elementId)?.renderDataSnapshot.type;
|
|
516
|
+
return (typeof type === 'string' &&
|
|
517
|
+
renderStrategyRegistry.supportsDirectProperty(type, key));
|
|
518
|
+
}
|
|
519
|
+
getCanonicalReloadEntries(currentWorkspaceData, seededEntries) {
|
|
520
|
+
const visitedElementIds = new Set();
|
|
521
|
+
const entries = [];
|
|
522
|
+
const visitHierarchy = (roots) => {
|
|
523
|
+
const pending = [...roots].reverse();
|
|
524
|
+
while (pending.length > 0) {
|
|
525
|
+
const entry = pending.pop();
|
|
526
|
+
if (!entry || visitedElementIds.has(entry.elementId)) {
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
const seededEntry = seededEntries.get(entry.elementId);
|
|
530
|
+
if (!seededEntry) {
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
visitedElementIds.add(entry.elementId);
|
|
534
|
+
entries.push(entry);
|
|
535
|
+
const children = seededEntry.rawDataSnapshot.children;
|
|
536
|
+
if (Array.isArray(children)) {
|
|
537
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
538
|
+
const childId = children[index];
|
|
539
|
+
if (typeof childId === 'string') {
|
|
540
|
+
pending.push({ elementId: childId, siblingIndex: index });
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
if (Array.isArray(currentWorkspaceData.children)) {
|
|
547
|
+
visitHierarchy(currentWorkspaceData.children.flatMap((elementId, index) => typeof elementId === 'string'
|
|
548
|
+
? [{ elementId, siblingIndex: index }]
|
|
549
|
+
: []));
|
|
550
|
+
}
|
|
551
|
+
seededEntries.forEach((_entry, elementId) => {
|
|
552
|
+
if (!visitedElementIds.has(elementId)) {
|
|
553
|
+
visitHierarchy([{ elementId }]);
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
return entries;
|
|
7
557
|
}
|
|
8
558
|
reload() {
|
|
559
|
+
this.clearProjection();
|
|
9
560
|
if (!sceneTree.currentWorkspace)
|
|
10
561
|
return;
|
|
11
562
|
const currentWorkspaceData = sceneTree.currentWorkspace.save();
|
|
12
|
-
this._workspace = currentWorkspaceData;
|
|
13
563
|
// Create root render node
|
|
14
564
|
render.switchWorkspace({
|
|
15
565
|
label: currentWorkspaceData.id,
|
|
16
566
|
x: 0,
|
|
17
567
|
y: 0
|
|
18
568
|
});
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
569
|
+
// Rebuild parents before children and siblings in canonical hierarchy order.
|
|
570
|
+
try {
|
|
571
|
+
const seededEntries = new Map();
|
|
572
|
+
sceneTree.getAllElements().forEach((element) => {
|
|
573
|
+
const elementId = element.get('id');
|
|
574
|
+
if (element.get('type') === EntityTypes.WORKSPACE) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const entry = this.computedDataMirror.seed(elementId, 'reload');
|
|
578
|
+
if (entry) {
|
|
579
|
+
seededEntries.set(elementId, entry);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
this.getCanonicalReloadEntries(currentWorkspaceData, seededEntries).forEach(({ elementId, siblingIndex }) => {
|
|
583
|
+
const renderElementData = seededEntries.get(elementId)?.renderDataSnapshot;
|
|
584
|
+
if (renderElementData) {
|
|
585
|
+
this.addElement(renderElementData, siblingIndex);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
catch (error) {
|
|
590
|
+
emitDiagnosticCounter('computed-mirror-reload-seed-failed');
|
|
591
|
+
this.clearProjection();
|
|
592
|
+
throw error;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
_getRenderData(id) {
|
|
596
|
+
return this.computedDataMirror.composeRenderData(id);
|
|
597
|
+
}
|
|
598
|
+
projectionOutcome(elementId, status) {
|
|
599
|
+
return { status, elementId };
|
|
600
|
+
}
|
|
601
|
+
releaseProjectedElement(elementId) {
|
|
602
|
+
this.projectedElementIds.add(elementId);
|
|
603
|
+
render.removeElement(elementId);
|
|
604
|
+
this.projectedElementIds.delete(elementId);
|
|
605
|
+
}
|
|
606
|
+
resyncElement(elementId) {
|
|
607
|
+
emitDiagnosticCounter('computed-mirror-projection-mismatch');
|
|
608
|
+
this.pendingElementUpdates.delete(elementId);
|
|
609
|
+
this.computedDataMirror.delete(elementId);
|
|
610
|
+
let entry;
|
|
611
|
+
try {
|
|
612
|
+
entry = this.computedDataMirror.seed(elementId, 'resync');
|
|
613
|
+
}
|
|
614
|
+
catch {
|
|
615
|
+
emitDiagnosticCounter('computed-mirror-resync-failed');
|
|
616
|
+
this.releaseProjectedElement(elementId);
|
|
617
|
+
this.computedDataMirror.delete(elementId);
|
|
618
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
619
|
+
}
|
|
620
|
+
if (!entry) {
|
|
621
|
+
emitDiagnosticCounter('computed-mirror-resync-removed');
|
|
622
|
+
this.releaseProjectedElement(elementId);
|
|
623
|
+
return this.projectionOutcome(elementId, 'removed');
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
this.addElement(entry.renderDataSnapshot);
|
|
627
|
+
}
|
|
628
|
+
catch {
|
|
629
|
+
emitDiagnosticCounter('computed-mirror-resync-failed');
|
|
630
|
+
this.releaseProjectedElement(elementId);
|
|
631
|
+
this.computedDataMirror.delete(elementId);
|
|
632
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
633
|
+
}
|
|
634
|
+
emitDiagnosticCounter('computed-mirror-resync-success');
|
|
635
|
+
return this.projectionOutcome(elementId, 'resynced');
|
|
636
|
+
}
|
|
637
|
+
projectAddedElement(id, index) {
|
|
638
|
+
let entry;
|
|
639
|
+
try {
|
|
640
|
+
entry = this.computedDataMirror.seed(id, 'add');
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
this.pendingElementUpdates.delete(id);
|
|
644
|
+
emitDiagnosticCounter('computed-mirror-add-seed-failed');
|
|
645
|
+
this.releaseProjectedElement(id);
|
|
646
|
+
this.computedDataMirror.delete(id);
|
|
647
|
+
return this.projectionOutcome(id, 'failed');
|
|
648
|
+
}
|
|
649
|
+
if (!entry) {
|
|
650
|
+
this.pendingElementUpdates.delete(id);
|
|
651
|
+
this.releaseProjectedElement(id);
|
|
652
|
+
return this.projectionOutcome(id, 'removed');
|
|
653
|
+
}
|
|
654
|
+
try {
|
|
655
|
+
this.addElement(entry.renderDataSnapshot, index);
|
|
656
|
+
return this.projectionOutcome(id, 'applied');
|
|
657
|
+
}
|
|
658
|
+
catch {
|
|
659
|
+
this.pendingElementUpdates.delete(id);
|
|
660
|
+
emitDiagnosticCounter('computed-mirror-add-seed-failed');
|
|
661
|
+
this.releaseProjectedElement(id);
|
|
662
|
+
this.computedDataMirror.delete(id);
|
|
663
|
+
return this.projectionOutcome(id, 'failed');
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
projectAddedElementFromRawData(data, index) {
|
|
667
|
+
const { id } = data;
|
|
668
|
+
let entry;
|
|
669
|
+
try {
|
|
670
|
+
entry = this.computedDataMirror.seedFromRawData(id, data);
|
|
671
|
+
}
|
|
672
|
+
catch {
|
|
673
|
+
this.pendingElementUpdates.delete(id);
|
|
674
|
+
emitDiagnosticCounter('computed-mirror-add-seed-failed');
|
|
675
|
+
this.releaseProjectedElement(id);
|
|
676
|
+
this.computedDataMirror.delete(id);
|
|
677
|
+
return this.projectionOutcome(id, 'failed');
|
|
678
|
+
}
|
|
679
|
+
if (!entry) {
|
|
680
|
+
this.pendingElementUpdates.delete(id);
|
|
681
|
+
this.releaseProjectedElement(id);
|
|
682
|
+
return this.projectionOutcome(id, 'removed');
|
|
683
|
+
}
|
|
684
|
+
try {
|
|
685
|
+
this.addElement(entry.renderDataSnapshot, index);
|
|
686
|
+
return this.projectionOutcome(id, 'applied');
|
|
687
|
+
}
|
|
688
|
+
catch {
|
|
689
|
+
this.pendingElementUpdates.delete(id);
|
|
690
|
+
emitDiagnosticCounter('computed-mirror-add-seed-failed');
|
|
691
|
+
this.releaseProjectedElement(id);
|
|
692
|
+
this.computedDataMirror.delete(id);
|
|
693
|
+
return this.projectionOutcome(id, 'failed');
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
failProjectedAddition(elementId) {
|
|
697
|
+
this.pendingElementUpdates.delete(elementId);
|
|
698
|
+
this.releaseProjectedElement(elementId);
|
|
699
|
+
this.computedDataMirror.delete(elementId);
|
|
700
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
701
|
+
}
|
|
702
|
+
synchronizeAddedElement(outcome, parentId, index) {
|
|
703
|
+
if (outcome.status !== 'applied') {
|
|
704
|
+
return outcome;
|
|
705
|
+
}
|
|
706
|
+
const parentOutcome = this.synchronizeParentMembership(parentId, outcome.elementId, index, 'add');
|
|
707
|
+
if (parentOutcome?.status !== 'failed' &&
|
|
708
|
+
parentOutcome?.status !== 'removed') {
|
|
709
|
+
return outcome;
|
|
710
|
+
}
|
|
711
|
+
this.pendingElementUpdates.delete(outcome.elementId);
|
|
712
|
+
this.releaseProjectedElement(outcome.elementId);
|
|
713
|
+
this.computedDataMirror.delete(outcome.elementId);
|
|
714
|
+
return this.projectionOutcome(outcome.elementId, 'failed');
|
|
715
|
+
}
|
|
716
|
+
addElementById(id, parentId, index) {
|
|
717
|
+
return this.synchronizeAddedElement(this.projectAddedElement(id, index), parentId, index);
|
|
718
|
+
}
|
|
719
|
+
addElements(entries) {
|
|
720
|
+
if (entries.length === 0) {
|
|
721
|
+
return [];
|
|
722
|
+
}
|
|
723
|
+
const elementIds = entries.map(({ data }) => data.id);
|
|
724
|
+
if (new Set(elementIds).size !== elementIds.length ||
|
|
725
|
+
entries.some(({ data, parentId, index }) => typeof data.id !== 'string' ||
|
|
726
|
+
data.id.length === 0 ||
|
|
727
|
+
data.parentId !== parentId ||
|
|
728
|
+
typeof parentId !== 'string' ||
|
|
729
|
+
parentId.length === 0 ||
|
|
730
|
+
!Number.isInteger(index) ||
|
|
731
|
+
index < 0)) {
|
|
732
|
+
return entries.map(({ data }) => this.projectionOutcome(data.id, 'failed'));
|
|
733
|
+
}
|
|
734
|
+
const indexedEntriesByParent = new Map();
|
|
735
|
+
entries.forEach((entry, inputIndex) => {
|
|
736
|
+
const parentEntries = indexedEntriesByParent.get(entry.parentId) ?? [];
|
|
737
|
+
parentEntries.push({ entry, inputIndex });
|
|
738
|
+
indexedEntriesByParent.set(entry.parentId, parentEntries);
|
|
739
|
+
});
|
|
740
|
+
const outcomes = entries.map(({ data }) => this.projectionOutcome(data.id, 'failed'));
|
|
741
|
+
indexedEntriesByParent.forEach((indexedEntries, parentId) => {
|
|
742
|
+
if (!sceneTree.getElementById(parentId)) {
|
|
743
|
+
return;
|
|
24
744
|
}
|
|
745
|
+
const sortedEntries = [...indexedEntries].sort((left, right) => left.entry.index - right.entry.index);
|
|
746
|
+
sortedEntries.forEach(({ entry, inputIndex }) => {
|
|
747
|
+
outcomes[inputIndex] = this.projectAddedElementFromRawData(entry.data, entry.index);
|
|
748
|
+
});
|
|
749
|
+
const hasProjectionFailure = sortedEntries.some(({ inputIndex }) => outcomes[inputIndex]?.status !== 'applied');
|
|
750
|
+
const parentOutcome = hasProjectionFailure
|
|
751
|
+
? this.projectionOutcome(parentId, 'failed')
|
|
752
|
+
: this.synchronizeParentMembershipBatch(parentId, sortedEntries.map(({ entry }) => entry));
|
|
753
|
+
if (parentOutcome?.status !== 'failed' &&
|
|
754
|
+
parentOutcome?.status !== 'removed') {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
sortedEntries.forEach(({ entry, inputIndex }) => {
|
|
758
|
+
if (outcomes[inputIndex]?.status === 'applied') {
|
|
759
|
+
outcomes[inputIndex] = this.failProjectedAddition(entry.data.id);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
25
762
|
});
|
|
763
|
+
return outcomes;
|
|
26
764
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
765
|
+
addElement(data, siblingIndex) {
|
|
766
|
+
this.projectedElementIds.add(data.id);
|
|
767
|
+
const element = siblingIndex === undefined
|
|
768
|
+
? render.addElement(data)
|
|
769
|
+
: render.addElement(data, siblingIndex);
|
|
770
|
+
if (!element) {
|
|
771
|
+
throw new Error(`Render failed to rebuild element ${data.id}`);
|
|
772
|
+
}
|
|
773
|
+
return element;
|
|
774
|
+
}
|
|
775
|
+
removeElement(data, parentId, index) {
|
|
776
|
+
this.pendingElementUpdates.delete(data.id);
|
|
777
|
+
this.projectedElementIds.add(data.id);
|
|
778
|
+
render.removeElement(data.id, parentId);
|
|
779
|
+
this.projectedElementIds.delete(data.id);
|
|
780
|
+
this.computedDataMirror.delete(data.id);
|
|
781
|
+
const parentOutcome = this.synchronizeParentMembership(parentId, data.id, index, 'remove');
|
|
782
|
+
return parentOutcome?.status === 'failed'
|
|
783
|
+
? this.projectionOutcome(data.id, 'failed')
|
|
784
|
+
: this.projectionOutcome(data.id, 'removed');
|
|
785
|
+
}
|
|
786
|
+
removeElements(entries) {
|
|
787
|
+
if (entries.length === 0) {
|
|
788
|
+
return [];
|
|
789
|
+
}
|
|
790
|
+
const elementIds = entries.map(({ data }) => data.id);
|
|
791
|
+
if (new Set(elementIds).size !== elementIds.length ||
|
|
792
|
+
entries.some(({ data, parentId, index }) => typeof data.id !== 'string' ||
|
|
793
|
+
data.id.length === 0 ||
|
|
794
|
+
data.parentId !== parentId ||
|
|
795
|
+
typeof parentId !== 'string' ||
|
|
796
|
+
parentId.length === 0 ||
|
|
797
|
+
!Number.isInteger(index) ||
|
|
798
|
+
index < 0)) {
|
|
799
|
+
return entries.map(({ data }) => this.projectionOutcome(data.id, 'failed'));
|
|
800
|
+
}
|
|
801
|
+
const entriesByParent = new Map();
|
|
802
|
+
entries.forEach((entry) => {
|
|
803
|
+
const parentEntries = entriesByParent.get(entry.parentId) ?? [];
|
|
804
|
+
parentEntries.push(entry);
|
|
805
|
+
entriesByParent.set(entry.parentId, parentEntries);
|
|
806
|
+
});
|
|
807
|
+
const renderDataByElementId = new Map();
|
|
808
|
+
for (const { data } of entries) {
|
|
809
|
+
const renderData = this._getRenderData(data.id);
|
|
810
|
+
if (!renderData) {
|
|
811
|
+
return entries.map(({ data: failedData }) => this.projectionOutcome(failedData.id, 'failed'));
|
|
812
|
+
}
|
|
813
|
+
renderDataByElementId.set(data.id, renderData);
|
|
814
|
+
}
|
|
815
|
+
const appliedParentRemovals = [];
|
|
816
|
+
for (const [parentId, parentEntries] of entriesByParent) {
|
|
817
|
+
const parentOutcome = this.synchronizeParentRemovalBatch(parentId, parentEntries);
|
|
818
|
+
if (parentOutcome?.status === 'failed' ||
|
|
819
|
+
parentOutcome?.status === 'removed') {
|
|
820
|
+
for (const applied of [...appliedParentRemovals].reverse()) {
|
|
821
|
+
if (!this.compensateParentRemovalBatch(applied.parentId, applied.entries)) {
|
|
822
|
+
throw new Error(`Render failed to compensate parent ${applied.parentId}`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
return entries.map(({ data }) => this.projectionOutcome(data.id, 'failed'));
|
|
826
|
+
}
|
|
827
|
+
if (parentOutcome?.status === 'applied') {
|
|
828
|
+
appliedParentRemovals.push({
|
|
829
|
+
parentId,
|
|
830
|
+
entries: parentEntries
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
const visuallyRemovedEntries = [];
|
|
835
|
+
try {
|
|
836
|
+
for (const entry of entries) {
|
|
837
|
+
this.pendingElementUpdates.delete(entry.data.id);
|
|
838
|
+
render.removeElement(entry.data.id, entry.parentId);
|
|
839
|
+
visuallyRemovedEntries.push(entry);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
catch (error) {
|
|
843
|
+
for (const applied of [...appliedParentRemovals].reverse()) {
|
|
844
|
+
if (!this.compensateParentRemovalBatch(applied.parentId, applied.entries)) {
|
|
845
|
+
throw new Error(`Render failed to compensate parent ${applied.parentId}`);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
const restoreEntriesByParent = new Map();
|
|
849
|
+
visuallyRemovedEntries.forEach((entry) => {
|
|
850
|
+
const parentEntries = restoreEntriesByParent.get(entry.parentId) ?? [];
|
|
851
|
+
parentEntries.push(entry);
|
|
852
|
+
restoreEntriesByParent.set(entry.parentId, parentEntries);
|
|
853
|
+
});
|
|
854
|
+
for (const parentEntries of restoreEntriesByParent.values()) {
|
|
855
|
+
for (const entry of [...parentEntries].sort((left, right) => left.index - right.index)) {
|
|
856
|
+
const renderData = renderDataByElementId.get(entry.data.id);
|
|
857
|
+
if (!renderData) {
|
|
858
|
+
throw new Error(`Render lost removal compensation for ${entry.data.id}`);
|
|
859
|
+
}
|
|
860
|
+
this.addElement(renderData, entry.index);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
throw error;
|
|
864
|
+
}
|
|
865
|
+
entries.forEach(({ data }) => {
|
|
866
|
+
this.pendingElementUpdates.delete(data.id);
|
|
867
|
+
this.projectedElementIds.delete(data.id);
|
|
868
|
+
this.computedDataMirror.delete(data.id);
|
|
869
|
+
});
|
|
870
|
+
return entries.map(({ data }) => this.projectionOutcome(data.id, 'removed'));
|
|
871
|
+
}
|
|
872
|
+
moveElements(moves) {
|
|
873
|
+
const elementId = moves[0]?.elementId ?? '';
|
|
874
|
+
if (!Array.isArray(moves) ||
|
|
875
|
+
moves.length === 0 ||
|
|
876
|
+
new Set(moves.map((move) => move.elementId)).size !== moves.length) {
|
|
877
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
878
|
+
}
|
|
879
|
+
const canonicalParents = new Map();
|
|
880
|
+
const targetParentId = moves[0].after.parentId;
|
|
881
|
+
const affectedParentIds = [
|
|
882
|
+
targetParentId,
|
|
883
|
+
...moves.map((move) => move.before.parentId)
|
|
884
|
+
].filter((parentId, index, parentIds) => parentIds.indexOf(parentId) === index);
|
|
885
|
+
for (const parentId of affectedParentIds) {
|
|
886
|
+
const parent = sceneTree.getElementById(parentId);
|
|
887
|
+
if (!parent && parentId !== targetParentId) {
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
const parentData = parent?.save();
|
|
891
|
+
const children = parentData?.children;
|
|
892
|
+
if (!parent || !Array.isArray(children)) {
|
|
893
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
894
|
+
}
|
|
895
|
+
canonicalParents.set(parentId, [...children]);
|
|
896
|
+
}
|
|
897
|
+
const mirrorsMatchBefore = moves.every((move) => {
|
|
898
|
+
if (!this.computedDataMirror.matchesElementParent(move.elementId, move.before.parentId)) {
|
|
899
|
+
return false;
|
|
900
|
+
}
|
|
901
|
+
const source = sceneTree.getElementById(move.before.parentId);
|
|
902
|
+
return (source?.get('type') === EntityTypes.WORKSPACE ||
|
|
903
|
+
this.computedDataMirror.matchesParentChild(move.before.parentId, move.elementId, move.before.index));
|
|
904
|
+
});
|
|
905
|
+
const mirrorsMatchAfter = moves.every((move) => this.computedDataMirror.matchesElementParent(move.elementId, move.after.parentId));
|
|
906
|
+
const canonicalMatchesAfter = moves.every((move) => sceneTree.getElementById(move.elementId)?.get('parentId') ===
|
|
907
|
+
move.after.parentId &&
|
|
908
|
+
canonicalParents.get(move.after.parentId)?.includes(move.elementId) ===
|
|
909
|
+
true);
|
|
910
|
+
if ((!mirrorsMatchBefore && !mirrorsMatchAfter) || !canonicalMatchesAfter) {
|
|
911
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
912
|
+
}
|
|
913
|
+
const affectedElementIds = new Set(moves.map((move) => move.elementId));
|
|
914
|
+
affectedParentIds.forEach((parentId) => {
|
|
915
|
+
const parent = sceneTree.getElementById(parentId);
|
|
916
|
+
if (parent && parent.get('type') !== EntityTypes.WORKSPACE) {
|
|
917
|
+
affectedElementIds.add(parentId);
|
|
918
|
+
}
|
|
919
|
+
});
|
|
920
|
+
try {
|
|
921
|
+
affectedElementIds.forEach((affectedElementId) => {
|
|
922
|
+
if (!this.computedDataMirror.seed(affectedElementId, 'hierarchy')) {
|
|
923
|
+
throw new Error(`Render hierarchy snapshot is missing for ${affectedElementId}`);
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
canonicalParents.forEach((childIds, parentId) => {
|
|
927
|
+
render.projectHierarchy(parentId, childIds);
|
|
928
|
+
});
|
|
929
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
930
|
+
}
|
|
931
|
+
catch {
|
|
932
|
+
return this.projectionOutcome(elementId, 'failed');
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
applySubtreeChange(change) {
|
|
936
|
+
if (!Array.isArray(change.removed) || change.removed.length === 0) {
|
|
937
|
+
return this.projectionOutcome(change.elementId, 'failed');
|
|
938
|
+
}
|
|
939
|
+
if (change.action === SCENE_TREE_ACTIONS.REMOVE_SUBTREE) {
|
|
940
|
+
let failed = false;
|
|
941
|
+
change.removed.forEach(({ data, parentId, index }) => {
|
|
942
|
+
failed || (failed = this.removeElement(data, parentId, index).status === 'failed');
|
|
943
|
+
});
|
|
944
|
+
return this.projectionOutcome(change.elementId, failed ? 'failed' : 'removed');
|
|
945
|
+
}
|
|
946
|
+
if (change.action === SCENE_TREE_ACTIONS.RESTORE_SUBTREE) {
|
|
947
|
+
const rootEntry = change.removed.find(({ elementId }) => elementId === change.elementId);
|
|
948
|
+
if (!rootEntry) {
|
|
949
|
+
return this.projectionOutcome(change.elementId, 'failed');
|
|
950
|
+
}
|
|
951
|
+
const externalParent = sceneTree.getElementById(rootEntry.parentId);
|
|
952
|
+
if (externalParent &&
|
|
953
|
+
externalParent.get('type') !== EntityTypes.WORKSPACE &&
|
|
954
|
+
!this.computedDataMirror.seed(rootEntry.parentId, 'hierarchy')) {
|
|
955
|
+
return this.projectionOutcome(change.elementId, 'failed');
|
|
956
|
+
}
|
|
957
|
+
try {
|
|
958
|
+
for (const { elementId, index } of [...change.removed].reverse()) {
|
|
959
|
+
const entry = this.computedDataMirror.seed(elementId, 'hierarchy');
|
|
960
|
+
if (!entry) {
|
|
961
|
+
throw new Error(`Render subtree snapshot is missing for ${elementId}`);
|
|
962
|
+
}
|
|
963
|
+
this.addElement(entry.renderDataSnapshot, index);
|
|
964
|
+
}
|
|
965
|
+
return this.projectionOutcome(change.elementId, 'applied');
|
|
966
|
+
}
|
|
967
|
+
catch {
|
|
968
|
+
return this.projectionOutcome(change.elementId, 'failed');
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return this.projectionOutcome(change.elementId, 'failed');
|
|
972
|
+
}
|
|
973
|
+
synchronizeParentMembership(parentId, childId, index, action) {
|
|
974
|
+
if (!parentId) {
|
|
30
975
|
return null;
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
976
|
+
}
|
|
977
|
+
const parent = sceneTree.getElementById(parentId);
|
|
978
|
+
if (!parent || parent.get('type') === EntityTypes.WORKSPACE) {
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
981
|
+
if (action === 'add' &&
|
|
982
|
+
index !== undefined &&
|
|
983
|
+
this.computedDataMirror.matchesElementParent(childId, parentId) &&
|
|
984
|
+
this.computedDataMirror.matchesParentChild(parentId, childId, index)) {
|
|
985
|
+
emitDiagnosticCounter('computed-mirror-add-parent-already-synchronized');
|
|
986
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
987
|
+
}
|
|
988
|
+
const applyResult = this.computedDataMirror.applyChildMembershipChange(parentId, childId, index, action);
|
|
989
|
+
if (!applyResult) {
|
|
990
|
+
return this.resyncElement(parentId);
|
|
991
|
+
}
|
|
992
|
+
this.recordDirtyChange(parentId, 1, this.pendingElementUpdates.has(parentId));
|
|
993
|
+
this.pendingElementUpdates.add(parentId);
|
|
994
|
+
this.scheduleFlush();
|
|
995
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
37
996
|
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
41
|
-
this.
|
|
997
|
+
synchronizeParentMembershipBatch(parentId, additions) {
|
|
998
|
+
const parent = sceneTree.getElementById(parentId);
|
|
999
|
+
if (!parent) {
|
|
1000
|
+
return this.projectionOutcome(parentId, 'failed');
|
|
42
1001
|
}
|
|
1002
|
+
if (parent.get('type') === EntityTypes.WORKSPACE) {
|
|
1003
|
+
return null;
|
|
1004
|
+
}
|
|
1005
|
+
const alreadySynchronized = additions.every(({ data, index }) => this.computedDataMirror.matchesElementParent(data.id, parentId) &&
|
|
1006
|
+
this.computedDataMirror.matchesParentChild(parentId, data.id, index));
|
|
1007
|
+
if (alreadySynchronized) {
|
|
1008
|
+
emitDiagnosticCounter('computed-mirror-add-parent-already-synchronized', additions.length);
|
|
1009
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
1010
|
+
}
|
|
1011
|
+
if (additions.some(({ data }) => !this.computedDataMirror.matchesElementParent(data.id, parentId))) {
|
|
1012
|
+
return this.projectionOutcome(parentId, 'failed');
|
|
1013
|
+
}
|
|
1014
|
+
const applyResult = this.computedDataMirror.applyChildAdditionBatch(parentId, additions);
|
|
1015
|
+
if (!applyResult) {
|
|
1016
|
+
return this.projectionOutcome(parentId, 'failed');
|
|
1017
|
+
}
|
|
1018
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
1019
|
+
}
|
|
1020
|
+
synchronizeParentRemovalBatch(parentId, removals) {
|
|
1021
|
+
const parent = sceneTree.getElementById(parentId);
|
|
1022
|
+
if (parent?.get('type') === EntityTypes.WORKSPACE) {
|
|
1023
|
+
return null;
|
|
1024
|
+
}
|
|
1025
|
+
const applyResult = this.computedDataMirror.applyChildRemovalBatch(parentId, removals);
|
|
1026
|
+
if (!applyResult) {
|
|
1027
|
+
return this.projectionOutcome(parentId, 'failed');
|
|
1028
|
+
}
|
|
1029
|
+
if (!parent) {
|
|
1030
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
1031
|
+
}
|
|
1032
|
+
this.recordDirtyChange(parentId, removals.length, this.pendingElementUpdates.has(parentId));
|
|
1033
|
+
this.pendingElementUpdates.add(parentId);
|
|
1034
|
+
this.scheduleFlush();
|
|
1035
|
+
return this.projectionOutcome(parentId, 'applied');
|
|
43
1036
|
}
|
|
44
|
-
|
|
45
|
-
|
|
1037
|
+
compensateParentRemovalBatch(parentId, removals) {
|
|
1038
|
+
const applyResult = this.computedDataMirror.applyChildAdditionBatch(parentId, removals);
|
|
1039
|
+
if (!applyResult) {
|
|
1040
|
+
emitDiagnosticCounter('computed-mirror-child-remove-batch-compensation-failed');
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
emitDiagnosticCounter('computed-mirror-child-remove-batch-compensated');
|
|
1044
|
+
this.recordDirtyChange(parentId, removals.length, this.pendingElementUpdates.has(parentId));
|
|
1045
|
+
this.pendingElementUpdates.add(parentId);
|
|
1046
|
+
this.scheduleFlush();
|
|
1047
|
+
return true;
|
|
46
1048
|
}
|
|
47
|
-
|
|
48
|
-
|
|
1049
|
+
updateElement(elementId, owner, key, before, after, _options) {
|
|
1050
|
+
const applyResult = this.computedDataMirror.applyTopLevelChange(elementId, owner, key, before, after);
|
|
1051
|
+
if (!applyResult) {
|
|
1052
|
+
return this.resyncElement(elementId);
|
|
1053
|
+
}
|
|
1054
|
+
const [effectiveChange] = applyResult.effectiveChanges;
|
|
1055
|
+
if (!effectiveChange) {
|
|
1056
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1057
|
+
}
|
|
1058
|
+
if (this.isDirectRenderProperty(elementId, key)) {
|
|
1059
|
+
this.recordDirtyChange(elementId, 1, this.pendingFrameFlush);
|
|
1060
|
+
measureBrowserDragPhase('render-scene-tree:update-direct-property', () => render.updateElement(elementId, key, effectiveChange.before, effectiveChange.after));
|
|
1061
|
+
this.pendingFrameFlush = true;
|
|
1062
|
+
this.scheduleFlush();
|
|
1063
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1064
|
+
}
|
|
1065
|
+
// Non-direct updates commit through the complete frame snapshot.
|
|
1066
|
+
this.recordDirtyChange(elementId, 1, this.pendingElementUpdates.has(elementId));
|
|
1067
|
+
this.pendingElementUpdates.add(elementId);
|
|
1068
|
+
this.scheduleFlush();
|
|
1069
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1070
|
+
}
|
|
1071
|
+
updateElementBatch(elementId, changes, _options) {
|
|
1072
|
+
const applyResult = this.computedDataMirror.applyTopLevelChanges(elementId, changes);
|
|
1073
|
+
if (!applyResult) {
|
|
1074
|
+
return this.resyncElement(elementId);
|
|
1075
|
+
}
|
|
1076
|
+
const { effectiveChanges } = applyResult;
|
|
1077
|
+
if (effectiveChanges.length === 0) {
|
|
1078
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1079
|
+
}
|
|
1080
|
+
const hasComputedFullUpdate = effectiveChanges.some(({ key }) => !this.isDirectRenderProperty(elementId, key));
|
|
1081
|
+
this.recordDirtyChange(elementId, effectiveChanges.length, hasComputedFullUpdate
|
|
1082
|
+
? this.pendingElementUpdates.has(elementId)
|
|
1083
|
+
: this.pendingFrameFlush);
|
|
1084
|
+
effectiveChanges.forEach(({ key, before, after }) => {
|
|
1085
|
+
if (!hasComputedFullUpdate &&
|
|
1086
|
+
this.isDirectRenderProperty(elementId, key)) {
|
|
1087
|
+
measureBrowserDragPhase('render-scene-tree:update-direct-property', () => render.updateElement(elementId, key, before, after));
|
|
1088
|
+
this.pendingFrameFlush = true;
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
if (hasComputedFullUpdate) {
|
|
1092
|
+
this.pendingElementUpdates.add(elementId);
|
|
1093
|
+
}
|
|
1094
|
+
this.scheduleFlush();
|
|
1095
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1096
|
+
}
|
|
1097
|
+
updateElementPatch(elementId, patch, options) {
|
|
1098
|
+
if (options?.undoable !== false) {
|
|
1099
|
+
Object.keys(patch.values ?? {}).forEach((key) => {
|
|
1100
|
+
emitDiagnosticCounter(`computed-mirror-undoable-refresh-key-${key}`);
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
const didStage = measureBrowserDragPhase('render-scene-tree:apply-computed-patch', () => this.computedDataMirror.applyComputedPatch(elementId, patch));
|
|
1104
|
+
if (!didStage) {
|
|
1105
|
+
return this.resyncElement(elementId);
|
|
1106
|
+
}
|
|
1107
|
+
const directValueChanges = Object.entries(patch.values ?? {}).filter(([key]) => this.isDirectRenderProperty(elementId, key));
|
|
1108
|
+
const hasComputedFullUpdate = Object.keys(patch.records ?? {}).length > 0 ||
|
|
1109
|
+
Object.keys(patch.values ?? {}).some((key) => !this.isDirectRenderProperty(elementId, key));
|
|
1110
|
+
const changeCount = Object.keys(patch.values ?? {}).length +
|
|
1111
|
+
Object.values(patch.records ?? {}).reduce((sum, recordPatch) => sum +
|
|
1112
|
+
Object.keys(recordPatch.set ?? {}).length +
|
|
1113
|
+
Object.keys(recordPatch.remove ?? {}).length, 0);
|
|
1114
|
+
this.recordDirtyChange(elementId, changeCount, hasComputedFullUpdate
|
|
1115
|
+
? this.pendingElementUpdates.has(elementId)
|
|
1116
|
+
: this.pendingFrameFlush);
|
|
1117
|
+
directValueChanges.forEach(([key, change]) => {
|
|
1118
|
+
if (!hasComputedFullUpdate) {
|
|
1119
|
+
measureBrowserDragPhase('render-scene-tree:update-direct-property', () => render.updateElement(elementId, key, change.before, change.after));
|
|
1120
|
+
this.pendingFrameFlush = true;
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
if (hasComputedFullUpdate) {
|
|
1124
|
+
this.pendingElementUpdates.add(elementId);
|
|
1125
|
+
}
|
|
1126
|
+
this.scheduleFlush();
|
|
1127
|
+
return this.projectionOutcome(elementId, 'applied');
|
|
1128
|
+
}
|
|
1129
|
+
commitPendingComputedDataChanges() {
|
|
1130
|
+
this.scheduleFlush();
|
|
1131
|
+
}
|
|
1132
|
+
recordDirtyChange(elementId, changeCount, didCoalesce) {
|
|
1133
|
+
emitDiagnosticCounter('dirty-change-count', changeCount);
|
|
1134
|
+
if (didCoalesce) {
|
|
1135
|
+
emitDiagnosticCounter('dirty-change-coalesced-count', changeCount);
|
|
1136
|
+
}
|
|
1137
|
+
if (!this.pendingElementUpdates.has(elementId)) {
|
|
1138
|
+
emitDiagnosticCounter('dirty-element-count');
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
scheduleFlush() {
|
|
1142
|
+
if (!this.hasPendingChanges()) {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
if (this.pendingFlush) {
|
|
1146
|
+
emitDiagnosticCounter('render-scene-tree-flush-coalesced');
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
this.pendingFlush = true;
|
|
1150
|
+
if (this.frameAlignedFlush) {
|
|
1151
|
+
emitDiagnosticCounter('render-scene-tree-frame-aligned-flush');
|
|
1152
|
+
render.requestRender();
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
const schedule = typeof queueMicrotask === 'function'
|
|
1156
|
+
? queueMicrotask
|
|
1157
|
+
: (callback) => {
|
|
1158
|
+
Promise.resolve().then(callback);
|
|
1159
|
+
};
|
|
1160
|
+
schedule(() => {
|
|
1161
|
+
this.flushPendingChanges();
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
hasPendingChanges() {
|
|
1165
|
+
return this.pendingFrameFlush || this.pendingElementUpdates.size > 0;
|
|
1166
|
+
}
|
|
1167
|
+
getProjectionSnapshotCount() {
|
|
1168
|
+
return this.computedDataMirror.size;
|
|
1169
|
+
}
|
|
1170
|
+
resetProjection() {
|
|
1171
|
+
const clearedEntryCount = this.computedDataMirror.clear();
|
|
1172
|
+
this.pendingElementUpdates.clear();
|
|
1173
|
+
this.pendingFrameFlush = false;
|
|
1174
|
+
this.pendingFlush = false;
|
|
1175
|
+
emitDiagnosticCounter('computed-mirror-reset-entry-count', clearedEntryCount);
|
|
1176
|
+
}
|
|
1177
|
+
clearProjection() {
|
|
1178
|
+
const projectedElementIds = new Set([
|
|
1179
|
+
...this.projectedElementIds,
|
|
1180
|
+
...this.computedDataMirror.elementIds
|
|
1181
|
+
]);
|
|
1182
|
+
this.pendingElementUpdates.clear();
|
|
1183
|
+
this.pendingFrameFlush = false;
|
|
1184
|
+
this.pendingFlush = false;
|
|
1185
|
+
let releasedEntryCount = 0;
|
|
1186
|
+
let firstFailure;
|
|
1187
|
+
projectedElementIds.forEach((elementId) => {
|
|
1188
|
+
try {
|
|
1189
|
+
this.releaseProjectedElement(elementId);
|
|
1190
|
+
this.computedDataMirror.delete(elementId);
|
|
1191
|
+
releasedEntryCount += 1;
|
|
1192
|
+
}
|
|
1193
|
+
catch (error) {
|
|
1194
|
+
firstFailure ?? (firstFailure = error);
|
|
1195
|
+
}
|
|
1196
|
+
});
|
|
1197
|
+
try {
|
|
1198
|
+
render.switchWorkspace({ label: '', x: 0, y: 0 });
|
|
1199
|
+
}
|
|
1200
|
+
catch (error) {
|
|
1201
|
+
firstFailure ?? (firstFailure = error);
|
|
1202
|
+
}
|
|
1203
|
+
emitDiagnosticCounter('computed-mirror-reset-entry-count', releasedEntryCount);
|
|
1204
|
+
if (firstFailure !== undefined) {
|
|
1205
|
+
throw firstFailure;
|
|
1206
|
+
}
|
|
49
1207
|
}
|
|
50
|
-
|
|
51
|
-
|
|
1208
|
+
flushPendingChanges() {
|
|
1209
|
+
if (this.flushingPendingChanges) {
|
|
1210
|
+
emitDiagnosticCounter('render-scene-tree-flush-reentrant-skipped');
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
const didFlush = this.applyPendingChanges();
|
|
1214
|
+
if (didFlush) {
|
|
1215
|
+
render.flushFrame();
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
flushPendingChangesForFrame() {
|
|
1219
|
+
return this.applyPendingChanges();
|
|
1220
|
+
}
|
|
1221
|
+
applyPendingChanges() {
|
|
1222
|
+
if (this.flushingPendingChanges) {
|
|
1223
|
+
emitDiagnosticCounter('render-scene-tree-flush-reentrant-skipped');
|
|
1224
|
+
return false;
|
|
1225
|
+
}
|
|
1226
|
+
if (!this.pendingFrameFlush && this.pendingElementUpdates.size === 0) {
|
|
1227
|
+
this.pendingFlush = false;
|
|
1228
|
+
return false;
|
|
1229
|
+
}
|
|
1230
|
+
return measureBrowserDragPhase('render-scene-tree:flush', () => {
|
|
1231
|
+
this.flushingPendingChanges = true;
|
|
1232
|
+
const hadPendingFrameFlush = this.pendingFrameFlush;
|
|
1233
|
+
try {
|
|
1234
|
+
this.pendingFlush = false;
|
|
1235
|
+
emitDiagnosticCounter('computed-mirror-commit-count');
|
|
1236
|
+
this.pendingFrameFlush = false;
|
|
1237
|
+
const ids = Array.from(this.pendingElementUpdates);
|
|
1238
|
+
emitDiagnosticCounter('product-render-per-render-frame', ids.length);
|
|
1239
|
+
ids.forEach((id) => {
|
|
1240
|
+
this.pendingElementUpdates.delete(id);
|
|
1241
|
+
const data = this._getRenderData(id);
|
|
1242
|
+
if (data) {
|
|
1243
|
+
try {
|
|
1244
|
+
measureBrowserDragPhase('render-scene-tree:update-element', () => render.updateElement(id, 'computed',
|
|
1245
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1246
|
+
undefined,
|
|
1247
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1248
|
+
undefined, data));
|
|
1249
|
+
}
|
|
1250
|
+
catch (error) {
|
|
1251
|
+
this.pendingElementUpdates.add(id);
|
|
1252
|
+
throw error;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
return true;
|
|
1257
|
+
}
|
|
1258
|
+
catch (error) {
|
|
1259
|
+
this.pendingFrameFlush || (this.pendingFrameFlush = hadPendingFrameFlush);
|
|
1260
|
+
throw error;
|
|
1261
|
+
}
|
|
1262
|
+
finally {
|
|
1263
|
+
this.flushingPendingChanges = false;
|
|
1264
|
+
}
|
|
1265
|
+
});
|
|
52
1266
|
}
|
|
53
1267
|
}
|
|
1268
|
+
let activeRenderSceneTree = null;
|
|
1269
|
+
let pendingRenderLayerInstalled = false;
|
|
1270
|
+
let pendingRenderTeardownInstalled = false;
|
|
1271
|
+
const installPendingRenderLayer = (store) => {
|
|
1272
|
+
activeRenderSceneTree = store;
|
|
1273
|
+
const renderWithLayer = render;
|
|
1274
|
+
if (typeof renderWithLayer.registerLayer !== 'function') {
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
const renderWithLifecycle = render;
|
|
1278
|
+
if (!pendingRenderTeardownInstalled &&
|
|
1279
|
+
typeof renderWithLifecycle.registerTeardownCleanup === 'function') {
|
|
1280
|
+
renderWithLifecycle.registerTeardownCleanup(() => {
|
|
1281
|
+
activeRenderSceneTree?.clearProjection();
|
|
1282
|
+
});
|
|
1283
|
+
pendingRenderTeardownInstalled = true;
|
|
1284
|
+
}
|
|
1285
|
+
if (!pendingRenderLayerInstalled) {
|
|
1286
|
+
renderWithLayer.registerLayer({
|
|
1287
|
+
name: SCENE_TREE_PENDING_RENDER_LAYER,
|
|
1288
|
+
layer: {},
|
|
1289
|
+
shouldUpdate: () => activeRenderSceneTree?.hasPendingChanges() ?? false,
|
|
1290
|
+
update: () => activeRenderSceneTree?.flushPendingChangesForFrame() ?? false
|
|
1291
|
+
});
|
|
1292
|
+
pendingRenderLayerInstalled = true;
|
|
1293
|
+
}
|
|
1294
|
+
return true;
|
|
1295
|
+
};
|
|
54
1296
|
export { RenderSceneTree };
|
|
55
1297
|
const renderSceneTree = new RenderSceneTree();
|
|
56
1298
|
export default renderSceneTree;
|