Skip to content

Commit

Permalink
[feature] null transport support, sse prep
Browse files Browse the repository at this point in the history
  • Loading branch information
apla committed Sep 6, 2019
1 parent 17756ac commit 0173b54
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/livereload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class LiveReload {
error () {}
};

// i can haz sockets?
if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
this.console.error('LiveReload disabled because the browser does not seem to support web sockets');
return;
}

// i can haz options?
if ('LiveReloadOptions' in window) {
this.options = new Options();
Expand All @@ -47,9 +41,32 @@ class LiveReload {
}
}

// i can haz sockets?
if (this.options.transport === 'ws' && !(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
this.console.error('LiveReload disabled because the browser does not seem to support web sockets');
return;
// } else if (this.options.transport === 'sse' && !(this.EventSource = this.window.EventSource)) {
// this.console.error('LiveReload disabled because the browser does not seem to support EventSource');
// return;
}

// i can haz reloader?
this.reloader = new Reloader(this.window, this.console, Timer);

if (this.options.transport === null) {
// page will generate reload events itself by LiveReload.performReload()
// } else if (this.options.transport === 'sse') {
// this.initSSETransport();
} else if (this.options.transport === 'ws') {
this.initWSTransport();
}

this.initialized = true;

}

initWSTransport () {

// i can haz connection?
this.connector = new Connector(this.options, this.WebSocket, Timer, {
connecting: () => {},
Expand Down Expand Up @@ -107,8 +124,6 @@ class LiveReload {
}
}
});

this.initialized = true;
}

on (eventName, handler) {
Expand Down
15 changes: 15 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Options {
this.host = null;
this.port = 35729;

this.transport = 'ws';
this.path = 'livereload';

this.snipver = null;
this.ext = null;
this.extver = null;
Expand All @@ -22,6 +25,18 @@ class Options {
value = +value;
}

if (value === 'null') {
value = null;
}

if (value === 'true') {
value = true;
}

if (value === 'false') {
value = false;
}

this[name] = value;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/options_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Options', function () {
const options = Options.extract(dom.window.document);
assert.ok(options);
assert.strictEqual('somewhere.com', options.host);
assert.strictEqual('ws', options.transport);
return assert.strictEqual(9876, options.port);
});

Expand Down Expand Up @@ -100,6 +101,15 @@ describe('Options', function () {
return assert.strictEqual('somewhere.org', options.host);
});

it('should allow to set null transport', function () {
const dom = new JSDOM('<script src="//somewhere.com/132324324/23243443/4343/livereload.js?transport=null"></script>', {
url: 'https://somewhere.org/'
});
const options = Options.extract(dom.window.document);
assert.ok(options);
return assert.strictEqual(null, options.transport);
});

return it('should recognize protocol-relative https URL', function () {
const dom = new JSDOM('<script src="//somewhere.com/132324324/23243443/4343/livereload.js"></script>', {
url: 'https://somewhere.org/'
Expand Down

0 comments on commit 0173b54

Please sign in to comment.