audiogram-beta/store.js

155 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-11-24 12:16:57 -05:00
//@ts-check
2022-11-27 23:56:37 -05:00
const size = 100/6;
/** @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
};
/** @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; }
2022-11-27 23:56:37 -05:00
}
2022-11-24 12:16:57 -05:00
2022-11-28 18:42:41 -05:00
/** @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
};
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 */
/** @typedef {{Name:string, Plot:Array<TestFrequency>}} Test */
/** @typedef {{Test?:Test, Freq?:TestFrequency, Mark?:TestFrequencySample}} Context */
2022-11-28 18:42:41 -05:00
/** @typedef {{Chan:number, Freq:number, Stim:number, Live:Context, Tests:Array<Test>}} State */
2022-11-27 23:56:37 -05:00
/** @type {State} */
export const Initial =
2022-11-24 12:16:57 -05:00
{
2022-11-28 18:42:41 -05:00
Chan: 0,
2022-11-27 23:56:37 -05:00
Freq: 3,
Stim: 30,
2022-11-28 18:42:41 -05:00
Live:
2022-11-27 23:56:37 -05:00
{
Test: undefined,
Freq: undefined,
Mark: undefined
},
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-11-28 10:00:18 -05:00
const column = ColumnMapping[inState.Freq];
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-11-28 18:42:41 -05:00
inState.Live.Mark = inState.Chan == 0 ? freq.UserL : freq.UserR;
return true;
2022-11-27 23:56:37 -05:00
}
2022-11-28 10:00:18 -05:00
return false;
}
};
/** @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);
}
else if (Name == "Mark")
2022-11-28 10:00:18 -05:00
{
2022-11-28 18:42:41 -05:00
if(clone.Live.Freq)
{
const channelKey = clone.Chan == 0 ? "UserL" : "UserR";
const channelVal = Data !== null ? {Stim:clone.Stim, Resp:Data} : undefined;
clone.Live.Mark = clone.Live.Freq[channelKey] = channelVal;
}
}
else if( Name=="Stim" || Name=="Chan" || Name=="Freq")
{
clone[Name] = ApplyLimit(Data, ToneLimit[Name]);
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-11-24 12:16:57 -05:00
}