Skip to content

Commit

Permalink
Merge pull request #108 from mklabs/dev
Browse files Browse the repository at this point in the history
ES6 rewrite: Merge dev branch into master
  • Loading branch information
mklabs committed Oct 15, 2016
2 parents 7408308 + b83382d commit 2f3db7f
Show file tree
Hide file tree
Showing 24 changed files with 584 additions and 638 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": ["es2015"],

"plugins": [
"add-module-exports"
]
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src*
*node_modules
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"es6": true,
"mocha": true
},

"parserOptions": {
"sourceType": "module"
},

"extends": "standard",

"rules": {
"semi": ["error", "always"],
"no-multi-spaces": ["error", { "exceptions": { "VariableDeclarator": true, "ImportDeclaration": true } }],
"promise/param-names": 0
}
}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/*

tmp/phantomjs.log
node_modules
src*
tiny-lr.pid
File renamed without changes.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js

node_js:
- '5'
- '4'
- '0.12'
- '0.10'
- '6'
# - '5'
# - '4'
# - '0.12'
# - '0.10'
44 changes: 22 additions & 22 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
Copyright (c) 2012-2013 Mickael Daniel

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2012-2016 Mickael Daniel
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
all: help

help:
make -h

babel:
babel lib/ -d src/
babel test/ -d src_test/

test: eslint mocha

mocha: babel
mocha --reporter spec src_test/

serve:
node examples/express/server.js

eslint:
eslint . --debug

fix:
eslint . --fix

watch:
watchd lib/**.js test/wd/index.js package.json -c 'make test'
26 changes: 13 additions & 13 deletions examples/express/app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var fs = require('fs');
var path = require('path');
var express = require('express');
var tinylr = require('../..');
var debug = require('debug')('tinylr:server');
const fs = require('fs');
const path = require('path');
const express = require('express');
const tinylr = require('../..');
const debug = require('debug')('tinylr:server');

process.env.DEBUG = process.env.DEBUG || 'tinylr*';

var app = module.exports = express();

function logger(fmt) {
function logger (fmt) {
fmt = fmt || '%s - %s';

return function logger(req, res, next) {
return function logger (req, res, next) {
debug(fmt, req.method, req.url);
next();
}
};
}

function throttle(delay, fn) {
function throttle (delay, fn) {
var now = Date.now();

return function() {
return function () {
var from = Date.now();
var interval = from - now;
if (interval < delay) return;
Expand All @@ -29,14 +29,14 @@ function throttle(delay, fn) {
};
}

var watch = (function watch(em) {
(function watch (em) {
em = em || new (require('events').EventEmitter)();

em.on('rename', function(file) {
em.on('rename', function (file) {
tinylr.changed(file);
});

fs.watch(path.join(__dirname, 'styles/site.css'), throttle(200, function(ev, filename) {
fs.watch(path.join(__dirname, 'styles/site.css'), throttle(200, function (ev, filename) {
em.emit(ev, filename);
}));

Expand Down
13 changes: 4 additions & 9 deletions examples/express/server.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
var port = process.env.LR_PORT || process.env.PORT || 35729;
const port = process.env.LR_PORT || process.env.PORT || 35729;

var fs = require('fs');
var path = require('path');
var express = require('express');
var tinylr = require('../..');
var body = require('body-parser');
var debug = require('debug')('tinylr:server');
const debug = require('debug')('tinylr:server');

process.env.DEBUG = process.env.DEBUG || 'tinylr*';

var app = require('./app');
const app = require('./app');

app.listen(port, function(err) {
app.listen(port, function (err) {
if (err) throw err;
debug('listening on %d', port);
});
145 changes: 75 additions & 70 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,92 @@
import events from 'events';
import WebSocket from 'faye-websocket';

var util = require('util');
var events = require('events');
var WebSocket = require('faye-websocket');
const debug = require('debug')('tinylr:client');

module.exports = Client;
let idCounter = 0;

function Client(req, socket, head, options) {
options = this.options = options || {};
this.ws = new WebSocket(req, socket, head);
this.ws.onmessage = this.message.bind(this);
this.ws.onclose = this.close.bind(this);
this.id = this.uniqueId('ws');
}

util.inherits(Client, events.EventEmitter);
export default class Client extends events.EventEmitter {

Client.prototype.message = function message(event) {
var data = this.data(event);
if(this[data.command]) return this[data.command](data);
};
constructor (req, socket, head, options = {}) {
super();
this.options = options;
this.ws = new WebSocket(req, socket, head);
this.ws.onmessage = this.message.bind(this);
this.ws.onclose = this.close.bind(this);
this.id = this.uniqueId('ws');
}

Client.prototype.close = function close(event) {
if(this.ws) {
this.ws.close();
this.ws = null;
message (event) {
let data = this.data(event);
if (this[data.command]) return this[data.command](data);
}

this.emit('end', event);
};
close (event) {
if (this.ws) {
this.ws.close();
this.ws = null;
}

// Commands
this.emit('end', event);
}

Client.prototype.hello = function hello() {
this.send({
command: 'hello',
protocols: [
'http://livereload.com/protocols/official-7'
],
serverName: 'tiny-lr'
});
};
// Commands
hello () {
this.send({
command: 'hello',
protocols: [
'http://livereload.com/protocols/official-7'
],
serverName: 'tiny-lr'
});
}

Client.prototype.info = function info(data) {
this.plugins = data.plugins;
this.url = data.url;
};
info (data) {
if (data) {
debug('Info', data);
this.emit('info', Object.assign({}, data, { id: this.id }));
this.plugins = data.plugins;
this.url = data.url;
}

// Server commands
return Object.assign({}, data || {}, { id: this.id, url: this.url });
}

Client.prototype.reload = function reload(files) {
files.forEach(function(file) {
// Server commands
reload (files) {
files.forEach(function (file) {
this.send({
command: 'reload',
path: file,
liveCSS: this.options.liveCSS !== false,
liveImg: this.options.liveImg !== false
});
}, this);
}

alert (message) {
this.send({
command: 'reload',
path: file,
liveCSS: this.options.liveCSS !== false,
liveImg: this.options.liveImg !== false
command: 'alert',
message: message
});
}, this);
};

Client.prototype.alert = function alert(message) {
this.send({
command: 'alert',
message: message
});
};

// Utilities
}

Client.prototype.data = function _data(event) {
var data = {};
try {
data = JSON.parse(event.data);
} catch (e) {}
return data;
};
// Utilities
data (event) {
let data = {};
try {
data = JSON.parse(event.data);
} catch (e) {}
return data;
}

Client.prototype.send = function send(data) {
this.ws.send(JSON.stringify(data));
};
send (data) {
if (!this.ws) return;
this.ws.send(JSON.stringify(data));
}

var idCounter = 0;
Client.prototype.uniqueId = function uniqueId(prefix) {
var id = idCounter++;
return prefix ? prefix + id : id;
};
uniqueId (prefix) {
let id = idCounter++;
return prefix ? prefix + id : id;
}
}
Loading

0 comments on commit 2f3db7f

Please sign in to comment.