Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/stopwatch/register.ts
5899 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Register the time editor -- stopwatch
8
- set the file extension, icon, react component,
9
and how to init and remove the actions/store
10
*/
11
12
import { register_file_editor } from "@cocalc/frontend/project-file";
13
import { redux_name, Store, AppRedux } from "@cocalc/frontend/app-framework";
14
import { alert_message } from "@cocalc/frontend/alerts";
15
import EditorTime from "./editor";
16
import { TimeActions, StopwatchEditorState } from "./actions";
17
18
import { syncdb2 as new_syncdb } from "@cocalc/frontend/frame-editors/generic/client";
19
20
register_file_editor({
21
ext: ["time"],
22
23
icon: "stopwatch",
24
25
component: EditorTime,
26
27
init(path: string, redux: AppRedux, project_id: string): string {
28
const name = redux_name(project_id, path);
29
if (redux.getActions(name) !== undefined) {
30
return name; // already initialized
31
}
32
33
const store: Store<StopwatchEditorState> =
34
redux.createStore<StopwatchEditorState>(name);
35
const actions = redux.createActions(name, TimeActions);
36
37
actions._init(project_id, path);
38
39
const syncdb = new_syncdb({
40
project_id,
41
path,
42
primary_keys: ["id"],
43
string_cols: ["label"],
44
});
45
actions.syncdb = syncdb;
46
actions.store = store;
47
syncdb.once("error", (err) => {
48
const message = `Stopwatch error '${path}' -- ${err}`;
49
alert_message({ type: "error", message });
50
});
51
syncdb.on("change", actions._syncdb_change);
52
return name;
53
},
54
55
remove(path: string, redux: AppRedux, project_id: string): string {
56
const name = redux_name(project_id, path);
57
const actions: InstanceType<typeof TimeActions> = redux.getActions(name);
58
if (actions !== undefined && actions.syncdb !== undefined) {
59
actions.syncdb.close();
60
}
61
const store: Store<StopwatchEditorState> | undefined =
62
redux.getStore<StopwatchEditorState>(name);
63
if (store == undefined) {
64
return name;
65
}
66
// It is *critical* to first unmount the store, then the actions,
67
// or there will be a huge memory leak.
68
redux.removeStore(name);
69
redux.removeActions(name);
70
return name;
71
},
72
});
73
74