2016-04-14 04:01:51 +00:00
|
|
|
const _ = require('lodash')
|
2020-04-06 22:47:58 +00:00
|
|
|
const socket = require('../shared/socket')
|
2016-04-10 08:37:05 +00:00
|
|
|
const storage = {}
|
|
|
|
const events = {}
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
// Listen on all events
|
|
|
|
let onevent = socket.onevent
|
2018-06-26 18:35:12 +00:00
|
|
|
let disconnected = false
|
2016-04-10 08:37:05 +00:00
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
socket.onevent = function(packet) {
|
|
|
|
let args = packet.data || []
|
|
|
|
onevent.call(this, packet) // original call
|
|
|
|
packet.data = ['*'].concat(args)
|
|
|
|
onevent.call(this, packet)
|
|
|
|
}
|
2016-04-10 08:37:05 +00:00
|
|
|
|
2018-06-26 18:35:12 +00:00
|
|
|
socket.on('disconnect', () => {
|
|
|
|
disconnected = true
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('connect', () => {
|
|
|
|
if (disconnected) {
|
|
|
|
Object.keys(events).forEach(event => {
|
|
|
|
let name = event
|
|
|
|
let id = null
|
|
|
|
if (event.indexOf(':') > 0) {
|
|
|
|
name = event.split(':')[0]
|
|
|
|
id = Number(event.split(':')[1])
|
|
|
|
}
|
|
|
|
socket.emit(name, { id: id })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
disconnected = false
|
|
|
|
})
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
function genId(name, id) {
|
|
|
|
if (id) {
|
|
|
|
return `${name}:${id}`
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
2016-04-10 08:37:05 +00:00
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
const store = {
|
2018-06-26 18:35:12 +00:00
|
|
|
getId: function(name, id) {
|
|
|
|
return genId(name, id)
|
|
|
|
},
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
get: function(name, id) {
|
|
|
|
return storage[genId(name, id)]
|
2016-04-10 08:37:05 +00:00
|
|
|
},
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
listen: function(name, caller, id) {
|
|
|
|
events[genId(name, id)] = caller
|
2016-04-10 08:37:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
unlisten: function(name) {
|
|
|
|
delete events[name]
|
2016-04-14 04:01:51 +00:00
|
|
|
delete storage[name]
|
2016-04-10 08:37:05 +00:00
|
|
|
},
|
2016-04-14 04:01:51 +00:00
|
|
|
|
|
|
|
events: events,
|
|
|
|
storage: storage,
|
2016-04-10 08:37:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
socket.on('*', (event, data) => {
|
2018-06-26 18:35:12 +00:00
|
|
|
let id = data && data.id
|
|
|
|
|
|
|
|
let name = genId(event, id)
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
if (events[name]) {
|
|
|
|
storage[name] = data
|
|
|
|
events[name]()
|
|
|
|
}
|
2016-04-14 13:22:27 +00:00
|
|
|
if (event.indexOf('single') >= 0) {
|
2016-04-14 04:01:51 +00:00
|
|
|
let check = event.replace('single', 'all')
|
2018-06-26 18:35:12 +00:00
|
|
|
if (events[check]) {
|
2016-04-14 04:01:51 +00:00
|
|
|
let index = _.findIndex(storage[check], { id: data.id })
|
|
|
|
if (index > -1) {
|
|
|
|
storage[check][index] = data
|
2018-06-26 18:35:12 +00:00
|
|
|
events[check]()
|
|
|
|
} else {
|
|
|
|
storage[check].push(data)
|
|
|
|
events[check]()
|
2016-04-14 04:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-10 08:37:05 +00:00
|
|
|
})
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
|
|
|
|
window.store = store
|
|
|
|
|
2016-04-10 08:37:05 +00:00
|
|
|
module.exports = store
|