[refactor] updated the API
TODO: implement the countdown.store.ts Signed-off-by: s0wlz (Matthias Puchstein) <matthias@puchstein.lu>
This commit is contained in:
@@ -21,8 +21,8 @@ pub fn run() {
|
||||
countdown_delete,
|
||||
countdown_start,
|
||||
countdown_reset,
|
||||
countdown_pause,
|
||||
countdown_resume,
|
||||
countdown_pause,
|
||||
countdown_snapshot,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
|
||||
@@ -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}));
|
||||
}
|
||||
|
||||
14
src/features/countdown/model/countdown.mapper.ts
Normal file
14
src/features/countdown/model/countdown.mapper.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type {CountdownSnapshot, CountdownSnapshotDto} from "./countdown.types";
|
||||
import {type Duration, millisToDuration} from "../../../shared/time/duration";
|
||||
|
||||
export function mapSnapshotDtoToSnapshot(snapshotDto: CountdownSnapshotDto): CountdownSnapshot {
|
||||
const duration: Duration = millisToDuration(snapshotDto.duration);
|
||||
return {
|
||||
id: snapshotDto.id,
|
||||
label: snapshotDto.label,
|
||||
duration: duration,
|
||||
state: snapshotDto.state,
|
||||
start_epoch: snapshotDto.start_epoch_ms !== null ? new Date(snapshotDto.start_epoch_ms) : null,
|
||||
target_epoch: snapshotDto.target_epoch_ms !== null ? new Date(snapshotDto.target_epoch_ms) : null,
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,17 @@
|
||||
import type {Duration} from "../../../shared/time/duration";
|
||||
import type {EmptyPayload} from "../../../shared/payloads/empty";
|
||||
|
||||
export type CountdownState = "Idle" | "Running" | "Paused" | "Finished";
|
||||
|
||||
export type CountdownSnapshotDto = {
|
||||
id: number;
|
||||
label: string;
|
||||
duration: number;
|
||||
state: CountdownState;
|
||||
start_epoch_ms: number | null;
|
||||
target_epoch_ms: number | null;
|
||||
};
|
||||
|
||||
export type CountdownSnapshot = {
|
||||
id: number;
|
||||
label: string;
|
||||
@@ -12,7 +22,22 @@ export type CountdownSnapshot = {
|
||||
}
|
||||
|
||||
export type CountdownCommand =
|
||||
| "countdown_create"
|
||||
| "countdown_delete"
|
||||
| "countdown_list"
|
||||
| "countdown_start"
|
||||
| "countdown_pause"
|
||||
| "countdown_resume"
|
||||
| "countdown_reset";
|
||||
| "countdown_reset"
|
||||
| "countdown_snapshot";
|
||||
|
||||
export type CountdownPayload = EmptyPayload | CountdownIdPayload | CountdownCreatePayload;
|
||||
|
||||
export type CountdownIdPayload = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type CountdownCreatePayload = {
|
||||
label: string;
|
||||
duration: number;
|
||||
}
|
||||
@@ -1,48 +1 @@
|
||||
import {
|
||||
fetchCountdownSnapshot,
|
||||
pauseCountdown,
|
||||
resetCountdown,
|
||||
resumeCountdown,
|
||||
startCountdown,
|
||||
} from "../api/countdown.client";
|
||||
import {createCountdownView} from "../view";
|
||||
|
||||
export async function initCountdownController(container: HTMLElement): Promise<void> {
|
||||
const view = createCountdownView(container);
|
||||
|
||||
const refreshSnapshot = async (): Promise<void> => {
|
||||
try {
|
||||
const snapshot = await fetchCountdownSnapshot();
|
||||
view.setSnapshot(snapshot);
|
||||
} catch (error) {
|
||||
view.setError(`snapshot error: ${String(error)}`);
|
||||
}
|
||||
};
|
||||
|
||||
const runAction = async (action: () => Promise<void>): Promise<void> => {
|
||||
try {
|
||||
await action();
|
||||
await refreshSnapshot();
|
||||
} catch (error) {
|
||||
view.setError(`command error: ${String(error)}`);
|
||||
}
|
||||
};
|
||||
|
||||
view.onStart(() => {
|
||||
void runAction(startCountdown);
|
||||
});
|
||||
view.onPause(() => {
|
||||
void runAction(pauseCountdown);
|
||||
});
|
||||
view.onResume(() => {
|
||||
void runAction(resumeCountdown);
|
||||
});
|
||||
view.onReset(() => {
|
||||
void runAction(resetCountdown);
|
||||
});
|
||||
view.onRefresh(() => {
|
||||
void refreshSnapshot();
|
||||
});
|
||||
|
||||
await refreshSnapshot();
|
||||
}
|
||||
//TODO: Implement countdown store
|
||||
1
src/shared/payloads/empty.ts
Normal file
1
src/shared/payloads/empty.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type EmptyPayload = {}
|
||||
@@ -17,3 +17,7 @@ export function millisToDuration(millis: number): Duration {
|
||||
millis: millis % 1000,
|
||||
}
|
||||
}
|
||||
|
||||
export function durationToMillis(duration: Duration): number {
|
||||
return duration.hours * 3600000 + duration.minutes * 60000 + duration.seconds * 1000 + duration.millis;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user