Merge pull request #1449 from matrix-org/luke/groups-store

Factor-out GroupStore and create GroupStoreCache
pull/21833/head
Luke Barnard 2017-10-04 18:11:11 +01:00 committed by GitHub
commit 2ba0a801c4
4 changed files with 82 additions and 36 deletions

View File

@ -19,6 +19,7 @@ import sdk from './';
import MultiInviter from './utils/MultiInviter'; import MultiInviter from './utils/MultiInviter';
import { _t } from './languageHandler'; import { _t } from './languageHandler';
import MatrixClientPeg from './MatrixClientPeg'; import MatrixClientPeg from './MatrixClientPeg';
import GroupStoreCache from './stores/GroupStoreCache';
export function showGroupInviteDialog(groupId) { export function showGroupInviteDialog(groupId) {
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog"); const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
@ -86,10 +87,11 @@ function _onGroupInviteFinished(groupId, addrs) {
} }
function _onGroupAddRoomFinished(groupId, addrs) { function _onGroupAddRoomFinished(groupId, addrs) {
const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), groupId);
const errorList = []; const errorList = [];
return Promise.all(addrs.map((addr) => { return Promise.all(addrs.map((addr) => {
return MatrixClientPeg.get() return groupStore
.addRoomToGroup(groupId, addr.address) .addRoomToGroup(addr.address)
.catch(() => { errorList.push(addr.address); }) .catch(() => { errorList.push(addr.address); })
.reflect(); .reflect();
})).then(() => { })).then(() => {

View File

@ -27,7 +27,8 @@ import AccessibleButton from '../views/elements/AccessibleButton';
import Modal from '../../Modal'; import Modal from '../../Modal';
import classnames from 'classnames'; import classnames from 'classnames';
import GroupSummaryStore from '../../stores/GroupSummaryStore'; import GroupStoreCache from '../../stores/GroupStoreCache';
import GroupStore from '../../stores/GroupStore';
const RoomSummaryType = PropTypes.shape({ const RoomSummaryType = PropTypes.shape({
room_id: PropTypes.string.isRequired, room_id: PropTypes.string.isRequired,
@ -78,7 +79,7 @@ const CategoryRoomList = React.createClass({
if (!success) return; if (!success) return;
const errorList = []; const errorList = [];
Promise.all(addrs.map((addr) => { Promise.all(addrs.map((addr) => {
return this.context.groupSummaryStore return this.context.groupStore
.addRoomToGroupSummary(addr.address) .addRoomToGroupSummary(addr.address)
.catch(() => { errorList.push(addr.address); }) .catch(() => { errorList.push(addr.address); })
.reflect(); .reflect();
@ -157,7 +158,7 @@ const FeaturedRoom = React.createClass({
onDeleteClicked: function(e) { onDeleteClicked: function(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
this.context.groupSummaryStore.removeRoomFromGroupSummary( this.context.groupStore.removeRoomFromGroupSummary(
this.props.summaryInfo.room_id, this.props.summaryInfo.room_id,
).catch((err) => { ).catch((err) => {
console.error('Error whilst removing room from group summary', err); console.error('Error whilst removing room from group summary', err);
@ -252,7 +253,7 @@ const RoleUserList = React.createClass({
if (!success) return; if (!success) return;
const errorList = []; const errorList = [];
Promise.all(addrs.map((addr) => { Promise.all(addrs.map((addr) => {
return this.context.groupSummaryStore return this.context.groupStore
.addUserToGroupSummary(addr.address) .addUserToGroupSummary(addr.address)
.catch(() => { errorList.push(addr.address); }) .catch(() => { errorList.push(addr.address); })
.reflect(); .reflect();
@ -327,7 +328,7 @@ const FeaturedUser = React.createClass({
onDeleteClicked: function(e) { onDeleteClicked: function(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
this.context.groupSummaryStore.removeUserFromGroupSummary( this.context.groupStore.removeUserFromGroupSummary(
this.props.summaryInfo.user_id, this.props.summaryInfo.user_id,
).catch((err) => { ).catch((err) => {
console.error('Error whilst removing user from group summary', err); console.error('Error whilst removing user from group summary', err);
@ -373,14 +374,14 @@ const FeaturedUser = React.createClass({
}, },
}); });
const GroupSummaryContext = { const GroupContext = {
groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore).isRequired, groupStore: React.PropTypes.instanceOf(GroupStore).isRequired,
}; };
CategoryRoomList.contextTypes = GroupSummaryContext; CategoryRoomList.contextTypes = GroupContext;
FeaturedRoom.contextTypes = GroupSummaryContext; FeaturedRoom.contextTypes = GroupContext;
RoleUserList.contextTypes = GroupSummaryContext; RoleUserList.contextTypes = GroupContext;
FeaturedUser.contextTypes = GroupSummaryContext; FeaturedUser.contextTypes = GroupContext;
export default React.createClass({ export default React.createClass({
displayName: 'GroupView', displayName: 'GroupView',
@ -390,12 +391,12 @@ export default React.createClass({
}, },
childContextTypes: { childContextTypes: {
groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore), groupStore: React.PropTypes.instanceOf(GroupStore),
}, },
getChildContext: function() { getChildContext: function() {
return { return {
groupSummaryStore: this._groupSummaryStore, groupStore: this._groupStore,
}; };
}, },
@ -413,14 +414,14 @@ export default React.createClass({
componentWillMount: function() { componentWillMount: function() {
this._changeAvatarComponent = null; this._changeAvatarComponent = null;
this._initGroupSummaryStore(this.props.groupId); this._initGroupStore(this.props.groupId);
MatrixClientPeg.get().on("Group.myMembership", this._onGroupMyMembership); MatrixClientPeg.get().on("Group.myMembership", this._onGroupMyMembership);
}, },
componentWillUnmount: function() { componentWillUnmount: function() {
MatrixClientPeg.get().removeListener("Group.myMembership", this._onGroupMyMembership); MatrixClientPeg.get().removeListener("Group.myMembership", this._onGroupMyMembership);
this._groupSummaryStore.removeAllListeners(); this._groupStore.removeAllListeners();
}, },
componentWillReceiveProps: function(newProps) { componentWillReceiveProps: function(newProps) {
@ -429,7 +430,7 @@ export default React.createClass({
summary: null, summary: null,
error: null, error: null,
}, () => { }, () => {
this._initGroupSummaryStore(newProps.groupId); this._initGroupStore(newProps.groupId);
}); });
} }
}, },
@ -440,17 +441,15 @@ export default React.createClass({
this.setState({membershipBusy: false}); this.setState({membershipBusy: false});
}, },
_initGroupSummaryStore: function(groupId) { _initGroupStore: function(groupId) {
this._groupSummaryStore = new GroupSummaryStore( this._groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), groupId);
MatrixClientPeg.get(), this.props.groupId, this._groupStore.on('update', () => {
);
this._groupSummaryStore.on('update', () => {
this.setState({ this.setState({
summary: this._groupSummaryStore.getSummary(), summary: this._groupStore.getSummary(),
error: null, error: null,
}); });
}); });
this._groupSummaryStore.on('error', (err) => { this._groupStore.on('error', (err) => {
this.setState({ this.setState({
summary: null, summary: null,
error: err, error: err,
@ -527,7 +526,7 @@ export default React.createClass({
editing: false, editing: false,
summary: null, summary: null,
}); });
this._initGroupSummaryStore(this.props.groupId); this._initGroupStore(this.props.groupId);
}).catch((e) => { }).catch((e) => {
this.setState({ this.setState({
saving: false, saving: false,
@ -606,7 +605,7 @@ export default React.createClass({
this.setState({ this.setState({
publicityBusy: true, publicityBusy: true,
}); });
this._groupSummaryStore.setGroupPublicity(publicity).then(() => { this._groupStore.setGroupPublicity(publicity).then(() => {
this.setState({ this.setState({
publicityBusy: false, publicityBusy: false,
}); });

View File

@ -17,19 +17,20 @@ limitations under the License.
import EventEmitter from 'events'; import EventEmitter from 'events';
/** /**
* Stores the group summary for a room and provides an API to change it * Stores the group summary for a room and provides an API to change it and
* other useful group APIs that may have an effect on the group summary.
*/ */
export default class GroupSummaryStore extends EventEmitter { export default class GroupStore extends EventEmitter {
constructor(matrixClient, groupId) { constructor(matrixClient, groupId) {
super(); super();
this._groupId = groupId; this.groupId = groupId;
this._matrixClient = matrixClient; this._matrixClient = matrixClient;
this._summary = {}; this._summary = {};
this._fetchSummary(); this._fetchSummary();
} }
_fetchSummary() { _fetchSummary() {
this._matrixClient.getGroupSummary(this._groupId).then((resp) => { this._matrixClient.getGroupSummary(this.groupId).then((resp) => {
this._summary = resp; this._summary = resp;
this._notifyListeners(); this._notifyListeners();
}).catch((err) => { }).catch((err) => {
@ -45,33 +46,38 @@ export default class GroupSummaryStore extends EventEmitter {
return this._summary; return this._summary;
} }
addRoomToGroup(roomId) {
return this._matrixClient
.addRoomToGroup(this.groupId, roomId);
}
addRoomToGroupSummary(roomId, categoryId) { addRoomToGroupSummary(roomId, categoryId) {
return this._matrixClient return this._matrixClient
.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 this._matrixClient
.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 this._matrixClient
.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 this._matrixClient
.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 this._matrixClient
.setGroupPublicity(this._groupId, isPublished) .setGroupPublicity(this.groupId, isPublished)
.then(this._fetchSummary.bind(this)); .then(this._fetchSummary.bind(this));
} }
} }

View File

@ -0,0 +1,39 @@
/*
Copyright 2017 New Vector 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.
*/
import GroupStore from './GroupStore';
class GroupStoreCache {
constructor() {
this.groupStore = null;
}
getGroupStore(matrixClient, groupId) {
if (!this.groupStore || this.groupStore.groupId !== groupId) {
// This effectively throws away the reference to any previous GroupStore,
// allowing it to be GCd once the components referencing it have stopped
// referencing it.
this.groupStore = new GroupStore(matrixClient, groupId);
}
return this.groupStore;
}
}
let singletonGroupStoreCache = null;
if (!singletonGroupStoreCache) {
singletonGroupStoreCache = new GroupStoreCache();
}
module.exports = singletonGroupStoreCache;