Migrate OperationStore to TypeScript

This commit is contained in:
Danila Fedorin 2021-08-12 11:05:55 -07:00
parent 8c966627bc
commit 77f75fd968
3 changed files with 35 additions and 14 deletions

View File

@ -32,7 +32,7 @@ import {OlmSessionStore} from "./stores/OlmSessionStore.js";
import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js"; import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js";
import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore"; import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore";
import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore"; import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore";
import {OperationStore} from "./stores/OperationStore.js"; import {OperationStore} from "./stores/OperationStore";
import {AccountDataStore} from "./stores/AccountDataStore.js"; import {AccountDataStore} from "./stores/AccountDataStore.js";
export class Transaction { export class Transaction {

View File

@ -2,7 +2,7 @@ import {iterateCursor, reqAsPromise} from "./utils";
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js";
import {RoomMemberStore} from "./stores/RoomMemberStore"; import {RoomMemberStore} from "./stores/RoomMemberStore";
import {SessionStore} from "./stores/SessionStore"; import {SessionStore} from "./stores/SessionStore";
import {encodeScopeTypeKey} from "./stores/OperationStore.js"; import {encodeScopeTypeKey} from "./stores/OperationStore";
// 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

View File

@ -14,23 +14,43 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {MIN_UNICODE, MAX_UNICODE} from "./common"; import {MIN_UNICODE, MAX_UNICODE} from "./common";
import {Store} from "../Store";
export function encodeScopeTypeKey(scope, type) { export function encodeScopeTypeKey(scope: string, type: string): string {
return `${scope}|${type}`; return `${scope}|${type}`;
} }
interface Operation {
id: string;
type: string;
scope: string;
userIds: string[];
scopeTypeKey: string;
roomKeyMessage: RoomKeyMessage;
}
interface RoomKeyMessage {
room_id: string;
session_id: string;
session_key: string;
algorithm: string;
chain_index: number;
}
export class OperationStore { export class OperationStore {
constructor(store) { private _store: Store<Operation>;
constructor(store: Store<Operation>) {
this._store = store; this._store = store;
} }
getAll() { getAll(): Promise<Operation[]> {
return this._store.selectAll(); return this._store.selectAll();
} }
async getAllByTypeAndScope(type, scope) { async getAllByTypeAndScope(type: string, scope: string): Promise<Operation[]> {
const key = encodeScopeTypeKey(scope, type); const key = encodeScopeTypeKey(scope, type);
const results = []; const results: Operation[] = [];
await this._store.index("byScopeAndType").iterateWhile(key, value => { await this._store.index("byScopeAndType").iterateWhile(key, value => {
if (value.scopeTypeKey !== key) { if (value.scopeTypeKey !== key) {
return false; return false;
@ -41,20 +61,20 @@ export class OperationStore {
return results; return results;
} }
add(operation) { add(operation: Operation): Promise<IDBValidKey> {
operation.scopeTypeKey = encodeScopeTypeKey(operation.scope, operation.type); operation.scopeTypeKey = encodeScopeTypeKey(operation.scope, operation.type);
this._store.add(operation); return this._store.add(operation);
} }
update(operation) { update(operation: Operation): Promise<IDBValidKey> {
this._store.put(operation); return this._store.put(operation);
} }
remove(id) { remove(id: string): Promise<undefined> {
this._store.delete(id); return this._store.delete(id);
} }
async removeAllForScope(scope) { async removeAllForScope(scope: string): Promise<undefined> {
const range = this._store.IDBKeyRange.bound( const range = this._store.IDBKeyRange.bound(
encodeScopeTypeKey(scope, MIN_UNICODE), encodeScopeTypeKey(scope, MIN_UNICODE),
encodeScopeTypeKey(scope, MAX_UNICODE) encodeScopeTypeKey(scope, MAX_UNICODE)
@ -64,5 +84,6 @@ export class OperationStore {
cur.delete(); cur.delete();
return true; return true;
}); });
return;
} }
} }