From 4017fa7f1d4581423feff35cdf070bcddfe0b5e2 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 4 Oct 2017 16:56:35 +0100 Subject: [PATCH 1/4] Factor-out GroupStore and create GroupStoreCache In order to provide feedback when adding a room to a group, the group summarry store needs to be extended to store the list of rooms in a group. This commit is the first step required. The next step is to get the GroupRoomList listening to updates from GroupStore and expose the list of rooms from GroupStore. (We're running out of words to describe the hierachy of things that store things) --- src/GroupAddressPicker.js | 6 ++- src/components/structures/GroupView.js | 49 +++++++++---------- .../{GroupSummaryStore.js => GroupStore.js} | 10 +++- src/stores/GroupStoreCache.js | 37 ++++++++++++++ 4 files changed, 73 insertions(+), 29 deletions(-) rename src/stores/{GroupSummaryStore.js => GroupStore.js} (89%) create mode 100644 src/stores/GroupStoreCache.js diff --git a/src/GroupAddressPicker.js b/src/GroupAddressPicker.js index 00eed78f76..cfd2590780 100644 --- a/src/GroupAddressPicker.js +++ b/src/GroupAddressPicker.js @@ -19,6 +19,7 @@ import sdk from './'; import MultiInviter from './utils/MultiInviter'; import { _t } from './languageHandler'; import MatrixClientPeg from './MatrixClientPeg'; +import GroupStoreCache from './stores/GroupStoreCache'; export function showGroupInviteDialog(groupId) { const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog"); @@ -86,10 +87,11 @@ function _onGroupInviteFinished(groupId, addrs) { } function _onGroupAddRoomFinished(groupId, addrs) { + const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), groupId); const errorList = []; return Promise.all(addrs.map((addr) => { - return MatrixClientPeg.get() - .addRoomToGroup(groupId, addr.address) + return groupStore + .addRoomToGroup(addr.address) .catch(() => { errorList.push(addr.address); }) .reflect(); })).then(() => { diff --git a/src/components/structures/GroupView.js b/src/components/structures/GroupView.js index 5381f9add3..337ac6ab75 100644 --- a/src/components/structures/GroupView.js +++ b/src/components/structures/GroupView.js @@ -27,7 +27,8 @@ import AccessibleButton from '../views/elements/AccessibleButton'; import Modal from '../../Modal'; import classnames from 'classnames'; -import GroupSummaryStore from '../../stores/GroupSummaryStore'; +import GroupStoreCache from '../../stores/GroupStoreCache'; +import GroupStore from '../../stores/GroupStore'; const RoomSummaryType = PropTypes.shape({ room_id: PropTypes.string.isRequired, @@ -78,7 +79,7 @@ const CategoryRoomList = React.createClass({ if (!success) return; const errorList = []; Promise.all(addrs.map((addr) => { - return this.context.groupSummaryStore + return this.context.groupStore .addRoomToGroupSummary(addr.address) .catch(() => { errorList.push(addr.address); }) .reflect(); @@ -157,7 +158,7 @@ const FeaturedRoom = React.createClass({ onDeleteClicked: function(e) { e.preventDefault(); e.stopPropagation(); - this.context.groupSummaryStore.removeRoomFromGroupSummary( + this.context.groupStore.removeRoomFromGroupSummary( this.props.summaryInfo.room_id, ).catch((err) => { console.error('Error whilst removing room from group summary', err); @@ -252,7 +253,7 @@ const RoleUserList = React.createClass({ if (!success) return; const errorList = []; Promise.all(addrs.map((addr) => { - return this.context.groupSummaryStore + return this.context.groupStore .addUserToGroupSummary(addr.address) .catch(() => { errorList.push(addr.address); }) .reflect(); @@ -327,7 +328,7 @@ const FeaturedUser = React.createClass({ onDeleteClicked: function(e) { e.preventDefault(); e.stopPropagation(); - this.context.groupSummaryStore.removeUserFromGroupSummary( + this.context.groupStore.removeUserFromGroupSummary( this.props.summaryInfo.user_id, ).catch((err) => { console.error('Error whilst removing user from group summary', err); @@ -373,14 +374,14 @@ const FeaturedUser = React.createClass({ }, }); -const GroupSummaryContext = { - groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore).isRequired, +const GroupContext = { + groupStore: React.PropTypes.instanceOf(GroupStore).isRequired, }; -CategoryRoomList.contextTypes = GroupSummaryContext; -FeaturedRoom.contextTypes = GroupSummaryContext; -RoleUserList.contextTypes = GroupSummaryContext; -FeaturedUser.contextTypes = GroupSummaryContext; +CategoryRoomList.contextTypes = GroupContext; +FeaturedRoom.contextTypes = GroupContext; +RoleUserList.contextTypes = GroupContext; +FeaturedUser.contextTypes = GroupContext; export default React.createClass({ displayName: 'GroupView', @@ -390,12 +391,12 @@ export default React.createClass({ }, childContextTypes: { - groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore), + groupStore: React.PropTypes.instanceOf(GroupStore), }, getChildContext: function() { return { - groupSummaryStore: this._groupSummaryStore, + groupStore: this._groupStore, }; }, @@ -413,14 +414,14 @@ export default React.createClass({ componentWillMount: function() { this._changeAvatarComponent = null; - this._initGroupSummaryStore(this.props.groupId); + this._initGroupStore(this.props.groupId); MatrixClientPeg.get().on("Group.myMembership", this._onGroupMyMembership); }, componentWillUnmount: function() { MatrixClientPeg.get().removeListener("Group.myMembership", this._onGroupMyMembership); - this._groupSummaryStore.removeAllListeners(); + this._groupStore.removeAllListeners(); }, componentWillReceiveProps: function(newProps) { @@ -429,7 +430,7 @@ export default React.createClass({ summary: null, error: null, }, () => { - this._initGroupSummaryStore(newProps.groupId); + this._initGroupStore(newProps.groupId); }); } }, @@ -440,17 +441,15 @@ export default React.createClass({ this.setState({membershipBusy: false}); }, - _initGroupSummaryStore: function(groupId) { - this._groupSummaryStore = new GroupSummaryStore( - MatrixClientPeg.get(), this.props.groupId, - ); - this._groupSummaryStore.on('update', () => { + _initGroupStore: function(groupId) { + this._groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), groupId); + this._groupStore.on('update', () => { this.setState({ - summary: this._groupSummaryStore.getSummary(), + summary: this._groupStore.getSummary(), error: null, }); }); - this._groupSummaryStore.on('error', (err) => { + this._groupStore.on('error', (err) => { this.setState({ summary: null, error: err, @@ -527,7 +526,7 @@ export default React.createClass({ editing: false, summary: null, }); - this._initGroupSummaryStore(this.props.groupId); + this._initGroupStore(this.props.groupId); }).catch((e) => { this.setState({ saving: false, @@ -606,7 +605,7 @@ export default React.createClass({ this.setState({ publicityBusy: true, }); - this._groupSummaryStore.setGroupPublicity(publicity).then(() => { + this._groupStore.setGroupPublicity(publicity).then(() => { this.setState({ publicityBusy: false, }); diff --git a/src/stores/GroupSummaryStore.js b/src/stores/GroupStore.js similarity index 89% rename from src/stores/GroupSummaryStore.js rename to src/stores/GroupStore.js index aa6e74529b..cffffd8b2e 100644 --- a/src/stores/GroupSummaryStore.js +++ b/src/stores/GroupStore.js @@ -17,9 +17,10 @@ limitations under the License. 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 may have an effect on the group summary. */ -export default class GroupSummaryStore extends EventEmitter { +export default class GroupStore extends EventEmitter { constructor(matrixClient, groupId) { super(); this._groupId = groupId; @@ -45,6 +46,11 @@ export default class GroupSummaryStore extends EventEmitter { return this._summary; } + addRoomToGroup(roomId) { + return this._matrixClient + .addRoomToGroup(this._groupId, roomId); + } + addRoomToGroupSummary(roomId, categoryId) { return this._matrixClient .addRoomToGroupSummary(this._groupId, roomId, categoryId) diff --git a/src/stores/GroupStoreCache.js b/src/stores/GroupStoreCache.js new file mode 100644 index 0000000000..ade0445e97 --- /dev/null +++ b/src/stores/GroupStoreCache.js @@ -0,0 +1,37 @@ +/* +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.groupStores = {}; + } + + getGroupStore(matrixClient, groupId) { + if (!this.groupStores[groupId]) { + this.groupStores[groupId] = new GroupStore(matrixClient, groupId); + } + return this.groupStores[groupId]; + } +} + + +let singletonGroupStoreCache = null; +if (!singletonGroupStoreCache) { + singletonGroupStoreCache = new GroupStoreCache(); +} +module.exports = singletonGroupStoreCache; From b16eb1713e3ad231d9b578d9b3259813d8cff60e Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 4 Oct 2017 17:01:44 +0100 Subject: [PATCH 2/4] Typo --- src/stores/GroupStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stores/GroupStore.js b/src/stores/GroupStore.js index cffffd8b2e..f15561bbb0 100644 --- a/src/stores/GroupStore.js +++ b/src/stores/GroupStore.js @@ -18,7 +18,7 @@ import EventEmitter from 'events'; /** * Stores the group summary for a room and provides an API to change it and - * other useful group APIs may have an effect on the group summary. + * other useful group APIs that may have an effect on the group summary. */ export default class GroupStore extends EventEmitter { constructor(matrixClient, groupId) { From c1318e91024323fe466a3092f2d664dc7716b8f7 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 4 Oct 2017 17:51:38 +0100 Subject: [PATCH 3/4] Only maintain one GroupStore in the GroupStoreCache So that the group store data is up-to-date and to prevent group stores hanging around in memory --- src/stores/GroupStore.js | 16 ++++++++-------- src/stores/GroupStoreCache.js | 12 +++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/stores/GroupStore.js b/src/stores/GroupStore.js index f15561bbb0..941f4c8ec2 100644 --- a/src/stores/GroupStore.js +++ b/src/stores/GroupStore.js @@ -23,14 +23,14 @@ import EventEmitter from 'events'; export default class GroupStore extends EventEmitter { constructor(matrixClient, groupId) { super(); - this._groupId = groupId; + this.groupId = groupId; this._matrixClient = matrixClient; this._summary = {}; this._fetchSummary(); } _fetchSummary() { - this._matrixClient.getGroupSummary(this._groupId).then((resp) => { + this._matrixClient.getGroupSummary(this.groupId).then((resp) => { this._summary = resp; this._notifyListeners(); }).catch((err) => { @@ -48,36 +48,36 @@ export default class GroupStore extends EventEmitter { addRoomToGroup(roomId) { return this._matrixClient - .addRoomToGroup(this._groupId, roomId); + .addRoomToGroup(this.groupId, roomId); } addRoomToGroupSummary(roomId, categoryId) { return this._matrixClient - .addRoomToGroupSummary(this._groupId, roomId, categoryId) + .addRoomToGroupSummary(this.groupId, roomId, categoryId) .then(this._fetchSummary.bind(this)); } addUserToGroupSummary(userId, roleId) { return this._matrixClient - .addUserToGroupSummary(this._groupId, userId, roleId) + .addUserToGroupSummary(this.groupId, userId, roleId) .then(this._fetchSummary.bind(this)); } removeRoomFromGroupSummary(roomId) { return this._matrixClient - .removeRoomFromGroupSummary(this._groupId, roomId) + .removeRoomFromGroupSummary(this.groupId, roomId) .then(this._fetchSummary.bind(this)); } removeUserFromGroupSummary(userId) { return this._matrixClient - .removeUserFromGroupSummary(this._groupId, userId) + .removeUserFromGroupSummary(this.groupId, userId) .then(this._fetchSummary.bind(this)); } setGroupPublicity(isPublished) { return this._matrixClient - .setGroupPublicity(this._groupId, isPublished) + .setGroupPublicity(this.groupId, isPublished) .then(this._fetchSummary.bind(this)); } } diff --git a/src/stores/GroupStoreCache.js b/src/stores/GroupStoreCache.js index ade0445e97..fbe456f5dc 100644 --- a/src/stores/GroupStoreCache.js +++ b/src/stores/GroupStoreCache.js @@ -18,18 +18,20 @@ import GroupStore from './GroupStore'; class GroupStoreCache { constructor() { - this.groupStores = {}; + this.groupStore = null; } getGroupStore(matrixClient, groupId) { - if (!this.groupStores[groupId]) { - this.groupStores[groupId] = new GroupStore(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.groupStores[groupId]; + return this.groupStore; } } - let singletonGroupStoreCache = null; if (!singletonGroupStoreCache) { singletonGroupStoreCache = new GroupStoreCache(); From cbb36b163b3d2dbd6f9a95bae1fab994287d0721 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 4 Oct 2017 18:05:40 +0100 Subject: [PATCH 4/4] Typo --- src/stores/GroupStoreCache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stores/GroupStoreCache.js b/src/stores/GroupStoreCache.js index fbe456f5dc..bf340521b5 100644 --- a/src/stores/GroupStoreCache.js +++ b/src/stores/GroupStoreCache.js @@ -22,7 +22,7 @@ class GroupStoreCache { } getGroupStore(matrixClient, 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, // allowing it to be GCd once the components referencing it have stopped // referencing it.