From c1318e91024323fe466a3092f2d664dc7716b8f7 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 4 Oct 2017 17:51:38 +0100 Subject: [PATCH] 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();