luisherranz / deepsignal

DeepSignal 🧶 - Preact signals, but using regular JavaScript objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error computed side-effects error when accessing getter

wittjosiah opened this issue · comments

When attempting to write a getter which consolidates a nested object, signals throws an error.

Reproduction:

const state = deepSignal({
  x: {
    a: 1,
    b: 2,
  },
  get y() {
    return Object.values((state.x as RevertDeepSignal<typeof state.x>) ?? {});
  },
});

console.log(state.y);

Error:

Error: Computed cannot have side-effects
      at mutationDetected (node_modules/.pnpm/@preact+signals-core@1.3.1/node_modules/@preact/signals-core/src/index.ts:5:8)
      at value (node_modules/.pnpm/@preact+signals-core@1.3.1/node_modules/@preact/signals-core/src/index.ts:314:4)
      at Object.values [as ownKeys] (node_modules/.pnpm/deepsignal@1.3.3_react@18.2.0/node_modules/deepsignal/core/src/index.ts:120:29)
      at Function.values (<anonymous>)

Interesting. It should be easy to fix. I just need to find another way to trick the minifier into thinking that the access to the .value shouldn't be removed in this line.

Thanks for the report!

PR with the fix:

Thanks for taking a look at this so quickly! I'll try it out once it's released.

Released as deepsignal@1.3.4. Please let me know if this fixed the issue 🙂

@luisherranz I'm getting back from some vacation earlier this week. Hoping I'll be able to try this out next week and I'll let you know if it resolves the issue I was seeing.

@luisherranz I can confirm that I'm able to run the above code now without it crashing 👍 my apologies for the delay

Cool, thanks 🙂