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

First cut

This commit is contained in:
Kegan Dougal
2017-01-26 16:57:59 +00:00
parent 776cfed2b3
commit 522105a858

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@ limitations under the License.
*/ */
"use strict"; "use strict";
import q from "q";
/** /**
* This is an internal module. See {@link IndexedDBStore} for the public class. * This is an internal module. See {@link IndexedDBStore} for the public class.
* @module store/indexeddb * @module store/indexeddb
@@ -41,7 +43,7 @@ module.exports.IndexedDBStore.prototype = {
*/ */
connect: function() { connect: function() {
if (this.db) { if (this.db) {
return Promise.resolve(); return q();
} }
const req = this.indexedDB.open("matrix-js-sdk", VERSION); const req = this.indexedDB.open("matrix-js-sdk", VERSION);
req.onupgradeneeded = (ev) => { req.onupgradeneeded = (ev) => {
@@ -51,19 +53,117 @@ module.exports.IndexedDBStore.prototype = {
createDatabase(db); createDatabase(db);
} }
// Expand as needed. // Expand as needed.
} };
return promiseify(req).then((ev) => { return promiseifyRequest(req).then((ev) => {
this.db = ev.target.result; this.db = ev.target.result;
}); });
},
/**
* Clear the entire database. This should be used when logging out of a client
* to prevent mixing data between accounts.
* @return {Promise} Resolved when the database is cleared.
*/
clearDatabase: function() {
return promiseifyRequest(this.indexedDB.deleteDatabase("matrix-js-sdk"));
},
/**
* Persist a list of Room objects.
* @param {Room[]} rooms An array of rooms
* @return {Promise} Resolves if the rooms were persisted.
*/
persistRooms: function(rooms) {
return q.try(() => {
const txn = this.db.transaction(["rooms"], "readwrite");
const store = txn.objectStore("rooms");
for (let i =0; i < rooms.length; i++) {
store.put(rooms[i]); // put == UPSERT
} }
return promiseifyTxn(txn);
});
},
/**
* Persist a sync token.
* @param {string} syncToken The token to persist.
* @return {Promise} Resolves if the token was persisted.
*/
persistSyncToken: function(syncToken) {
const obj = {
clobber: "-", // constant key so will always clobber
syncToken: syncToken,
};
return q.try(() => {
const txn = this.db.transaction(["config"], "readwrite");
const store = txn.objectStore("config");
store.put(obj); // put == UPSERT
return promiseifyTxn(txn);
});
},
/**
* Persist a list of account data events.
* @param {MatrixEvent[]} accountData An array of user-scoped account data events
* @return {Promise} Resolves if the events were persisted.
*/
persistAccountData: function(accountData) {
return q.try(() => {
const txn = this.db.transaction(["accountData"], "readwrite");
const store = txn.objectStore("accountData");
for (let i =0; i < accountData.length; i++) {
store.put(accountData[i]); // put == UPSERT
} }
return promiseifyTxn(txn);
});
},
/**
* Persist a list of User objects.
* @param {User[]} users An array of users
* @return {Promise} Resolves if the users were persisted.
*/
persistUsers: function(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(users[i]); // put == UPSERT
}
return promiseifyTxn(txn);
});
},
};
function createDatabase(db) { function createDatabase(db) {
// TODO: Make object stores and indexes. // Make room store, clobber based on room ID. (roomId property of Room objects)
db.createObjectStore("rooms", { keyPath: ["roomId"] });
// Make user store, clobber based on user ID. (userId property of User objects)
db.createObjectStore("users", { keyPath: ["userId"] });
// Make account data store, clobber based on event type.
// (event.type property of MatrixEvent objects)
db.createObjectStore("accountData", { keyPath: ["event.type"] });
// Make configuration store (sync tokens, etc), always clobber (const key).
db.createObjectStore("config", { keyPath: ["clobber"] });
} }
function promiseify(req) { function promiseifyTxn(txn) {
return new Promise((resolve, reject) => { return new q.Promise((resolve, reject) => {
txn.oncomplete = function(event) {
resolve(event);
};
txn.onerror = function(event) {
reject(event);
};
});
}
function promiseifyRequest(req) {
return new q.Promise((resolve, reject) => {
req.onsuccess = function(event) { req.onsuccess = function(event) {
resolve(event); resolve(event);
}; };