1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

WIP memleak fixes (341->295MB)

This commit is contained in:
Kegan Dougal
2017-03-16 18:02:17 +00:00
parent dec4e67135
commit 0c1c10a0e0
5 changed files with 61 additions and 20 deletions

View File

@@ -2777,7 +2777,7 @@ MatrixClient.prototype.startClient = function(opts) {
} }
// periodically poll for turn servers if we support voip // periodically poll for turn servers if we support voip
checkTurnServers(this); //checkTurnServers(this);
if (this._syncApi) { if (this._syncApi) {
// This shouldn't happen since we thought the client was not running // This shouldn't happen since we thought the client was not running

View File

@@ -231,7 +231,7 @@ export default class DeviceList {
// refresh request). // refresh request).
// By checking it is at least a string, we can eliminate a class of // By checking it is at least a string, we can eliminate a class of
// silly errors. // silly errors.
if (typeof userId !== 'string') { if (typeof userId !== 'string' && typeof userId !== 'object') {
throw new Error('userId must be a string; was '+userId); throw new Error('userId must be a string; was '+userId);
} }
this._pendingUsersWithNewDevices[userId] = true; this._pendingUsersWithNewDevices[userId] = true;

View File

@@ -49,6 +49,8 @@ module.exports.EventStatus = {
CANCELLED: "cancelled", CANCELLED: "cancelled",
}; };
const interns = {};
/** /**
* Construct a Matrix Event object * Construct a Matrix Event object
* @constructor * @constructor
@@ -75,7 +77,31 @@ module.exports.EventStatus = {
module.exports.MatrixEvent = function MatrixEvent( module.exports.MatrixEvent = function MatrixEvent(
event, event,
) { ) {
["state_key", "type", "sender", "room_id"].forEach((prop) => {
if (!event[prop]) {
return;
}
if (!interns[event[prop]]) {
interns[event[prop]] = event[prop];
}
event[prop] = interns[event[prop]];
});
["membership", "avatar_url", "displayname"].forEach((prop) => {
if (!event.content || !event.content[prop]) {
return;
}
if (!interns[event.content[prop]]) {
interns[event.content[prop]] = event.content[prop];
}
event.content[prop] = interns[event.content[prop]];
});
this.event = event || {}; this.event = event || {};
this.sender = null; this.sender = null;
this.target = null; this.target = null;
this.status = null; this.status = null;

View File

@@ -33,7 +33,7 @@ const VERSION = 1;
// so infrequently that the /sync size gets bigger on reload. Writing more // so infrequently that the /sync size gets bigger on reload. Writing more
// often does not affect the length of the pause since the entire /sync // often does not affect the length of the pause since the entire /sync
// response is persisted each time. // response is persisted each time.
const WRITE_DELAY_MS = 1000 * 60 * 5; // once every 5 minutes const WRITE_DELAY_MS = 1000 * 60 * 100; // once every 5 minutes
/** /**
* Construct a new Indexed Database store backend. This requires a call to * Construct a new Indexed Database store backend. This requires a call to

View File

@@ -82,6 +82,9 @@ function SyncApi(client, opts) {
this._keepAliveTimer = null; this._keepAliveTimer = null;
this._connectionReturnedDefer = null; this._connectionReturnedDefer = null;
this._notifEvents = []; // accumulator of sync events in the current sync response this._notifEvents = []; // accumulator of sync events in the current sync response
client._fns = {
// eventName: function
};
if (client.getNotifTimelineSet()) { if (client.getNotifTimelineSet()) {
reEmit(client, client.getNotifTimelineSet(), reEmit(client, client.getNotifTimelineSet(),
@@ -425,7 +428,8 @@ SyncApi.prototype.sync = function() {
// no push rules for guests, no access to POST filter for guests. // no push rules for guests, no access to POST filter for guests.
self._sync({}); self._sync({});
} else { } else {
getPushRules(); //getPushRules();
self._sync({});
} }
}; };
@@ -470,6 +474,7 @@ SyncApi.prototype.retryImmediately = function() {
SyncApi.prototype._sync = function(syncOptions) { SyncApi.prototype._sync = function(syncOptions) {
const client = this.client; const client = this.client;
const self = this; const self = this;
console.log("_sync");
if (!this._running) { if (!this._running) {
debuglog("Sync no longer running: exiting."); debuglog("Sync no longer running: exiting.");
@@ -532,10 +537,13 @@ SyncApi.prototype._sync = function(syncOptions) {
} }
let isCachedResponse = false; let isCachedResponse = false;
console.log("syncOptions => ", syncOptions, self.opts);
if (self.opts.syncAccumulator && !syncOptions.hasSyncedBefore) { if (self.opts.syncAccumulator && !syncOptions.hasSyncedBefore) {
let data = self.opts.syncAccumulator.getJSON(); let data = self.opts.syncAccumulator.getJSON();
// Don't do an HTTP hit to /sync. Instead, load up the persisted /sync data, // Don't do an HTTP hit to /sync. Instead, load up the persisted /sync data,
// if there is data there. // if there is data there.
console.log("Data: ", data);
if (data.nextBatch) { if (data.nextBatch) {
debuglog("sync(): not doing HTTP hit, instead returning stored /sync data"); debuglog("sync(): not doing HTTP hit, instead returning stored /sync data");
// We must deep copy the stored data so that the /sync processing code doesn't // We must deep copy the stored data so that the /sync processing code doesn't
@@ -553,10 +561,15 @@ SyncApi.prototype._sync = function(syncOptions) {
} }
if (!isCachedResponse) { if (!isCachedResponse) {
//debuglog('Starting sync since=' + syncToken); /*
debuglog('Starting sync since=' + syncToken);
this._currentSyncRequest = client._http.authedRequest( this._currentSyncRequest = client._http.authedRequest(
undefined, "GET", "/sync", qps, undefined, clientSideTimeoutMs, undefined, "GET", "/sync", qps, undefined, clientSideTimeoutMs,
); ); */
var d = q.defer();
this._currentSyncRequest = d.promise;
} }
this._currentSyncRequest.done(function(data) { this._currentSyncRequest.done(function(data) {
@@ -1214,21 +1227,23 @@ function createNewUser(client, userId) {
return user; return user;
} }
function reEmit(reEmitEntity, emittableEntity, eventNames) { function reEmit(client, emittableEntity, eventNames) {
utils.forEach(eventNames, function(eventName) { eventNames.forEach((name) => {
// setup a listener on the entity (the Room, User, etc) for this event if (!client._fns[name]) {
emittableEntity.on(eventName, function() { client._fns[name] = function() {
// take the args from the listener and reuse them, adding the // take the args from the listener and reuse them, adding the
// event name to the arg list so it works with .emit() // event name to the arg list so it works with .emit()
// Transformation Example: // Transformation Example:
// listener on "foo" => function(a,b) { ... } // listener on "foo" => function(a,b) { ... }
// Re-emit on "thing" => thing.emit("foo", a, b) // Re-emit on "thing" => thing.emit("foo", a, b)
const newArgs = [eventName]; const newArgs = [name];
for (let i = 0; i < arguments.length; i++) { for (let i = 0; i < arguments.length; i++) {
newArgs.push(arguments[i]); newArgs.push(arguments[i]);
}
client.emit(...newArgs);
} }
reEmitEntity.emit(...newArgs); }
}); emittableEntity.on(name, client._fns[name]);
}); });
} }