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
This commit is contained in:
Luke Barnard 2017-10-04 17:51:38 +01:00
parent b16eb1713e
commit c1318e9102
2 changed files with 15 additions and 13 deletions

View file

@ -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));
}
}

View file

@ -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();