Commit ebd3c7cd authored by Florian Müllner's avatar Florian Müllner
Browse files

serverRoomManager: Add class for caching room lists

Room lists tend to be mostly static and may take a while to load,
so it makes sense to cache them rather than loading them every
time a list is requested. For that purpose, add a small class to
manage and cache the room list of all active accounts.

https://bugzilla.gnome.org/show_bug.cgi?id=763200
parent acea0169
......@@ -12,6 +12,7 @@ const Lang = imports.lang;
const MainWindow = imports.mainWindow;
const PasteManager = imports.pasteManager;
const RoomManager = imports.roomManager;
const ServerRoomManager = imports.serverRoomManager;
const TelepathyClient = imports.telepathyClient;
const UserTracker = imports.userTracker;
const Utils = imports.utils;
......@@ -169,6 +170,7 @@ const Application = new Lang.Class({
this._accountsMonitor = AccountsMonitor.getDefault();
this._userStatusMonitor = UserTracker.getUserStatusMonitor();
this._networksManager = NetworksManager.getDefault();
this._serverRoomManager = ServerRoomManager.getDefault();
this._accountsMonitor.connect('account-status-changed',
Lang.bind(this, this._onAccountStatusChanged));
......
......@@ -16,6 +16,7 @@
<file>roomList.js</file>
<file>roomManager.js</file>
<file>roomStack.js</file>
<file>serverRoomManager.js</file>
<file>tabCompletion.js</file>
<file>telepathyClient.js</file>
<file>userList.js</file>
......
const GLib = imports.gi.GLib;
const Tp = imports.gi.TelepathyGLib;
const AccountsMonitor = imports.accountsMonitor;
const Lang = imports.lang;
const Signals = imports.signals;
let _singleton = null;
function getDefault() {
if (_singleton == null)
_singleton = new _ServerRoomManager();
return _singleton;
}
const _ServerRoomManager = new Lang.Class({
Name: '_ServerRoomManager',
_init: function() {
this._roomLists = new Map();
this._accountsMonitor = AccountsMonitor.getDefault();
this._accountsMonitor.connect('account-status-changed',
Lang.bind(this, this._onAccountStatusChanged));
this._accountsMonitor.connect('account-removed',
Lang.bind(this, this._onAccountRemoved));
this._accountsMonitor.prepare(() => {
this._accountsMonitor.enabledAccounts.forEach(a => {
this._onAccountStatusChanged(this._accountsMonitor, a);
});
});
},
getRoomInfos: function(account) {
let roomList = this._roomLists.get(account);
if (!roomList || roomList.list.listing)
return [];
return roomList.rooms.slice();
},
isLoading: function(account) {
let roomList = this._roomLists.get(account);
if (!roomList)
return account.connection_status == Tp.ConnectionStatus.CONNECTING;
return roomList.list.listing;
},
_onAccountStatusChanged: function(mon, account) {
if (account.connection_status == Tp.ConnectionStatus.CONNECTING)
this.emit('loading-changed', account);
if (account.connection_status != Tp.ConnectionStatus.CONNECTED)
return;
if (this._roomLists.has(account))
return;
let roomList = new Tp.RoomList({ account: account });
roomList.init_async(GLib.PRIORITY_DEFAULT, null, (o, res) => {
roomList.init_finish(res);
roomList.start();
});
roomList.connect('got-room', Lang.bind(this, this._onGotRoom));
roomList.connect('notify::listing',
Lang.bind(this, this._onListingChanged));
this._roomLists.set(account, { list: roomList, rooms: [] });
},
_onAccountRemoved: function(mon, account) {
let roomList = this._roomLists.get(account);
if (!roomList)
return;
roomList.list.run_dispose();
this._roomLists.delete(account);
},
_onGotRoom: function(list, roomInfo) {
let roomList = this._roomLists.get(list.account);
if (!roomList)
return;
debug('Got room %s for account %s'.format(roomInfo.get_name(),
list.account.display_name));
roomList.rooms.push(roomInfo);
},
_onListingChanged: function(list) {
this.emit('loading-changed', list.account);
}
});
Signals.addSignalMethods(_ServerRoomManager.prototype);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment