Path: blob/master/src/packages/frontend/editors/stopwatch/actions.ts
1691 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6The actions -- what you can do with a timer, and also the7underlying synchronized state.8*/910import { history_path } from "@cocalc/util/misc";11import { webapp_client } from "@cocalc/frontend/webapp-client";12import { Actions, Store, TypedMap } from "@cocalc/frontend/app-framework";13import { List } from "immutable";1415export interface StopwatchEditorState {16name: string;17timers?: List<TimerRecord>;18error?: string;19}2021export type TimerState = "paused" | "running" | "stopped";2223interface Timer {24id: number;25label?: string;26total?: number;27state: TimerState;28time: number;29countdown?: number; // a countdown time is the same a stopwatch, but has this field, which is the number of seconds.30}3132type TimerRecord = TypedMap<Timer>;3334export class TimeActions extends Actions<StopwatchEditorState> {35private project_id: string;36private path: string;37public syncdb: any;38public store: Store<StopwatchEditorState>;3940public _init(project_id: string, path: string): void {41this._syncdb_change = this._syncdb_change.bind(this);42this.project_id = project_id;43this.path = path;44// be explicit about exactly what state is in the store45this.setState({46timers: undefined,47});48}4950public init_error(err): void {51this.setState({52error: err,53});54}5556public _syncdb_change(): void {57this.setState({58timers: this.syncdb.get(),59});6061if (this.syncdb.get_doc().size === 0) {62this.addStopwatch();63}64}6566private _set(obj: Partial<Timer>): void {67this.syncdb.set(obj);68this.syncdb.commit();69this.syncdb.save_to_disk();70}7172public addStopwatch(): void {73// make id equal to the largest current id (or 0 if none)74let id = 0;75this.syncdb.get().map((data) => {76id = Math.max(data.get("id"), id);77});78id += 1; // our new stopwatch has the largest id (so at the bottom)79this._set({80id,81label: "",82total: 0,83state: "stopped",84time: webapp_client.server_time() - 0,85});86}8788public deleteStopwatch(id: number): void {89this.syncdb.delete({ id });90if (this.syncdb.get_doc().size === 0) {91this.addStopwatch();92}93this.syncdb.commit();94this.syncdb.save_to_disk();95}9697public resetStopwatch(id: number): void {98this._set({99id,100total: 0,101state: "stopped",102time: webapp_client.server_time() - 0,103});104}105106public startStopwatch(id: number): void {107this._set({108id,109time: webapp_client.server_time() - 0,110state: "running",111});112}113114public pauseStopwatch(id: number): void {115const x = this.syncdb && this.syncdb.get_one({ id });116if (x == null) return;117this._set({118id,119time: webapp_client.server_time() - 0,120total: x.get("total") + (webapp_client.server_time() - x.get("time")),121state: "paused",122});123}124125public setLabel(id: number, label: string): void {126const x = this.syncdb && this.syncdb.get_one({ id });127if (x == null) return;128this._set({ id, label, state: x.get("state"), time: x.get("time") });129}130131// Set current displayed time on clock or starting value for timer (and reset timer to be at that value),132// where the input is the time in seconds.133public setCountdown(id: number, time: number): void {134const x = this.syncdb && this.syncdb.get_one({ id });135if (x == null) return;136this._set({137id,138countdown: time,139});140}141142public time_travel(): void {143this.redux.getProjectActions(this.project_id).open_file({144path: history_path(this.path),145foreground: true,146});147}148149public undo(): void {150if (this.syncdb) {151this.syncdb.undo();152}153}154155public redo(): void {156if (this.syncdb) {157this.syncdb.redo();158}159}160161public addTimer(): void {162// make id equal to the largest current id (or 0 if none)163let id = 0;164this.syncdb.get().map((data) => {165id = Math.max(data.get("id"), id);166});167id += 1; // our new timer has the largest id (so at the bottom)168this._set({169id,170label: "",171total: 0,172state: "stopped",173time: webapp_client.server_time() - 0,174countdown: 60,175});176}177}178179180