diff --git a/app/client/src/ce/actions/evaluationActionsList.ts b/app/client/src/ce/actions/evaluationActionsList.ts index 639c18571f31..a8bb980d00d9 100644 --- a/app/client/src/ce/actions/evaluationActionsList.ts +++ b/app/client/src/ce/actions/evaluationActionsList.ts @@ -107,7 +107,8 @@ export const EVALUATE_REDUX_ACTIONS = [ // Buffer ReduxActionTypes.BUFFERED_ACTION, // Generic - ReduxActionTypes.TRIGGER_EVAL, + // ReduxActionTypes.TRIGGER_EVAL, + ReduxActionTypes.TRIGGER_EVAL_BATCH, ]; // Topics used for datasource and query form evaluations export const FORM_EVALUATION_REDUX_ACTIONS = [ diff --git a/app/client/src/ce/constants/ReduxActionConstants.tsx b/app/client/src/ce/constants/ReduxActionConstants.tsx index 0ecaa63d846e..4fc0c71e3da7 100644 --- a/app/client/src/ce/constants/ReduxActionConstants.tsx +++ b/app/client/src/ce/constants/ReduxActionConstants.tsx @@ -297,6 +297,7 @@ const EvaluationActionTypes = { BUFFERED_ACTION: "BUFFERED_ACTION", CLEAR_CACHE: "CLEAR_CACHE", TRIGGER_EVAL: "TRIGGER_EVAL", + TRIGGER_EVAL_BATCH: "TRIGGER_EVAL_BATCH", UPDATE_ACTION_DATA: "UPDATE_ACTION_DATA", SET_ACTIVE_EDITOR_FIELD: "SET_ACTIVE_EDITOR_FIELD", RESET_ACTIVE_EDITOR_FIELD: "RESET_ACTIVE_EDITOR_FIELD", diff --git a/app/client/src/sagas/ActionExecution/PluginActionSaga.ts b/app/client/src/sagas/ActionExecution/PluginActionSaga.ts index 59c312ba893d..16250891df0a 100644 --- a/app/client/src/sagas/ActionExecution/PluginActionSaga.ts +++ b/app/client/src/sagas/ActionExecution/PluginActionSaga.ts @@ -2,14 +2,15 @@ import { all, call, delay, + fork, put, + race, select, take, takeEvery, takeLatest, } from "redux-saga/effects"; import * as Sentry from "@sentry/react"; -import type { updateActionDataPayloadType } from "actions/pluginActionActions"; import { clearActionResponse, executePageLoadActions, @@ -1674,10 +1675,9 @@ function* softRefreshActionsSaga() { yield put({ type: ReduxActionTypes.SWITCH_ENVIRONMENT_SUCCESS }); } -function* handleUpdateActionData( - action: ReduxAction, -) { - const { actionDataPayload, parentSpan } = action.payload; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function* handleUpdateActionData(action: any) { + const { actionDataPayload, parentSpan } = action; yield call( evalWorker.request, @@ -1690,6 +1690,77 @@ function* handleUpdateActionData( } } +function* captureActionsWithinPeriodTriggers() { + while (true) { + const buffer = []; // Initialize a new buffer for each batch + const endTime = Date.now() + 10000; + // eslint-disable-next-line prefer-const + + while (Date.now() < endTime) { + try { + // Use a non-blocking `take` to capture actions within the period + + const { action } = yield race({ + action: take(ReduxActionTypes.TRIGGER_EVAL), + del: delay(1000), + }); + + if (!action) continue; + + buffer.push(action); + } catch (e) { + // Handle errors if needed + } + } + + // After the time period, dispatch the collected actions + if (buffer.length > 0) { + yield put({ + type: ReduxActionTypes.TRIGGER_EVAL_BATCH, + }); + } + } +} + +// Use a channel to queue all actions + +function* captureActionsWithinPeriod() { + while (true) { + const buffer = []; // Initialize a new buffer for each batch + const endTime = Date.now() + 10000; + let parentSpan; + // eslint-disable-next-line prefer-const + + while (Date.now() < endTime) { + try { + // Use a non-blocking `take` to capture actions within the period + + const { action } = yield race({ + action: take(ReduxActionTypes.UPDATE_ACTION_DATA), + del: delay(1000), + }); + + if (!action) continue; + + const { actionDataPayload } = action.payload; + + parentSpan = action.payload.parentSpan; + buffer.push(...actionDataPayload); + } catch (e) { + // Handle errors if needed + } + } + + // After the time period, dispatch the collected actions + if (buffer.length > 0) { + yield fork(handleUpdateActionData, { + parentSpan, + actionDataPayload: buffer, + }); + } + } +} + export function* watchPluginActionExecutionSagas() { yield all([ takeLatest(ReduxActionTypes.RUN_ACTION_REQUEST, runActionSaga), @@ -1703,6 +1774,10 @@ export function* watchPluginActionExecutionSagas() { ), takeLatest(ReduxActionTypes.PLUGIN_SOFT_REFRESH, softRefreshActionsSaga), takeEvery(ReduxActionTypes.EXECUTE_JS_UPDATES, makeUpdateJSCollection), - takeEvery(ReduxActionTypes.UPDATE_ACTION_DATA, handleUpdateActionData), + takeEvery(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod), + takeEvery( + ReduxActionTypes.START_EVALUATION, + captureActionsWithinPeriodTriggers, + ), ]); } diff --git a/app/client/src/workers/Evaluation/fns/utils/TriggerEmitter.ts b/app/client/src/workers/Evaluation/fns/utils/TriggerEmitter.ts index df79758b4ba9..dcbb49c35585 100644 --- a/app/client/src/workers/Evaluation/fns/utils/TriggerEmitter.ts +++ b/app/client/src/workers/Evaluation/fns/utils/TriggerEmitter.ts @@ -82,12 +82,12 @@ const logsHandler = deferredBatchedActionHandler((batchedData) => TriggerEmitter.on(BatchKey.process_logs, logsHandler); -const storeUpdatesHandler = priorityBatchedActionHandler((batchedData) => - WorkerMessenger.ping({ +const storeUpdatesHandler = priorityBatchedActionHandler((batchedData) => { + return WorkerMessenger.ping({ method: MAIN_THREAD_ACTION.PROCESS_STORE_UPDATES, data: batchedData, - }), -); + }); +}); TriggerEmitter.on(BatchKey.process_store_updates, storeUpdatesHandler);