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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
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: 08bed34

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.

export function createWeakSet<T extends object>(initial?: T[]): ReactiveWeakSet<T> {
return new ReactiveWeakSet(initial);
export function createWeakSet<T extends object = object>(
values?: readonly T[] | null,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interaface updated from latest WeakSet types definition

}
}

/** @deprecated */
export type SignalSet<T> = Accessor<T[]> & ReactiveSet<T>;

/** @deprecated */
export function createSet<T>(initial?: T[]): SignalSet<T> {
const set = new ReactiveSet(initial);
export function createSet<T>(values?: readonly T[] | null): SignalSet<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interaface updated from latest Set types definition

@@ -104,41 +114,47 @@ export class ReactiveSet<T> extends Set<T> {
export class ReactiveWeakSet<T extends object> extends WeakSet<T> {
#triggers = new TriggerCache<T>(WeakMap);

constructor(values?: Iterable<T> | null) {
constructor(values?: readonly T[] | null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interface updated from latest WeakSet types definition

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 this.values();
}
forEach(callbackfn: (value: T, value2: T, set: this) => void) {
forEach(fn: (value1: T, value2: T, set: Set<T>) => void): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update typings with latest definitions

for (const key of super.keys()) {
this.#triggers.track(key);
yield [key, key];
for (const entry of super.entries()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using entries() for better memory management (no need to spawn new arrays while yielding)

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.

return new Proxy(() => [...map], {
get: (_, b) => map[b as keyof typeof map],
}) as SignalMap<K, V>;
}

/** @deprecated */
export function createWeakMap<K extends object, V>(initial?: [K, V][]): ReactiveWeakMap<K, V> {
return new ReactiveWeakMap(initial);
export function createWeakMap<K extends object = object, V = any>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated typings with latest TS definitions

}
}

/** @deprecated */
export type SignalMap<K, V> = Accessor<[K, V][]> & ReactiveMap<K, V>;

/** @deprecated */
export function createMap<K, V>(initial?: [K, V][]): SignalMap<K, V> {
const map = new ReactiveMap(initial);
export function createMap<K, V>(entries?: readonly (readonly [K, V])[] | null): SignalMap<K, V> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated typings with latest TS definitions

batch(() => {
this.#keyTriggers.dirty(key);
this.#valueTriggers.dirty(key);
if (currentValue !== undefined) this.#valueTriggers.dirty(key);
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 optimization to not dirty a state for the same values.

this.#valueTriggers.dirty(key);
super.set(key, value);
if (!hasKey) this.#keyTriggers.dirty(key);
if (value !== currentValue) this.#valueTriggers.dirty(key);
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 optimization to not dirty a state for the same values.

super();
if (initial) for (const v of initial) super.set(v[0], v[1]);
if (entries) for (const entry of entries) super.set(...entry);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should probably use apply here to avoid destructering

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

batch(() => {
this.#keyTriggers.dirty(key);
this.#keyTriggers.dirty($KEYS);
this.#valueTriggers.dirty(key);
if (currentValue !== undefined) this.#valueTriggers.dirty(key);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

optimization to not dirty a value that is already undefined

this.#valueTriggers.dirty(key);
super.set(key, value);

if (value !== currentValue) this.#valueTriggers.dirty(key);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

optimization to not dirty a value that is already the same value

*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

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.

None yet

1 participant