keep string key since we have to support IE11

This commit is contained in:
Bruno Windels 2020-08-19 16:07:58 +02:00
parent 9ff4f3839c
commit 5ad7b74b2b
2 changed files with 27 additions and 33 deletions

View File

@ -1,5 +1,6 @@
import {iterateCursor} from "./utils.js"; import {iterateCursor} from "./utils.js";
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/RoomMember.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/RoomMember.js";
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
// FUNCTIONS SHOULD ONLY BE APPENDED!! // FUNCTIONS SHOULD ONLY BE APPENDED!!
// the index in the array is the database version // the index in the array is the database version
@ -30,10 +31,7 @@ function createInitialStores(db) {
} }
//v2 //v2
async function createMemberStore(db, txn) { async function createMemberStore(db, txn) {
const roomMembers = db.createObjectStore("roomMembers", {keyPath: [ const roomMembers = new RoomMemberStore(db.createObjectStore("roomMembers", {keyPath: "key"}));
"roomId",
"userId"
]});
// migrate existing member state events over // migrate existing member state events over
const roomState = txn.objectStore("roomState"); const roomState = txn.objectStore("roomState");
await iterateCursor(roomState.openCursor(), entry => { await iterateCursor(roomState.openCursor(), entry => {
@ -41,25 +39,8 @@ async function createMemberStore(db, txn) {
roomState.delete(entry.key); roomState.delete(entry.key);
const member = RoomMember.fromMemberEvent(entry.roomId, entry.event); const member = RoomMember.fromMemberEvent(entry.roomId, entry.event);
if (member) { if (member) {
roomMembers.add(member.serialize()); roomMembers.set(member.serialize());
} }
} }
}); });
} }
function migrateKeyPathToArray(db, isNew) {
if (isNew) {
// create the new stores with the final name
} else {
// create the new stores with a tmp name
// migrate the data over
// change the name
}
// maybe it is ok to just run all the migration steps?
// it might be a bit slower to create a store twice ...
// but at least the path of migration or creating a new store
// will go through the same code
//
// might not even be slower, as this is all happening within one transaction
}

View File

@ -1,24 +1,37 @@
// no historical members for now /*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function encodeKey(roomId, userId) {
return `${roomId}|${userId}`;
}
// no historical members
export class RoomMemberStore { export class RoomMemberStore {
constructor(roomMembersStore) { constructor(roomMembersStore) {
this._roomMembersStore = roomMembersStore; this._roomMembersStore = roomMembersStore;
} }
get(roomId, userId) { get(roomId, userId) {
return this._roomMembersStore.get([roomId, userId]); return this._roomMembersStore.get(encodeKey(roomId, userId));
} }
async set(member) { async set(member) {
member.key = encodeKey(member.roomId, member.userId);
return this._roomMembersStore.put(member); return this._roomMembersStore.put(member);
} }
/*
async getMemberAtSortKey(roomId, userId, sortKey) {
}
async getSortedMembers(roomId, offset, amount) {
}
*/
} }