Skip to content

Commit 0985ba9

Browse files
committed
set
1 parent 435aa92 commit 0985ba9

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

β€Žpackages/svelte/src/reactivity/map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { increment } from './utils.js';
66

77
/**
88
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
9-
* Reading contents of the map in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293?version=5.25.10) below)
9+
* Reading contents of the map in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293) below)
1010
* will cause it to be re-evaluated as necessary when the map is updated.
1111
*
1212
* Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).

β€Žpackages/svelte/src/reactivity/set.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'un
1010
var inited = false;
1111

1212
/**
13+
* A reactive version of the built-in [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) object.
14+
* Reading contents of the set in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `set.size` or calling `set.has(...)` as in the [example](https://svelte.dev/playground/53438b51194b4882bcc18cddf9f96f15) below)
15+
* will cause it to be re-evaluated as necessary when the set is updated.
16+
*
17+
* Note that values in a reactive set are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
18+
*
19+
* ```svelte
20+
* <script>
21+
* import { SvelteSet } from 'svelte/reactivity';
22+
* let monkeys = new SvelteSet();
23+
*
24+
* function toggle(monkey) {
25+
* if (monkeys.has(monkey)) {
26+
* monkeys.delete(monkey);
27+
* } else {
28+
* monkeys.add(monkey);
29+
* }
30+
* }
31+
* </script>
32+
*
33+
* {#each ['πŸ™ˆ', 'πŸ™‰', 'πŸ™Š'] as monkey}
34+
* <button onclick={() => toggle(monkey)}>{monkey}</button>
35+
* {/each}
36+
*
37+
* <button onclick={() => monkeys.clear()}>clear</button>
38+
*
39+
* {#if monkeys.has('πŸ™ˆ')}<p>see no evil</p>{/if}
40+
* {#if monkeys.has('πŸ™‰')}<p>hear no evil</p>{/if}
41+
* {#if monkeys.has('πŸ™Š')}<p>speak no evil</p>{/if}
42+
* ```
43+
*
1344
* @template T
1445
* @extends {Set<T>}
1546
*/

β€Žpackages/svelte/types/index.d.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,40 @@ declare module 'svelte/reactivity' {
19351935
constructor(...params: any[]);
19361936
#private;
19371937
}
1938+
/**
1939+
* A reactive version of the built-in [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) object.
1940+
* Reading contents of the set in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `set.size` or calling `set.has(...)` as in the [example](https://svelte.dev/playground/53438b51194b4882bcc18cddf9f96f15) below)
1941+
* will cause it to be re-evaluated as necessary when the set is updated.
1942+
*
1943+
* Note that values in a reactive set are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
1944+
*
1945+
* ```svelte
1946+
* <script>
1947+
* import { SvelteSet } from 'svelte/reactivity';
1948+
* let monkeys = new SvelteSet();
1949+
*
1950+
* function toggle(monkey) {
1951+
* if (monkeys.has(monkey)) {
1952+
* monkeys.delete(monkey);
1953+
* } else {
1954+
* monkeys.add(monkey);
1955+
* }
1956+
* }
1957+
* </script>
1958+
*
1959+
* {#each ['πŸ™ˆ', 'πŸ™‰', 'πŸ™Š'] as monkey}
1960+
* <button onclick={() => toggle(monkey)}>{monkey}</button>
1961+
* {/each}
1962+
*
1963+
* <button onclick={() => monkeys.clear()}>clear</button>
1964+
*
1965+
* {#if monkeys.has('πŸ™ˆ')}<p>see no evil</p>{/if}
1966+
* {#if monkeys.has('πŸ™‰')}<p>hear no evil</p>{/if}
1967+
* {#if monkeys.has('πŸ™Š')}<p>speak no evil</p>{/if}
1968+
* ```
1969+
*
1970+
*
1971+
*/
19381972
export class SvelteSet<T> extends Set<T> {
19391973

19401974
constructor(value?: Iterable<T> | null | undefined);
@@ -1944,7 +1978,7 @@ declare module 'svelte/reactivity' {
19441978
}
19451979
/**
19461980
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
1947-
* Reading contents of the map in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293?version=5.25.10) below)
1981+
* Reading contents of the map in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived) (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293) below)
19481982
* will cause it to be re-evaluated as necessary when the map is updated.
19491983
*
19501984
* Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).

0 commit comments

Comments
Β (0)