Merge pull request #498 from matrix-org/dbkr/fast_lgm

Bring back the little green men without slowness
This commit is contained in:
Richard van der Hoff 2016-09-27 11:28:48 +01:00 committed by GitHub
commit 30ce35c3b4
3 changed files with 50 additions and 7 deletions

View file

@ -22,6 +22,7 @@ import Notifier from './Notifier'
import UserActivity from './UserActivity'; import UserActivity from './UserActivity';
import Presence from './Presence'; import Presence from './Presence';
import dis from './dispatcher'; import dis from './dispatcher';
import DMRoomMap from './utils/DMRoomMap';
/** /**
* Called at startup, to attempt to build a logged-in Matrix session. It tries * Called at startup, to attempt to build a logged-in Matrix session. It tries
@ -317,6 +318,7 @@ export function startMatrixClient() {
Notifier.start(); Notifier.start();
UserActivity.start(); UserActivity.start();
Presence.start(); Presence.start();
DMRoomMap.makeShared().start();
MatrixClientPeg.start(); MatrixClientPeg.start();
} }
@ -354,6 +356,7 @@ export function stopMatrixClient() {
Notifier.stop(); Notifier.stop();
UserActivity.stop(); UserActivity.stop();
Presence.stop(); Presence.stop();
DMRoomMap.shared().stop();
var cli = MatrixClientPeg.get(); var cli = MatrixClientPeg.get();
if (cli) { if (cli) {
cli.stopClient(); cli.stopClient();

View file

@ -70,8 +70,7 @@ module.exports = React.createClass({
}, },
_isDirectMessageRoom: function(roomId) { _isDirectMessageRoom: function(roomId) {
const dmRoomMap = new DMRoomMap(MatrixClientPeg.get()); var dmRooms = DMRoomMap.shared().getUserIdForRoomId(roomId);
var dmRooms = dmRoomMap.getUserIdForRoomId(roomId);
if (dmRooms) { if (dmRooms) {
return true; return true;
} else { } else {
@ -277,11 +276,9 @@ module.exports = React.createClass({
var RoomAvatar = sdk.getComponent('avatars.RoomAvatar'); var RoomAvatar = sdk.getComponent('avatars.RoomAvatar');
var directMessageIndicator; var directMessageIndicator;
// Temporarily turning off the LGM badges as isDirectMessageRoom is horribly unperformant if (this._isDirectMessageRoom(this.props.room.roomId)) {
// - see https://github.com/vector-im/vector-web/issues/2343 directMessageIndicator = <img src="img/icon_person.svg" className="mx_RoomTile_dm" width="11" height="13" alt="dm"/>;
// if (this._isDirectMessageRoom(this.props.room.roomId)) { }
// directMessageIndicator = <img src="img/icon_person.svg" className="mx_RoomTile_dm" width="11" height="13" alt="dm"/>;
// }
// These props are injected by React DnD, // These props are injected by React DnD,
// as defined by your `collect` function above: // as defined by your `collect` function above:

View file

@ -14,16 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import MatrixClientPeg from '../MatrixClientPeg';
/** /**
* Class that takes a Matrix Client and flips the m.direct map * Class that takes a Matrix Client and flips the m.direct map
* so the operation of mapping a room ID to which user it's a DM * so the operation of mapping a room ID to which user it's a DM
* with can be performed efficiently. * with can be performed efficiently.
*
* With 'start', this can also keep itself up to date over time.
*/ */
export default class DMRoomMap { export default class DMRoomMap {
constructor(matrixClient) { constructor(matrixClient) {
this.matrixClient = matrixClient; this.matrixClient = matrixClient;
this.roomToUser = null; this.roomToUser = null;
// XXX: Force-bind the event handler method because it
// doesn't call it with our object as the 'this'
// (use a static property arrow function for this when we can)
this._onAccountData = this._onAccountData.bind(this);
const mDirectEvent = matrixClient.getAccountData('m.direct'); const mDirectEvent = matrixClient.getAccountData('m.direct');
if (!mDirectEvent) { if (!mDirectEvent) {
this.userToRooms = {}; this.userToRooms = {};
@ -32,6 +41,40 @@ export default class DMRoomMap {
} }
} }
/**
* Makes and returns a new shared instance that can then be accessed
* with shared(). This returned instance is not automatically started.
*/
static makeShared() {
DMRoomMap._sharedInstance = new DMRoomMap(MatrixClientPeg.get());
return DMRoomMap._sharedInstance;
}
/**
* Returns a shared instance of the class
* that uses the singleton matrix client
* The shared instance must be started before use.
*/
static shared() {
return DMRoomMap._sharedInstance;
}
start() {
this._populateRoomToUser();
this.matrixClient.on("accountData", this._onAccountData);
}
stop() {
this.matrixClient.removeListener("accountData", this._onAccountData);
}
_onAccountData(ev) {
if (ev.getType() == 'm.direct') {
this.userToRooms = this.matrixClient.getAccountData('m.direct').getContent();
this._populateRoomToUser();
}
}
getDMRoomsForUserId(userId) { getDMRoomsForUserId(userId) {
// Here, we return the empty list if there are no rooms, // Here, we return the empty list if there are no rooms,
// since the number of conversations you have with this user is zero. // since the number of conversations you have with this user is zero.