Unverified Commit fbfc20e0 authored by Sean Hammond's avatar Sean Hammond Committed by GitHub

Merge pull request #705 from hypothesis/replace-inherits-with-classes

Replace "inherits" package with ES 2015 class inheritance
parents a34342cc 30f9dff4
......@@ -57,7 +57,6 @@
"gulp-sourcemaps": "^1.6.0",
"gulp-util": "^3.0.7",
"hammerjs": "^2.0.4",
"inherits": "^2.0.1",
"isparta": "^4.0.0",
"istanbul": "^0.4.5",
"jquery": "^3.2.1",
......
'use strict';
var angular = require('angular');
var inherits = require('inherits');
var proxyquire = require('proxyquire');
var EventEmitter = require('tiny-emitter');
......@@ -9,7 +8,11 @@ var events = require('../../events');
var noCallThru = require('../../../shared/test/util').noCallThru;
var searchClients;
function FakeSearchClient(searchFn, opts) {
class FakeSearchClient extends EventEmitter {
constructor(searchFn, opts) {
super();
assert.ok(searchFn);
searchClients.push(this);
this.cancel = sinon.stub();
......@@ -26,15 +29,17 @@ function FakeSearchClient(searchFn, opts) {
this.emit('end');
});
}
}
inherits(FakeSearchClient, EventEmitter);
function FakeRootThread() {
class FakeRootThread extends EventEmitter {
constructor() {
super();
this.thread = sinon.stub().returns({
totalChildren: 0,
});
}
}
inherits(FakeRootThread, EventEmitter);
describe('sidebar.components.sidebar-content', function () {
var $rootScope;
......
'use strict';
var angular = require('angular');
var inherits = require('inherits');
var EventEmitter = require('tiny-emitter');
function FakeRootThread() {
class FakeRootThread extends EventEmitter {
constructor() {
super();
this.thread = sinon.stub();
}
}
inherits(FakeRootThread, EventEmitter);
describe('StreamContentController', function () {
var $componentController;
......
......@@ -3,7 +3,6 @@
var angular = require('angular');
var EventEmitter = require('tiny-emitter');
var inherits = require('inherits');
var immutable = require('seamless-immutable');
var events = require('../../events');
......@@ -43,7 +42,9 @@ var threadFixtures = immutable({
var fakeVirtualThread;
var fakeSettings = {};
function FakeVirtualThreadList($scope, $window, rootThread, options) {
class FakeVirtualThreadList extends EventEmitter {
constructor($scope, $window, rootThread, options) {
super();
fakeVirtualThread = this; // eslint-disable-line consistent-this
......@@ -64,8 +65,8 @@ function FakeVirtualThreadList($scope, $window, rootThread, options) {
this.yOffsetOf = function () {
return 42;
};
}
}
inherits(FakeVirtualThreadList, EventEmitter);
describe('threadList', function () {
var threadListContainers;
......
'use strict';
var EventEmitter = require('tiny-emitter');
var inherits = require('inherits');
/**
* Client for the Hypothesis search API.
*
* SearchClient handles paging through results, canceling search etc.
*
*/
class SearchClient extends EventEmitter {
/**
* @param {Object} searchFn - Function for querying the search API
* @param {Object} opts - Search options
* @constructor
*/
function SearchClient(searchFn, opts) {
constructor(searchFn, opts) {
super();
opts = opts || {};
var DEFAULT_CHUNK_SIZE = 200;
......@@ -24,10 +25,9 @@ function SearchClient(searchFn, opts) {
this._incremental = true;
}
this._canceled = false;
}
inherits(SearchClient, EventEmitter);
}
SearchClient.prototype._getBatch = function (query, offset) {
_getBatch(query, offset) {
var searchQuery = Object.assign({
limit: this._chunkSize,
offset: offset,
......@@ -75,9 +75,9 @@ SearchClient.prototype._getBatch = function (query, offset) {
}
self.emit('end');
});
};
}
/**
/**
* Perform a search against the Hypothesis API.
*
* Emits a 'results' event with an array of annotations as they become
......@@ -87,18 +87,19 @@ SearchClient.prototype._getBatch = function (query, offset) {
* Emits an 'error' event if the search fails.
* Emits an 'end' event once the search completes.
*/
SearchClient.prototype.get = function (query) {
get(query) {
this._results = [];
this._getBatch(query, 0);
};
}
/**
/**
* Cancel the current search and emit the 'end' event.
* No further events will be emitted after this.
*/
SearchClient.prototype.cancel = function () {
cancel() {
this._canceled = true;
this.emit('end');
};
}
}
module.exports = SearchClient;
'use strict';
var EventEmitter = require('tiny-emitter');
var inherits = require('inherits');
var proxyquire = require('proxyquire');
var events = require('../../events');
......@@ -42,7 +41,10 @@ var fixtures = {
// the most recently created FakeSocket instance
var fakeWebSocket = null;
function FakeSocket(url) {
class FakeSocket extends EventEmitter {
constructor(url) {
super();
fakeWebSocket = this; // eslint-disable-line consistent-this
this.url = url;
......@@ -62,8 +64,8 @@ function FakeSocket(url) {
this.close = function () {
this.didClose = true;
};
}
}
inherits(FakeSocket, EventEmitter);
describe('Streamer', function () {
var fakeAnnotationMapper;
......
......@@ -2,7 +2,6 @@
var EventEmitter = require('tiny-emitter');
var debounce = require('lodash.debounce');
var inherits = require('inherits');
/**
* @typedef Options
......@@ -26,13 +25,17 @@ var inherits = require('inherits');
* applications this also helps significantly with UI responsiveness by limiting
* the number of watchers (functions created by template expressions or
* '$scope.$watch' calls) that have to be run on every '$scope.$digest()' cycle.
*
*/
class VirtualThreadList extends EventEmitter {
/*
* @param {Window} container - The Window displaying the list of annotation threads.
* @param {Thread} rootThread - The initial Thread object for the top-level
* threads.
* @param {Options} options
*/
function VirtualThreadList($scope, window_, rootThread, options) {
constructor($scope, window_, rootThread, options) {
super();
var self = this;
this._rootThread = rootThread;
......@@ -56,35 +59,34 @@ function VirtualThreadList($scope, window_, rootThread, options) {
this.scrollRoot.removeEventListener('scroll', debouncedUpdate);
this.window.removeEventListener('resize', debouncedUpdate);
};
}
inherits(VirtualThreadList, EventEmitter);
}
/**
/**
* Detach event listeners and clear any pending timeouts.
*
* This should be invoked when the UI view presenting the virtual thread list
* is torn down.
*/
VirtualThreadList.prototype.detach = function () {
detach() {
this._detach();
};
}
/**
/**
* Sets the root thread containing all conversations matching the current
* filters.
*
* This should be called with the current Thread object whenever the set of
* matching annotations changes.
*/
VirtualThreadList.prototype.setRootThread = function (thread) {
setRootThread(thread) {
if (thread === this._rootThread) {
return;
}
this._rootThread = thread;
this._updateVisibleThreads();
};
}
/**
/**
* Sets the actual height for a thread.
*
* When calculating the amount of space required for offscreen threads,
......@@ -94,22 +96,22 @@ VirtualThreadList.prototype.setRootThread = function (thread) {
* @param {string} id - The annotation ID or $tag
* @param {number} height - The height of the annotation thread.
*/
VirtualThreadList.prototype.setThreadHeight = function (id, height) {
setThreadHeight(id, height) {
if (isNaN(height) || height <= 0) {
throw new Error('Invalid thread height %d', height);
}
this._heights[id] = height;
};
}
VirtualThreadList.prototype._height = function (id) {
_height(id) {
// Default guess of the height required for a threads that have not been
// measured
var DEFAULT_HEIGHT = 200;
return this._heights[id] || DEFAULT_HEIGHT;
};
}
/** Return the vertical offset of an annotation card from the top of the list. */
VirtualThreadList.prototype.yOffsetOf = function (id) {
/** Return the vertical offset of an annotation card from the top of the list. */
yOffsetOf(id) {
var self = this;
var allThreads = this._rootThread.children;
var matchIndex = allThreads.findIndex(function (thread) {
......@@ -121,15 +123,15 @@ VirtualThreadList.prototype.yOffsetOf = function (id) {
return allThreads.slice(0, matchIndex).reduce(function (offset, thread) {
return offset + self._height(thread.id);
}, 0);
};
}
/**
/**
* Recalculates the set of visible threads and estimates of the amount of space
* required for offscreen threads above and below the viewport.
*
* Emits a `changed` event with the recalculated set of visible threads.
*/
VirtualThreadList.prototype._updateVisibleThreads = function () {
_updateVisibleThreads() {
// Space above the viewport in pixels which should be considered 'on-screen'
// when calculating the set of visible threads
var MARGIN_ABOVE = 800;
......@@ -198,6 +200,7 @@ VirtualThreadList.prototype._updateVisibleThreads = function () {
visibleThreads: visibleThreads,
invisibleThreads: invisibleThreads,
});
};
}
}
module.exports = VirtualThreadList;
......@@ -2,7 +2,6 @@
var retry = require('retry');
var EventEmitter = require('tiny-emitter');
var inherits = require('inherits');
// see https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
var CLOSE_NORMAL = 1000;
......@@ -19,7 +18,10 @@ var RECONNECT_MIN_DELAY = 1000;
* - Uses the standard EventEmitter API for reporting open, close, error
* and message events.
*/
function Socket(url) {
class Socket extends EventEmitter {
constructor(url) {
super();
var self = this;
// queue of JSON objects which have not yet been submitted
......@@ -125,8 +127,7 @@ function Socket(url) {
};
connect();
}
}
inherits(Socket, EventEmitter);
module.exports = Socket;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment