Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/green-needles-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@solid-primitives/map": patch
"@solid-primitives/set": patch
---

Update `ReactiveSet`/`ReactiveMap` constructors to allow passing any iterable as the initial value.
4 changes: 2 additions & 2 deletions packages/map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ReactiveMap<K, V> extends Map<K, V> {
#keyTriggers = new TriggerCache<K | typeof $KEYS>();
#valueTriggers = new TriggerCache<K>();

constructor(initial?: [K, V][]) {
constructor(initial?: Iterable<readonly [K, V]> | 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.

super();
if (initial) for (const v of initial) super.set(v[0], v[1]);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export class ReactiveWeakMap<K extends object, V> extends WeakMap<K, V> {
#keyTriggers = new TriggerCache<K>(WeakMap);
#valueTriggers = new TriggerCache<K>(WeakMap);

constructor(initial?: [K, V][]) {
constructor(initial?: Iterable<readonly [K, V]> | null) {
Copy link
Contributor Author

@AlexErrant AlexErrant Jun 13, 2023

Choose a reason for hiding this comment

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

c.f. https://github.com/microsoft/TypeScript/blob/main/src/lib/es2015.iterable.d.ts#LL147C40-L147C67

Note that TypeScript doesn't support undefined or null; I think this is a bug because the spec says:

  1. If iterable is either undefined or null, return map.

The TS PR making "Map constructor argument optional and nullable" didn't include WeakMap, which I believe is an oversight. We'll see if/when I get enough nerve to submit a PR to TypeScript 😅

Copy link
Member

Choose a reason for hiding this comment

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

since we are doing a if (initial) check, we could allow any falsy type.
But we should probably limit to nullable if we decide to change the check

super();
if (initial) for (const v of initial) super.set(v[0], v[1]);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/set/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const $KEYS = Symbol("track-keys");
export class ReactiveSet<T> extends Set<T> {
#triggers = new TriggerCache<T | typeof $KEYS>();

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

@AlexErrant AlexErrant Jun 13, 2023

Choose a reason for hiding this comment

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

c.f. https://github.com/microsoft/TypeScript/blob/main/src/lib/es2015.iterable.d.ts#LL189C21-L189C42

I'm not sure why this is readonly; looking at the gitblame for the above line in the TS repo seems to indicate it was never readonly. Commit where readonly was added.

Copy link
Member

Choose a reason for hiding this comment

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

readonly was there only to allow passing readonly arrays, instead of forcing you to copy to satisfy ts.
with iterable it's not an issue.

super();
if (values) for (const v of values) super.add(v);
}
Expand Down Expand Up @@ -94,7 +94,7 @@ export class ReactiveSet<T> extends Set<T> {
export class ReactiveWeakSet<T extends object> extends WeakSet<T> {
#triggers = new TriggerCache<T>(WeakMap);

constructor(values?: readonly T[] | null) {
constructor(values?: Iterable<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.

https://github.com/microsoft/TypeScript/blob/main/src/lib/es2015.iterable.d.ts#LL195C47-L195C60

The above comments also apply here - in particular the readonly comment and the fact that TS doesn't have WeakSetConstructor support null or undefined, which (again) doesn't conform to the spec:

  1. If iterable is either undefined or null, return set.

super();
if (values) for (const v of values) super.add(v);
}
Expand Down