tc39 / proposal-mass-proxy-revocation

Proposal for revoking proxies en masse.
MIT License
5 stars 1 forks source link

Can a Promise be the basis of the RevocationController in a shim? (maybe obsolete) #11

Closed ajvincent closed 2 years ago

ajvincent commented 2 years ago
// At controller construction
let massRevoker;
let tracker = { revoked: false };
let controllerPromise = new Promise(resolve => massRevoker = resolve);
controllerPromise = controllerPromise.then(() => tracker.revoked = true);

// At Proxy.revocable(target, handler, controller)

function addRevokerReference(weakRefToRevoker) {
  controllerPromise.then(() => {
    const revoker = weakRefToRevoker.deref();
    if (revoker)
      revoker();
  });
}

if (controller) {
  addRevokerReference(new WeakRef(revoker));
}

We still want to add the third argument, but a shim could implement asynchronous revocation this way.

This approach is notably worse in terms of memory allocation and GC pressure, because it implies each call to Proxy.revoke() attaches a Promise via .then() for each proxy we create. A solution in the JavaScript engine is still preferable.

ajvincent commented 2 years ago

Note: this issue and original comment predate the replacement of RevocationController with a signaling API.