redux-devtools/packages/redux-devtools-cli/src/worker.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-01-10 20:23:33 +03:00
var SCWorker = require('socketcluster/scworker');
2019-01-04 22:44:48 +03:00
var express = require('express');
var app = express();
var routes = require('./routes');
2019-01-04 01:30:48 +03:00
var createStore = require('./store');
class Worker extends SCWorker {
run() {
var httpServer = this.httpServer;
var scServer = this.scServer;
var options = this.options;
var store = createStore(options);
httpServer.on('request', app);
2019-01-08 19:35:12 +03:00
app.use(routes(options, store, scServer));
2019-01-04 01:30:48 +03:00
scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
2019-01-04 01:30:48 +03:00
var channel = req.event;
var data = req.data;
2019-01-10 21:51:14 +03:00
if (
channel.substr(0, 3) === 'sc-' ||
channel === 'respond' ||
channel === 'log'
) {
2019-01-04 01:30:48 +03:00
scServer.exchange.publish(channel, data);
} else if (channel === 'log-noid') {
2019-01-10 21:51:14 +03:00
scServer.exchange.publish('log', { id: req.socket.id, data: data });
2019-01-04 01:30:48 +03:00
}
next();
});
scServer.addMiddleware(scServer.MIDDLEWARE_SUBSCRIBE, function (req, next) {
2019-01-04 01:30:48 +03:00
next();
if (req.channel === 'report') {
2019-01-10 21:51:14 +03:00
store
.list()
.then(function (data) {
2019-01-10 21:51:14 +03:00
req.socket.emit(req.channel, { type: 'list', data: data });
})
.catch(function (error) {
2019-01-10 21:51:14 +03:00
console.error(error); // eslint-disable-line no-console
});
2019-01-04 01:30:48 +03:00
}
});
scServer.on('connection', function (socket) {
2019-01-04 01:30:48 +03:00
var channelToWatch, channelToEmit;
socket.on('login', function (credentials, respond) {
2019-01-04 01:30:48 +03:00
if (credentials === 'master') {
channelToWatch = 'respond';
channelToEmit = 'log';
} else {
channelToWatch = 'log';
channelToEmit = 'respond';
}
this.exchange.subscribe('sc-' + socket.id).watch(function (msg) {
2019-01-04 01:30:48 +03:00
socket.emit(channelToWatch, msg);
});
respond(null, channelToWatch);
});
socket.on('getReport', function (id, respond) {
2019-01-10 21:51:14 +03:00
store
.get(id)
.then(function (data) {
2019-01-10 21:51:14 +03:00
respond(null, data);
})
.catch(function (error) {
2019-01-10 21:51:14 +03:00
console.error(error); // eslint-disable-line no-console
});
2019-01-04 01:30:48 +03:00
});
socket.on('disconnect', function () {
2019-01-04 01:30:48 +03:00
var channel = this.exchange.channel('sc-' + socket.id);
channel.unsubscribe();
channel.destroy();
2019-01-10 21:51:14 +03:00
scServer.exchange.publish(channelToEmit, {
id: socket.id,
type: 'DISCONNECTED',
2019-01-10 21:51:14 +03:00
});
2019-01-04 01:30:48 +03:00
});
});
2019-01-10 21:51:14 +03:00
}
2019-01-04 01:30:48 +03:00
}
new Worker();