audiogram-beta/src/store.js

183 lines
5.7 KiB
JavaScript
Raw Normal View History

2022-12-10 10:27:23 -05:00
import React from "react";
2022-12-05 23:22:37 -05:00
2022-12-03 09:36:01 -05:00
const size = 1/6;
2022-12-04 16:19:22 -05:00
/** @type {Array<Store.ColumnMapping>} */
2022-11-27 23:56:37 -05:00
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 ]
];
2022-12-10 09:56:35 -05:00
/** Looks up a frequency in ColumnMapping
* @type {(inFrequency:number)=>Store.ColumnMapping|false} */
2022-11-27 23:56:37 -05:00
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-10 09:56:35 -05:00
/** Creates a new Store.Context object that contain the current selections
* @type {(inState:Store.State, inTest?:Store.Test)=>Store.Context} */
2022-12-10 00:33:38 -05:00
const Reselect =(inState, inTest)=>
2022-11-28 18:42:41 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.Context} */
const output = { Test:inTest??inState.Live.Test };
const column = ColumnMapping[inState.Freq.Value];
2022-12-10 09:56:35 -05:00
if(column && output.Test)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
const hz = column[0];
2022-12-10 09:56:35 -05:00
for(let i=0; i<output.Test.Plot.length; i++)
2022-11-27 23:56:37 -05:00
{
2022-12-10 09:56:35 -05:00
const plot = output.Test.Plot[i];
2022-12-10 00:33:38 -05:00
if(plot.Hz == hz)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
output.Freq = plot;
output.Mark = plot[`User${inState.Chan.Value ? "R" : "L"}`];
2022-11-27 23:56:37 -05:00
}
}
2022-11-28 10:00:18 -05:00
}
2022-12-10 00:33:38 -05:00
return output;
2022-11-28 10:00:18 -05:00
};
2022-12-03 09:36:01 -05:00
2022-12-10 09:56:35 -05:00
/** Creates a new Store.DrawGroup object for the given Test and settings
* @type {(inTest:Store.Test|undefined, inChan:number, inStim:Store.Range, inIsUser:boolean)=>Store.DrawGroup} */
2022-12-10 00:33:38 -05:00
const Redraw =(inTest, inChan, inStim, inIsUser)=>
2022-12-03 09:36:01 -05:00
{
2022-12-04 16:19:22 -05:00
/** @type {Store.DrawGroup} */
2022-12-03 13:32:04 -05:00
const output = {Points:[], Paths:[]};
2022-12-10 00:33:38 -05:00
if(inTest)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
let plot;
for(let i=0; i<inTest.Plot.length; i++)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
plot = inTest.Plot[i];
2022-12-10 09:56:35 -05:00
const mark = plot[`${inIsUser ? "User" : "Test"}${inChan ? "R" : "L"}`]
2022-12-10 00:33:38 -05:00
if(mark)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
const lookup = ColumnLookup(plot.Hz);
if(lookup)
{
/** @type {Store.DrawPoint} */
const point = {
X: lookup[1],
Y: (mark.Stim - inStim.Min)/(inStim.Max - inStim.Min),
Mark: mark
};
output.Points.push(point);
}
2022-12-03 09:36:01 -05:00
}
}
2022-12-10 00:33:38 -05:00
for(let i=1; i<output.Points.length; i++)
2022-12-05 23:22:37 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.DrawLine} */
const line = {Head:output.Points[i-1], Tail:output.Points[i]};
if(line.Head.Mark.Resp && line.Tail.Mark.Resp)
{
output.Paths.push(line);
}
2022-12-05 23:22:37 -05:00
}
}
2022-12-03 13:32:04 -05:00
return output;
2022-12-03 09:36:01 -05:00
}
2022-12-04 16:19:22 -05:00
/** @type {Store.Reducer} */
2022-11-28 10:00:18 -05:00
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")
{
2022-12-10 00:33:38 -05:00
const test = clone.Tests[Data];
clone.Live = Reselect(clone, test);
2022-12-03 19:05:26 -05:00
clone.Draw = {
2022-12-10 00:33:38 -05:00
UserL: Redraw(test, 0, clone.Stim, true ),
UserR: Redraw(test, 1, clone.Stim, true ),
TestL: Redraw(test, 0, clone.Stim, false),
TestR: Redraw(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-10 00:33:38 -05:00
const key = clone.Chan.Value == 0 ? "UserL" : "UserR";
clone.Live.Mark = Data !== null ? {Stim:clone.Stim.Value, Resp:Data} : undefined;
clone.Live.Freq[key] = clone.Live.Mark;
clone.Draw[key] = Redraw(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-10 11:57:45 -05:00
const tone = clone[Name];
2022-12-03 21:40:23 -05:00
tone.Value += Data*tone.Step;
2022-12-10 09:56:35 -05:00
tone.Value = Math.max(tone.Value, tone.Min);
tone.Value = Math.min(tone.Value, tone.Max);
2022-12-10 11:57:45 -05:00
if(Name != "Stim")
{
clone.Live = Reselect(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-05 23:22:37 -05:00
}
2022-12-10 09:56:35 -05:00
/** @type {Store.State} */
export const Initial =
{
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 },
Live:
{
Test: undefined,
Freq: undefined,
Mark: undefined
},
Draw:
{
UserL:{Points:[], Paths:[]},
UserR:{Points:[], Paths:[]},
TestL:{Points:[], Paths:[]},
TestR:{Points:[], Paths:[]}
},
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 } },
{ Hz: 2000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 3000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 4000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 6000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 8000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } }
]
}
]
};
2022-12-05 23:22:37 -05:00
/** @type {preact.Context<Store.Binding>} */
export const Context = React.createContext([Initial, (_a)=>{}]);
2022-12-10 09:56:35 -05:00
2022-12-05 23:22:37 -05:00
/** @type {(props:{children:preact.ComponentChildren})=>preact.VNode} */
export const Provider =(props)=>
{
/** @type {Store.Binding} */
2022-12-10 09:56:35 -05:00
const reducer = React.useReducer(Reducer, Initial, ()=>Reducer(Initial, {Name:"Test", Data:0}));
2022-12-05 23:22:37 -05:00
return React.createElement(Context.Provider, {value:reducer, children:props.children});
2022-12-10 09:56:35 -05:00
};
2022-12-05 23:22:37 -05:00
/** @type {()=>Store.Binding} */
export const Consumer =()=> React.useContext(Context);