contributions/docs/GlobalFunctions.md
Global functions in Appsmith are available through the right-hand pane and in the JS editor. They allow users to perform different tasks throughout the Appsmith application.
ActionType object. This is the name of the function.Fieldtype object. These are the field names for each argument the function accepts.fieldConfigs with your field’s getter, setting, and view. The getter is the setting used to extract the field value from the function. the setter is used to set the value in function when the field is updated. The view is the component used to edit the field valuerenderField function to change things like field label etc.,baseOptions array (you will need to add a constant for the message in the constants/messages.ts) - This will show up as a message when you hover over the function.getFieldFromValue function (Look at the setInterval code example for guidance). After following these 3 steps, you should be able to view your global function listed on the right hand pane, along with the fields to enter parameters.if (value.indexOf("setInterval") !== -1) {
fields.push(
{
field: FieldType.CALLBACK_FUNCTION_FIELD,
},
{
field: FieldType.DELAY_FIELD,
},
{
field: FieldType.ID_FIELD,
},
);
}
ActionTriggerType enumActionTriggerFunctionNames datatype (Look at the code example for guidance)[ActionTriggerType.*SET_INTERVAL*]: "setInterval",
export type SetIntervalDescription = {
type: ActionTriggerType.SET_INTERVAL;
payload: {
callback: string;
interval: number;
id?: string;
};
};
Finally add this global function description to the ActionDescription data structure.
Go to the sagas/ActionExecution folder:
import {
ClearIntervalDescription,
SetIntervalDescription,
} from "@appsmith/entities/DataTree/actionTriggers";
import {
executeAppAction,
TriggerMeta,
} from "@appsmith/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 (e) {
yield call(logActionExecutionError, e.message);
}
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];
}
Add an entry to the sagas/ActionCreator/ActionExecutionSagas.ts file’s executeActionTriggers function. Use this example for guidance
case ActionTriggerType.SET_INTERVAL:
yield call(setIntervalSaga, trigger.payload, eventType, triggerMeta);
break;
In the workers/Actions.ts file - This file has all the global functions listed in this file. Add the entry for the global function you have created, using this example as a guide:
setInterval: function(callback: Function, interval: number, id?: string) {
return {
type: ActionTriggerType.SET_INTERVAL,
payload: {
callback: callback.toString(),
interval,
id,
},
executionType: ExecutionType.TRIGGER,
};
}
setInterval: {
"!doc": "Execute triggers at a given interval",
"!type": "fn(callback: fn, interval: number, id?: string) -> void",
},