CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/course/redux.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// standard non-CoCalc libraries
7
import { Map, Set } from "immutable";
8
9
import { AppRedux } from "../app-framework";
10
import { CourseActions } from "./actions";
11
import { create_sync_db } from "./sync";
12
import { AssignmentRecord, CourseStore } from "./store";
13
14
import { SyncDB } from "@cocalc/sync/editor/db/sync";
15
16
const syncdbs: { [name: string]: SyncDB } = {};
17
18
function redux_name(project_id: string, course_filename: string): string {
19
return `editor-${project_id}-${course_filename}`;
20
}
21
export function init_redux(
22
course_filename: string,
23
redux: AppRedux,
24
course_project_id: string,
25
the_redux_name?: string,
26
): string {
27
if (the_redux_name == null) {
28
the_redux_name = redux_name(course_project_id, course_filename);
29
}
30
if (redux.getActions(the_redux_name) != null) {
31
// already initalized
32
return the_redux_name;
33
}
34
35
// DO NOT initialize settings here. They are initialized in sync.ts to prevent a
36
// race condition involving automatic course configuration and settings
37
const initial_store_state: any = {
38
activity: Map<number, string>(),
39
assignments: Map<string, AssignmentRecord>(),
40
configure_projects: "",
41
error: undefined,
42
active_feedback_edits: Map(),
43
handouts: Map(),
44
saving: false,
45
show_save_button: false,
46
students: Map(),
47
tab: "students",
48
unsaved: false,
49
course_filename,
50
course_project_id,
51
expanded_students: Set(), // Set of student id's (string) which should be expanded on render
52
expanded_assignments: Set(), // Set of assignment id's (string) which should be expanded on render
53
expanded_handouts: Set(), // Set of handout id's (string) which should be expanded on render
54
expanded_peer_configs: Set(), // Set of assignment configs (key = assignment_id) which should be expanded on render
55
expanded_skip_gradings: Set(),
56
active_student_sort: { column_name: "last_name", is_descending: false },
57
active_assignment_sort: { column_name: "due_date", is_descending: false },
58
action_all_projects_state: "any",
59
};
60
61
const store: CourseStore = redux.createStore(
62
the_redux_name,
63
CourseStore as any,
64
initial_store_state,
65
) as CourseStore;
66
67
const actions: CourseActions = redux.createActions(
68
the_redux_name,
69
CourseActions,
70
);
71
actions.syncdb = syncdbs[the_redux_name] = create_sync_db(
72
redux,
73
actions,
74
store,
75
course_filename,
76
);
77
78
return the_redux_name;
79
}
80
81
export function remove_redux(
82
course_filename: string,
83
redux: AppRedux,
84
course_project_id: string,
85
the_redux_name?: string,
86
) {
87
if (the_redux_name == null) {
88
the_redux_name = redux_name(course_project_id, course_filename);
89
}
90
91
// Remove the listener for changes in the collaborators on this project.
92
const actions: CourseActions = redux.getActions(the_redux_name);
93
if (actions == null) {
94
// already cleaned up and removed.
95
return;
96
}
97
redux
98
.getStore("projects")
99
.removeListener(
100
"change",
101
actions.handle_projects_store_update.bind(actions),
102
);
103
104
// Remove the store and actions.
105
redux.removeStore(the_redux_name);
106
redux.removeActions(the_redux_name);
107
if (syncdbs[the_redux_name] != null) {
108
syncdbs[the_redux_name].close();
109
}
110
delete syncdbs[the_redux_name];
111
return the_redux_name;
112
}
113
114