PromucFlow_constructor/app/client/src/workers/Evaluation/evalTreeWithChanges.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

feat: JSObject variable as a state (JSObject variable mutation) (#19926) Fixes #19653 Fixes #14568 Fixes #17199 Fixes #14989 In this PR, we introduce a new feature in JSObject where the `variables` are now state and widgets are reactive to the change in the variable value. - It means that `JSObject.myVar1 = "Hello world"` would show `Hello world` where ever a binding `{{JSObject.myVar1}}` is used. Further changes - JSObject run functionality, executes all the functions in async evaluation. - `executeSyncJS` flow is removed - `resolvedFunctions` state is moved to JSCollection class. - unEval JSObject value i.e., currentJSCollectionState is moved to JSCollection class. - `evalTreeWithChanges` is introduced - A new flow to trigger evaluation from the worker and send the updated dataTree to mainThread. - This would open up a new possibility of features in evaluation mentioned [here](https://www.notion.so/appsmith/RFC-Dependent-Property-in-Widgets-f3b29ad652b549dd8c49189f48dbbc4b) - Introduction of `updateDataTreeHandler` to accept new dataTree from the worker. ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? ### Jest Test - `Mutation.test.ts` - `JSVariableProxy.test.ts` - `removeProxy.test.ts` ### Cypress test - Mutation with - numbers - array - object - map - set ### Test Plan - https://github.com/appsmithorg/TestSmith/issues/2186 ### Issues raised during DP testing - https://github.com/appsmithorg/appsmith/pull/19926#issuecomment-1453275688 - https://github.com/appsmithorg/appsmith/pull/19926#issuecomment-1478975487 - https://github.com/appsmithorg/appsmith/pull/19926#issuecomment-1482929425 - https://github.com/appsmithorg/appsmith/pull/19926#issuecomment-1486611858 Co-authored-by: Rimil Dey <rimildeyjsr@gmail.com> Co-authored-by: Rimil Dey <rimil@appsmith.com> Co-authored-by: arunvjn <32433245+arunvjn@users.noreply.github.com>
2023-04-07 07:41:36 +00:00
import type {
ConfigTree,
DataTree,
UnEvalTree,
} from "entities/DataTree/dataTreeFactory";
import { dataTreeEvaluator } from "./handlers/evalTree";
import type { DataTreeDiff } from "@appsmith/workers/Evaluation/evaluationUtils";
import type { EvalMetaUpdates } from "@appsmith/workers/common/DataTreeEvaluator/types";
import type { DependencyMap, EvalError } from "utils/DynamicBindingUtils";
import { makeEntityConfigsAsObjProperties } from "@appsmith/workers/Evaluation/dataTreeUtils";
import type { EvalTreeResponseData } from "./types";
import { MessageType, sendMessage } from "utils/MessageUtil";
import { MAIN_THREAD_ACTION } from "@appsmith/workers/Evaluation/evalWorkerActions";
import type { UpdateDataTreeMessageData } from "sagas/EvalWorkerActionSagas";
import type { JSUpdate } from "utils/JSPaneUtils";
export function evalTreeWithChanges(updatedValuePaths: string[][]) {
let evalOrder: string[] = [];
let jsUpdates: Record<string, JSUpdate> = {};
let unEvalUpdates: DataTreeDiff[] = [];
let nonDynamicFieldValidationOrder: string[] = [];
const isCreateFirstTree = false;
let dataTree: DataTree = {};
const errors: EvalError[] = [];
const logs: any[] = [];
const dependencies: DependencyMap = {};
let evalMetaUpdates: EvalMetaUpdates = [];
let staleMetaIds: string[] = [];
const pathsToClearErrorsFor: any[] = [];
if (dataTreeEvaluator) {
const setupUpdateTreeResponse =
dataTreeEvaluator.setupUpdateTreeWithDifferences(updatedValuePaths);
evalOrder = setupUpdateTreeResponse.evalOrder;
unEvalUpdates = setupUpdateTreeResponse.unEvalUpdates;
jsUpdates = setupUpdateTreeResponse.jsUpdates;
nonDynamicFieldValidationOrder =
setupUpdateTreeResponse.nonDynamicFieldValidationOrder;
const updateResponse = dataTreeEvaluator.evalAndValidateSubTree(
evalOrder,
nonDynamicFieldValidationOrder,
dataTreeEvaluator.oldConfigTree,
unEvalUpdates,
);
dataTree = makeEntityConfigsAsObjProperties(dataTreeEvaluator.evalTree, {
evalProps: dataTreeEvaluator.evalProps,
});
evalMetaUpdates = JSON.parse(
JSON.stringify(updateResponse.evalMetaUpdates),
);
staleMetaIds = updateResponse.staleMetaIds;
}
const evalTreeResponse: EvalTreeResponseData = {
dataTree,
dependencies,
errors,
evalMetaUpdates,
evaluationOrder: evalOrder,
jsUpdates,
logs,
unEvalUpdates,
isCreateFirstTree,
configTree: dataTreeEvaluator?.oldConfigTree as ConfigTree,
staleMetaIds,
pathsToClearErrorsFor,
isNewWidgetAdded: false,
};
const data: UpdateDataTreeMessageData = {
workerResponse: evalTreeResponse,
unevalTree: dataTreeEvaluator?.getOldUnevalTree() as UnEvalTree,
};
sendMessage.call(self, {
messageType: MessageType.DEFAULT,
body: {
data,
method: MAIN_THREAD_ACTION.UPDATE_DATATREE,
},
});
}