Skip to content

Commit

Permalink
move uncommon things out of common
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Oct 27, 2024
1 parent 49651af commit 8b36684
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
30 changes: 0 additions & 30 deletions ui/common/src/algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,3 @@ export function findMapped<T, U>(arr: T[], callback: (el: T) => U | undefined):
}
return undefined;
}

export type SparseSet<T> = Set<T> | T;
export type SparseMap<V> = Map<string, SparseSet<V>>;

export function spread<T>(v: undefined | SparseSet<T>): T[] {
return v === undefined ? [] : v instanceof Set ? [...v] : [v];
}

export function spreadMap<T>(m: SparseMap<T>): [string, T[]][] {
return [...m].map(([k, v]) => [k, spread(v)]);
}

export function getSpread<T>(m: SparseMap<T>, key: string): T[] {
return spread(m.get(key));
}

export function remove<T>(m: SparseMap<T>, 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<T>(m: SparseMap<T>, 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]));
}
}
18 changes: 14 additions & 4 deletions ui/voice/src/move/voice.move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,8 +50,8 @@ export function initModule({
let choices: Map<string, Uci> | 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];

Expand Down
30 changes: 30 additions & 0 deletions ui/voice/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,33 @@ export function as<T>(v: T, f: () => void): () => T {
return v;
};
}

export type SparseSet<T> = Set<T> | T;
export type SparseMap<V> = Map<string, SparseSet<V>>;

export function spread<T>(v: undefined | SparseSet<T>): T[] {
return v === undefined ? [] : v instanceof Set ? [...v] : [v];
}

export function spreadMap<T>(m: SparseMap<T>): [string, T[]][] {
return [...m].map(([k, v]) => [k, spread(v)]);
}

export function getSpread<T>(m: SparseMap<T>, key: string): T[] {
return spread(m.get(key));
}

export function remove<T>(m: SparseMap<T>, 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<T>(m: SparseMap<T>, 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]));
}
}

0 comments on commit 8b36684

Please sign in to comment.