[refactor] updated the API
TODO: implement the countdown.store.ts Signed-off-by: s0wlz (Matthias Puchstein) <matthias@puchstein.lu>
This commit is contained in:
@@ -1,45 +1,52 @@
|
||||
import {invokeCommand} from "../../../shared/tauri/invoke";
|
||||
import type {CountdownCommand, CountdownSnapshot, CountdownState} from "../model/countdown.types";
|
||||
import {type Duration, millisToDuration} from "../../../shared/time/duration";
|
||||
import type {
|
||||
CountdownCommand,
|
||||
CountdownPayload,
|
||||
CountdownSnapshot,
|
||||
CountdownSnapshotDto
|
||||
} from "../model/countdown.types";
|
||||
import {mapSnapshotDtoToSnapshot} from "../model/countdown.mapper";
|
||||
import {type Duration, durationToMillis} from "../../../shared/time/duration";
|
||||
|
||||
type CountdownSnapshotDto = {
|
||||
id: number;
|
||||
label: string;
|
||||
duration: number;
|
||||
state: CountdownState;
|
||||
start_epoch_ms: number | null;
|
||||
target_epoch_ms: number | null;
|
||||
};
|
||||
|
||||
export async function fetchCountdownSnapshot(): Promise<CountdownSnapshot> {
|
||||
let temp = await invokeCommand<CountdownSnapshotDto>("countdown_snapshot");
|
||||
const duration: Duration = millisToDuration(temp.duration);
|
||||
return {
|
||||
id: temp.id,
|
||||
label: temp.label,
|
||||
duration: duration,
|
||||
state: temp.state,
|
||||
start_epoch: temp.start_epoch_ms !== null ? new Date(temp.start_epoch_ms) : null,
|
||||
target_epoch: temp.target_epoch_ms !== null ? new Date(temp.target_epoch_ms) : null,
|
||||
};
|
||||
async function invokeCountdownCommand(command: CountdownCommand, payload: CountdownPayload): Promise<void> {
|
||||
await invokeCommand<void>(command, payload);
|
||||
}
|
||||
|
||||
async function invokeCountdownCommand(command: CountdownCommand): Promise<void> {
|
||||
await invokeCommand<void>(command);
|
||||
export async function createCountdown(label: string, duration: Duration): Promise<number> {
|
||||
return await invokeCommand("countdown_create", {label, duration: durationToMillis(duration)});
|
||||
}
|
||||
|
||||
export async function startCountdown(): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_start");
|
||||
export async function listCountdowns(): Promise<CountdownSnapshot[]> {
|
||||
let snapshotsDto = await invokeCommand<CountdownSnapshotDto[]>("countdown_list", {});
|
||||
let snapshots: CountdownSnapshot[] = [];
|
||||
let snapshot: CountdownSnapshotDto;
|
||||
for (snapshot of snapshotsDto) {
|
||||
snapshots.push(mapSnapshotDtoToSnapshot(snapshot));
|
||||
}
|
||||
return snapshots;
|
||||
}
|
||||
|
||||
export async function pauseCountdown(): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_pause");
|
||||
export async function deleteCountdown(id: number): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_delete", {id});
|
||||
}
|
||||
|
||||
export async function resumeCountdown(): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_resume");
|
||||
export async function startCountdown(id: number): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_start", {id});
|
||||
}
|
||||
|
||||
export async function resetCountdown(): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_reset");
|
||||
export async function resumeCountdown(id: number): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_resume", {id});
|
||||
}
|
||||
|
||||
export async function pauseCountdown(id: number): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_pause", {id});
|
||||
}
|
||||
|
||||
export async function resetCountdown(id: number): Promise<void> {
|
||||
await invokeCountdownCommand("countdown_reset", {id});
|
||||
}
|
||||
|
||||
export async function fetchCountdownSnapshot(id: number): Promise<CountdownSnapshot> {
|
||||
return mapSnapshotDtoToSnapshot(await invokeCommand<CountdownSnapshotDto>("countdown_snapshot", {id}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user