Remove notes, serialize functions properly, add data method.

Notes don't belong in core. Not sure why I included them :(

Functions are now toString'd when toJSON is called, allowing them to be
sent over the wire.

The new data method for clients is meant to set environment variables.
Send in the name of the scope and it will be merged in. This API may
change later to allow brevity.
This commit is contained in:
Jesse Gibson
2016-04-27 16:44:11 -06:00
parent 4edd691277
commit 46a4be5529
7 changed files with 20 additions and 45 deletions

View File

@@ -95,6 +95,7 @@ module.exports = {
"no-caller": "error",
"no-catch-shadow": "error",
"no-confusing-arrow": "error",
"no-console": "off",
"no-continue": "off",
"no-div-regex": "error",
"no-duplicate-imports": "error",

View File

@@ -1,34 +0,0 @@
var panic = require('panic-server');
panic.serve(8080, 'localhost');
var browsers = panic.clients.filter(function (client) {
return client.platform !== 'Node.js';
});
var servers = panic.clients.excluding(browsers);
servers.matching({
os: /OS X/
});
servers.update('gun', {
key: 'panic-test',
path: 'stuff'
});
servers.run(function (done) {
// do things
done();
})
.then(function () {
// do things
})
.catch(function () {
// do things
});
function Test(name, cb) {
this.name = name;
this.cb = cb;
this.TDO = {};
}
Test.prototype = {
toJSON: function () {
return this.TDO;
}
};

View File

@@ -7,6 +7,7 @@ function ClientList() {
this.clients = {};
}
Function.prototype.toJSON = Function.prototype.toString;
var API = ClientList.prototype = new Emitter();
API.each = function (cb) {
@@ -50,7 +51,7 @@ API.filter = function (query) {
if (query instanceof Function && query(client, ID)) {
list.add(client);
return;
} else if (typeof query === 'string') {
} else if (typeof query === 'string' || query instanceof RegExp) {
query = {
name: query
};
@@ -66,9 +67,6 @@ API.filter = function (query) {
};
API.excluding = function (exclude) {
if (!(exclude instanceof ClientList)) {
throw new Error('Exclusion set is not a ClientList');
}
return this.filter(function (client) {
return !exclude.get(client.socket.id);
});
@@ -114,4 +112,11 @@ API.run = function (cb) {
});
};
API.set = function (name, data) {
function send(client) {
client.socket.emit('data', name, data);
}
return this.each(send).on('add', send);
};
module.exports = ClientList;

View File

@@ -1,2 +1,3 @@
'use strict';
var List = require('./ClientList');
module.exports = new List();

View File

@@ -1,3 +1,4 @@
'use strict';
var server = require('./server');
var clients = require('./clients');

View File

@@ -1,3 +1,4 @@
'use strict';
function match(query, platform) {
var key, value, matches = true;
for (key in query) {

View File

@@ -1,13 +1,13 @@
'use strict';
var io = require('socket.io');
var fs = require('fs');
var clients = require('./clients');
var file = require.resolve('../../panic-client/panic.js');
var server = require('http').createServer(function (req, res) {
if (req.url !== '/panic.js' && req.url !== '/') {
return;
if (req.url === '/panic.js' || req.url === '/') {
fs.createReadStream(file).pipe(res);
}
var path = require.resolve('../../panic-client/panic.js');
fs.createReadStream(path).pipe(res);
});
function open(config) {
@@ -17,10 +17,10 @@ function open(config) {
server.listen(config.port, config.hostname);
io(server).on('connection', function (client) {
client.on('handshake', function (platform) {
io(server).on('connection', function (socket) {
socket.on('handshake', function (platform) {
clients.add({
socket: client,
socket: socket,
platform: platform
});
});