@legumeinfo/web-components
Version:
Web Components for the Legume Information System and other AgBio databases
31 lines • 1.17 kB
JavaScript
/**
* Substitutes the value of a global variable with another global variable's
* value, calls the decorated method, and then restores the original value of
* the substituted global variable. If the second global variable is not defined
* then the original global variable's value is used.
*
* @param target - The global variable to have its value substituted.
* @param source - The global variable to use as the target's substitute.
*/
export function globalSubstitution(target, source) {
return (_, __, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const defaultGlobal = window[target];
if (window[source] !== undefined) {
window[target] = window[source];
}
try {
const result = originalMethod.apply(this, args);
window[target] = defaultGlobal;
return result;
}
catch (error) {
window[target] = defaultGlobal;
throw error;
}
};
return descriptor;
};
}
//# sourceMappingURL=decorators.js.map