I recently was trying to proxy a MediaStream Object in javascript And noticed i could but there was an error. I used the following code.
function createChangeableStream(stream) {
stream["_stream_"]=stream;
return new Proxy(stream,{
set: function(obj, prop, value){
if(prop==="_stream_")
obj._stream_=value;
else
obj._stream_[prop]=value;
},
get: function(obj, prop) {
if(prop==="_stream_")
return obj._stream_;
else
return obj._stream_[prop];
}
});
}
since i could not change the target... i used the above workaround.
Each time i try to call MediaStream.getTracks(), i get TypeError: Illegal invocation I would be grateful for your help. The function works with any other objects. and the 'target' is changed by modifying _stream_ property. I also realized that this
let x=new Proxy(stream,{});
x.getTracks()
doesn't work.
Okay for anyone with almost similar problem i succeeded evading the error with the following code.
function createChangeableStream(stream) {
stream["_stream_"]=stream;
stream["_call_handler_"]=function (obj,prop,args) {
return obj[prop](...args);
}
return new Proxy(stream,{
set: function(obj, prop, value){
if(prop==="_stream_")
obj._stream_=value;
else
obj._stream_[prop]=value;
},
get: function(obj, prop) {
if(prop==="_stream_")
return obj._stream_;
else if((typeof obj._stream_[prop])==="function"){
return function () {
return obj._call_handler_(obj._stream_,prop,[...arguments]);
}
}else
return obj._stream_[prop];
}
});
}
but i still don't know why the error exists at the first place. maybe its a bug, i don't know.