1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

Remove old (de)serialize stuff. Inline more things

This commit is contained in:
Kegan Dougal
2017-02-10 16:51:55 +00:00
parent 6949b6666e
commit 0b9bc2a7a7
2 changed files with 34 additions and 55 deletions

View File

@@ -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

View File

@@ -89,13 +89,17 @@ IndexedDBStoreBackend.prototype = {
* @return {Promise} Resolves if the data was persisted.
*/
persistSyncData: function(nextBatch, roomsData) {
const obj = {
console.log("persisting sync data");
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,
};
console.log("persisting sync data");
return this._upsert("sync", [obj]);
}); // 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<User[]>} 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);
});
});
},
};
/**