luisherranz / deepsignal

DeepSignal 🧶 - Preact signals, but using regular JavaScript objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stop deepSignal from proxying certain properties or classes

wittjosiah opened this issue · comments

Problem

Currently shouldProxy omits functions and builtins from proxying https://github.com/luisherranz/deepsignal/blob/main/packages/deepsignal/core/src/index.ts#L137-L144, however in the project I'm working on currently we have other classes that are being stored in a deepsignal that we need to omit from proxying (in this case because they are already proxies and double-proxying causes issues).

Current Workaround

I was able to work around the problem for now by adding the constructor to globalThis so that deepsignal thinks it's a builtin, but this isn't ideal. It seems quite likely to me that there will be other use cases that will have classes that want to be stored in deepsignal but not have them proxied from one reason or another.

Proposed Solutions

I can see a few ways this might be solved:

  • a helper function
import { deepSignal, omit } from 'deepsignal';

deepSignal({ a: { b: 1 }, c: omit({ d: 2 }) })
  • a second argument for options
import { deepSignal, omit } from 'deepsignal';

deepSignal({ a: { b: 1 }, c: { d: 1 } }, { omit: ['c'] })
  • expose a list of constructors should proxy omits (less flexible but only needs to be specified once per constructor)
import { deepSignal, omit } from 'deepsignal';

omit(SomeConstructor);
deepSignal({ a: { b: 1}, c: new SomeConstructor({ d: 1 }) });

I think this functionality could be added without adding too much to size bundle. If you're open to this functionality being added but don't have time to add it, let me know what your preferred path would be and I'd be happy to send you a PR.

Sounds legit, but let me think about it and I'll get back to you 🙂

I've opened a pull request: