Tone attributes update

This commit is contained in:
Seth Trowbridge 2022-11-28 18:42:41 -05:00
parent 0bf774e6b0
commit a602bfd295
4 changed files with 139 additions and 62 deletions

View File

@ -1,7 +1,7 @@
{ {
"tasks": "tasks":
{ {
"fs": "deno run -A https://deno.land/std@0.166.0/http/file_server.ts", "fs": "deno run -A --no-lock https://deno.land/std@0.166.0/http/file_server.ts",
"test": "deno test *_test.js --watch" "test": "deno test 'store_test.js' --no-lock"
} }
} }

View File

@ -1,9 +0,0 @@
{
"version": "2",
"remote": {
"https://deno.land/std@0.166.0/fmt/colors.ts": "9e36a716611dcd2e4865adea9c4bec916b5c60caad4cdcdc630d4974e6bb8bd4",
"https://deno.land/std@0.166.0/testing/_diff.ts": "a23e7fc2b4d8daa3e158fa06856bedf5334ce2a2831e8bf9e509717f455adb2c",
"https://deno.land/std@0.166.0/testing/_format.ts": "cd11136e1797791045e639e9f0f4640d5b4166148796cad37e6ef75f7d7f3832",
"https://deno.land/std@0.166.0/testing/asserts.ts": "1e340c589853e82e0807629ba31a43c84ebdcdeca910c4a9705715dfdb0f5ce8"
}
}

View File

