comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Reserved property names that don't get proxied

Open bkotsopoulossc opened this issue 2 years ago • 0 comments

I have a use case where I proxy() some objects, but every object has a specific property that I don't want to be proxied, and I want to remain local. For example:

proxy({
  someFunc: () => {},
  someOtherFunc: () => {},
  _my_internal_id: 1234,
})

Here, I want someFunc and someOtherFunc to be proxied like normal, but I want _my_internal_id to remain local, and be able to set/get it synchronously.

So far I've accomplished this with the following change:

  const proxy = new Proxy(target, {
    get(target, prop, rec) {
      if (typeof prop === "string" && reservedProps.has(prop)) {
        return Reflect.get(target, prop, rec)
      }

      // ... existing
    },
    set(target, prop, rawValue) {
      if (typeof prop === "string" && reservedProps.has(prop)) {
        // @ts-ignore
        target[prop] = rawValue
        return true
      }

      // ... existing
    },
   ...
}

I wonder if there is interest in accepting a patch to support this generically? I'm not exactly sure what would be the cleanest way to do it, but maybe a global API you would call from both the worker and main to set the reserved properties? It would be tricky to make the type system aware of this, that would maybe need a build time variable instead.

Thoughts? @surma

bkotsopoulossc avatar Mar 14 '22 16:03 bkotsopoulossc