2020-11-03 13:05:40 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2020-04-17 16:15:09 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
|
import { put, debounce, takeEvery, all } from "redux-saga/effects";
|
2024-08-06 14:52:22 +00:00
|
|
|
import type { ReduxAction } from "ee/constants/ReduxActionConstants";
|
|
|
|
|
import { ReduxActionTypes } from "ee/constants/ReduxActionConstants";
|
2021-08-27 09:25:28 +00:00
|
|
|
import { batchActionSuccess } from "actions/batchActions";
|
2021-11-05 05:49:19 +00:00
|
|
|
import * as log from "loglevel";
|
2020-04-13 08:24:13 +00:00
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
const BATCH_PRIORITY = {
|
2022-05-25 09:46:14 +00:00
|
|
|
[ReduxActionTypes.META_UPDATE_DEBOUNCED_EVAL]: {
|
|
|
|
|
priority: 0,
|
|
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
|
|
|
|
[ReduxActionTypes.SET_META_PROP_AND_EVAL]: {
|
2020-04-17 16:15:09 +00:00
|
|
|
priority: 0,
|
|
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
|
|
|
|
[ReduxActionTypes.RESET_WIDGET_META]: {
|
|
|
|
|
priority: 0,
|
|
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
2023-12-08 10:16:31 +00:00
|
|
|
[ReduxActionTypes.RESET_WIDGET_META_UPDATES]: {
|
|
|
|
|
priority: 0,
|
|
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
2020-04-17 16:15:09 +00:00
|
|
|
[ReduxActionTypes.UPDATE_WIDGET_PROPERTY]: {
|
|
|
|
|
priority: 0,
|
|
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
2021-08-27 09:25:28 +00:00
|
|
|
[ReduxActionTypes.EXECUTE_TRIGGER_REQUEST]: {
|
2020-04-17 16:15:09 +00:00
|
|
|
priority: 1,
|
|
|
|
|
needsSaga: true,
|
|
|
|
|
},
|
|
|
|
|
[ReduxActionTypes.EXECUTE_PAGE_LOAD_ACTIONS]: {
|
|
|
|
|
priority: 1,
|
|
|
|
|
needsSaga: true,
|
|
|
|
|
},
|
2020-07-03 08:58:58 +00:00
|
|
|
[ReduxActionTypes.UPDATE_ACTION_PROPERTY]: {
|
2020-07-28 10:41:51 +00:00
|
|
|
priority: 0,
|
2020-07-03 08:58:58 +00:00
|
|
|
needsSaga: false,
|
|
|
|
|
},
|
2020-07-28 10:41:51 +00:00
|
|
|
[ReduxActionTypes.UPDATE_ACTION_INIT]: {
|
|
|
|
|
priority: 1,
|
|
|
|
|
needsSaga: true,
|
|
|
|
|
},
|
2020-04-17 16:15:09 +00:00
|
|
|
};
|
|
|
|
|
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-17 16:15:09 +00:00
|
|
|
const batches: ReduxAction<any>[][] = [];
|
|
|
|
|
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-13 08:24:13 +00:00
|
|
|
function* storeUpdatesSaga(action: ReduxAction<ReduxAction<any>>) {
|
2020-04-17 16:15:09 +00:00
|
|
|
try {
|
|
|
|
|
const priority = BATCH_PRIORITY[action.payload.type].priority;
|
|
|
|
|
const currentPriorityBatch = batches[priority] || [];
|
|
|
|
|
currentPriorityBatch.push(action.payload);
|
|
|
|
|
_.set(batches, `[${priority}]`, currentPriorityBatch);
|
|
|
|
|
yield put({ type: ReduxActionTypes.EXECUTE_BATCH });
|
|
|
|
|
} catch (e) {
|
2021-11-05 05:49:19 +00:00
|
|
|
log.error(`${action.payload.type} action priority not set`);
|
2020-04-17 16:15:09 +00:00
|
|
|
}
|
2020-04-13 08:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* executeBatchSaga() {
|
2020-04-17 16:15:09 +00:00
|
|
|
for (let priority = 0; priority < batches.length; priority++) {
|
|
|
|
|
const batch = batches[priority];
|
|
|
|
|
if (Array.isArray(batch) && batch.length) {
|
2020-12-24 04:32:25 +00:00
|
|
|
const needsSaga = batch.filter((b) => BATCH_PRIORITY[b.type].needsSaga);
|
|
|
|
|
const canBatch = batch.filter((b) => !BATCH_PRIORITY[b.type].needsSaga);
|
2020-04-17 16:15:09 +00:00
|
|
|
batches[priority] = [];
|
2022-06-21 13:57:34 +00:00
|
|
|
// @ts-expect-error: Types are not available
|
2020-04-17 16:15:09 +00:00
|
|
|
yield put(canBatch);
|
|
|
|
|
if (needsSaga.length) {
|
|
|
|
|
for (const sagaAction of needsSaga) {
|
|
|
|
|
yield put(sagaAction);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-21 04:25:32 +00:00
|
|
|
yield put(batchActionSuccess(batch));
|
2020-04-17 16:15:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-13 08:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* root() {
|
2020-04-17 16:15:09 +00:00
|
|
|
yield all([
|
|
|
|
|
debounce(20, ReduxActionTypes.EXECUTE_BATCH, executeBatchSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.BATCHED_UPDATE, storeUpdatesSaga),
|
|
|
|
|
]);
|
2020-04-13 08:24:13 +00:00
|
|
|
}
|