@ -23,20 +23,45 @@ export const ColumnLookup =(inFrequency)=>
if(map[0] == inFrequency){ return map; } if(map[0] == inFrequency){ return map; }
} }
return false; return false;
};
/** @typedef {{Min:number, Max:number}} Limit */
/** @type {Record<string, Limit>} */
export const ToneLimit =
{
Freq: { Min: 0, Max: ColumnMapping.length-1 },
Stim: { Min: -10, Max: 120 },
Chan: { Min: 0, Max: 1},
};
/** @type {(inValue:number, inLimit:Limit)=>number} */
export const ApplyLimit =(inValue, inLimit)=>
{
if(inValue < inLimit.Min){ return inLimit.Min; }
else if(inValue > inLimit.Max) { return inLimit.Max; }
else{ return inValue; }
} }
/** @typedef {(freq:TestFrequency, chan:number)=>TestFrequencySample|undefined} MarkLookup */
/** @type {Record<string, MarkLookup>} */
export const ChanMark =
{
User: (freq, chan)=> chan == 0 ? freq.UserL : freq.UserR,
Test: (freq, chan)=> chan == 0 ? freq.TestL : freq.TestR
};
/** @typedef {{Stim:number, Resp:boolean}} TestFrequencySample */ /** @typedef {{Stim:number, Resp:boolean}} TestFrequencySample */
/** @typedef {{Hz:number, TestL:TestFrequencySample, TestR:TestFrequencySample, UserL?:TestFrequencySample, UserR?:TestFrequencySample}} TestFrequency */ /** @typedef {{Hz:number, TestL:TestFrequencySample, TestR:TestFrequencySample, UserL?:TestFrequencySample, UserR?:TestFrequencySample}} TestFrequency */
/** @typedef {{Name:string, Plot:Array<TestFrequency>}} Test */ /** @typedef {{Name:string, Plot:Array<TestFrequency>}} Test */
/** @typedef {{Test?:Test, Freq?:TestFrequency, Mark?:TestFrequencySample}} Context */ /** @typedef {{Test?:Test, Freq?:TestFrequency, Mark?:TestFrequencySample}} Context */
/** @typedef {{Chan:"left"|"right", Freq:number, Stim:number, Selection:Context, Tests:Array<Test>}} State */ /** @typedef {{Chan:number, Freq:number, Stim:number, Live:Context, Tests:Array<Test>}} State */
/** @type {State} */ /** @type {State} */
export const Initial = export const Initial =
{ {
Chan: "left", Chan: 0,
Freq: 3, Freq: 3,
Stim: 30, Stim: 30,
Selection: Live:
{ {
Test: undefined, Test: undefined,
Freq: undefined, Freq: undefined,
@ -54,26 +79,30 @@ export const Initial =
] ]
}; };
/** @typedef {{Name:"Mark", Data:boolean|undefined}} ActionMark */ /** @typedef {{Name:"Mark", Data:boolean|null}} ActionMark */
/** @typedef {{Name:"Test", Data:number}} ActionTest*/ /** @typedef {{Name:"Test", Data:number}} ActionTest */
/** @typedef {ActionMark|ActionTest} Action */ /** @typedef {{Name:"Chan", Data:number}} ActionChan */
/** @typedef {{Name:"Freq", Data:number}} ActionFreq */
/** @typedef {{Name:"Stim", Data:number}} ActionStim */
/** @typedef {ActionMark|ActionTest|ActionChan|ActionFreq|ActionStim} Action */
/** @typedef {(inState:State, inAction:Action)=>State} Reducer */ /** @typedef {(inState:State, inAction:Action)=>State} Reducer */
/** @typedef {(inState:State)=>boolean} SelectionUpdater */ /** @typedef {(inState:State)=>boolean} SelectionUpdater */
/** @type {Record<string, SelectionUpdater>} */ /** @type {Record<string, SelectionUpdater>} */
const Update = { const Update =
{
Freq(inState) Freq(inState)
{ {
const column = ColumnMapping[inState.Freq]; const column = ColumnMapping[inState.Freq];
if(column && inState.Selection.Test) if(column && inState.Live.Test)
{ {
const hz = column[0]; const hz = column[0];
inState.Selection.Freq = undefined; inState.Live.Freq = undefined;
for(let i=0; i<inState.Selection.Test.Plot.length; i++) for(let i=0; i<inState.Live.Test.Plot.length; i++)
{ {
const plot = inState.Selection.Test.Plot[i]; const plot = inState.Live.Test.Plot[i];
if(plot.Hz == hz) if(plot.Hz == hz)
{ {
inState.Selection.Freq = plot; inState.Live.Freq = plot;
return true; return true;
} }
} }
@ -82,19 +111,11 @@ const Update = {
}, },
Mark(inState) Mark(inState)
{ {
const freq = inState.Selection.Freq; const freq = inState.Live.Freq;
if(freq) if(freq)
{ {
if(inState.Chan == "left" && freq.UserL) inState.Live.Mark = inState.Chan == 0 ? freq.UserL : freq.UserR;
{ return true;
inState.Selection.Mark = freq.UserL;
return true;
}
else if(inState.Chan == "right" && freq.UserR)
{
inState.Selection.Mark = freq.UserR;
return true;
}
} }
return false; return false;
} }
@ -103,16 +124,32 @@ const Update = {
export function Reducer(inState, inAction) export function Reducer(inState, inAction)
{ {
const clone = {...inState}; const clone = {...inState};
const {Name, Data} = inAction;
switch(inAction.Name) if(Name == "Test")
{ {
case "Test" : clone.Live.Test = clone.Tests[Data];
Update.Freq(clone);
Update.Mark(clone);
}
else if (Name == "Mark")
{
if(clone.Live.Freq)
{ {
clone.Selection.Test = clone.Tests[inAction.Data]; const channelKey = clone.Chan == 0 ? "UserL" : "UserR";
Update.Freq(clone); const channelVal = Data !== null ? {Stim:clone.Stim, Resp:Data} : undefined;
Update.Mark(clone); clone.Live.Mark = clone.Live.Freq[channelKey] = channelVal;
break;
} }
} }
else if( Name=="Stim" || Name=="Chan" || Name=="Freq")
{
clone[Name] = ApplyLimit(Data, ToneLimit[Name]);
if(Name != "Stim")
{
Update.Freq(clone);
Update.Mark(clone);
}
}
return clone; return clone;
} }

View File

@ -2,7 +2,7 @@ import { assertEquals } from "https://deno.land/std@0.166.0/testing/asserts.ts";
import { Initial, Reducer, ColumnMapping } from "./store.js"; import { Initial, Reducer, ColumnMapping } from "./store.js";
const States = { const States = {
list:[], list:[Initial],
get latest() get latest()
{ {
return this.list[this.list.length-1]; return this.list[this.list.length-1];
@ -13,49 +13,98 @@ const States = {
} }
}; };
States.latest = Initial;
Deno.test("Initial State", async (t)=> Deno.test("Initial State", async (t)=>
{ {
const state = Initial;
await t.step("Selections are empty", ()=> await t.step("Selections are empty", ()=>
{ {
assertEquals(state.Selection.Test, undefined); assertEquals(States.latest.Live.Test, undefined);
assertEquals(state.Selection.Freq, undefined); assertEquals(States.latest.Live.Freq, undefined);
assertEquals(state.Selection.Mark, undefined); assertEquals(States.latest.Live.Mark, undefined);
}); });
await t.step("Frequency index maps to 1k hz", ()=> await t.step("Frequency index maps to 1k hz", ()=>
{ {
assertEquals(state.Freq, 3); assertEquals(States.latest.Freq, 3);
assertEquals(ColumnMapping[state.Freq][0], 1000); assertEquals(ColumnMapping[States.latest.Freq][0], 1000);
}); });
await t.step("The first test has its 2nd plot at 1k hz, and no user marks", ()=> await t.step("The first test has its 2nd plot at 1k hz", ()=>
{ {
const plot = state.Tests[0].Plot[1]; const plot = States.latest.Tests[0].Plot[1];
assertEquals(plot.Hz, 1000); assertEquals(plot.Hz, 1000);
assertEquals(plot.UserL, undefined);
assertEquals(plot.UserR, undefined);
}); });
}) })
Deno.test("Select Test", async (t)=> Deno.test("Select First Test", async (t)=>
{ {
let state;
await t.step("dispatch action", ()=> await t.step("dispatch action", ()=>
{ {
state = Reducer(Initial, {Name:"Test", Data:0}); States.latest = Reducer(States.latest, {Name:"Test", Data:0});
}); });
await t.step("check selections: test and freq, but no mark", ()=> await t.step("check selections: test and freq, but no mark", ()=>
{ {
assertEquals(state.Selection.Test, state.Tests[0]); const s = States.latest;
assertEquals(state.Selection.Freq, state.Tests[0].Plot[1]); assertEquals(s.Live.Test, s.Tests[0]);
assertEquals(state.Selection.Mark, undefined); assertEquals(s.Live.Freq, s.Tests[0].Plot[1]);
assertEquals(s.Live.Mark, undefined);
});
});
Deno.test("Make Marks", async (t)=>
{
let s;
await t.step("Left channel selected", ()=>
{
assertEquals(States.latest.Chan, 0);
});
await t.step("Dispatch Mark action", ()=>
{
States.latest = Reducer(States.latest, {Name:"Mark", Data:true});
});
s = States.latest;
await t.step("Check selections: test, freq, and mark", ()=>
{
assertEquals(s.Live.Test, s.Tests[0]);
assertEquals(s.Live.Freq, s.Tests[0].Plot[1]);
assertEquals(s.Live.Mark, s.Tests[0].Plot[1].UserL);
}); });
await t.step("Check marked value", ()=>
{
assertEquals(s.Live.Mark.Stim, s.Stim);
assertEquals(s.Live.Mark.Resp, true);
});
await t.step("Dispatch Mark delete action", ()=>
}) {
States.latest = Reducer(States.latest, {Name:"Mark", Data:null});
});
s = States.latest;
await t.step("Check marked value", ()=>
{
assertEquals(States.latest.Live.Mark, undefined);
});
});
Deno.test("Update Tone State", async(t)=>
{
await t.step("all three", ()=>
{
States.latest = Reducer(States.latest, {Name:"Freq", Data:2});
States.latest = Reducer(States.latest, {Name:"Stim", Data:25});
States.latest = Reducer(States.latest, {Name:"Chan", Data:1});
});
await t.step("check tone values", ()=>
{
assertEquals(States.latest.Stim, 25);
assertEquals(States.latest.Freq, 2);
assertEquals(States.latest.Chan, 1);
});
});