GoogleChromeLabs / GoogleChromeLabs/comlink

Reserved property names that don't get proxied

Open
#583 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
12.8k
Forks
435
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.