|
|
@ -1,6 +1,7 @@
|
|
|
|
const { pop } = require('../uuids')
|
|
|
|
const { pop } = require('../uuids')
|
|
|
|
const { channelEvents } = require('./channels')
|
|
|
|
const { channelEvents } = require('./channels')
|
|
|
|
const crud = require('./crud')
|
|
|
|
const crud = require('./crud')
|
|
|
|
|
|
|
|
const { recvUpdate } = require('./streams')
|
|
|
|
|
|
|
|
|
|
|
|
const connections = {};
|
|
|
|
const connections = {};
|
|
|
|
|
|
|
|
|
|
|
@ -28,26 +29,32 @@ const handleMsg = async (msg, id) => {
|
|
|
|
if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) {
|
|
|
|
if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) {
|
|
|
|
const channel = d.subscribe.split(':')[0]
|
|
|
|
const channel = d.subscribe.split(':')[0]
|
|
|
|
connections[id].events.push(d.subscribe)
|
|
|
|
connections[id].events.push(d.subscribe)
|
|
|
|
connections[id].connection.send(JSON.stringify(await crud[channel].getAll()))
|
|
|
|
const dm = {
|
|
|
|
|
|
|
|
event: `${channel}:read`,
|
|
|
|
|
|
|
|
data: await crud[channel].getAll(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[id].connection.send(JSON.stringify(dm))
|
|
|
|
} else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) {
|
|
|
|
} else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) {
|
|
|
|
const ev = d.event.split(':')
|
|
|
|
const ev = d.event.split(':')
|
|
|
|
const channel = ev[0]
|
|
|
|
const channel = ev[0]
|
|
|
|
const event = ev[1]
|
|
|
|
const event = ev[1]
|
|
|
|
console.log(`received event for ${channel} with data %s`, d.data)
|
|
|
|
console.log(`received event for ${channel} with data %s`, d.data)
|
|
|
|
|
|
|
|
let dm;
|
|
|
|
switch (event) {
|
|
|
|
switch (event) {
|
|
|
|
case 'create':
|
|
|
|
case 'create':
|
|
|
|
const da = await crud[channel].create(d.data)
|
|
|
|
dm = await crud[channel].create(d.data)
|
|
|
|
connections[id].connection.send(JSON.stringify(da))
|
|
|
|
recvUpdate(channel, dm)
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'update':
|
|
|
|
case 'update':
|
|
|
|
const du = await crud[channel].update(d.data.id, d.data.data)
|
|
|
|
dm = await crud[channel].update(d.data.id, d.data.data)
|
|
|
|
connections[id].connection.send(JSON.stringify(du))
|
|
|
|
recvUpdate(channel, dm)
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
case 'delete':
|
|
|
|
await crud[channel].delete(d.data.id)
|
|
|
|
await crud[channel].delete(d.data.id)
|
|
|
|
|
|
|
|
recvUpdate(channel, 'delete')
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
fanoutMsg(channel, await crud[channel].getAll())
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -60,7 +67,11 @@ const handleMsg = async (msg, id) => {
|
|
|
|
const fanoutMsg = (channel, data) => {
|
|
|
|
const fanoutMsg = (channel, data) => {
|
|
|
|
Object.keys(connections).forEach((k) => {
|
|
|
|
Object.keys(connections).forEach((k) => {
|
|
|
|
if (connections[k].events.indexOf(`${channel}:read`) !== -1) {
|
|
|
|
if (connections[k].events.indexOf(`${channel}:read`) !== -1) {
|
|
|
|
connections[k].connection.send(JSON.stringify(data))
|
|
|
|
const d = {
|
|
|
|
|
|
|
|
event: `${channel}:read`,
|
|
|
|
|
|
|
|
data,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[k].connection.send(JSON.stringify(d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|