I am using two different libraries.
The first one provides the following class:
class VolumeLoader {
load(string: string): Promise<any> // Returns a promise to attach callbacks
// rest of methods
}
The second one is using the previous loader like this
const loader = new Loader()
loader.load(string, callback) // Provides an argument to attach callback (without promises)
How can I modify VolumeLoader
so it can accept string, callback
?
I CAN NOT MODIFY ORIGINAL CLASS
What I've tried:
// Creating a new class using the previous loader. The load function works but I lose the rest of methods.
class _VolumeLoader {
loader = new VolumeLoader
load (string, callback) {
this.loader.load(string).then(callback)
}
// rest of methods are lost! :(
}
// Modifying directly the prototype
const OriginalVolumeLoader = VolumeLoader;
const _VolumeLoader = VolumeLoader
_VolumeLoader.prototype.load = function (string, callback) {
OriginalVolumeLoader.prototype.load.call(this, string).then(callback)
}
// Creates an infinite loop, not sure why :(
None worked :(
You need to save the original load
method alone to avoid the infinite loop. Doing OriginalVolumeLoader = VolumeLoader
will only create another reference to the same class
in memory, so changes to the OriginalVolumeLoader
end up as changes to VolumeLoader
too.
Use:
const origLoad = VolumeLoader.prototype.load;
VolumeLoader.prototype.load = function (string, callback) {
origLoad.call(this, string).then(callback)
};