audiogram-beta/src/store.js

238 lines
7.3 KiB
JavaScript
Raw Normal View History

2022-11-24 12:16:57 -05:00
//@ts-check
2022-12-03 09:36:01 -05:00
const size = 1/6;
2022-11-27 23:56:37 -05:00
/** @typedef {[frequency:number, position:number, normal:boolean]} ColumnMapping */
/** @type {Array<ColumnMapping>} */
export const ColumnMapping = [
[ 125, size*0.0, true ],
[ 250, size*1.0, true ],
[ 500, size*2.0, true ],
[1000, size*3.0, true ],
[2000, size*4.0, true ],
[3000, size*4.5, false],
[4000, size*5.0, true ],
[6000, size*5.5, false],
[8000, size*6.0, true ]
];
/** @type {(inFrequency:number)=>ColumnMapping|false} */
export const ColumnLookup =(inFrequency)=>
{
for(let i=0; i<ColumnMapping.length; i++)
2022-11-24 12:16:57 -05:00
{
2022-11-27 23:56:37 -05:00
const map = ColumnMapping[i];
if(map[0] == inFrequency){ return map; }
2022-11-24 12:16:57 -05:00
}
2022-11-27 23:56:37 -05:00
return false;
2022-11-28 18:42:41 -05:00
};
2022-12-03 09:36:01 -05:00
/** @type {(freq:TestFrequency, chan:number, user:boolean)=>TestFrequencySample|undefined} */
export const MarkGet =(freq, chan, user)=> freq[/** @type {"UserL"|"UserR"|"TestL"|"TestR"} */ (`${user ? "User" : "Test"}${chan ? "R" : "L"}`)];
/** @type {(freq:TestFrequency, chan:number, mark:TestFrequencySample|undefined)=>TestFrequencySample|undefined} */
export const MarkSet =(freq, chan, mark)=> freq[ chan ? "UserR" : "UserL" ] = mark;
2022-12-03 21:40:23 -05:00
/** @typedef {{Min:number, Max:number, Value:number, Step:number}} Range */
2022-11-27 23:56:37 -05:00
/** @typedef {{Stim:number, Resp:boolean}} TestFrequencySample */
/** @typedef {{Hz:number, TestL:TestFrequencySample, TestR:TestFrequencySample, UserL?:TestFrequencySample, UserR?:TestFrequencySample}} TestFrequency */
2022-12-03 19:05:26 -05:00
/** @typedef {{Name:string, Plot:Array<TestFrequency>}} Test */
2022-11-27 23:56:37 -05:00
/** @typedef {{Test?:Test, Freq?:TestFrequency, Mark?:TestFrequencySample}} Context */
2022-12-03 22:51:22 -05:00
/** @typedef {{Chan:Range, Freq:Range, Stim:Range, Live:Context, Draw:{UserL:DrawGroup, UserR:DrawGroup, TestL:DrawGroup, TestR:DrawGroup}, Tests:Array<Test>}} State @memberof Store*/
2022-11-27 23:56:37 -05:00
/** @type {State} */
export const Initial =
2022-11-24 12:16:57 -05:00
{
2022-12-03 21:40:23 -05:00
Chan: { Min:0, Max:1, Value:0, Step:1 },
Freq: { Min:2, Max:8, Value:2, Step:1 },
Stim: { Min:-10, Max:120, Value:30, Step:5 },
2022-11-28 18:42:41 -05:00
Live:
2022-11-27 23:56:37 -05:00
{
Test: undefined,
Freq: undefined,
Mark: undefined
},
2022-12-03 19:05:26 -05:00
Draw:
{
UserL:{Points:[], Paths:[]},
UserR:{Points:[], Paths:[]},
TestL:{Points:[], Paths:[]},
TestR:{Points:[], Paths:[]}
},
2022-11-27 23:56:37 -05:00
Tests: [
{
Name: "Patient A Asymmetric Notch",
Plot:
[
{ Hz: 500, TestL: { Stim: 30, Resp: true }, TestR: { Stim: 50, Resp: true } },
{ Hz: 1000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } }
]
}
]
2022-11-24 12:16:57 -05:00
};
2022-11-28 18:42:41 -05:00
/** @typedef {{Name:"Mark", Data:boolean|null}} ActionMark */
/** @typedef {{Name:"Test", Data:number}} ActionTest */
/** @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 */
2022-11-27 23:56:37 -05:00
/** @typedef {(inState:State, inAction:Action)=>State} Reducer */
2022-11-28 10:00:18 -05:00
/** @typedef {(inState:State)=>boolean} SelectionUpdater */
/** @type {Record<string, SelectionUpdater>} */
2022-11-28 18:42:41 -05:00
const Update =
{
2022-11-28 10:00:18 -05:00
Freq(inState)
2022-11-27 23:56:37 -05:00
{
2022-12-03 21:40:23 -05:00
const column = ColumnMapping[inState.Freq.Value];
2022-11-28 18:42:41 -05:00
if(column && inState.Live.Test)
2022-11-27 23:56:37 -05:00
{
2022-11-28 10:00:18 -05:00
const hz = column[0];
2022-11-28 18:42:41 -05:00
inState.Live.Freq = undefined;
for(let i=0; i<inState.Live.Test.Plot.length; i++)
2022-11-27 23:56:37 -05:00
{
2022-11-28 18:42:41 -05:00
const plot = inState.Live.Test.Plot[i];
2022-11-28 10:00:18 -05:00
if(plot.Hz == hz)
2022-11-27 23:56:37 -05:00
{
2022-11-28 18:42:41 -05:00
inState.Live.Freq = plot;
2022-11-28 10:00:18 -05:00
return true;
2022-11-27 23:56:37 -05:00
}
}
}
2022-11-28 10:00:18 -05:00
return false;
},
Mark(inState)
{
2022-11-28 18:42:41 -05:00
const freq = inState.Live.Freq;
2022-11-28 10:00:18 -05:00
if(freq)
2022-11-27 23:56:37 -05:00
{
2022-12-03 21:40:23 -05:00
inState.Live.Mark = MarkGet(freq, inState.Chan.Value, true);
2022-11-28 18:42:41 -05:00
return true;
2022-11-27 23:56:37 -05:00
}
2022-11-28 10:00:18 -05:00
return false;
}
};
2022-12-03 09:36:01 -05:00
2022-12-03 19:05:26 -05:00
/** @typedef {{X:number, Y:number, Mark:TestFrequencySample}} DrawPoint */
2022-12-03 13:32:04 -05:00
/** @typedef {{Points:Array<DrawPoint>, Paths:Array<Array<DrawPoint>>}} DrawGroup */
/** @typedef {{Left:DrawGroup, Right:DrawGroup}} DrawChart */
2022-12-03 19:05:26 -05:00
/** @typedef {{User?:DrawChart, Test?:DrawChart}} DrawTest */
2022-12-03 21:40:23 -05:00
/** @type {(inTest:Test, inChan:number, inStim:Range, inIsUser:boolean)=>DrawGroup} */
export function Congtiguous(inTest, inChan, inStim, inIsUser)
2022-12-03 09:36:01 -05:00
{
2022-12-03 13:32:04 -05:00
/** @type {DrawGroup} */
const output = {Points:[], Paths:[]};
2022-12-03 09:36:01 -05:00
let plot;
let valid = false;
2022-12-03 13:32:04 -05:00
/** @type {Array<DrawPoint>} */
let segment = [];
2022-12-03 09:36:01 -05:00
for(let i=0; i<inTest.Plot.length; i++)
{
plot = inTest.Plot[i];
2022-12-03 21:40:23 -05:00
const mark = MarkGet(plot, inChan, inIsUser);
2022-12-03 13:32:04 -05:00
if(mark)
2022-12-03 09:36:01 -05:00
{
const lookup = ColumnLookup(plot.Hz);
if(lookup)
{
2022-12-03 13:32:04 -05:00
/** @type {DrawPoint} */
2022-12-03 19:05:26 -05:00
const point = {
X: lookup[1]*100,
2022-12-03 21:40:23 -05:00
Y: (mark.Stim - inStim.Min)/(inStim.Max - inStim.Min) * 100,
2022-12-03 19:05:26 -05:00
Mark: mark
};
2022-12-03 13:32:04 -05:00
output.Points.push(point);
if(mark.Resp)
{
if(!valid)
2022-12-03 09:36:01 -05:00
{
2022-12-03 13:32:04 -05:00
segment = [];
output.Paths.push(segment);
2022-12-03 09:36:01 -05:00
}
2022-12-03 13:32:04 -05:00
valid = true;
segment.push(point);
}
else
{
valid = false;
}
2022-12-03 09:36:01 -05:00
}
}
}
2022-12-03 13:32:04 -05:00
return output;
2022-12-03 09:36:01 -05:00
}
2022-11-28 10:00:18 -05:00
/** @type {Reducer} */
export function Reducer(inState, inAction)
{
const clone = {...inState};
2022-11-28 18:42:41 -05:00
const {Name, Data} = inAction;
2022-11-28 10:00:18 -05:00
2022-11-28 18:42:41 -05:00
if(Name == "Test")
{
clone.Live.Test = clone.Tests[Data];
Update.Freq(clone);
Update.Mark(clone);
2022-12-03 19:05:26 -05:00
clone.Draw = {
2022-12-03 21:40:23 -05:00
UserL: Congtiguous(clone.Live.Test, 0, clone.Stim, true ),
UserR: Congtiguous(clone.Live.Test, 1, clone.Stim, true ),
TestL: Congtiguous(clone.Live.Test, 0, clone.Stim, false),
TestR: Congtiguous(clone.Live.Test, 1, clone.Stim, false)
2022-12-03 19:05:26 -05:00
};
2022-11-28 18:42:41 -05:00
}
else if (Name == "Mark")
2022-11-28 10:00:18 -05:00
{
2022-12-03 19:05:26 -05:00
if(clone.Live.Test && clone.Live.Freq)
2022-11-28 18:42:41 -05:00
{
2022-12-03 21:40:23 -05:00
clone.Live.Mark = MarkSet(clone.Live.Freq, clone.Chan.Value, Data !== null ? {Stim:clone.Stim.Value, Resp:Data} : undefined);
2022-12-03 19:05:26 -05:00
clone.Draw = {...clone.Draw};
2022-12-03 21:40:23 -05:00
clone.Draw[clone.Chan.Value == 0 ? "UserL" : "UserR"] = Congtiguous(clone.Live.Test, clone.Chan.Value, clone.Stim, true);
2022-11-28 18:42:41 -05:00
}
}
else if( Name=="Stim" || Name=="Chan" || Name=="Freq")
{
2022-12-03 21:40:23 -05:00
const tone = {...clone[Name]};
tone.Value += Data*tone.Step;
if(tone.Value < tone.Min){ tone.Value = tone.Min; }
if(tone.Value > tone.Max){ tone.Value = tone.Max; }
clone[Name] = tone;
2022-11-28 18:42:41 -05:00
if(Name != "Stim")
2022-11-28 10:00:18 -05:00
{
Update.Freq(clone);
Update.Mark(clone);
}
2022-11-27 23:56:37 -05:00
}
2022-11-28 18:42:41 -05:00
2022-11-27 23:56:37 -05:00
return clone;
2022-12-03 21:40:23 -05:00
}
/*
const minified =
[
1,
[
[20, 30, 50, 40, 60, 80],[20, 30, 50, 40, 60, 80]
]
];
const Expand =(inMin)=>
{
const outTests = [];
const inFreq = inMin[0];
for(let i=1; i<inMin.length; i++)
{
let inTest = inMin[i];
let inTestName = inTest[0];
const outTest = {
Name:inTest[0],
Plot:[]
};
outTests.push(outTest);
const outFreq = {Hz:0, TestL:{Stim:0, Resp:true}, TestR:{Stim:0, Resp:true}, UserL:undefined, UserR:undefined};
}
}
*/