Skip to content

Commit 8891748

Browse files
authored
fix: optimize saving global variables (#4936)
Reduced amount iterations over all instances to optimize rebinding variables on save.
1 parent 95cbb1c commit 8891748

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

apps/builder/app/shared/data-variables.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,7 @@ export const computeExpression = (
189189
}
190190
};
191191

192-
const findMaskedVariablesByInstanceId = ({
193-
startingInstanceId,
194-
instances,
195-
dataSources,
196-
}: {
197-
startingInstanceId: Instance["id"];
198-
instances: Instances;
199-
dataSources: DataSources;
200-
}) => {
192+
const getParentInstanceById = (instances: Instances) => {
201193
const parentInstanceById = new Map<Instance["id"], Instance["id"]>();
202194
for (const instance of instances.values()) {
203195
// interrupt lookup because slot variables cannot be passed to slot content
@@ -210,6 +202,18 @@ const findMaskedVariablesByInstanceId = ({
210202
}
211203
}
212204
}
205+
return parentInstanceById;
206+
};
207+
208+
const findMaskedVariablesByInstanceId = ({
209+
startingInstanceId,
210+
parentInstanceById,
211+
dataSources,
212+
}: {
213+
startingInstanceId: Instance["id"];
214+
parentInstanceById: Map<Instance["id"], Instance["id"]>;
215+
dataSources: DataSources;
216+
}) => {
213217
let currentId: undefined | string = startingInstanceId;
214218
const instanceIdsPath: Instance["id"][] = [];
215219
while (currentId) {
@@ -244,7 +248,7 @@ export const findAvailableVariables = ({
244248
}) => {
245249
const maskedVariables = findMaskedVariablesByInstanceId({
246250
startingInstanceId,
247-
instances,
251+
parentInstanceById: getParentInstanceById(instances),
248252
dataSources,
249253
});
250254
const availableVariables: DataSource[] = [];
@@ -486,6 +490,8 @@ export const rebindTreeVariablesMutable = ({
486490
for (const dataSource of dataSources.values()) {
487491
unsetNameById.set(dataSource.id, dataSource.name);
488492
}
493+
// precompute parent instances outside of traverse
494+
const parentInstanceById = getParentInstanceById(instances);
489495
traverseExpressions({
490496
startingInstanceId,
491497
pages,
@@ -497,8 +503,8 @@ export const rebindTreeVariablesMutable = ({
497503
// restore all masked variables of current scope
498504
const maskedVariables = findMaskedVariablesByInstanceId({
499505
startingInstanceId: instanceId,
506+
parentInstanceById,
500507
dataSources,
501-
instances,
502508
});
503509
let maskedIdByName = new Map(maskedVariables);
504510
if (args) {
@@ -537,7 +543,7 @@ export const deleteVariableMutable = (
537543
const startingInstanceId = dataSource.scopeInstanceId ?? "";
538544
const maskedIdByName = findMaskedVariablesByInstanceId({
539545
startingInstanceId,
540-
instances: data.instances,
546+
parentInstanceById: getParentInstanceById(data.instances),
541547
dataSources: data.dataSources,
542548
});
543549
// unset deleted variable in expressions

0 commit comments

Comments
 (0)