There is a class with some several methods. One of them:
drawGeojson(layerid: string, geojsonObject) {
this.ngZone.run(() => {}
}
How to refactor the code - so that, on request, you can execute the method body with a wrapper
this.ngZone.run(() => {}
and without it?
I need something this:
The default call: drawGeojson()
The proxy call Wrapper.drawGeojson();
CodePudding user response:
Seems like you want a decorator function.
function makeConditional<T extends unknown[], K>(fn: (...args: T) => K) {
return function(...args: [...T, boolean]): K | undefined {
if (args.pop()) {
return fn(...args as any);
}
}
}
and then used like
drawGeojson = makeConditional(function(layerId: string, geojsonObject: whatever) { ... });
CodePudding user response:
If you want conditionally execute
this.ngZone.run(() => {})
You can add an additional parameter to your method and depend on whether it execute or not.
wrapperZone(method: Function, execute: boolean) {
execute ? this.ngZone.run(() => {
method()
}) : method()
}
wrapperZone(this.drawGeojson.bind(this), true)