From bdaad5b940af6edb6cdc2926e294f1da4394435a Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 3 Jun 2015 10:37:41 +0100 Subject: [PATCH] Fix up stylistic warnings. --- lib/matrix-promise.js | 4 + lib/matrix.js | 184 +++++++++++++++++++++++++----------------- 2 files changed, 112 insertions(+), 76 deletions(-) diff --git a/lib/matrix-promise.js b/lib/matrix-promise.js index 66e362691..1e938b779 100644 --- a/lib/matrix-promise.js +++ b/lib/matrix-promise.js @@ -34,5 +34,9 @@ matrixcs.request(function(opts, callback) { }); return defer.promise; }); + +/** + * Export a modified matrix library with Promise support. + */ module.exports = matrixcs; diff --git a/lib/matrix.js b/lib/matrix.js index f3056f5f8..6345fa77c 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -9,7 +9,7 @@ TODO: */ // wrap in a closure for browsers -var init = function(exports){ +var init = function(exports) { // expose the underlying request object so different environments can use // different request libs (e.g. request or browser-request) var request; @@ -35,7 +35,7 @@ var init = function(exports){ var requiredKeys = [ "baseUrl" ]; - for (var i=0; i 1) { return displayName + " (" + userId + ")"; } @@ -361,17 +383,21 @@ var init = function(exports){ * High level helper method to call initialSync, emit the resulting events, * and then start polling the eventStream for new events. * @param {function} callback Callback invoked whenever new event are available - * @param {Number} historyLen amount of historical timeline events to emit during from the initial sync + * @param {Number} historyLen amount of historical timeline events to + * emit during from the initial sync. */ startClient: function(callback, historyLen) { historyLen = historyLen || 12; - + var self = this; if (!this.fromToken) { this.initialSync(historyLen, function(err, data) { if (err) { if (this.config && this.config.debug) { - console.error("startClient error on initialSync: %s", JSON.stringify(err)); + console.error( + "startClient error on initialSync: %s", + JSON.stringify(err) + ); } callback(err); } else { @@ -385,7 +411,9 @@ var init = function(exports){ events.push(new MatrixEvent(data.rooms[i].state[j])); } for (j = 0; j < data.rooms[i].messages.chunk.length; j++) { - events.push(new MatrixEvent(data.rooms[i].messages.chunk[j])); + events.push( + new MatrixEvent(data.rooms[i].messages.chunk[j]) + ); } } callback(undefined, events, false); @@ -398,7 +426,7 @@ var init = function(exports){ this._pollForEvents(callback); } }, - + _pollForEvents: function(callback) { var self = this; if (!this.clientRunning) { @@ -407,7 +435,10 @@ var init = function(exports){ this.eventStream(this.fromToken, 30000, function(err, data) { if (err) { if (this.config && this.config.debug) { - console.error("error polling for events via eventStream: %s", JSON.stringify(err)); + console.error( + "error polling for events via eventStream: %s", + JSON.stringify(err) + ); } callback(err); // retry every few seconds @@ -419,15 +450,16 @@ var init = function(exports){ var events = []; for (var j = 0; j < data.chunk.length; j++) { events.push(new MatrixEvent(data.chunk[j])); - } + } callback(undefined, events, true); self._pollForEvents(callback); } }); }, - + /* - * High level helper method to stop the client from polling and allow a clean shutdown + * High level helper method to stop the client from polling and allow a + * clean shutdown. */ stopClient: function() { this.clientRunning = false; @@ -449,12 +481,12 @@ var init = function(exports){ }, setRoomName: function(roomId, name, callback) { - return this.sendStateEvent(roomId, "m.room.name", {name: name}, + return this.sendStateEvent(roomId, "m.room.name", {name: name}, undefined, callback); }, setRoomTopic: function(roomId, topic, callback) { - return this.sendStateEvent(roomId, "m.room.topic", {topic: topic}, + return this.sendStateEvent(roomId, "m.room.topic", {topic: topic}, undefined, callback); }, @@ -592,7 +624,7 @@ var init = function(exports){ }, invite: function(roomId, userId, callback) { - return this._membershipChange(roomId, userId, "invite", undefined, + return this._membershipChange(roomId, userId, "invite", undefined, callback); }, @@ -629,7 +661,7 @@ var init = function(exports){ ); return this._doAuthedRequest(callback, "PUT", path, undefined, { - membership : membershipValue, + membership: membershipValue, reason: reason }); }, @@ -656,11 +688,11 @@ var init = function(exports){ getProfileInfo: function(userId, info, callback) { if (isFunction(info)) { callback = info; info = undefined; } - var path = info ? - encodeUri("/profile/$userId/$info", - { $userId: userId, $info: info } ) : - encodeUri("/profile/$userId", - { $userId: userId } ); + var path = info ? + encodeUri("/profile/$userId/$info", + { $userId: userId, $info: info }) : + encodeUri("/profile/$userId", + { $userId: userId }); return this._doAuthedRequest(callback, "GET", path); }, @@ -710,7 +742,7 @@ var init = function(exports){ }); var validStates = ["offline", "online", "unavailable"]; if (validStates.indexOf(presence) == -1) { - throw new Error("Bad presence value: "+presence); + throw new Error("Bad presence value: " + presence); } var content = { presence: presence @@ -758,7 +790,7 @@ var init = function(exports){ self.store.setPresenceEvents( map(data.presence, eventMapper) ); - for (var i = 0 ; i < data.rooms.length; i++) { + for (var i = 0; i < data.rooms.length; i++) { self.store.setStateEvents( map(data.rooms[i].state, eventMapper) ); @@ -777,7 +809,7 @@ var init = function(exports){ roomInitialSync: function(roomId, limit, callback) { if (isFunction(limit)) { callback = limit; limit = undefined; } - var path = encodeUri("/rooms/$roomId/initialSync", + var path = encodeUri("/rooms/$roomId/initialSync", {$roomId: roomId} ); if (!limit) { @@ -868,7 +900,7 @@ var init = function(exports){ addPushRule: function(scope, kind, ruleId, body, callback) { // NB. Scope not uri encoded because devices need the '/' - var path = encodeUri("/pushrules/"+scope+"/$kind/$ruleId", { + var path = encodeUri("/pushrules/" + scope + "/$kind/$ruleId", { $kind: kind, $ruleId: ruleId }); @@ -879,7 +911,7 @@ var init = function(exports){ deletePushRule: function(scope, kind, ruleId, callback) { // NB. Scope not uri encoded because devices need the '/' - var path = encodeUri("/pushrules/"+scope+"/$kind/$ruleId", { + var path = encodeUri("/pushrules/" + scope + "/$kind/$ruleId", { $kind: kind, $ruleId: ruleId }); @@ -947,15 +979,15 @@ var init = function(exports){ var path = encodeUri("/_matrix/media/v1/identicon/$ident", { $ident: identiconString }); - return this.credentials.baseUrl + path + - (Object.keys(params).length === 0 ? "" : + return this.credentials.baseUrl + path + + (Object.keys(params).length === 0 ? "" : ("?" + encodeParams(params))); }, /** * Get the content repository url with query parameters. - * @returns An object with a 'base', 'path' and 'params' for base URL, - * path and query parameters respectively. + * @return {Object} An object with a 'base', 'path' and 'params' for + * base URL, path and query parameters respectively. */ getContentUri: function() { var params = { @@ -986,7 +1018,7 @@ var init = function(exports){ _doRequest: function(callback, method, path, params, data) { var fullUri = this.credentials.baseUrl + CLIENT_PREFIX + path; if (!params) { params = {}; } - return this._request(callback, method, fullUri, params, data); + return this._request(callback, method, fullUri, params, data); }, _doV2Request: function(callback, method, path, params, data) { @@ -1069,7 +1101,7 @@ var init = function(exports){ if (typeof exports === 'undefined') { // this assigns to "window" on browsers - init(this.matrixcs={}); // jshint ignore:line + init(this.matrixcs = {}); // jshint ignore:line } else { init(exports);