1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

move getOrCreateClient from sync.js to client.js

This commit is contained in:
Matthew Hodgson
2016-09-05 02:44:24 +01:00
parent 2daa1b6007
commit ed5c061566
2 changed files with 50 additions and 49 deletions

View File

@@ -2277,6 +2277,54 @@ MatrixClient.prototype.getFilter = function(userId, filterId, allowCached) {
}); });
}; };
/**
* @param {string} filterName
* @param {Filter} filter
* @return {Promise<String>} Filter ID
*/
MatrixClient.prototype.getOrCreateFilter = function(filterName, filter) {
var filterId = this.store.getFilterIdByName(filterName);
var promise = q();
var self = this;
if (filterId) {
// check that the existing filter matches our expectations
promise = self.getFilter(self.credentials.userId,
filterId, true
).then(function(existingFilter) {
var oldDef = existingFilter.getDefinition();
var newDef = filter.getDefinition();
if (utils.deepCompare(oldDef, newDef)) {
// super, just use that.
// debuglog("Using existing filter ID %s: %s", filterId,
// JSON.stringify(oldDef));
return q(filterId);
}
// debuglog("Existing filter ID %s: %s; new filter: %s",
// filterId, JSON.stringify(oldDef), JSON.stringify(newDef));
return;
});
}
return promise.then(function(existingId) {
if (existingId) {
return existingId;
}
// create a new filter
return self.createFilter(filter.getDefinition()
).then(function(createdFilter) {
// debuglog("Created new filter ID %s: %s", createdFilter.filterId,
// JSON.stringify(createdFilter.getDefinition()));
self.store.setFilterIdByName(filterName, createdFilter.filterId);
return createdFilter.filterId;
});
});
};
/** /**
* Gets a bearer token from the Home Server that the user can * Gets a bearer token from the Home Server that the user can
* present to a third party in order to prove their ownership * present to a third party in order to prove their ownership

View File

@@ -148,7 +148,7 @@ SyncApi.prototype.syncLeftRooms = function() {
timeout: 0 // don't want to block since this is a single isolated req timeout: 0 // don't want to block since this is a single isolated req
}; };
return this._getOrCreateFilter( return client.getOrCreateFilter(
getFilterName(client.credentials.userId, "LEFT_ROOMS"), filter getFilterName(client.credentials.userId, "LEFT_ROOMS"), filter
).then(function(filterId) { ).then(function(filterId) {
qps.filter = filterId; qps.filter = filterId;
@@ -389,7 +389,7 @@ SyncApi.prototype.sync = function() {
var filter = new Filter(client.credentials.userId); var filter = new Filter(client.credentials.userId);
filter.setTimelineLimit(self.opts.initialSyncLimit); filter.setTimelineLimit(self.opts.initialSyncLimit);
self._getOrCreateFilter( client.getOrCreateFilter(
getFilterName(client.credentials.userId), filter getFilterName(client.credentials.userId), filter
).done(function(filterId) { ).done(function(filterId) {
self._sync({ filterId: filterId }); self._sync({ filterId: filterId });
@@ -837,53 +837,6 @@ SyncApi.prototype._pokeKeepAlive = function() {
}); });
}; };
/**
* @param {string} filterName
* @param {Filter} filter
* @return {Promise<String>} Filter ID
*/
SyncApi.prototype._getOrCreateFilter = function(filterName, filter) {
var client = this.client;
var filterId = client.store.getFilterIdByName(filterName);
var promise = q();
if (filterId) {
// check that the existing filter matches our expectations
promise = client.getFilter(client.credentials.userId,
filterId, true
).then(function(existingFilter) {
var oldDef = existingFilter.getDefinition();
var newDef = filter.getDefinition();
if (utils.deepCompare(oldDef, newDef)) {
// super, just use that.
debuglog("Using existing filter ID %s: %s", filterId,
JSON.stringify(oldDef));
return q(filterId);
}
debuglog("Existing filter ID %s: %s; new filter: %s",
filterId, JSON.stringify(oldDef), JSON.stringify(newDef));
return;
});
}
return promise.then(function(existingId) {
if (existingId) {
return existingId;
}
// create a new filter
return client.createFilter(filter.getDefinition()
).then(function(createdFilter) {
debuglog("Created new filter ID %s: %s", createdFilter.filterId,
JSON.stringify(createdFilter.getDefinition()));
client.store.setFilterIdByName(filterName, createdFilter.filterId);
return createdFilter.filterId;
});
});
};
/** /**
* @param {Object} obj * @param {Object} obj
* @return {Object[]} * @return {Object[]}