From 0bb3cfcfadec1e0b223133ce24ad7152fc83b30c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 4 Feb 2022 17:49:10 +0100 Subject: [PATCH] WIP3 --- .../session/leftpanel/LeftPanelViewModel.js | 7 +- src/logging/LogItem.ts | 3 +- src/logging/types.ts | 2 +- src/matrix/Session.js | 18 ++--- src/matrix/Sync.js | 2 +- src/matrix/room/RoomSummary.js | 5 +- src/matrix/room/create.ts | 6 +- src/observable/map/LogMap.js | 70 +++++++++++++++++++ 8 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 src/observable/map/LogMap.js diff --git a/src/domain/session/leftpanel/LeftPanelViewModel.js b/src/domain/session/leftpanel/LeftPanelViewModel.js index d85e5943..597c0da6 100644 --- a/src/domain/session/leftpanel/LeftPanelViewModel.js +++ b/src/domain/session/leftpanel/LeftPanelViewModel.js @@ -21,6 +21,7 @@ import {InviteTileViewModel} from "./InviteTileViewModel.js"; import {RoomBeingCreatedTileViewModel} from "./RoomBeingCreatedTileViewModel.js"; import {RoomFilter} from "./RoomFilter.js"; import {ApplyMap} from "../../../observable/map/ApplyMap.js"; +import {LogMap} from "../../../observable/map/LogMap.js"; import {addPanelIfNeeded} from "../../navigation/index.js"; export class LeftPanelViewModel extends ViewModel { @@ -38,7 +39,7 @@ export class LeftPanelViewModel extends ViewModel { _mapTileViewModels(roomsBeingCreated, invites, rooms) { // join is not commutative, invites will take precedence over rooms - return roomsBeingCreated.join(invites).join(rooms).mapValues((item, emitChange) => { + const joined = invites.join(roomsBeingCreated, rooms).mapValues((item, emitChange) => { let vm; if (item.isBeingCreated) { vm = new RoomBeingCreatedTileViewModel(this.childOptions({roomBeingCreated: item, emitChange})); @@ -54,6 +55,10 @@ export class LeftPanelViewModel extends ViewModel { } return vm; }); + return joined; + // return new LogMap(joined, (op, key, value) => { + // console.log("room list", op, key, value); + // }); } _updateCurrentVM(vm) { diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index cf3da284..b47b69c1 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -108,13 +108,14 @@ export class LogItem implements ILogItem { return item; } - set(key: string | object, value?: unknown): void { + set(key: string | object, value?: unknown): ILogItem { if(typeof key === "object") { const values = key; Object.assign(this._values, values); } else { this._values[key] = value; } + return this; } serialize(filter: LogFilter, parentStartTime: number | undefined, forced: boolean): ISerializedItem | undefined { diff --git a/src/logging/types.ts b/src/logging/types.ts index f4a20ee0..bf9861a5 100644 --- a/src/logging/types.ts +++ b/src/logging/types.ts @@ -43,7 +43,7 @@ export interface ILogItem { readonly values: LogItemValues; wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T; log(labelOrValues: LabelOrValues, logLevel?: LogLevel): ILogItem; - set(key: string | object, value: unknown): void; + set(key: string | object, value: unknown): ILogItem; runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem; wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void; refDetached(logItem: ILogItem, logLevel?: LogLevel): void; diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 49ae0f9c..4a9b0bad 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -64,10 +64,10 @@ export class Session { this._activeArchivedRooms = new Map(); this._invites = new ObservableMap(); this._inviteUpdateCallback = (invite, params) => this._invites.update(invite.id, params); - this._roomsBeingCreatedUpdateCallback = (rbc, params) => { + this._roomsBeingCreatedUpdateCallback = (rbc, params, log) => { this._roomsBeingCreated.update(rbc.localId, params); if (rbc.roomId && !!this.rooms.get(rbc.roomId)) { - this._tryReplaceRoomBeingCreated(rbc.roomId); + this._tryReplaceRoomBeingCreated(rbc.roomId, log); } }; this._roomsBeingCreated = new ObservableMap(); @@ -686,16 +686,16 @@ export class Session { } } - _tryReplaceRoomBeingCreated(roomId) { - console.trace("_tryReplaceRoomBeingCreated " + roomId); + _tryReplaceRoomBeingCreated(roomId, log) { for (const [,roomBeingCreated] of this._roomsBeingCreated) { if (roomBeingCreated.roomId === roomId) { const observableStatus = this._observedRoomStatus.get(roomBeingCreated.localId); if (observableStatus) { - console.log("marking room as replaced", observableStatus.get()); + this._platform.logger.wrapOrRun(log, `replacing room being created`, log => { + log.set("localId", roomBeingCreated.localId) + .set("roomId", roomBeingCreated.roomId); + }); observableStatus.set(observableStatus.get() | RoomStatus.Replaced); - } else { - console.log("no observableStatus"); } this._roomsBeingCreated.remove(roomBeingCreated.localId); return; @@ -703,12 +703,12 @@ export class Session { } } - applyRoomCollectionChangesAfterSync(inviteStates, roomStates, archivedRoomStates) { + applyRoomCollectionChangesAfterSync(inviteStates, roomStates, archivedRoomStates, log) { // update the collections after sync for (const rs of roomStates) { if (rs.shouldAdd) { this._rooms.add(rs.id, rs.room); - this._tryReplaceRoomBeingCreated(rs.id); + this._tryReplaceRoomBeingCreated(rs.id, log); } else if (rs.shouldRemove) { this._rooms.remove(rs.id); } diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index e9faa89b..5eb50b5c 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -316,7 +316,7 @@ export class Sync { for(let is of inviteStates) { log.wrap("invite", log => is.invite.afterSync(is.changes, log), log.level.Detail); } - this._session.applyRoomCollectionChangesAfterSync(inviteStates, roomStates, archivedRoomStates); + this._session.applyRoomCollectionChangesAfterSync(inviteStates, roomStates, archivedRoomStates, log); } _openSyncTxn() { diff --git a/src/matrix/room/RoomSummary.js b/src/matrix/room/RoomSummary.js index d0c78659..82087200 100644 --- a/src/matrix/room/RoomSummary.js +++ b/src/matrix/room/RoomSummary.js @@ -96,7 +96,10 @@ function processRoomAccountData(data, event) { } export function processStateEvent(data, event) { - if (event.type === "m.room.encryption") { + if (event.type === "m.room.create") { + data = data.cloneIfNeeded(); + data.lastMessageTimestamp = event.origin_server_ts; + } else if (event.type === "m.room.encryption") { const algorithm = event.content?.algorithm; if (!data.encryption && algorithm === MEGOLM_ALGORITHM) { data = data.cloneIfNeeded(); diff --git a/src/matrix/room/create.ts b/src/matrix/room/create.ts index 24adfcf1..c42da436 100644 --- a/src/matrix/room/create.ts +++ b/src/matrix/room/create.ts @@ -121,7 +121,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { console.log("done creating the room now", this._roomId); // TODO: somehow check in Session if we need to replace this with a joined room // in case the room appears first in sync, and this request returns later - this.emitChange(); + this.emitChange(undefined, log); } private async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise { @@ -139,8 +139,8 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } } - private emitChange() { - this.updateCallback(this); + private emitChange(params?, log?) { + this.updateCallback(this, params, log); this.emit("change"); } diff --git a/src/observable/map/LogMap.js b/src/observable/map/LogMap.js new file mode 100644 index 00000000..4b8bb686 --- /dev/null +++ b/src/observable/map/LogMap.js @@ -0,0 +1,70 @@ +/* +Copyright 2020 Bruno Windels + +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. +*/ + +import {BaseObservableMap} from "./BaseObservableMap.js"; + +export class LogMap extends BaseObservableMap { + constructor(source, log) { + super(); + this._source = source; + this.log = log; + this._subscription = null; + } + + onAdd(key, value) { + this.log("add", key, value); + this.emitAdd(key, value); + } + + onRemove(key, value) { + this.log("remove", key, value); + this.emitRemove(key, value); + } + + onUpdate(key, value, params) { + this.log("update", key, value, params); + this.emitUpdate(key, value, params); + } + + onSubscribeFirst() { + this.log("subscribeFirst"); + this._subscription = this._source.subscribe(this); + super.onSubscribeFirst(); + } + + onUnsubscribeLast() { + super.onUnsubscribeLast(); + this._subscription = this._subscription(); + this.log("unsubscribeLast"); + } + + onReset() { + this.log("reset"); + this.emitReset(); + } + + [Symbol.iterator]() { + return this._source[Symbol.iterator](); + } + + get size() { + return this._source.size; + } + + get(key) { + return this._source.get(key); + } +}