Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the ownKeys trap #26

Merged
merged 10 commits into from
Jun 23, 2023
Merged

Conversation

luisherranz
Copy link
Owner

What

Add support for the ownKeys trap, which is used with for..in, getOwnPropertyNames or Object.keys/values/entries.

  • I also exported the RevertDeepSignal type which is useful when using Object.values().
  • I also added some missing tests for similar array subscriptions, although those where working fine because they access array.length and we were already updating that signal when items are added or removed from the array.

Why

To be able to subscribe when properties are added or removed from objects.

const store = deepSignal({ a: 1 });

effect(() => {
  for (const key in store) {
    console.log(`${key}: ${store[key]}`);
  }
});

store.b = 2; // console.logs `a: 1` and `b: 2`

How

I added a new map that stores a fake signal for each object. When ownKeys is accessed, the computations are subscribed to that signal. When a property is added or removed from the object, we update the signal to retrigger the subscribed computations.

@luisherranz luisherranz self-assigned this Jun 23, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 23, 2023

Size Change: +343 B (+6%) 🔍

Total Size: 6.1 kB

Filename Size Change
packages/deepsignal/core/dist/deepsignal-core.js 994 B +55 B (+6%) 🔍
packages/deepsignal/core/dist/deepsignal-core.mjs 985 B +62 B (+7%) 🔍
packages/deepsignal/dist/deepsignal.js 1.03 kB +56 B (+6%) 🔍
packages/deepsignal/dist/deepsignal.mjs 1.03 kB +56 B (+6%) 🔍
packages/deepsignal/react/dist/deepsignal-react.js 1.03 kB +58 B (+6%) 🔍
packages/deepsignal/react/dist/deepsignal-react.mjs 1.03 kB +56 B (+6%) 🔍

compressed-size-action

@luisherranz
Copy link
Owner Author

@DAreRodz, sorry to bother you again, but if you could take a look here, it would be fantastic. Many thanks 🙂

@rozek
Copy link

rozek commented Jun 23, 2023

Congratulations!

Looks much more elegant than my quick hack from yesterday!

However: would you ming adding support for Object.defineProperty(...) as well? To my current understanding, simply trapping set does not cover all possibilities to add a new property to an object...

@luisherranz
Copy link
Owner Author

However: would you ming adding support for Object.defineProperty(...) as well? To my current understanding, simply trapping set does not cover all possibilities to add a new property to an object...

Can you open another issue for that and explain the use cases, etc? Thanks!

Copy link
Collaborator

@DAreRodz DAreRodz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍 It's no bother at all, @luisherranz. 😄

@luisherranz luisherranz merged commit 750d263 into main Jun 23, 2023
@luisherranz luisherranz deleted the add-support-for-own-keys branch June 23, 2023 16:40
@github-actions github-actions bot mentioned this pull request Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: get informed about insertion/removal of properties
3 participants