PromucFlow_constructor/app/client/src/sagas/ActionExecution/SetIntervalSaga.ts
Arsalan Yaldram f58451aa5f
feat: upgrade to create react app 5 (#14000)
* Updated Typescript types.

* Typefixes after merge with release.

* chore: GenericApiResponse Removed alltogether.

* chore: resolved ApiResponse unknown errors removed PageListPayload.

* Added shouldBeDefined.

* fix: Resolved type errors.

* fix: Typescript upgrade to 4.5 and type fixes.

* feat: upgrade to cra 5

* feat: uncomment service worker registeration

* force secure websocket protocol

* jest test fixes

* fix: react function lint rule removed

* fix: klona test case.

* fix: typescirpt issues resolved

* fix: timeout for colorpicker test and change env.

* feat: update client-build.yml file

* fix: remove brotliplugin use compression plugin

* fix: build config fixed

* fix: upgrade webpack plugin

* fix: add branchbutton test to todo.

* fix: remove branch button test.

* fix: Add tailwind theme values, fix cypress tests

* fix: Typescript type fixes.

* feat: run jest tests in silent mode

* fix: cypress rgb values add branchbutton jest test

* fix: review comments, fixes for error.message

* fix: increase cache size for the workbox

* fix: remove OrgApi.ts file

* fix: cypress.json file remove credentials

* fix: downgrade react and react-dom packages

Co-authored-by: rahulramesha <rahul@appsmith.com>
2022-06-21 19:27:34 +05:30

85 lines
2.4 KiB
TypeScript

import {
ClearIntervalDescription,
SetIntervalDescription,
} from "entities/DataTree/actionTriggers";
import {
executeAppAction,
TriggerMeta,
} from "sagas/ActionExecution/ActionExecutionSagas";
import { call, delay, spawn } from "redux-saga/effects";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import {
logActionExecutionError,
TriggerFailureError,
} from "sagas/ActionExecution/errorUtils";
const TIMER_WITHOUT_ID_KEY = "timerWithoutId";
const activeTimers: Record<string, true | string> = {
[TIMER_WITHOUT_ID_KEY]: true,
};
export function* setIntervalSaga(
payload: SetIntervalDescription["payload"],
eventType: EventType,
triggerMeta: TriggerMeta,
) {
if (payload.id) {
activeTimers[payload.id] = payload.callback;
}
yield spawn(executeInIntervals, payload, eventType, triggerMeta);
}
function* executeInIntervals(
payload: SetIntervalDescription["payload"],
eventType: EventType,
triggerMeta: TriggerMeta,
) {
const { callback, id = TIMER_WITHOUT_ID_KEY, interval } = payload;
while (
// only execute if the id exists in the activeTimers obj
id in activeTimers &&
/*
While editing the callback can change for the same id.
At that time we want only execute the new callback
so end the loop if the callback is not the same as the one this
saga was started
But if no id is provided, it will always run
*/
(activeTimers[id] === callback || id === TIMER_WITHOUT_ID_KEY)
) {
// Even if there is an error, the set interval should still keep
// running. This is according to the spec of setInterval
try {
yield call(executeAppAction, {
dynamicString: `{{${callback}}}`,
// pass empty object to execute it as a callback function
callbackData: [{}],
event: { type: eventType },
triggerPropertyName: triggerMeta.triggerPropertyName,
source: triggerMeta.source,
});
} catch (error) {
logActionExecutionError(
(error as Error).message,
triggerMeta.source,
triggerMeta.triggerPropertyName,
);
}
yield delay(interval);
}
}
export function* clearIntervalSaga(
payload: ClearIntervalDescription["payload"],
) {
if (!(payload.id in activeTimers)) {
throw new TriggerFailureError(
`Failed to clear interval. No timer active with id "${payload.id}"`,
);
}
delete activeTimers[payload.id];
}