Skip to content

Commit 435aa92

Browse files
committed
map
1 parent 6a6e2af commit 435aa92

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

packages/svelte/src/reactivity/map.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,47 @@ import { get } from '../internal/client/runtime.js';
55
import { increment } from './utils.js';
66

77
/**
8+
* 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)
10+
* will cause it to be re-evaluated as necessary when the map is updated.
11+
*
12+
* Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
13+
*
14+
* ```svelte
15+
* <script>
16+
* import { SvelteMap } from 'svelte/reactivity';
17+
* import { result } from './game.js';
18+
*
19+
* let board = new SvelteMap();
20+
* let player = $state('x');
21+
* let winner = $derived(result(board));
22+
*
23+
* function reset() {
24+
* player = 'x';
25+
* board.clear();
26+
* }
27+
* </script>
28+
*
29+
* <div class="board">
30+
* {#each Array(9), i}
31+
* <button
32+
* disabled={board.has(i) || winner}
33+
* onclick={() => {
34+
* board.set(i, player);
35+
* player = player === 'x' ? 'o' : 'x';
36+
* }}
37+
* >{board.get(i)}</button>
38+
* {/each}
39+
* </div>
40+
*
41+
* {#if winner}
42+
* <p>{winner} wins!</p>
43+
* <button onclick={reset}>reset</button>
44+
* {:else}
45+
* <p>{player} is next</p>
46+
* {/if}
47+
* ```
48+
*
849
* @template K
950
* @template V
1051
* @extends {Map<K, V>}

packages/svelte/types/index.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,50 @@ declare module 'svelte/reactivity' {
19421942
add(value: T): this;
19431943
#private;
19441944
}
1945+
/**
1946+
* 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)
1948+
* will cause it to be re-evaluated as necessary when the map is updated.
1949+
*
1950+
* Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
1951+
*
1952+
* ```svelte
1953+
* <script>
1954+
* import { SvelteMap } from 'svelte/reactivity';
1955+
* import { result } from './game.js';
1956+
*
1957+
* let board = new SvelteMap();
1958+
* let player = $state('x');
1959+
* let winner = $derived(result(board));
1960+
*
1961+
* function reset() {
1962+
* player = 'x';
1963+
* board.clear();
1964+
* }
1965+
* </script>
1966+
*
1967+
* <div class="board">
1968+
* {#each Array(9), i}
1969+
* <button
1970+
* disabled={board.has(i) || winner}
1971+
* onclick={() => {
1972+
* board.set(i, player);
1973+
* player = player === 'x' ? 'o' : 'x';
1974+
* }}
1975+
* >{board.get(i)}</button>
1976+
* {/each}
1977+
* </div>
1978+
*
1979+
* {#if winner}
1980+
* <p>{winner} wins!</p>
1981+
* <button onclick={reset}>reset</button>
1982+
* {:else}
1983+
* <p>{player} is next</p>
1984+
* {/if}
1985+
* ```
1986+
*
1987+
*
1988+
*/
19451989
export class SvelteMap<K, V> extends Map<K, V> {
19461990

19471991
constructor(value?: Iterable<readonly [K, V]> | null | undefined);

0 commit comments

Comments
 (0)