Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Improve RoomList performance via side-stepping React #807

Merged
merged 17 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"react": "^15.4.0",
"react-addons-css-transition-group": "15.3.2",
"react-dom": "^15.4.0",
"react-gemini-scrollbar": "matrix-org/react-gemini-scrollbar#5e97aef",
"react-gemini-scrollbar": "matrix-org/react-gemini-scrollbar#39d858c",
"sanitize-html": "^1.11.1",
"text-encoding-utf-8": "^1.0.1",
"velocity-vector": "vector-im/velocity#059e3b2",
Expand Down
62 changes: 62 additions & 0 deletions src/ConstantTimeDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2017 Vector Creations Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// singleton which dispatches invocations of a given type & argument
// rather than just a type (as per EventEmitter and Flux's dispatcher etc)
//
// This means you can have a single point which listens for an EventEmitter event
// and then dispatches out to one of thousands of RoomTiles (for instance) rather than
// having each RoomTile register for the EventEmitter event and having to
// iterate over all of them.
class ConstantTimeDispatcher {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes ConstantTimeDispatcher different from just using the type and arg as the key name? E.g:

EventEmitter.on(`${type} ${arg}`, function() { ... });
EventEmitter.on("room !curbaf:matrix.org", function() { ... });

From what I can see, there is no difference at all other than sugar coating and not needing to decide on a suitable delimiter (" " in this case). If you want to be pedantic, unregister operations are more expensive in ConstantTimeDispatcher because it requires you to loop over all the listeners for a given type, whereas the EventEmitter form is just a hash lookup.

I'd rather we didn't re-invent the wheel on this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer: CTD actually exposes an API with the right shape & (in future) typing rather than relying on the kludge of concatenating the keys into the event type. Also, we're not using EventEmitter anywhere in matrix-react-sdk (or riot-web) yet, other than receiving events from matrix-js-sdk, so if we're adding a new dispatcher it might as well be CTD as much as EventEmitter.

That said, I'd be happy if CTD became an API facade for EventEmitter in future - or if we skipped it entirely in favour of some other dispatch mechanism in future; see comments below...

constructor() {
// type -> arg -> [ listener(arg, params) ]
this.listeners = {};
}

register(type, arg, listener) {
if (!this.listeners[type]) this.listeners[type] = {};
if (!this.listeners[type][arg]) this.listeners[type][arg] = [];
this.listeners[type][arg].push(listener);
}

unregister(type, arg, listener) {
if (this.listeners[type] && this.listeners[type][arg]) {
var i = this.listeners[type][arg].indexOf(listener);
if (i > -1) {
this.listeners[type][arg].splice(i, 1);
}
}
else {
console.warn("Unregistering unrecognised listener (type=" + type + ", arg=" + arg + ")");
}
}

dispatch(type, arg, params) {
if (!this.listeners[type] || !this.listeners[type][arg]) {
console.warn("No registered listeners for dispatch (type=" + type + ", arg=" + arg + ")");
return;
}
this.listeners[type][arg].forEach(listener=>{
listener.call(arg, params);
});
}
}

if (!global.constantTimeDispatcher) {
global.constantTimeDispatcher = new ConstantTimeDispatcher();
}
module.exports = global.constantTimeDispatcher;
1 change: 1 addition & 0 deletions src/KeyCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ module.exports = {
DELETE: 46,
KEY_D: 68,
KEY_E: 69,
KEY_K: 75,
};
1 change: 1 addition & 0 deletions src/components/structures/TimelinePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ var TimelinePanel = React.createClass({
this.props.timelineSet.room.setUnreadNotificationCount('highlight', 0);
dis.dispatch({
action: 'on_room_read',
room: this.props.timelineSet.room,
});
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/views/dialogs/QuestionDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export default React.createClass({
this.props.onFinished(false);
},

componentDidMount: function() {
if (this.props.focus) {
this.refs.button.focus();
}
},

render: function() {
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
const cancelButton = this.props.hasCancelButton ? (
Expand All @@ -63,7 +69,7 @@ export default React.createClass({
{this.props.description}
</div>
<div className="mx_Dialog_buttons">
<button className="mx_Dialog_primary" onClick={this.onOk} autoFocus={this.props.focus}>
<button ref="button" className="mx_Dialog_primary" onClick={this.onOk}>
{this.props.button}
</button>
{this.props.extraButtons}
Expand Down
Loading