Why would you want to revoke a proxy? Could you provide a practical application of Proxy.revocable()
?
Based on the MDN Proxy.revocable() documentation I understand it allows garbage collection. However, wouldn't it also allow garbage collection if you just deleted the Proxy object?
Addendum: If there is something wrong with this question, please let me know what it is and I will gladly rephrase it or add additional information.
@Amy:
I thought it seemed like it was an unnecessary builtin feature, since I could create a revocable proxy like so:
function createRevocable(o,handlers){
var revoked = false
var handlerWrapper = {
get(...args){
if(revoked){
throw Error('Sorry, this object went poof.')
}
if(typeof handlers.get == 'function'){
return handlers.get(...args)
}else{
return Reflect.get(...args)
}
}
}
var p = new Proxy(o,Object.assign({},handlers,handlerWrapper))
var r = function(){
revoked = true
}
return {r,p}
}
var a = createRevocable({a:1},{})
var prox = a.p
var revoke = a.r
console.log(prox.a) //1
revoke()
console.log(prox.a)
However, it seems that garbage collection really is the issue, since I can revoke access from the object, but cannot delete the internal reference to it inside the Proxy object. Unless...
function createRevocable(o,handlers){
var revoked = false
var proxyProxyHandlers = {
get(target,prop,rec){
if(revoked){
throw Error('Sorry, this object went poof.')
}
return p[prop]
}
,//etc...
}
var p = new Proxy(o,handlers)
var proxyProxy = new Proxy(Object.create(null),proxyProxyHandlers)
var r = function(){
// Delete the one and only reference to the proxy:
delete p
revoked = true
}
return {r,p:proxyProxy}
}
var a = createRevocable({a:1},{})
var prox = a.p
var revoke = a.r
console.log(prox.a) //1
revoke()
console.log(prox.a)
So, by creating a two layered proxy you can create a garbage collectable and revocable proxy. So, why is it built in? Ease of use? Or am I missing something? It's not entirely garbage collectable though, there is still a thin wrapper with a reference to an object with a null prototype.