-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
147 lines (134 loc) · 4.49 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var jsonParse = require('json-stream')
, through = require('through2')
, vdom = require('virtual-dom')
, h = vdom.h
, AtomicEmitter = require('atomic-emitter')
module.exports = setup
module.exports.consumes = ['ui', 'editor', 'api']
module.exports.provides = ['presence'] // used by cursor broadcast plugin
function setup(plugin, imports, register) {
var ui = imports.ui
, editor = imports.editor
, api = imports.api
ui.reduxReducerMap.presence = reducer
function reducer(state, action) {
if(!state) {
return {
active: false
, users: {}
}
}
if('PRESENCE_ACTIVATE' === action.type) {
return {...state, active: true}
}
if('PRESENCE_DEACTIVATE' === action.type) {
return {...state, active: false, users: {}}
}
if('PRESENCE_LOAD_USER' === action.type) {
return {...state, users: {...state.users, [action.payload.id]: action.payload}}
}
if('PRESENCE_REMOVE_USER' === action.type) {
var newState = {...state, users: {...state.users}}
delete newState.users[action.payload]
return newState
}
return state
}
var presence = {
action_activate: function() {
return {type: 'PRESENCE_ACTIVATE'}
}
, action_deactivate: function() {
return {type: 'PRESENCE_DEACTIVATE'}
}
, action_loadUser: function*(userId) {
var user = yield api.action_user_get(userId)
return yield {type: 'PRESENCE_LOAD_USER', payload: user}
}
, action_removeUser: function(user) {
return {type: 'PRESENCE_REMOVE_USER', payload: user}
}
, onRenderUser: AtomicEmitter()
}
editor.onLoad((editableDoc, broadcast, onClose) => {
ui.store.dispatch(presence.action_activate())
ui.store.dispatch({type: 'PRESENCE_LOAD_USER'
, payload: ui.store.getState().session.user})
presence.stream = broadcast.createDuplexStream(new Buffer('presence'))
presence.stream
.pipe(jsonParse())
.pipe(through.obj(function(list, enc, cb) {
// Update models
var users = Object.keys(list)
users.forEach(function(userId) {
if(ui.store.getState().presence.users[userId]) return
ui.store.dispatch(presence.action_loadUser(userId))
})
// Remove users that left
Object.keys(ui.store.getState().presence.users)
.forEach((userId) => {
if(!~users.indexOf(userId)) ui.store.dispatch(presence.action_removeUser(userId))
})
cb()
}))
// fetch users regularly
var interval = setInterval(function() {
Object.keys(ui.store.getState().presence.users)
.forEach(function(userId) {
ui.store.dispatch(presence.action_loadUser(userId))
})
}, 10000)
onClose(_=> {
clearInterval(interval)
presence.stream = null
ui.store.dispatch(presence.action_deactivate())
})
})
ui.onRenderBody((store, children) => {
if(store.getState().presence.active) children.unshift(render(store))
})
function render(store) {
var state = store.getState().presence
, count = Object.keys(state.users).length
return h('div.Presence', [
h('h5.Presence__Title', [
ui._('plugin-presence/users')({count})+' '
, h('small', ui._('plugin-presence/users-subheading')({count}))
])
, h('ul.Presence__Users.list-unstyled'
, Object.keys(state.users).map(function(userId) {
return renderUser(store, state.users[userId])
}))
])
}
function renderUser(store, user) {
var state = store.getState()
var children = [
h('span.Presence__User__name', user.attributes.name)
, state.session.user.id === user.id? h('small', h('em', ' '+ui._('plugin-presence/you')())) : ''
]
, props = {}
presence.onRenderUser.emit(store, user, props, children)
return h('li.Presence__User'+(state.session.user.id === user.id? '.mark':'')
, props
, children
)
}
register(null, {presence})
}