What is a revocable proxy in JavaScript for?

88
February 13, 2020, at 02:40 AM

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.

READ ALSO
Querying the Stripe API for all charges per customer?

Querying the Stripe API for all charges per customer?

Is it possible to query the Stripe API for all charges for a specific customer or do we need to retrieve all charges, and then filter them for the customer?

65
Pop up Menu getting cut off at the bottom of the page

Pop up Menu getting cut off at the bottom of the page

I have a playlist and once the playlist items hit near the bottom of the screenthe pop up menu gets cut off as i'm using the x and y mouse coordinates to display the menu i guess from the upper left hand corner

47
Can we upload PDF generated from react-pdf to server?

Can we upload PDF generated from react-pdf to server?

We are using @react-pdf/renderer to generate pdf documents in our application, is there a way to upload those generated documents to backend server?

41
Is there anyway to get videojs-record to work on phone?

Is there anyway to get videojs-record to work on phone?

Hi so as you can see i have a problem with videojs-recorder, problem is i can't get access to my cam on phoneI use code down below and i am testing it with remote dev tools on chrome

46