From 0b9bc2a7a74fe887f5d3575aea70e36b51df43c7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 10 Feb 2017 16:51:55 +0000 Subject: [PATCH] Remove old (de)serialize stuff. Inline more things --- src/models/user.js | 22 -------------- src/store/indexeddb.js | 67 +++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/models/user.js b/src/models/user.js index c1cf37fda..dbfaeb791 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -19,7 +19,6 @@ limitations under the License. */ const EventEmitter = require("events").EventEmitter; const utils = require("../utils"); - const MatrixEvent = require("./event").MatrixEvent; /** * Construct a new User. A User must have an ID and can optionally have extra @@ -61,27 +60,6 @@ function User(userId) { } utils.inherits(User, EventEmitter); -/** - * Deserialize this user from a JSON object. - * @static - * @param {Object} obj The User object from User.serialize(). - * @return {User} A user - */ -User.deserialize = function(obj) { - const user = new User(obj.userId); - if (obj.event) { - user.setPresenceEvent(new MatrixEvent(obj.event)); - } - return user; -}; - -User.prototype.serialize = function() { - return { - userId: this.userId, - event: (this.events.presence ? this.events.presence.event : null), - }; -}; - /** * Update this User with the given presence event. May fire "User.presence", * "User.avatarUrl" and/or "User.displayName" if this event updates this user's diff --git a/src/store/indexeddb.js b/src/store/indexeddb.js index edaff6c10..25f032274 100644 --- a/src/store/indexeddb.js +++ b/src/store/indexeddb.js @@ -89,13 +89,17 @@ IndexedDBStoreBackend.prototype = { * @return {Promise} Resolves if the data was persisted. */ persistSyncData: function(nextBatch, roomsData) { - const obj = { - clobber: "-", // constant key so will always clobber - nextBatch: nextBatch, - roomsData: roomsData, - }; console.log("persisting sync data"); - return this._upsert("sync", [obj]); + return q.try(() => { + const txn = this.db.transaction(["sync"], "readwrite"); + const store = txn.objectStore("sync"); + store.put({ + clobber: "-", // constant key so will always clobber + nextBatch: nextBatch, + roomsData: roomsData, + }); // put == UPSERT + return promiseifyTxn(txn); + }); }, /** @@ -122,7 +126,19 @@ IndexedDBStoreBackend.prototype = { * @return {Promise} Resolves if the users were persisted. */ persistUsers: function(users) { - return this._upsert("users", users); + return q.try(() => { + const txn = this.db.transaction(["users"], "readwrite"); + const store = txn.objectStore("users"); + for (let i = 0; i < users.length; i++) { + store.put({ + userId: users[i].userId, + event: (users[i].events.presence ? + users[i].events.presence.event : + null), + }); // put == UPSERT + } + return promiseifyTxn(txn); + }); }, /** @@ -130,7 +146,17 @@ IndexedDBStoreBackend.prototype = { * @return {Promise} A list of users. */ loadUsers: function() { - return this._deserializeAll("users", User); + return q.try(() => { + const txn = this.db.transaction(["users"], "readonly"); + const store = txn.objectStore("users"); + return selectQuery(store, undefined, (cursor) => { + const user = new User(cursor.value.userId); + if (cursor.value.event) { + user.setPresenceEvent(new MatrixEvent(cursor.value.event)); + } + return user; + }); + }); }, /** @@ -165,31 +191,6 @@ IndexedDBStoreBackend.prototype = { }); }); }, - - _upsert: function(storeName, rows) { - return q.try(() => { - const txn = this.db.transaction([storeName], "readwrite"); - const store = txn.objectStore(storeName); - for (let i = 0; i < rows.length; i++) { - if (typeof rows[i].serialize === "function") { - store.put(rows[i].serialize()); // put == UPSERT - } else { - store.put(rows[i]); // put == UPSERT - } - } - return promiseifyTxn(txn); - }); - }, - - _deserializeAll: function(storeName, Cls) { - return q.try(() => { - const txn = this.db.transaction([storeName], "readonly"); - const store = txn.objectStore(storeName); - return selectQuery(store, undefined, (cursor) => { - return Cls.deserialize(cursor.value); - }); - }); - }, }; /**