2023-02-11 18:33:20 +00:00
|
|
|
import { all, call, put, spawn, take } from "redux-saga/effects";
|
2023-01-16 11:56:18 +00:00
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
|
|
|
|
import { MAIN_THREAD_ACTION } from "@appsmith/workers/Evaluation/evalWorkerActions";
|
|
|
|
|
import log from "loglevel";
|
|
|
|
|
import { evalErrorHandler } from "../sagas/PostEvaluationSagas";
|
|
|
|
|
import { Channel } from "redux-saga";
|
|
|
|
|
import { storeLogs } from "../sagas/DebuggerSagas";
|
2023-01-28 13:12:11 +00:00
|
|
|
import {
|
|
|
|
|
BatchedJSExecutionData,
|
|
|
|
|
BatchedJSExecutionErrors,
|
|
|
|
|
} from "reducers/entityReducers/jsActionsReducer";
|
2023-01-16 11:56:18 +00:00
|
|
|
import { MessageType, TMessage } from "utils/MessageUtil";
|
|
|
|
|
import {
|
|
|
|
|
ResponsePayload,
|
|
|
|
|
evalWorker,
|
|
|
|
|
executeTriggerRequestSaga,
|
|
|
|
|
} from "../sagas/EvaluationsSaga";
|
|
|
|
|
import { logJSFunctionExecution } from "@appsmith/sagas/JSFunctionExecutionSaga";
|
|
|
|
|
import { handleStoreOperations } from "./ActionExecution/StoreActionSaga";
|
2023-02-11 18:33:20 +00:00
|
|
|
import isEmpty from "lodash/isEmpty";
|
|
|
|
|
import { sortJSExecutionDataByCollectionId } from "workers/Evaluation/JSObject/utils";
|
2023-01-16 11:56:18 +00:00
|
|
|
|
|
|
|
|
export function* handleEvalWorkerRequestSaga(listenerChannel: Channel<any>) {
|
|
|
|
|
while (true) {
|
|
|
|
|
const request: TMessage<any> = yield take(listenerChannel);
|
|
|
|
|
yield spawn(handleEvalWorkerMessage, request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* lintTreeActionHandler(message: any) {
|
|
|
|
|
const { body } = message;
|
|
|
|
|
const { data } = body;
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.LINT_TREE,
|
|
|
|
|
payload: {
|
|
|
|
|
pathsToLint: data.lintOrder,
|
|
|
|
|
unevalTree: data.unevalTree,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* processLogsHandler(message: any) {
|
|
|
|
|
const { body } = message;
|
|
|
|
|
const { data } = body;
|
2023-02-11 18:33:20 +00:00
|
|
|
yield call(storeLogs, data);
|
2023-01-16 11:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* processJSFunctionExecution(message: any) {
|
|
|
|
|
const { body } = message;
|
2023-01-28 13:12:11 +00:00
|
|
|
const {
|
|
|
|
|
data: { JSExecutionData, JSExecutionErrors },
|
|
|
|
|
} = body;
|
|
|
|
|
const {
|
|
|
|
|
sortedData,
|
|
|
|
|
sortedErrors,
|
|
|
|
|
}: {
|
|
|
|
|
sortedData: BatchedJSExecutionData;
|
|
|
|
|
sortedErrors: BatchedJSExecutionErrors;
|
2023-02-11 18:33:20 +00:00
|
|
|
} = yield* sortJSExecutionDataByCollectionId(
|
2023-01-28 13:12:11 +00:00
|
|
|
JSExecutionData,
|
|
|
|
|
JSExecutionErrors,
|
2023-01-16 11:56:18 +00:00
|
|
|
);
|
2023-01-28 13:12:11 +00:00
|
|
|
if (!isEmpty(sortedData)) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SET_JS_FUNCTION_EXECUTION_DATA,
|
|
|
|
|
payload: sortedData,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!isEmpty(sortedErrors)) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SET_JS_FUNCTION_EXECUTION_ERRORS,
|
|
|
|
|
payload: sortedErrors,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-16 11:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* processTriggerHandler(message: any) {
|
|
|
|
|
const { body } = message;
|
|
|
|
|
const { data } = body;
|
|
|
|
|
const { eventType, trigger, triggerMeta } = data;
|
|
|
|
|
const { messageType } = message;
|
|
|
|
|
log.debug({ trigger: data.trigger });
|
|
|
|
|
const result: ResponsePayload = yield call(
|
|
|
|
|
executeTriggerRequestSaga,
|
|
|
|
|
trigger,
|
|
|
|
|
eventType,
|
|
|
|
|
triggerMeta,
|
|
|
|
|
);
|
|
|
|
|
if (messageType === MessageType.REQUEST)
|
|
|
|
|
yield call(evalWorker.respond, message.messageId, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* handleEvalWorkerMessage(message: TMessage<any>) {
|
|
|
|
|
const { body } = message;
|
|
|
|
|
const { data, method } = body;
|
|
|
|
|
switch (method) {
|
|
|
|
|
case MAIN_THREAD_ACTION.LINT_TREE: {
|
2023-02-11 18:33:20 +00:00
|
|
|
yield call(lintTreeActionHandler, message);
|
2023-01-16 11:56:18 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MAIN_THREAD_ACTION.PROCESS_LOGS: {
|
2023-02-11 18:33:20 +00:00
|
|
|
yield call(processLogsHandler, message);
|
2023-01-16 11:56:18 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MAIN_THREAD_ACTION.PROCESS_JS_FUNCTION_EXECUTION: {
|
2023-02-11 18:33:20 +00:00
|
|
|
yield call(processJSFunctionExecution, message);
|
2023-01-16 11:56:18 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MAIN_THREAD_ACTION.PROCESS_TRIGGER: {
|
2023-02-11 18:33:20 +00:00
|
|
|
yield call(processTriggerHandler, message);
|
2023-01-16 11:56:18 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MAIN_THREAD_ACTION.PROCESS_STORE_UPDATES: {
|
|
|
|
|
yield call(handleStoreOperations, data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MAIN_THREAD_ACTION.LOG_JS_FUNCTION_EXECUTION: {
|
|
|
|
|
yield logJSFunctionExecution(message);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-11 18:33:20 +00:00
|
|
|
case MAIN_THREAD_ACTION.PROCESS_BATCHED_TRIGGERS: {
|
|
|
|
|
const batchedTriggers = data;
|
|
|
|
|
yield all(
|
|
|
|
|
batchedTriggers.map((data: any) => {
|
|
|
|
|
const { eventType, trigger, triggerMeta } = data;
|
|
|
|
|
return call(
|
|
|
|
|
executeTriggerRequestSaga,
|
|
|
|
|
trigger,
|
|
|
|
|
eventType,
|
|
|
|
|
triggerMeta,
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-16 11:56:18 +00:00
|
|
|
}
|
2023-02-21 04:27:56 +00:00
|
|
|
|
2023-01-16 11:56:18 +00:00
|
|
|
yield call(evalErrorHandler, data?.errors || []);
|
|
|
|
|
}
|