2021-04-28 13:09:25 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-10-26 00:43:02 +02:00
|
|
|
<script lang="ts" context="module">
|
2021-04-28 13:09:25 +02:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2021-06-18 00:27:07 +02:00
|
|
|
type KeyType = Symbol | string;
|
2021-11-18 10:18:39 +01:00
|
|
|
type UpdaterMap = Map<KeyType, (event: Event) => Promise<boolean>>;
|
|
|
|
type StateMap = Map<KeyType, Promise<boolean>>;
|
2021-04-28 13:09:25 +02:00
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
const updaterMap: UpdaterMap = new Map();
|
|
|
|
const stateMap: StateMap = new Map();
|
2021-04-28 13:09:25 +02:00
|
|
|
const stateStore = writable(stateMap);
|
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
function updateAllStateWithCallback(
|
|
|
|
callback: (key: KeyType) => Promise<boolean>,
|
|
|
|
): void {
|
2021-05-26 01:21:33 +02:00
|
|
|
stateStore.update((map: StateMap): StateMap => {
|
|
|
|
const newMap = new Map() as StateMap;
|
2021-04-28 13:09:25 +02:00
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
for (const key of map.keys()) {
|
|
|
|
newMap.set(key, callback(key));
|
2021-04-28 13:09:25 +02:00
|
|
|
}
|
2021-05-26 01:21:33 +02:00
|
|
|
|
|
|
|
return newMap;
|
|
|
|
});
|
2021-04-28 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateAllState(event: Event): void {
|
2021-11-18 10:18:39 +01:00
|
|
|
updateAllStateWithCallback(
|
|
|
|
(key: KeyType): Promise<boolean> => updaterMap.get(key)!(event),
|
2021-05-07 15:10:28 +02:00
|
|
|
);
|
2021-04-28 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 15:10:28 +02:00
|
|
|
export function resetAllState(state: boolean): void {
|
2021-11-18 10:18:39 +01:00
|
|
|
updateAllStateWithCallback((): Promise<boolean> => Promise.resolve(state));
|
2021-04-28 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 00:27:07 +02:00
|
|
|
function updateStateByKey(key: KeyType, event: Event): void {
|
2021-05-26 01:21:33 +02:00
|
|
|
stateStore.update((map: StateMap): StateMap => {
|
|
|
|
map.set(key, updaterMap.get(key)!(event));
|
|
|
|
return map;
|
|
|
|
});
|
2021-04-28 13:09:25 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-10-26 00:43:02 +02:00
|
|
|
<script lang="ts">
|
2021-06-18 00:27:07 +02:00
|
|
|
export let key: KeyType;
|
2021-11-18 10:18:39 +01:00
|
|
|
export let update: (event: Event) => Promise<boolean>;
|
2021-05-07 15:10:28 +02:00
|
|
|
|
|
|
|
let state: boolean = false;
|
2021-04-28 13:09:25 +02:00
|
|
|
|
|
|
|
updaterMap.set(key, update);
|
|
|
|
|
|
|
|
stateStore.subscribe((map: StateMap): (() => void) => {
|
2021-11-18 10:18:39 +01:00
|
|
|
if (map.has(key)) {
|
|
|
|
const stateValue = map.get(key)!;
|
|
|
|
|
|
|
|
if (stateValue instanceof Promise) {
|
|
|
|
stateValue.then((value: boolean): void => {
|
|
|
|
state = value;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
state = stateValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state = false;
|
|
|
|
}
|
2021-04-28 13:09:25 +02:00
|
|
|
return () => map.delete(key);
|
|
|
|
});
|
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
stateMap.set(key, Promise.resolve(state));
|
2021-04-28 13:09:25 +02:00
|
|
|
|
|
|
|
function updateState(event: Event): void {
|
|
|
|
updateStateByKey(key, event);
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<slot {state} {updateState} />
|