Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/course/redux.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// standard non-CoCalc libraries6import { Map, Set } from "immutable";78import { AppRedux } from "../app-framework";9import { CourseActions } from "./actions";10import { create_sync_db } from "./sync";11import { AssignmentRecord, CourseStore } from "./store";1213import { SyncDB } from "@cocalc/sync/editor/db/sync";1415const syncdbs: { [name: string]: SyncDB } = {};1617function redux_name(project_id: string, course_filename: string): string {18return `editor-${project_id}-${course_filename}`;19}20export function init_redux(21course_filename: string,22redux: AppRedux,23course_project_id: string,24the_redux_name?: string,25): string {26if (the_redux_name == null) {27the_redux_name = redux_name(course_project_id, course_filename);28}29if (redux.getActions(the_redux_name) != null) {30// already initalized31return the_redux_name;32}3334// DO NOT initialize settings here. They are initialized in sync.ts to prevent a35// race condition involving automatic course configuration and settings36const initial_store_state: any = {37activity: Map<number, string>(),38assignments: Map<string, AssignmentRecord>(),39configure_projects: "",40error: undefined,41active_feedback_edits: Map(),42handouts: Map(),43saving: false,44show_save_button: false,45students: Map(),46tab: "students",47unsaved: false,48course_filename,49course_project_id,50expanded_students: Set(), // Set of student id's (string) which should be expanded on render51expanded_assignments: Set(), // Set of assignment id's (string) which should be expanded on render52expanded_handouts: Set(), // Set of handout id's (string) which should be expanded on render53expanded_peer_configs: Set(), // Set of assignment configs (key = assignment_id) which should be expanded on render54expanded_skip_gradings: Set(),55active_student_sort: { column_name: "last_name", is_descending: false },56active_assignment_sort: { column_name: "due_date", is_descending: false },57action_all_projects_state: "any",58};5960const store: CourseStore = redux.createStore(61the_redux_name,62CourseStore as any,63initial_store_state,64) as CourseStore;6566const actions: CourseActions = redux.createActions(67the_redux_name,68CourseActions,69);70actions.syncdb = syncdbs[the_redux_name] = create_sync_db(71redux,72actions,73store,74course_filename,75);7677return the_redux_name;78}7980export function remove_redux(81course_filename: string,82redux: AppRedux,83course_project_id: string,84the_redux_name?: string,85) {86if (the_redux_name == null) {87the_redux_name = redux_name(course_project_id, course_filename);88}8990// Remove the listener for changes in the collaborators on this project.91const actions: CourseActions = redux.getActions(the_redux_name);92if (actions == null) {93// already cleaned up and removed.94return;95}96redux97.getStore("projects")98.removeListener(99"change",100actions.handle_projects_store_update.bind(actions),101);102103// Remove the store and actions.104redux.removeStore(the_redux_name);105redux.removeActions(the_redux_name);106if (syncdbs[the_redux_name] != null) {107syncdbs[the_redux_name].close();108}109delete syncdbs[the_redux_name];110return the_redux_name;111}112113114