diff --git a/src/context/Patterns.tsx b/src/context/Patterns.tsx index 537414c..62e76d0 100644 --- a/src/context/Patterns.tsx +++ b/src/context/Patterns.tsx @@ -30,25 +30,22 @@ export const CurrentPatternAtom = atomWithRefresh(async (get) => } return null; }); -export const PulsesInCurrentPatternAtom = atomWithRefresh( - (get) => get(CurrentPatternAtom)?.pulses ?? [], -); export const CurrentPatternDuration = atom((get) => { const currentPattern = get(CurrentPatternAtom); if (!currentPattern) return 0; return totalDuration(currentPattern); }); export const SelectedPulseIdAtom = atom(null); -export const SelectedPulseAtom = atom((get) => { - const pulses = get(PulsesInCurrentPatternAtom); +export const SelectedPulseAtom = atom(async (get) => { + const pattern = await get(CurrentPatternAtom); const selectedPulseId = get(SelectedPulseIdAtom); - return pulses.find((pulse) => pulse.id === selectedPulseId) ?? null; + console.debug('[refresh selected pulse]', selectedPulseId, pattern); + return pattern?.pulses?.find((pulse) => pulse.id === selectedPulseId) ?? null; }); export function useSavePattern() { const refreshPatterns = useSetAtom(PatternsAtom); const selectedPatternId = useAtomValue(SelectedPatternIdAtom); - const refreshSelectedPattern = useSetAtom(CurrentPatternAtom); const { showToast } = useNotification(); const savePattern = useCallback( @@ -56,9 +53,6 @@ export function useSavePattern() { try { await invoke('save_pattern', { pattern }); refreshPatterns(); - if (pattern.id === selectedPatternId) { - refreshSelectedPattern(); - } return true; } catch (error) { console.error('[save pattern]', error); diff --git a/src/context/pattern-model.ts b/src/context/pattern-model.ts index e26ef6f..258a1cd 100644 --- a/src/context/pattern-model.ts +++ b/src/context/pattern-model.ts @@ -6,39 +6,39 @@ export enum FrequencyShifting { /** * Change frequency undergoes a linear transformation from previous pulse to current one. */ - Linear, + Linear = 0, /** * Change frequency undergoes a quadratic transformation from previous pulse to current one. */ - Quadratic, + Quadratic = 1, /** * Change frequency undergoes a cubic transformation from previous pulse to current one. */ - Cubic, + Cubic = 2, /** * Change frequency with quick fade in and fade out. */ - Ease, + Ease = 3, /** * Change frequency with spiking within range from previous pulse and current one. */ - Pulsating, + Pulsating = 4, /** * Based on frequency of previous pulse, take the twice of frequency of current pulse as the peak frequency. Follow the maximum frequency limitation. */ - Spiking, + Spiking = 5, /** * Randomize frequency within range from previous frequency and current one. */ - Randomize, + Randomize = 6, /** * Randomize frequency within minium and maximum frequency. */ - Maniac, + Maniac = 7, /** * Synchronize changes of frequency with pulse width changes. */ - Synchronized, + Synchronized = 8, } export interface ControlPoint {