Skip to content

Commit

Permalink
Use fat arrow functions const foo = () => { ... }; for callbacks
Browse files Browse the repository at this point in the history
and any other function that is passed around and it's not a top level function
  • Loading branch information
juanjoDiaz committed Jul 12, 2018
1 parent 0e4808b commit 651c23e
Show file tree
Hide file tree
Showing 28 changed files with 178 additions and 192 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
"no-var": "error",
"no-useless-constructor": "error",
"object-shorthand": ["error", "methods", { "avoidQuotes": true }],
"prefer-arrow-callback": "error",
"arrow-body-style": ["error", "as-needed", { "requireReturnForObjectLiteral": false } ],
"arrow-parens": ["error", "as-needed", { "requireForBlockBody": true }],
"arrow-spacing": ["error"],
"no-confusing-arrow": ["error", { "allowParens": true }],
}
}
5 changes: 4 additions & 1 deletion app/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// native support in the browsers, so that our error handler
// can catch script-loading errors.

(function(){
// No ES6 can be used in this file since it's used for the translation
/* eslint-disable prefer-arrow-callback */

(function() {
"use strict";

// Fallback for all uncought errors
Expand Down
36 changes: 17 additions & 19 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const UI = {
if (isTouchDevice) {
document.documentElement.classList.add("noVNC_touch");
// Remove the address bar
setTimeout(function() { window.scrollTo(0, 1); }, 100);
setTimeout(() => window.scrollTo(0, 1), 100);
}

// Restore control bar position
Expand Down Expand Up @@ -230,13 +230,13 @@ const UI = {

addTouchSpecificHandlers() {
document.getElementById("noVNC_mouse_button0")
.addEventListener('click', function () { UI.setMouseButton(1); });
.addEventListener('click', () => UI.setMouseButton(1));
document.getElementById("noVNC_mouse_button1")
.addEventListener('click', function () { UI.setMouseButton(2); });
.addEventListener('click', () => UI.setMouseButton(2));
document.getElementById("noVNC_mouse_button2")
.addEventListener('click', function () { UI.setMouseButton(4); });
.addEventListener('click', () => UI.setMouseButton(4));
document.getElementById("noVNC_mouse_button4")
.addEventListener('click', function () { UI.setMouseButton(0); });
.addEventListener('click', () => UI.setMouseButton(0));
document.getElementById("noVNC_keyboard_button")
.addEventListener('click', UI.toggleVirtualKeyboard);

Expand All @@ -250,7 +250,7 @@ const UI = {
document.getElementById("noVNC_keyboardinput")
.addEventListener('blur', UI.onblurVirtualKeyboard);
document.getElementById("noVNC_keyboardinput")
.addEventListener('submit', function () { return false; });
.addEventListener('submit', () => false);

document.documentElement
.addEventListener('mousedown', UI.keepVirtualKeyboard, true);
Expand Down Expand Up @@ -294,11 +294,11 @@ const UI = {

addMachineHandlers() {
document.getElementById("noVNC_shutdown_button")
.addEventListener('click', function() { UI.rfb.machineShutdown(); });
.addEventListener('click', () => UI.rfb.machineShutdown());
document.getElementById("noVNC_reboot_button")
.addEventListener('click', function() { UI.rfb.machineReboot(); });
.addEventListener('click', () => UI.rfb.machineReboot());
document.getElementById("noVNC_reset_button")
.addEventListener('click', function() { UI.rfb.machineReset(); });
.addEventListener('click', () => UI.rfb.machineReset());
document.getElementById("noVNC_power_button")
.addEventListener('click', UI.togglePowerPanel);
},
Expand Down Expand Up @@ -329,7 +329,7 @@ const UI = {
addSettingChangeHandler(name, changeFunc) {
const settingElem = document.getElementById("noVNC_setting_" + name);
if (changeFunc === undefined) {
changeFunc = function () { UI.saveSetting(name); };
changeFunc = () => UI.saveSetting(name);
}
settingElem.addEventListener('change', changeFunc);
},
Expand Down Expand Up @@ -552,8 +552,7 @@ const UI = {
const barDisplayStyle = window.getComputedStyle(bar).display;
if (barDisplayStyle !== 'none') {
bar.style.transitionDuration = '0s';
bar.addEventListener('transitionend', function () {
this.style.transitionDuration = ""; });
bar.addEventListener('transitionend', () => bar.style.transitionDuration = '');
}

const anchor = document.getElementById('noVNC_control_bar_anchor');
Expand Down Expand Up @@ -1029,7 +1028,7 @@ const UI = {
UI.rfb.addEventListener("disconnect", UI.disconnectFinished);
UI.rfb.addEventListener("credentialsrequired", UI.credentials);
UI.rfb.addEventListener("securityfailure", UI.securityFailed);
UI.rfb.addEventListener("capabilities", function () { UI.updatePowerButton(); });
UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
UI.rfb.addEventListener("bell", UI.bell);
UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
Expand Down Expand Up @@ -1153,9 +1152,8 @@ const UI = {
document.getElementById('noVNC_password_dlg')
.classList.add('noVNC_open');

setTimeout(function () {
document.getElementById('noVNC_password_input').focus();
}, 100);
setTimeout(() => document
.getElementById('noVNC_password_input').focus(), 100);

Log.Warn("Server asked for a password");
UI.showStatus(_("Password is required"), "warning");
Expand Down Expand Up @@ -1624,7 +1622,7 @@ const UI = {
const promise = document.getElementById('noVNC_bell').play();
// The standards disagree on the return value here
if (promise) {
promise.catch(function(e) {
promise.catch((e) => {
if (e.name === "NotAllowedError") {
// Ignore when the browser doesn't let us play audio.
// It is common that the browsers require audio to be
Expand Down Expand Up @@ -1655,12 +1653,12 @@ const UI = {
const LINGUAS = ["de", "el", "es", "nl", "pl", "sv", "tr", "zh_CN", "zh_TW"];
l10n.setup(LINGUAS);
if (l10n.language !== "en" && l10n.dictionary === undefined) {
WebUtil.fetchJSON('app/locale/' + l10n.language + '.json', function (translations) {
WebUtil.fetchJSON('app/locale/' + l10n.language + '.json', (translations) => {
l10n.dictionary = translations;

// wait for translations to load before loading the UI
UI.prime();
}, function (err) {
}, (err) => {
Log.Error("Failed to load translations: " + err);
UI.prime();
});
Expand Down
14 changes: 5 additions & 9 deletions app/webutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function initSettings (callback /*, ...callbackArgs */) {
"use strict";
const callbackArgs = Array.prototype.slice.call(arguments, 1);
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.get(function (cfg) {
window.chrome.storage.sync.get((cfg) => {
settings = cfg;
if (callback) {
callback.apply(this, callbackArgs);
Expand Down Expand Up @@ -201,7 +201,7 @@ export function injectParamIfMissing (path, param, value) {
query = [];
}

if (!query.some(function (v) { return v.startsWith(param_eq); })) {
if (!query.some(v => v.startsWith(param_eq))) {
query.push(param_eq + encodeURIComponent(value));
elem.search = "?" + query.join("&");
}
Expand All @@ -224,7 +224,7 @@ export function fetchJSON(path, resolve, reject) {
const req = new XMLHttpRequest();
req.open('GET', path);

req.onload = function () {
req.onload = () => {
if (req.status === 200) {
let resObj;
try {
Expand All @@ -238,13 +238,9 @@ export function fetchJSON(path, resolve, reject) {
}
};

req.onerror = function (evt) {
reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));
};
req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));

req.ontimeout = function (evt) {
reject(new Error("XHR timed out while trying to load '" + path + "'"));
};
req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'"));

req.send();
}
11 changes: 4 additions & 7 deletions core/input/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export default class Keyboard {

const target = this._target;
const downList = this._keyDownList;
['AltLeft', 'AltRight'].forEach(function (code) {
['AltLeft', 'AltRight'].forEach((code) => {
if (!(code in downList)) {
return;
}
Expand Down Expand Up @@ -338,11 +338,10 @@ export default class Keyboard {
const handler = this._eventHandlers.checkalt;
['mousedown', 'mouseup', 'mousemove', 'wheel',
'touchstart', 'touchend', 'touchmove',
'keydown', 'keyup'].forEach(function (type) {
'keydown', 'keyup'].forEach(type =>
document.addEventListener(type, handler,
{ capture: true,
passive: true });
});
passive: true }));
}

//Log.Debug("<< Keyboard.grab");
Expand All @@ -355,9 +354,7 @@ export default class Keyboard {
const handler = this._eventHandlers.checkalt;
['mousedown', 'mouseup', 'mousemove', 'wheel',
'touchstart', 'touchend', 'touchmove',
'keydown', 'keyup'].forEach(function (type) {
document.removeEventListener(type, handler);
});
'keydown', 'keyup'].forEach(type => document.removeEventListener(type, handler));
}

this._target.removeEventListener('keydown', this._eventHandlers.keydown);
Expand Down
54 changes: 24 additions & 30 deletions core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ export default class RFB extends EventTargetMixin {

this._sock = new Websock();
this._sock.on('message', this._handle_message.bind(this));
this._sock.on('open', function () {
this._sock.on('open', () => {
if ((this._rfb_connection_state === 'connecting') &&
(this._rfb_init_state === '')) {
this._rfb_init_state = 'ProtocolVersion';
Log.Debug("Starting VNC handshake");
} else {
this._fail("Unexpected server connection while " +
this._rfb_connection_state);
this._rfb_connection_state);
}
}.bind(this));
this._sock.on('close', function (e) {
});
this._sock.on('close', (e) => {
Log.Debug("WebSocket on-close event");
let msg = "";
if (e.code) {
Expand All @@ -236,18 +236,16 @@ export default class RFB extends EventTargetMixin {
break;
case 'disconnected':
this._fail("Unexpected server disconnect " +
"when already disconnected " + msg);
"when already disconnected " + msg);
break;
default:
this._fail("Unexpected server disconnect before connecting " +
msg);
msg);
break;
}
this._sock.off('close');
}.bind(this));
this._sock.on('error', function (e) {
Log.Warn("WebSocket on-error event");
});
this._sock.on('error', e => Log.Warn("WebSocket on-error event"));

// Slight delay of the actual connection so that the caller has
// time to set up callbacks
Expand Down Expand Up @@ -459,18 +457,15 @@ export default class RFB extends EventTargetMixin {
const stats = this._encStats;

Log.Info("Encoding stats for this connection:");
Object.keys(stats).forEach(function (key) {
Object.keys(stats).forEach((key) => {
const s = stats[key];
if (s[0] + s[1] > 0) {
Log.Info(" " + encodingName(key) + ": " + s[0] + " rects");
}
});

Log.Info("Encoding stats since page load:");
Object.keys(stats).forEach(function (key) {
const s = stats[key];
Log.Info(" " + encodingName(key) + ": " + s[1] + " rects");
});
Object.keys(stats).forEach(key => Log.Info(" " + encodingName(key) + ": " + stats[key][1] + " rects"));
}

_focusCanvas(event) {
Expand All @@ -489,10 +484,10 @@ export default class RFB extends EventTargetMixin {
_windowResize(event) {
// If the window resized then our screen element might have
// as well. Update the viewport dimensions.
window.requestAnimationFrame(function () {
window.requestAnimationFrame(() => {
this._updateClip();
this._updateScale();
}.bind(this));
});

if (this._resizeSession) {
// Request changing the resolution of the remote display to
Expand Down Expand Up @@ -640,8 +635,7 @@ export default class RFB extends EventTargetMixin {

this._rfb_connection_state = state;

const smsg = "New state '" + state + "', was '" + oldstate + "'.";
Log.Debug(smsg);
Log.Debug("New state '" + state + "', was '" + oldstate + "'.");

if (this._disconnTimer && state !== 'disconnecting') {
Log.Debug("Clearing disconnect timer");
Expand All @@ -664,10 +658,10 @@ export default class RFB extends EventTargetMixin {
case 'disconnecting':
this._disconnect();

this._disconnTimer = setTimeout(function () {
this._disconnTimer = setTimeout(() => {
Log.Error("Disconnection timed out.");
this._updateConnectionState('disconnected');
}.bind(this), DISCONNECT_TIMEOUT * 1000);
}, DISCONNECT_TIMEOUT * 1000);
break;

case 'disconnected':
Expand Down Expand Up @@ -2143,7 +2137,7 @@ RFB.encodingHandlers = {

let resetStreams = 0;
let streamId = -1;
const decompress = function (data, expected) {
const decompress = (data, expected) => {
for (let i = 0; i < 4; i++) {
if ((resetStreams >> i) & 1) {
this._FBU.zlibs[i].reset();
Expand All @@ -2159,9 +2153,9 @@ RFB.encodingHandlers = {

//return uncompressed.data;
return uncompressed;
}.bind(this);
};

const indexedToRGBX2Color = function (data, palette, width, height) {
const indexedToRGBX2Color = (data, palette, width, height) => {
// Convert indexed (palette based) image data to RGB
// TODO: reduce number of calculations inside loop
const dest = this._destBuff;
Expand Down Expand Up @@ -2220,9 +2214,9 @@ RFB.encodingHandlers = {
}

return dest;
}.bind(this);
};

const indexedToRGBX = function (data, palette, width, height) {
const indexedToRGBX = (data, palette, width, height) => {
// Convert indexed (palette based) image data to RGB
const dest = this._destBuff;
const total = width * height * 4;
Expand All @@ -2235,14 +2229,14 @@ RFB.encodingHandlers = {
}

return dest;
}.bind(this);
};

const rQi = this._sock.get_rQi();
const rQ = this._sock.rQwhole();
let cmode, data;
let cl_header, cl_data;

const handlePalette = function () {
const handlePalette = () => {
const numColors = rQ[rQi + 2] + 1;
const paletteSize = numColors * 3;
this._FBU.bytes += paletteSize;
Expand Down Expand Up @@ -2300,9 +2294,9 @@ RFB.encodingHandlers = {


return true;
}.bind(this);
};

const handleCopy = function () {
const handleCopy = () => {
let raw = false;
const uncompressedSize = this._FBU.width * this._FBU.height * 3;
if (uncompressedSize < 12) {
Expand Down Expand Up @@ -2340,7 +2334,7 @@ RFB.encodingHandlers = {
this._display.blitRgbImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, data, 0, false);

return true;
}.bind(this);
};

let ctl = this._sock.rQpeek8();

Expand Down
2 changes: 1 addition & 1 deletion core/util/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function releaseCapture () {

// There might be events already queued, so we need to wait for
// them to flush. E.g. contextmenu in Microsoft Edge
window.setTimeout(function(expected) {
window.setTimeout((expected) => {
// Only clear it if it's the expected grab (i.e. no one
// else has initiated a new grab)
if (_captureIndex === expected) {
Expand Down
Loading

0 comments on commit 651c23e

Please sign in to comment.