immutable = require('immutable')
misc = require('smc-util/misc')
{defaults, required} = misc
schema = require('smc-util/schema')
{webapp_client} = require('../webapp_client')
exports.create_sync_db = (redux, actions, store) =>
return if not redux? or not actions? or not store?
syncdb = webapp_client.sync_db
project_id : store.get('course_project_id')
path : store.get('course_filename')
primary_keys : ['table', 'handout_id', 'student_id', 'assignment_id']
string_cols : ['note', 'description', 'title', 'email_invite']
change_throttle : 500
save_interval : 3000
syncdb.once 'init', (err) =>
if err
actions?.set_error(err)
console.warn("Error opening '#{store.course_filename}' -- #{err}")
return
i = store.get('course_filename').lastIndexOf('.')
t =
settings :
title : store.get('course_filename').slice(0,i)
description : 'No description'
assignments : {}
students : {}
handouts : {}
for x in syncdb.get().toJS()
if x.table == 'settings'
misc.merge(t.settings, misc.copy_without(x, 'table'))
else if x.table == 'students'
t.students[x.student_id] = misc.copy_without(x, 'table')
else if x.table == 'assignments'
t.assignments[x.assignment_id] = misc.copy_without(x, 'table')
else if x.table == 'handouts'
t.handouts[x.handout_id] = misc.copy_without(x, 'table')
for k, v of t
t[k] = immutable.fromJS(v)
actions?.setState(t)
syncdb.on('change', (changes) -> actions?._syncdb_change(changes))
syncdb.on('sync', => redux.getProjectActions(@project_id).flag_file_activity(@filename))
projects_store = redux.getStore('projects')
projects_store.wait
until : (p_store) -> p_store.get_users(store.get('course_project_id'))?
timeout : 30
cb : ->
actions = actions
if not actions?
return
actions.lookup_nonregistered_students()
actions.configure_all_projects()
projects_store.on 'change', actions.handle_projects_store_update
actions.handle_projects_store_update(projects_store)
return syncdb