Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39598
1
# 3rd Party Libraries
2
3
# Internal Libraries
4
{React, rtypes} = require('../smc-react')
5
{webapp_client} = require('../webapp_client')
6
7
# Sibling Libraries
8
util = require('./util')
9
{get_store_def} = require('./store')
10
{ChatActions} = require('./actions')
11
{ChatRoom} = require('../smc_chat')
12
13
exports.init = init = (path, redux, project_id) ->
14
name = util.generate_name(project_id, path)
15
if redux.getActions(name)?
16
return name # already initialized
17
18
actions = redux.createActions(name, ChatActions)
19
store = redux.createStore(get_store_def(name))
20
21
syncdb = webapp_client.sync_db
22
project_id : project_id
23
path : path
24
primary_keys : ['date']
25
syncdb.once 'init', (err) =>
26
if err
27
mesg = "Error opening '#{path}' -- #{err}"
28
console.warn(mesg)
29
alert_message(type:"error", message:mesg)
30
return
31
actions.syncdb = syncdb
32
actions.store = store
33
actions.init_from_syncdb()
34
syncdb.on('change', actions._syncdb_change)
35
return name
36
37
exports.remove = remove = (path, redux, project_id) ->
38
name = util.generate_name(project_id, path)
39
actions = redux.getActions(name)
40
actions?.syncdb?.close()
41
store = redux.getStore(name)
42
if not store?
43
return
44
delete store.state
45
# It is *critical* to first unmount the store, then the actions,
46
# or there will be a huge memory leak.
47
redux.removeStore(name)
48
redux.removeActions(name)
49
return name
50
51
ChatEditorGenerator = (path, redux, project_id) ->
52
name = util.generate_name(project_id, path)
53
C_ChatRoom = ({actions}) ->
54
<ChatRoom
55
redux = {redux}
56
path = {path}
57
name = {name}
58
actions = {actions}
59
project_id = {project_id}
60
/>
61
62
C_ChatRoom.propTypes =
63
actions : rtypes.object.isRequired
64
65
return C_ChatRoom
66
67
require('../project_file').register_file_editor
68
ext : 'sage-chat'
69
icon : 'comment'
70
init : init
71
generator : ChatEditorGenerator
72
remove : remove
73
74