fix atom chain.

This commit is contained in:
Vixalie 2025-03-21 16:27:49 +08:00
parent 3253b8b98e
commit 65d2e739ef
2 changed files with 13 additions and 19 deletions

View File

@ -30,25 +30,22 @@ export const CurrentPatternAtom = atomWithRefresh<Pattern | null>(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<string | null>(null);
export const SelectedPulseAtom = atom<Pulse | null>((get) => {
const pulses = get(PulsesInCurrentPatternAtom);
export const SelectedPulseAtom = atom<Pulse | null>(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);

View File

@ -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 {