As part of some code refactoring, I am wanting to trace the sequence of calls to an API, since we seem to have broken something in the process. For this reason we are looking to proxy the API calls, to log what is called, using the Javascript Proxy object, but we are getting "TypeError: Illegal invocation" when we try.
The code (based on MDN docs):
function createProxy (glObj) {
const handler = {
get (target, prop, receiver) {
console.log('FN', prop);
const x = Reflect.get(target, prop, receiver);
return x;
}
};
return new Proxy(glObj, handler);
}
And how it is being used:
function webGlInit () {
const elementId = 'canvas';
const canvas = document.querySelector(`#${elementId}`); // $('#canvas')[0];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const glOptions = {
// antialias: false,
alpha: false,
premultipliedAlpha: false
};
const gl = canvas.getContext('webgl', glOptions) || canvas.getContext('experimental-webgl', glOptions);
const proxyGl = createProxy(gl);
// the line where "TypeError: Illegal invocation" is happening
proxyGl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
Any ideas on what we are doing wrong?
getProxy onwindow.locationin Javascript?, How to fixTypeError: Illegal Invocationon an XMLHttpRequest wrapped in an ES6 Proxy