tc39 / proposal-mass-proxy-revocation

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

give manual alternative example (need bikeshedding) #2

Open bmeck opened 2 years ago

bmeck commented 2 years ago

eg. (should be cleaned up)

'use strict';
const DEAD_PROXY = (() => {
  const {proxy, revoke} = Proxy.revocable({}, Reflect);
  revoke();
  return proxy;
})()
function createRevokeInSet(obj, revokePointer) {
  const {proxy, revoke} = Proxy.revocable(obj, {
    has(target, key) {
      if (revokePointer[0]) {
        revoke();
        return Reflect.has(DEAD_PROXY, key);
      }
      return Reflect.has(...arguments);
    }
  });
  return proxy;
}

// our proxy revocation grouping
const green = [false];
let a = {};
let b = {};

// make proxies
let aProxy = createRevokeInSet(a, green);
let bProxy = createRevokeInSet(b, green);

// revoke em
green[0] = true;

console.log('x' in aProxy);
bmeck commented 2 years ago

notably, manually doing this has no avenue to mark in O(1) time that all proxy target/handlers in a set are no longer strongly held.