An interceptor function that wraps the original function.
const action = (context, args) => {
context.nodeIds = [...context.nodeIds, ...args[0]];
};
const rollback = (context, prevContext) => {
context.nodeIds = prevContext.nodeIds;
};
const interceptor = createInterceptor(action, rollback);
const originalFunction = async function() {
console.log("Original function");
};
const context = { nodeIds: [] };
const args = [["node1", "node2"]];
interceptor(originalFunction, context, args);
This function creates an interceptor function that wraps an original function with custom actions
and rollback logic. The action
parameter specifies the custom action to be performed before calling
the original function, allowing you to modify the context or arguments as needed. The rollback
parameter
specifies the rollback function to be executed in case of an error, allowing you to revert the context to
its previous state. The interceptor function receives the original function, context, and arguments as parameters,
and can perform custom actions before and after calling the original function. The original function is called using
the call
method to ensure that the context is preserved. If an error occurs during the execution of the original
function, the interceptor will call the rollback function to revert the context to its previous state. This is useful
for implementing error handling, logging, or other custom logic around function calls.
Creates an interceptor function that wraps an original function with custom actions and rollback logic.