Add method to get member from storage

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-07-19 19:36:09 +05:30
parent 44b25e4157
commit 05f05bb577
3 changed files with 37 additions and 1 deletions

View file

@ -21,7 +21,7 @@ import {RelationWriter} from "./timeline/persistence/RelationWriter.js";
import {Timeline} from "./timeline/Timeline.js"; import {Timeline} from "./timeline/Timeline.js";
import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js"; import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js";
import {WrappedError} from "../error.js" import {WrappedError} from "../error.js"
import {fetchOrLoadMembers} from "./members/load.js"; import {fetchOrLoadMembers, loadMember} from "./members/load.js";
import {MemberList} from "./members/MemberList.js"; import {MemberList} from "./members/MemberList.js";
import {Heroes} from "./members/Heroes.js"; import {Heroes} from "./members/Heroes.js";
import {EventEntry} from "./timeline/entries/EventEntry.js"; import {EventEntry} from "./timeline/entries/EventEntry.js";
@ -54,6 +54,7 @@ export class BaseRoom extends EventEmitter {
this._observedEvents = null; this._observedEvents = null;
this._powerLevels = null; this._powerLevels = null;
this._powerLevelLoading = null; this._powerLevelLoading = null;
this._observedMembers = null;
} }
async _eventIdsToEntries(eventIds, txn) { async _eventIdsToEntries(eventIds, txn) {
@ -214,6 +215,23 @@ export class BaseRoom extends EventEmitter {
} }
} }
async observeMember(userId) {
if (!this._observedMembers) {
this._observedMembers = new Map();
}
const mapMember = this._observedMembers.get(userId);
if (mapMember) {
// Hit, we're already observing this member
return mapMember;
}
// Miss, load from storage and set in map
const member = await loadMember({roomId: this._roomId, userId, storage: this._storage});
const observableMember = new RetainedObservableValue(member, () => this._observedMembers.delete(userId));
this._observedMembers.set(userId, observableMember);
return observableMember;
}
/** @public */ /** @public */
async loadMemberList(log = null) { async loadMemberList(log = null) {
if (this._memberList) { if (this._memberList) {

View file

@ -209,6 +209,9 @@ export class Room extends BaseRoom {
if (this._memberList) { if (this._memberList) {
this._memberList.afterSync(memberChanges); this._memberList.afterSync(memberChanges);
} }
if (this._observedMembers) {
this._updateObservedMembers(memberChanges);
}
if (this._timeline) { if (this._timeline) {
for (const [userId, memberChange] of memberChanges.entries()) { for (const [userId, memberChange] of memberChanges.entries()) {
if (userId === this._user.id) { if (userId === this._user.id) {
@ -250,6 +253,15 @@ export class Room extends BaseRoom {
} }
} }
_updateObservedMembers(memberChanges) {
for (const [userId, memberChange] of memberChanges) {
const observableMember = this._observedMembers.get(userId);
if (observableMember) {
observableMember.set(memberChange.member);
}
}
}
needsAfterSyncCompleted({shouldFlushKeyShares}) { needsAfterSyncCompleted({shouldFlushKeyShares}) {
return shouldFlushKeyShares; return shouldFlushKeyShares;
} }

View file

@ -90,3 +90,9 @@ export async function fetchOrLoadMembers(options, logger) {
return loadMembers(options); return loadMembers(options);
} }
} }
export async function loadMember({roomId, userId, storage}) {
const txn = await storage.readTxn([storage.storeNames.roomMembers,]);
const member = await txn.roomMembers.get(roomId, userId);
return new RoomMember(member);
}