make it all work

This commit is contained in:
Connor Turland 2016-10-18 08:04:23 -04:00
parent 9ae28866d9
commit a0efb2cac8
6 changed files with 302 additions and 326 deletions

View file

@ -63,7 +63,7 @@ _Backbone.Map = Backbone.Model.extend({
this.on('saved', this.savedEvent)
},
savedEvent: function () {
Realtime.sendMapChange(this)
Realtime.updateMap(this)
},
authorizeToEdit: function (mapper) {
if (mapper && (
@ -370,7 +370,7 @@ _Backbone.init = function () {
return node
},
savedEvent: function () {
Realtime.sendTopicChange(this)
Realtime.updateTopic(this)
},
updateViews: function () {
var onPageWithTopicCard = Active.Map || Active.Topic
@ -549,7 +549,7 @@ _Backbone.init = function () {
return edge
},
savedEvent: function () {
Realtime.sendSynapseChange(this)
Realtime.updateSynapse(this)
},
updateViews: function () {
this.updateCardView()

View file

@ -10,7 +10,6 @@ export const INVITE_TO_JOIN = 'INVITE_TO_JOIN'
export const INVITE_A_CALL = 'INVITE_A_CALL'
export const JOIN_CALL = 'JOIN_CALL'
export const LEAVE_CALL = 'LEAVE_CALL'
export const REQUEST_MAPPER_INFO = 'REQUEST_MAPPER_INFO'
export const SEND_MAPPER_INFO = 'SEND_MAPPER_INFO'
export const SEND_COORDS = 'SEND_COORDS'
export const CREATE_MESSAGE = 'CREATE_MESSAGE'
@ -53,4 +52,3 @@ export const MAP_UPDATED = 'MAP_UPDATED'
export const LIVE_MAPS_RECEIVED = 'LIVE_MAPS_RECEIVED'
export const MAP_WENT_LIVE = 'MAP_WENT_LIVE'
export const MAP_CEASED_LIVE = 'MAP_CEASED_LIVE'

View file

@ -12,6 +12,7 @@
* - Metamaps.Synapses
* - Metamaps.Topics
*/
import _ from 'lodash'
import SimpleWebRTC from 'simplewebrtc'
@ -96,7 +97,6 @@ import {
inviteACall,
joinCall,
leaveCall,
requestMapperInfo,
sendMapperInfo,
sendCoords,
dragTopic,
@ -111,10 +111,6 @@ import {
updateMap
} from './sendable'
var socketReference = null
export const getSocket = () => socketReference
const Realtime = {
videoId: 'video-wrapper',
socket: null,
@ -133,10 +129,13 @@ const Realtime = {
self.addJuntoListeners()
self.socket = new SocketIoConnection({ url: Metamaps.Erb['REALTIME_SERVER']})
socketReference = self.socket
setupSendables(self)
self.socket.on('connect', function () {
console.log('connected')
subscribeToEvents(self.socket)
subscribeToEvents(self, self.socket)
if (!self.disconnected) {
self.startActiveMap()
} else self.disconnected = false
@ -233,7 +232,7 @@ const Realtime = {
if (Active.Map.authorizeToEdit(Active.Mapper)) {
self.turnOn()
self.setupSocket()
self.setupSendables()
self.setupLocalSendables()
}
self.room.addMessages(new Metamaps.Backbone.MessageCollection(Metamaps.Messages), true)
}
@ -273,9 +272,9 @@ const Realtime = {
var self = Realtime
// subscribe to rooms on the websocket?
self.checkForCall()
self.newMapperNotify()
self.joinMap()
},
setupSendables: function () {
setupLocalSendables: function () {
var self = Realtime
// local event listeners that trigger events
@ -496,8 +495,11 @@ const Realtime = {
yLimit = Math.min(yLimit, yMax - compassDiameter)
return {x: xLimit,y: yLimit}
},
requestLiveMaps,
}
}
const setupSendables = Realtime => {
[requestLiveMaps,
joinMap,
leaveMap,
checkForCall,
@ -508,7 +510,6 @@ const Realtime = {
inviteACall,
joinCall,
leaveCall,
requestMapperInfo,
sendMapperInfo,
sendCoords,
dragTopic,
@ -520,67 +521,37 @@ const Realtime = {
updateSynapse,
removeSynapse,
deleteSynapse,
updateMap
updateMap].forEach(sendable => Realtime[sendable.name] = sendable(Realtime, Realtime.socket))
}
const subscribeToEvents = socket => {
socket.on(INVITED_TO_CALL, invitedToCall)
socket.on(INVITED_TO_JOIN, invitedToJoin)
socket.on(CALL_ACCEPTED, callAccepted)
socket.on(CALL_DENIED, callDenied)
socket.on(INVITE_DENIED, inviteDenied)
socket.on(CALL_IN_PROGRESS, callInProgress)
socket.on(CALL_STARTED, callStarted)
socket.on(MAPPER_JOINED_CALL, mapperJoinedCall)
socket.on(MAPPER_LEFT_CALL, mapperLeftCall)
socket.on(MAPPER_LIST_UPDATED, mapperListUpdated)
socket.on(PEER_COORDS_UPDATED, peerCoordsUpdated)
socket.on(NEW_MAPPER, newMapper)
socket.on(LOST_MAPPER, lostMapper)
socket.on(MESSAGE_CREATED, messageCreated)
socket.on(TOPIC_DRAGGED, topicDragged)
socket.on(TOPIC_CREATED, topicCreated)
socket.on(TOPIC_UPDATED, topicUpdated)
socket.on(TOPIC_REMOVED, topicRemoved)
socket.on(TOPIC_DELETED, topicDeleted)
socket.on(SYNAPSE_CREATED, synapseCreated)
socket.on(SYNAPSE_UPDATED, synapseUpdated)
socket.on(SYNAPSE_REMOVED, synapseRemoved)
socket.on(SYNAPSE_DELETED, synapseDeleted)
socket.on(MAP_UPDATED, mapUpdated)
socket.on(LIVE_MAPS_RECEIVED, liveMapsReceived)
socket.on(MAP_WENT_LIVE, mapWentLive)
socket.on(MAP_CEASED_LIVE, mapCeasedLive)
}
const unsub = socket => {
socket.off(INVITED_TO_CALL)
socket.off(INVITED_TO_JOIN)
socket.off(CALL_ACCEPTED)
socket.off(CALL_DENIED)
socket.off(INVITE_DENIED)
socket.off(CALL_IN_PROGRESS)
socket.off(CALL_STARTED)
socket.off(MAPPER_JOINED_CALL)
socket.off(MAPPER_LEFT_CALL)
socket.off(MAPPER_LIST_UPDATED)
socket.off(PEER_COORDS_UPDATED)
socket.off(NEW_MAPPER)
socket.off(LOST_MAPPER)
socket.off(MESSAGE_CREATED)
socket.off(TOPIC_DRAGGED)
socket.off(TOPIC_CREATED)
socket.off(TOPIC_UPDATED)
socket.off(TOPIC_REMOVED)
socket.off(TOPIC_DELETED)
socket.off(SYNAPSE_CREATED)
socket.off(SYNAPSE_UPDATED)
socket.off(SYNAPSE_REMOVED)
socket.off(SYNAPSE_DELETED)
socket.off(MAP_UPDATED)
socket.off(LIVE_MAPS_RECEIVED)
socket.off(MAP_WENT_LIVE)
socket.off(MAP_CEASED_LIVE)
const subscribeToEvents = (Realtime, socket) => {
socket.on(INVITED_TO_CALL, invitedToCall(Realtime))
socket.on(INVITED_TO_JOIN, invitedToJoin(Realtime))
socket.on(CALL_ACCEPTED, callAccepted(Realtime))
socket.on(CALL_DENIED, callDenied(Realtime))
socket.on(INVITE_DENIED, inviteDenied(Realtime))
socket.on(CALL_IN_PROGRESS, callInProgress(Realtime))
socket.on(CALL_STARTED, callStarted(Realtime))
socket.on(MAPPER_JOINED_CALL, mapperJoinedCall(Realtime))
socket.on(MAPPER_LEFT_CALL, mapperLeftCall(Realtime))
socket.on(MAPPER_LIST_UPDATED, mapperListUpdated(Realtime))
socket.on(PEER_COORDS_UPDATED, peerCoordsUpdated(Realtime))
socket.on(NEW_MAPPER, newMapper(Realtime))
socket.on(LOST_MAPPER, lostMapper(Realtime))
socket.on(MESSAGE_CREATED, messageCreated(Realtime))
socket.on(TOPIC_DRAGGED, topicDragged(Realtime))
socket.on(TOPIC_CREATED, topicCreated(Realtime))
socket.on(TOPIC_UPDATED, topicUpdated(Realtime))
socket.on(TOPIC_REMOVED, topicRemoved(Realtime))
socket.on(TOPIC_DELETED, topicDeleted(Realtime))
socket.on(SYNAPSE_CREATED, synapseCreated(Realtime))
socket.on(SYNAPSE_UPDATED, synapseUpdated(Realtime))
socket.on(SYNAPSE_REMOVED, synapseRemoved(Realtime))
socket.on(SYNAPSE_DELETED, synapseDeleted(Realtime))
socket.on(MAP_UPDATED, mapUpdated(Realtime))
socket.on(LIVE_MAPS_RECEIVED, liveMapsReceived(Realtime))
socket.on(MAP_WENT_LIVE, mapWentLive(Realtime))
socket.on(MAP_CEASED_LIVE, mapCeasedLive(Realtime))
}
export default Realtime

View file

@ -7,10 +7,12 @@ import GlobalUI from '../GlobalUI'
import Control from '../Control'
import Map from '../Map'
import Mapper from '../Mapper'
import Realtime from './index'
var self = Realtime
import Topic from '../Topic'
import Synapse from '../Synapse'
import Util from '../Util'
import Visualize from '../Visualize'
export const synapseRemoved = data => {
export const synapseRemoved = self => data => {
var synapse = Metamaps.Synapses.get(data.mappableid)
if (synapse) {
var edge = synapse.get('edge')
@ -30,11 +32,11 @@ export const synapseRemoved = data => {
}
}
export const synapseDeleted = data => {
export const synapseDeleted = self => data => {
self.synapseRemoved(data)
}
export const synapseCreated = function (data) {
export const synapseCreated = self => data => {
var topic1, topic2, node1, node2, synapse, mapping, cancel, mapper
@ -82,7 +84,7 @@ export const synapseCreated = function (data) {
waitThenRenderSynapse()
}
export const topicRemoved = function (data) {
export const topicRemoved = self => data => {
var topic = Metamaps.Topics.get(data.mappableid)
if (topic) {
var node = topic.get('node')
@ -93,11 +95,11 @@ export const topicRemoved = function (data) {
}
}
export const topicDeleted = function (data) {
export const topicDeleted = self => data => {
self.topicRemoved(data)
}
export const topicCreated = function (data) {
export const topicCreated = self => data => {
var topic, mapping, mapper, cancel
function waitThenRenderTopic () {
@ -140,11 +142,11 @@ export const topicCreated = function (data) {
waitThenRenderTopic()
}
export const messageCreated = function (data) {
export const messageCreated = self => data => {
self.room.addMessages(new Metamaps.Backbone.MessageCollection(data))
}
export const mapUpdated = function (data) {
export const mapUpdated = self => data => {
var map = Active.Map
var isActiveMap = map && data.mapId === map.id
if (isActiveMap) {
@ -170,7 +172,7 @@ export const mapUpdated = function (data) {
}
}
export const topicUpdated = function (data) {
export const topicUpdated = self => data => {
var topic = Metamaps.Topics.get(data.topicId)
if (topic) {
var node = topic.get('node')
@ -183,7 +185,7 @@ export const topicUpdated = function (data) {
}
}
export const synapseUpdated = function (data) {
export const synapseUpdated = self => data => {
var synapse = Metamaps.Synapses.get(data.synapseId)
if (synapse) {
// edge reset necessary because fetch causes model reset
@ -197,7 +199,7 @@ export const synapseUpdated = function (data) {
}
}
export const topicDragged = function (positions) {
export const topicDragged = self => positions => {
var topic
var node
@ -211,13 +213,13 @@ export const topicDragged = function (positions) {
}
}
export const peerCoordsUpdated = function (data) {
export const peerCoordsUpdated = self => data => {
if (!self.mappersOnMap[data.userid]) return
self.mappersOnMap[data.userid].coords = {x: data.usercoords.x,y: data.usercoords.y}
self.positionPeerIcon(data.userid)
}
export const lostMapper = function (data) {
export const lostMapper = self => data => {
// data.userid
// data.username
delete self.mappersOnMap[data.userid]
@ -234,7 +236,7 @@ export const lostMapper = function (data) {
}
}
export const mapperListUpdated = function (data) {
export const mapperListUpdated = self => data => {
// data.userid
// data.username
// data.userimage
@ -261,7 +263,7 @@ export const mapperListUpdated = function (data) {
}
}
export const newMapper = function (data) {
export const newMapper = self => data => {
// data.userid
// data.username
// data.userimage
@ -298,26 +300,26 @@ export const newMapper = function (data) {
}
}
export const callAccepted = function (userid) {
export const callAccepted = self => userid => {
var username = self.mappersOnMap[userid].name
GlobalUI.notifyUser('Conversation starting...')
self.joinCall()
self.room.chat.invitationAnswered(userid)
}
export const callDenied = function (userid) {
export const callDenied = self => userid => {
var username = self.mappersOnMap[userid].name
GlobalUI.notifyUser(username + " didn't accept your invitation")
self.room.chat.invitationAnswered(userid)
}
export const inviteDenied = function (userid) {
export const inviteDenied = self => userid => {
var username = self.mappersOnMap[userid].name
GlobalUI.notifyUser(username + " didn't accept your invitation")
self.room.chat.invitationAnswered(userid)
}
export const invitedToCall = function (inviter) {
export const invitedToCall = self => inviter => {
self.room.chat.sound.stop(self.soundId)
self.soundId = self.room.chat.sound.play('sessioninvite')
@ -329,7 +331,7 @@ export const invitedToCall = function (inviter) {
GlobalUI.notifyUser(notifyText, true)
}
export const invitedToJoin = function (inviter) {
export const invitedToJoin = self => inviter => {
self.room.chat.sound.stop(self.soundId)
self.soundId = self.room.chat.sound.play('sessioninvite')
@ -340,7 +342,7 @@ export const invitedToJoin = function (inviter) {
GlobalUI.notifyUser(notifyText, true)
}
export const mapperJoinedCall = function (id) {
export const mapperJoinedCall = self => id => {
var mapper = self.mappersOnMap[id]
if (mapper) {
@ -355,7 +357,7 @@ export const mapperJoinedCall = function (id) {
}
}
export const mapperLeftCall = function (id) {
export const mapperLeftCall = self => id => {
var mapper = self.mappersOnMap[id]
if (mapper) {
if (self.inConversation) {
@ -372,7 +374,7 @@ export const mapperLeftCall = function (id) {
}
}
export const callInProgress = function () {
export const callInProgress = self => () => {
var notifyText = "There's a conversation happening, want to join?"
notifyText += ' <button type="button" class="toast-button button" onclick="Metamaps.Realtime.joinCall()">Yes</button>'
notifyText += ' <button type="button" class="toast-button button btn-no" onclick="Metamaps.GlobalUI.clearNotify()">No</button>'
@ -380,7 +382,7 @@ export const callInProgress = function () {
self.room.conversationInProgress()
}
export const callStarted = function () {
export const callStarted = self => () => {
if (self.inConversation) return
var notifyText = "There's a conversation starting, want to join?"
notifyText += ' <button type="button" class="toast-button button" onclick="Metamaps.Realtime.joinCall()">Yes</button>'
@ -389,3 +391,6 @@ export const callStarted = function () {
self.room.conversationInProgress()
}
export const liveMapsReceived = self => () => {}
export const mapWentLive = self => () => {}
export const mapCeasedLive = self => () => {}

View file

@ -13,7 +13,6 @@ import {
INVITE_A_CALL,
JOIN_CALL,
LEAVE_CALL,
REQUEST_MAPPER_INFO,
SEND_MAPPER_INFO,
SEND_COORDS,
CREATE_MESSAGE,
@ -29,16 +28,12 @@ import {
UPDATE_MAP
} from './events'
export const requestLiveMaps = function (self, socket) {
export const requestLiveMaps = (self, socket) => () => {
socket.emit(REQUEST_LIVE_MAPS)
}
export const checkForCall = function (self, socket) {
socket.emit(CHECK_FOR_CALL, { room: self.room.room, mapid: Active.Map.id })
}
export const newMapperNotify = function (self, socket) {
socket.emit(NEW_MAPPER_NOTIFY, {
export const joinMap = (self, socket) => () => {
socket.emit(JOIN_MAP, {
userid: Active.Mapper.id,
username: Active.Mapper.get('name'),
userimage: Active.Mapper.get('image'),
@ -47,7 +42,15 @@ export const newMapperNotify = function (self, socket) {
})
}
export const sendMapperInfo = (userid, self, socket) => {
export const leaveMap = (self, socket) => () => {
socket.emit(LEAVE_MAP)
}
export const checkForCall = (self, socket) => () => {
socket.emit(CHECK_FOR_CALL, { room: self.room.room, mapid: Active.Map.id })
}
export const sendMapperInfo = (self, socket) => userid => {
// send this new mapper back your details, and the awareness that you've loaded the map
var update = {
userToNotify: userid,
@ -60,7 +63,7 @@ export const sendMapperInfo = (userid, self, socket) => {
socket.emit(SEND_MAPPER_INFO, update)
}
export const joinCall = function (self, socket) {
export const joinCall = (self, socket) => () => {
self.webrtc.off('readyToCall')
self.webrtc.once('readyToCall', function () {
self.videoInitialized = true
@ -83,7 +86,7 @@ export const joinCall = function (self, socket) {
self.room.chat.mapperJoinedCall(Active.Mapper.id)
}
export const leaveCall = function (self, socket) {
export const leaveCall = (self, socket) => () => {
socket.emit(LEAVE_CALL, {
mapid: Active.Map.id,
id: Active.Mapper.id
@ -102,7 +105,6 @@ export const leaveCall = function (self, socket) {
}
export const acceptCall = (self, socket) => userid => {
var self = Realtime
self.room.chat.sound.stop(self.soundId)
socket.emit(ACCEPT_CALL, {
mapid: Active.Map.id,
@ -114,7 +116,7 @@ export const acceptCall = (self, socket) => userid => {
GlobalUI.clearNotify()
}
export const denyCall = (userid) => {
export const denyCall = (self, socket) => userid => {
self.room.chat.sound.stop(self.soundId)
socket.emit(DENY_CALL, {
mapid: Active.Map.id,
@ -124,7 +126,7 @@ export const denyCall = (userid) => {
GlobalUI.clearNotify()
}
export const denyInvite = (userid) => {
export const denyInvite = (self, socket) => userid => {
self.room.chat.sound.stop(self.soundId)
socket.emit(DENY_INVITE, {
mapid: Active.Map.id,
@ -134,7 +136,7 @@ export const denyInvite = (userid) => {
GlobalUI.clearNotify()
}
export const inviteACall = function (userid) {
export const inviteACall = (self, socket) => userid => {
socket.emit(INVITE_A_CALL, {
mapid: Active.Map.id,
inviter: Active.Mapper.id,
@ -144,7 +146,7 @@ export const inviteACall = function (userid) {
GlobalUI.clearNotify()
}
export const inviteToJoin = function (userid) {
export const inviteToJoin = (self, socket) => userid => {
socket.emit(INVITE_TO_JOIN, {
mapid: Active.Map.id,
inviter: Active.Mapper.id,
@ -153,7 +155,7 @@ export const inviteToJoin = function (userid) {
self.room.chat.invitationPending(userid)
}
export const sendCoords = function (coords) {
export const sendCoords = (self, socket) => coords => {
var map = Active.Map
var mapper = Active.Mapper
if (map.authorizeToEdit(mapper)) {
@ -166,41 +168,41 @@ export const sendCoords = function (coords) {
}
}
export const dragTopic = function (positions) {
export const dragTopic = (self, socket) => positions => {
if (Active.Map) {
positions.mapid = Active.Map.id
socket.emit(DRAG_TOPIC, positions)
}
}
export const updateTopic = function (topic) {
export const updateTopic = (self, socket) => topic => {
var data = {
topicId: topic.id
}
socket.emit(UPDATE_TOPIC, data)
}
export const updateSynapse = function (synapse) {
export const updateSynapse = (self, socket) => synapse => {
var data = {
synapseId: synapse.id
}
socket.emit(UPDATE_SYNAPSE, data)
}
export const updateMap = function (map) {
export const updateMap = (self, socket) => map => {
var data = {
mapId: map.id
}
socket.emit(UPDATE_MAP, data)
}
export const createMessage = function (data) {
export const createMessage = (self, socket) => data => {
var message = data.attributes
message.mapid = Active.Map.id
socket.emit(CREATE_MESSAGE, message)
}
export const createTopic = function (data) {
export const createTopic = (self, socket) => data => {
if (Active.Map) {
data.mapperid = Active.Mapper.id
data.mapid = Active.Map.id
@ -208,20 +210,20 @@ export const createTopic = function (data) {
}
}
export const deleteTopic = function (data) {
export const deleteTopic = (self, socket) => data => {
if (Active.Map) {
socket.emit(DELETE_TOPIC, data)
}
}
export const removeTopic = function (data) {
export const removeTopic = (self, socket) => data => {
if (Active.Map) {
data.mapid = Active.Map.id
socket.emit(REMOVE_TOPIC, data)
}
}
export const createSynapse = function (data) {
export const createSynapse = (self, socket) => data => {
if (Active.Map) {
data.mapperid = Active.Mapper.id
data.mapid = Active.Map.id
@ -229,14 +231,14 @@ export const createSynapse = function (data) {
}
}
export const deleteSynapse = function (data) {
export const deleteSynapse = (self, socket) => data => {
if (Active.Map) {
data.mapid = Active.Map.id
socket.emit(DELETE_SYNAPSE, data)
}
}
export const removeSynapse = function (data) {
export const removeSynapse = (self, socket) => data => {
if (Active.Map) {
data.mapid = Active.Map.id
socket.emit(REMOVE_SYNAPSE, data)

View file

@ -134,7 +134,7 @@ function start() {
})
// this will ping everyone on a map that there's a person just joined the map
socket.on(NEW_MAPPER, function (data) {
socket.on(JOIN_MAP, function (data) {
if (!livemaps[data.mapid]) {
livemaps[data.mapid] = data.map // { name: '', desc: '', numTopics: '' }
@ -186,7 +186,7 @@ function start() {
}
// this will ping everyone on a map that there's a person just left the map
socket.on('disconnect', end)
socket.on('endMapperNotify', end)
socket.on(LEAVE_MAP, end)
socket.on(SEND_COORDS, function (data) {
var peer = {
@ -217,7 +217,7 @@ function start() {
var mapId = data.mapid
delete data.mapid
//socket.broadcast.emit('maps-' + mapId + '-newTopic', data)
socket.broadbast.emit(TOPIC_CREATED, data)
socket.broadcast.emit(TOPIC_CREATED, data)
})
socket.on(UPDATE_TOPIC, function (data) {