anki/ts/components/WithState.svelte
Damien Elmes 0026506543 update ts deps
- prettier's formatting has changed, so files needed to be reformatted
- dart is spitting out deprecation warnings like:

254 │   2: $spacer / 2,
    │      ^^^^^^^^^^^
    ╵
    bazel-out/darwin-fastbuild/bin/ts/sass/bootstrap/_variables.scss 254:6  @import
    ts/sass/button_mixins.scss 2:9                                          @use
    ts/components/ColorPicker.svelte 2:5                                    root stylesheet

DEPRECATION WARNING: Using / for division is deprecated and will be removed in Dart Sass 2.0.0.

Recommendation: math.div($grid-gutter-width, 2)
2021-05-26 09:37:40 +10:00

66 lines
1.8 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="typescript" context="module">
import { writable } from "svelte/store";
type UpdaterMap = Map<string, (event: Event) => boolean>;
type StateMap = Map<string, boolean>;
const updaterMap = new Map() as UpdaterMap;
const stateMap = new Map() as StateMap;
const stateStore = writable(stateMap);
function updateAllStateWithCallback(callback: (key: string) => boolean): void {
stateStore.update((map: StateMap): StateMap => {
const newMap = new Map() as StateMap;
for (const key of map.keys()) {
newMap.set(key, callback(key));
}
return newMap;
});
}
export function updateAllState(event: Event): void {
updateAllStateWithCallback((key: string): boolean =>
updaterMap.get(key)!(event)
);
}
export function resetAllState(state: boolean): void {
updateAllStateWithCallback((): boolean => state);
}
function updateStateByKey(key: string, event: Event): void {
stateStore.update((map: StateMap): StateMap => {
map.set(key, updaterMap.get(key)!(event));
return map;
});
}
</script>
<script lang="typescript">
export let key: string;
export let update: (event: Event) => boolean;
let state: boolean = false;
updaterMap.set(key, update);
stateStore.subscribe((map: StateMap): (() => void) => {
state = Boolean(map.get(key));
return () => map.delete(key);
});
stateMap.set(key, state);
function updateState(event: Event): void {
updateStateByKey(key, event);
}
</script>
<slot {state} {updateState} />