You might consider creating an @AuraEnabled
method that calls the methods you only want to run once, and execute that first from LWC. Then you can exclude the one-off apex methods entirely from the iterated calls. You add an additional server call, yes, but with the benefit of greater code simplicity, maintainability, and elegance. Any performance degradation should be unnoticeable.
Example (your method ABC
is runOnEachIteration
here for clarity):
CHANGE
// LWC:
for (const item of items) {
await runOnEachIteration();
}
// APEX:
@AuraEnabled
public static void runOnEachIteration() {
if (/* try to figure out if it hasn't been run yet */) {
doThisOnce();
doThatOnce();
}
doThisForEach();
}
INTO
// LWC:
if (items.length > 0) {
await runOnce();
for (const item of items) {
await runOnEachIteration();
}
}
// APEX:
@AuraEnabled
public static void runOnce() {
doThisOnce();
doThatOnce();
}
@AuraEnabled
public static void runOnEachIteration() {
doThisForEach();
}