the answer to this question will probably be "it cannot be done with out writing a custom javaScript parser" but here i go: while developing some lightweight scripting tool i ran into a need to do some automatic cleanup behind the scenes when some block variables goes out of scope.
(this tool has no serious parsing done under the hood..., i just take normal javascript text source code, augment it a bit, add some dependencies on context and create a function out of it and execute it)
i would like it to be done automatically with out the end user that wrote the script even realizing it is happening behind the scenes.
for example i got this block
{
const activatingElement=await $getElement('//input[@class="button" and @type="submit"]');
await $wait(defaultWaitTime);
let transaction = $startTransaction({customTransactionName:"LoginAndLoad_page"});
await activatingElement.click();
await $waitFor({query:'(//tbody[@class="ui-datatable-data ui-widget-content"]//tr[@role="row"])[1]'});
$endTransaction(transaction);
console.log(this);
await activatingElement.dispose();
}
i would like to cause the last line await activatingElement.dispose();
to be executed automatically just before the const activatingElement
defined in the block goes out of scope. i control the method that defines the value for this variable ($getElement) so i can change it as i want. but i don't want to just edit the source code and write the dispose before the block ends, since it will be brittle in case of nested blocks. so i would just like this method to be automatically called just before this variable goes out of scope. is there any trick i can use to do it?
this is needed to clear some browser elements for GC, they are not elements inside my own nodejs code execution, they are handles for a code that executes on a browser and unless i call dispose() on them then necessary resources might be unavailable for cleaning.
is there any way to do it? i thought of using traps with proxy, but don't think it can be done on out of scope to be variables (only on property deletion)