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

Fix and improve ReactiveSet, ReactiveMap and Trigger #593

Closed
wants to merge 4 commits into from

Conversation

Exelord
Copy link
Contributor

@Exelord Exelord commented Mar 27, 2024

No description provided.

Copy link

changeset-bot bot commented Mar 27, 2024

⚠️ No Changeset found

Latest commit: f97bd7e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -50,6 +50,11 @@ export class TriggerCache<T> {
this.#map.get(key)?.$$();
}

dirtyAll() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dirtyAll is needed to be able to clean all tracked keys. Otherwise the user has no access to keys and not able to clear them without own keys tracking.

This is was an issue for Set and Map implementation where we have iterated over currently defined keys not all tracked keys.

for (const v of super.keys()) this.#triggers.dirty(v);
super.clear();
this.#triggers.dirty($KEYS);
this.#triggers.dirtyAll();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an important fix, to clear all tracked keys not only the ones currently defined.

clear(): void {
if (super.size) {
super.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clearing, and all super operations should always happen before dirtying, otherwise the computations can have access to an old state.

super.forEach(callbackfn as any);

for (const value of super.values()) {
this.#triggers.track(value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While using forEach on Set we could track the used values to trigger a recomputation when a value gets updated.

return super.has(v);

keys(): IterableIterator<T> {
return this.values();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case of Set for correctness keys are equal values, not the other way around.

clear(): void {
if (super.size) {
super.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clear should happen before dirting in order to avoid old state in computations

*entries(): IterableIterator<[K, V]> {
for (const entry of super.entries()) {
this.#keyTriggers.track(entry[0]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

entries should track keys as well

Copy link
Member

@atk atk left a comment

Choose a reason for hiding this comment

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

Apart from the constructor not merely putting the entries into super(), this is all fine.

packages/map/src/index.ts Show resolved Hide resolved
@Exelord
Copy link
Contributor Author

Exelord commented Jun 9, 2024

Thank you Alex, this is a still a draft PR though, I need to dig into it more to understand better potential issues with the new changes.

Copy link
Member

@thetarnav thetarnav left a comment

Choose a reason for hiding this comment

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

I like most of the essential improvements here.
But it's all drowned by formatting/rename changes it's hard to focus on whats important.
Whats missing in this pr btw?

Comment on lines +90 to +92
const hasKey = super.has(key);
const currentValue = super.get(key);
const result = super.set(key, value);
Copy link
Member

Choose a reason for hiding this comment

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

Why bring them here, whats wrong with them being inlined? Is this important or just a preference?

@Exelord
Copy link
Contributor Author

Exelord commented Sep 8, 2024

I will try to work on it this week, review, and cleanup all polishing parts for now.

@Exelord Exelord mentioned this pull request Sep 8, 2024
@Exelord
Copy link
Contributor Author

Exelord commented Sep 8, 2024

I will split this PR into smaller ones for easier review. Set PR just landed: #688

I will work on Map fixes, when times allow

@Exelord Exelord closed this Sep 8, 2024
@Exelord
Copy link
Contributor Author

Exelord commented Oct 4, 2024

Map PR: #704

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.

3 participants