@mui/x-internal-gestures
Version:
The core engine of GestureEvents, a modern and robust multi-pointer gesture detection library for JavaScript.
39 lines (35 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createProxy = void 0;
const createProxy = target => {
const proxy = new Proxy(target, {
get(obj, prop) {
if (prop === 'setup') {
return options => {
const mode = Reflect.get(obj, 'pointerManager').mode;
// Calling setup pretty much clears this proxy by creating a new instance
// This new instance will NOT be a proxy.
// @ts-expect-error, constructor is a function...
return new obj.constructor(mode).setup(options);
};
}
const value = Reflect.get(obj, prop);
// If the property is not a function, we return it as is.
if (typeof value !== 'function') {
return value;
}
// If we are trying to call a method on the proxy,
// we ensure that we run the method on a new instance of the gesture.
// This is useful for tests where we want to ensure no pointers are left hanging in the pointer manager.
return async (...args) => {
const mode = Reflect.get(obj, 'pointerManager').mode;
// @ts-expect-error, constructor is a function...
return new obj.constructor(mode)[prop](...args);
};
}
});
return proxy;
};
exports.createProxy = createProxy;