Skip to content

Instantly share code, notes, and snippets.

@zHaytam
Created September 22, 2021 22:00
Show Gist options
  • Save zHaytam/39b7378caf0d4e67983f38b32e53db54 to your computer and use it in GitHub Desktop.
Save zHaytam/39b7378caf0d4e67983f38b32e53db54 to your computer and use it in GitHub Desktop.
const cachedFunctions = {}
function batchJsInterop(calls) {
return calls.map(call => {
try {
// Find function if needed
if (!(call.identifier in cachedFunctions)) {
var result = window;
var lastSegment = null;
call.identifier.split('.').forEach(seg => {
lastSegment = result;
result = result[seg];
});
result.bind(lastSegment);
cachedFunctions[call.identifier] = result;
}
// Invoke
let args = call.args ? call.args : [];
return {
success: true,
returnValue: cachedFunctions[call.identifier].apply(null, args)
};
} catch (e) {
return {
success: false,
error: e.message
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment