From 8b366848527baeee2b1967f72145b03670c0526b Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Sun, 27 Oct 2024 08:02:43 -0500 Subject: [PATCH] move uncommon things out of common --- ui/common/src/algo.ts | 30 ------------------------------ ui/voice/src/move/voice.move.ts | 18 ++++++++++++++---- ui/voice/src/util.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/ui/common/src/algo.ts b/ui/common/src/algo.ts index 0e3dd94a872c..bb860c26a14f 100644 --- a/ui/common/src/algo.ts +++ b/ui/common/src/algo.ts @@ -63,33 +63,3 @@ export function findMapped(arr: T[], callback: (el: T) => U | undefined): } return undefined; } - -export type SparseSet = Set | T; -export type SparseMap = Map>; - -export function spread(v: undefined | SparseSet): T[] { - return v === undefined ? [] : v instanceof Set ? [...v] : [v]; -} - -export function spreadMap(m: SparseMap): [string, T[]][] { - return [...m].map(([k, v]) => [k, spread(v)]); -} - -export function getSpread(m: SparseMap, key: string): T[] { - return spread(m.get(key)); -} - -export function remove(m: SparseMap, key: string, val: T): void { - const v = m.get(key); - if (v === val) m.delete(key); - else if (v instanceof Set) v.delete(val); -} - -export function pushMap(m: SparseMap, key: string, val: T): void { - const v = m.get(key); - if (!v) m.set(key, val); - else { - if (v instanceof Set) v.add(val); - else if (v !== val) m.set(key, new Set([v as T, val])); - } -} diff --git a/ui/voice/src/move/voice.move.ts b/ui/voice/src/move/voice.move.ts index 2e0f9de88220..66740e5c4e48 100644 --- a/ui/voice/src/move/voice.move.ts +++ b/ui/voice/src/move/voice.move.ts @@ -8,9 +8,19 @@ import { MoveRootCtrl, MoveUpdate } from 'chess/moveRootCtrl'; import { VoiceMove, VoiceCtrl, Entry, Match } from '../voice'; import { coloredArrows, numberedArrows, brushes } from './arrows'; import { settingNodes } from './view'; -import { spread, type SparseMap, spreadMap, getSpread, remove, pushMap } from 'common/algo'; -import { type Transform, movesTo, findTransforms, as } from '../util'; import { MsgType } from '../interfaces'; +import { + type Transform, + type SparseMap, + spread, + spreadMap, + getSpread, + remove, + pushMap, + movesTo, + findTransforms, + as, +} from '../util'; export function initModule({ root, @@ -40,8 +50,8 @@ export function initModule({ let choices: Map | undefined; // map choice arrows (yes, blue, red, 1, 2, etc) to moves let choiceTimeout: number | undefined; // timeout for ambiguity choices const clarityPref = prop.storedIntProp('voice.clarity', 0); - const colorsPref = prop.storedBooleanPropWithEffect('voice.useColors', true, _ => initTimerRec()); - const timerPref = prop.storedIntPropWithEffect('voice.timer', 3, _ => initTimerRec()); + const colorsPref = prop.storedBooleanPropWithEffect('voice.useColors', true, () => initTimerRec()); + const timerPref = prop.storedIntPropWithEffect('voice.timer', 3, () => initTimerRec()); const listenHandlers = [handleConfirm, handleCommand, handleAmbiguity, handleMove]; diff --git a/ui/voice/src/util.ts b/ui/voice/src/util.ts index 0cfe3b708fab..9bc680212cb4 100644 --- a/ui/voice/src/util.ts +++ b/ui/voice/src/util.ts @@ -83,3 +83,33 @@ export function as(v: T, f: () => void): () => T { return v; }; } + +export type SparseSet = Set | T; +export type SparseMap = Map>; + +export function spread(v: undefined | SparseSet): T[] { + return v === undefined ? [] : v instanceof Set ? [...v] : [v]; +} + +export function spreadMap(m: SparseMap): [string, T[]][] { + return [...m].map(([k, v]) => [k, spread(v)]); +} + +export function getSpread(m: SparseMap, key: string): T[] { + return spread(m.get(key)); +} + +export function remove(m: SparseMap, key: string, val: T): void { + const v = m.get(key); + if (v === val) m.delete(key); + else if (v instanceof Set) v.delete(val); +} + +export function pushMap(m: SparseMap, key: string, val: T): void { + const v = m.get(key); + if (!v) m.set(key, val); + else { + if (v instanceof Set) v.add(val); + else if (v !== val) m.set(key, new Set([v as T, val])); + } +}