Path: blob/master/src/packages/frontend/editors/stopwatch/register.ts
5899 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Register the time editor -- stopwatch7- set the file extension, icon, react component,8and how to init and remove the actions/store9*/1011import { register_file_editor } from "@cocalc/frontend/project-file";12import { redux_name, Store, AppRedux } from "@cocalc/frontend/app-framework";13import { alert_message } from "@cocalc/frontend/alerts";14import EditorTime from "./editor";15import { TimeActions, StopwatchEditorState } from "./actions";1617import { syncdb2 as new_syncdb } from "@cocalc/frontend/frame-editors/generic/client";1819register_file_editor({20ext: ["time"],2122icon: "stopwatch",2324component: EditorTime,2526init(path: string, redux: AppRedux, project_id: string): string {27const name = redux_name(project_id, path);28if (redux.getActions(name) !== undefined) {29return name; // already initialized30}3132const store: Store<StopwatchEditorState> =33redux.createStore<StopwatchEditorState>(name);34const actions = redux.createActions(name, TimeActions);3536actions._init(project_id, path);3738const syncdb = new_syncdb({39project_id,40path,41primary_keys: ["id"],42string_cols: ["label"],43});44actions.syncdb = syncdb;45actions.store = store;46syncdb.once("error", (err) => {47const message = `Stopwatch error '${path}' -- ${err}`;48alert_message({ type: "error", message });49});50syncdb.on("change", actions._syncdb_change);51return name;52},5354remove(path: string, redux: AppRedux, project_id: string): string {55const name = redux_name(project_id, path);56const actions: InstanceType<typeof TimeActions> = redux.getActions(name);57if (actions !== undefined && actions.syncdb !== undefined) {58actions.syncdb.close();59}60const store: Store<StopwatchEditorState> | undefined =61redux.getStore<StopwatchEditorState>(name);62if (store == undefined) {63return name;64}65// It is *critical* to first unmount the store, then the actions,66// or there will be a huge memory leak.67redux.removeStore(name);68redux.removeActions(name);69return name;70},71});727374