I created a transformer named sharedUtils with following script:
function addRowPVP(table, tableState, columns) {
let result = tableState.value;
return result;
}
function deleteRow(table, tableState) {
// do something
}
// Export the functions
return {
deleteRow,
addRowPVP
};
For a button (Add Property), its Click event just does the following Run Script:
sharedUtils.value.addRow(tableAlertProperty, tableAlertPropertyState);
For a button (Delte Property), its Click event just does the following Run Script:
sharedUtils.value.deleteRow(tableAlertProperty, tableAlertPropertyState);
Problem: It works well; but after some time, retools reports the error:
Error:Cannot read properties of null (reading 'addRowPVP')
buttonAddAlertProperty
in run script, line 1(buttonAddAlertProperty)
in buttonAddAlertProperty click event handler(buttonAddAlertProperty)
from user interaction
Actually I did not change the transformer script and the button's script. I had to duplicate the script as a new script, delete the current sharedUtils, and then rename the new script as sharedUtils. It works well again.
However, after several hours, the same problem happened.
I tried the backup app, and found that
when the script worked well, if I tried to input sharedUtils in the console, retools reports: Failed to execute 'postMessage' on 'Window':
when the script failed to be invoked, if I tried to input sharedUtils in the console, retools reports that it is an object and its value is null.
It may be a limitation with Transformers. Their purpose is to transform data, like filter an array of elements returned by a query, or map over it and make some changes with the purpose of rendering the output on a component. Here are some examples.
The part of the doc that says "You can use use JavaScriptlibraries and utility methods" contains a misleading link for utility methods (and an extra 'use,' clearly).
A JS query is a great alternative, the only tradeoff is that every time we run it, we are defining the functions again. But unless we are defining a ton of functions in one JS query, we're not causing a considerable impact on memory in the great scheme of things.