Make GroupStore use MatrixClientPeg

To avoid weirdness with using a cached matrix client
This commit is contained in:
Luke Barnard 2017-11-28 11:54:05 +00:00
parent 1a3ad5a342
commit 7ec4010881
11 changed files with 30 additions and 49 deletions

View file

@ -114,7 +114,7 @@ function _onGroupInviteFinished(groupId, addrs) {
function _onGroupAddRoomFinished(groupId, addrs, addRoomsPublicly) { function _onGroupAddRoomFinished(groupId, addrs, addRoomsPublicly) {
const matrixClient = MatrixClientPeg.get(); const matrixClient = MatrixClientPeg.get();
const groupStore = GroupStoreCache.getGroupStore(matrixClient, groupId); const groupStore = GroupStoreCache.getGroupStore(groupId);
const errorList = []; const errorList = [];
return Promise.all(addrs.map((addr) => { return Promise.all(addrs.map((addr) => {
return groupStore return groupStore

View file

@ -469,7 +469,7 @@ export default React.createClass({
if (group && group.inviter && group.inviter.userId) { if (group && group.inviter && group.inviter.userId) {
this._fetchInviterProfile(group.inviter.userId); this._fetchInviterProfile(group.inviter.userId);
} }
this._groupStore = GroupStoreCache.getGroupStore(this._matrixClient, groupId); this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => { this._groupStore.registerListener(() => {
const summary = this._groupStore.getSummary(); const summary = this._groupStore.getSummary();
if (summary.profile) { if (summary.profile) {

View file

@ -244,7 +244,7 @@ module.exports = React.createClass({
_doNaiveGroupRoomSearch: function(query) { _doNaiveGroupRoomSearch: function(query) {
const lowerCaseQuery = query.toLowerCase(); const lowerCaseQuery = query.toLowerCase();
const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), this.props.groupId); const groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
const results = []; const results = [];
groupStore.getGroupRooms().forEach((r) => { groupStore.getGroupRooms().forEach((r) => {
const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery); const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery);

View file

@ -59,9 +59,7 @@ module.exports = React.createClass({
}, },
_initGroupStore(groupId) { _initGroupStore(groupId) {
this._groupStore = GroupStoreCache.getGroupStore( this._groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
this.context.matrixClient, this.props.groupId,
);
this._groupStore.registerListener(this.onGroupStoreUpdated); this._groupStore.registerListener(this.onGroupStoreUpdated);
}, },

View file

@ -20,17 +20,12 @@ import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache'; import GroupStoreCache from '../../../stores/GroupStoreCache';
import GeminiScrollbar from 'react-gemini-scrollbar'; import GeminiScrollbar from 'react-gemini-scrollbar';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import withMatrixClient from '../../../wrappers/withMatrixClient';
const INITIAL_LOAD_NUM_MEMBERS = 30; const INITIAL_LOAD_NUM_MEMBERS = 30;
export default withMatrixClient(React.createClass({ export default React.createClass({
displayName: 'GroupMemberList', displayName: 'GroupMemberList',
contextTypes: {
matrixClient: PropTypes.object.isRequired,
},
propTypes: { propTypes: {
groupId: PropTypes.string.isRequired, groupId: PropTypes.string.isRequired,
}, },
@ -49,7 +44,7 @@ export default withMatrixClient(React.createClass({
}, },
_initGroupStore: function(groupId) { _initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId); this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => { this._groupStore.registerListener(() => {
this._fetchMembers(); this._fetchMembers();
}); });
@ -174,4 +169,4 @@ export default withMatrixClient(React.createClass({
</div> </div>
); );
}, },
})); });

View file

@ -16,7 +16,6 @@ limitations under the License.
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { MatrixClient } from 'matrix-js-sdk';
import sdk from '../../../index'; import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache'; import GroupStoreCache from '../../../stores/GroupStoreCache';
import GroupStore from '../../../stores/GroupStore'; import GroupStore from '../../../stores/GroupStore';
@ -29,10 +28,6 @@ export default React.createClass({
groupId: PropTypes.string.isRequired, groupId: PropTypes.string.isRequired,
}, },
contextTypes: {
matrixClient: PropTypes.instanceOf(MatrixClient),
},
getInitialState() { getInitialState() {
return { return {
busy: false, busy: false,
@ -46,7 +41,7 @@ export default React.createClass({
}, },
_initGroupStore: function(groupId) { _initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId); this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => { this._groupStore.registerListener(() => {
this.setState({ this.setState({
isGroupPublicised: this._groupStore.getGroupPublicity(), isGroupPublicised: this._groupStore.getGroupPublicity(),

View file

@ -61,9 +61,7 @@ module.exports = React.createClass({
}, },
_initGroupStore(groupId) { _initGroupStore(groupId) {
this._groupStore = GroupStoreCache.getGroupStore( this._groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
this.context.matrixClient, this.props.groupId,
);
this._groupStore.registerListener(this.onGroupStoreUpdated); this._groupStore.registerListener(this.onGroupStoreUpdated);
}, },

View file

@ -19,15 +19,10 @@ import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache'; import GroupStoreCache from '../../../stores/GroupStoreCache';
import GeminiScrollbar from 'react-gemini-scrollbar'; import GeminiScrollbar from 'react-gemini-scrollbar';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import {MatrixClient} from 'matrix-js-sdk';
const INITIAL_LOAD_NUM_ROOMS = 30; const INITIAL_LOAD_NUM_ROOMS = 30;
export default React.createClass({ export default React.createClass({
contextTypes: {
matrixClient: React.PropTypes.instanceOf(MatrixClient).isRequired,
},
propTypes: { propTypes: {
groupId: PropTypes.string.isRequired, groupId: PropTypes.string.isRequired,
}, },
@ -46,7 +41,7 @@ export default React.createClass({
}, },
_initGroupStore: function(groupId) { _initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId); this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => { this._groupStore.registerListener(() => {
this._fetchRooms(); this._fetchRooms();
}); });

View file

@ -17,6 +17,7 @@ limitations under the License.
import EventEmitter from 'events'; import EventEmitter from 'events';
import { groupMemberFromApiObject, groupRoomFromApiObject } from '../groups'; import { groupMemberFromApiObject, groupRoomFromApiObject } from '../groups';
import FlairStore from './FlairStore'; import FlairStore from './FlairStore';
import MatrixClientPeg from '../MatrixClientPeg';
/** /**
* Stores the group summary for a room and provides an API to change it and * Stores the group summary for a room and provides an API to change it and
@ -31,13 +32,12 @@ export default class GroupStore extends EventEmitter {
GroupRooms: 'GroupRooms', GroupRooms: 'GroupRooms',
}; };
constructor(matrixClient, groupId) { constructor(groupId) {
super(); super();
if (!groupId) { if (!groupId) {
throw new Error('GroupStore needs a valid groupId to be created'); throw new Error('GroupStore needs a valid groupId to be created');
} }
this.groupId = groupId; this.groupId = groupId;
this._matrixClient = matrixClient;
this._summary = {}; this._summary = {};
this._rooms = []; this._rooms = [];
this._members = []; this._members = [];
@ -50,7 +50,7 @@ export default class GroupStore extends EventEmitter {
} }
_fetchMembers() { _fetchMembers() {
this._matrixClient.getGroupUsers(this.groupId).then((result) => { MatrixClientPeg.get().getGroupUsers(this.groupId).then((result) => {
this._members = result.chunk.map((apiMember) => { this._members = result.chunk.map((apiMember) => {
return groupMemberFromApiObject(apiMember); return groupMemberFromApiObject(apiMember);
}); });
@ -61,7 +61,7 @@ export default class GroupStore extends EventEmitter {
this.emit('error', err); this.emit('error', err);
}); });
this._matrixClient.getGroupInvitedUsers(this.groupId).then((result) => { MatrixClientPeg.get().getGroupInvitedUsers(this.groupId).then((result) => {
this._invitedMembers = result.chunk.map((apiMember) => { this._invitedMembers = result.chunk.map((apiMember) => {
return groupMemberFromApiObject(apiMember); return groupMemberFromApiObject(apiMember);
}); });
@ -78,7 +78,7 @@ export default class GroupStore extends EventEmitter {
} }
_fetchSummary() { _fetchSummary() {
this._matrixClient.getGroupSummary(this.groupId).then((resp) => { MatrixClientPeg.get().getGroupSummary(this.groupId).then((resp) => {
this._summary = resp; this._summary = resp;
this._ready[GroupStore.STATE_KEY.Summary] = true; this._ready[GroupStore.STATE_KEY.Summary] = true;
this._notifyListeners(); this._notifyListeners();
@ -88,7 +88,7 @@ export default class GroupStore extends EventEmitter {
} }
_fetchRooms() { _fetchRooms() {
this._matrixClient.getGroupRooms(this.groupId).then((resp) => { MatrixClientPeg.get().getGroupRooms(this.groupId).then((resp) => {
this._rooms = resp.chunk.map((apiRoom) => { this._rooms = resp.chunk.map((apiRoom) => {
return groupRoomFromApiObject(apiRoom); return groupRoomFromApiObject(apiRoom);
}); });
@ -145,19 +145,19 @@ export default class GroupStore extends EventEmitter {
} }
addRoomToGroup(roomId, isPublic) { addRoomToGroup(roomId, isPublic) {
return this._matrixClient return MatrixClientPeg.get()
.addRoomToGroup(this.groupId, roomId, isPublic) .addRoomToGroup(this.groupId, roomId, isPublic)
.then(this._fetchRooms.bind(this)); .then(this._fetchRooms.bind(this));
} }
updateGroupRoomVisibility(roomId, isPublic) { updateGroupRoomVisibility(roomId, isPublic) {
return this._matrixClient return MatrixClientPeg.get()
.updateGroupRoomVisibility(this.groupId, roomId, isPublic) .updateGroupRoomVisibility(this.groupId, roomId, isPublic)
.then(this._fetchRooms.bind(this)); .then(this._fetchRooms.bind(this));
} }
removeRoomFromGroup(roomId) { removeRoomFromGroup(roomId) {
return this._matrixClient return MatrixClientPeg.get()
.removeRoomFromGroup(this.groupId, roomId) .removeRoomFromGroup(this.groupId, roomId)
// Room might be in the summary, refresh just in case // Room might be in the summary, refresh just in case
.then(this._fetchSummary.bind(this)) .then(this._fetchSummary.bind(this))
@ -165,12 +165,12 @@ export default class GroupStore extends EventEmitter {
} }
inviteUserToGroup(userId) { inviteUserToGroup(userId) {
return this._matrixClient.inviteUserToGroup(this.groupId, userId) return MatrixClientPeg.get().inviteUserToGroup(this.groupId, userId)
.then(this._fetchMembers.bind(this)); .then(this._fetchMembers.bind(this));
} }
acceptGroupInvite() { acceptGroupInvite() {
return this._matrixClient.acceptGroupInvite(this.groupId) return MatrixClientPeg.get().acceptGroupInvite(this.groupId)
// The user might be able to see more rooms now // The user might be able to see more rooms now
.then(this._fetchRooms.bind(this)) .then(this._fetchRooms.bind(this))
// The user should now appear as a member // The user should now appear as a member
@ -178,33 +178,33 @@ export default class GroupStore extends EventEmitter {
} }
addRoomToGroupSummary(roomId, categoryId) { addRoomToGroupSummary(roomId, categoryId) {
return this._matrixClient return MatrixClientPeg.get()
.addRoomToGroupSummary(this.groupId, roomId, categoryId) .addRoomToGroupSummary(this.groupId, roomId, categoryId)
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
addUserToGroupSummary(userId, roleId) { addUserToGroupSummary(userId, roleId) {
return this._matrixClient return MatrixClientPeg.get()
.addUserToGroupSummary(this.groupId, userId, roleId) .addUserToGroupSummary(this.groupId, userId, roleId)
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
removeRoomFromGroupSummary(roomId) { removeRoomFromGroupSummary(roomId) {
return this._matrixClient return MatrixClientPeg.get()
.removeRoomFromGroupSummary(this.groupId, roomId) .removeRoomFromGroupSummary(this.groupId, roomId)
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
removeUserFromGroupSummary(userId) { removeUserFromGroupSummary(userId) {
return this._matrixClient return MatrixClientPeg.get()
.removeUserFromGroupSummary(this.groupId, userId) .removeUserFromGroupSummary(this.groupId, userId)
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
setGroupPublicity(isPublished) { setGroupPublicity(isPublished) {
return this._matrixClient return MatrixClientPeg.get()
.setGroupPublicity(this.groupId, isPublished) .setGroupPublicity(this.groupId, isPublished)
.then(() => { FlairStore.invalidatePublicisedGroups(this._matrixClient.credentials.userId); }) .then(() => { FlairStore.invalidatePublicisedGroups(MatrixClientPeg.get().credentials.userId); })
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
} }

View file

@ -21,12 +21,12 @@ class GroupStoreCache {
this.groupStore = null; this.groupStore = null;
} }
getGroupStore(matrixClient, groupId) { getGroupStore(groupId) {
if (!this.groupStore || this.groupStore.groupId !== groupId) { if (!this.groupStore || this.groupStore.groupId !== groupId) {
// This effectively throws away the reference to any previous GroupStore, // This effectively throws away the reference to any previous GroupStore,
// allowing it to be GCd once the components referencing it have stopped // allowing it to be GCd once the components referencing it have stopped
// referencing it. // referencing it.
this.groupStore = new GroupStore(matrixClient, groupId); this.groupStore = new GroupStore(groupId);
} }
this.groupStore._fetchSummary(); this.groupStore._fetchSummary();
return this.groupStore; return this.groupStore;

View file

@ -119,7 +119,7 @@ export default class MultiInviter {
let doInvite; let doInvite;
if (this.groupId !== null) { if (this.groupId !== null) {
doInvite = GroupStoreCache doInvite = GroupStoreCache
.getGroupStore(MatrixClientPeg.get(), this.groupId) .getGroupStore(this.groupId)
.inviteUserToGroup(addr); .inviteUserToGroup(addr);
} else { } else {
doInvite = inviteToRoom(this.roomId, addr); doInvite = inviteToRoom(this.roomId, addr);