From 2d386556a671af6b7ced1f89d7918a9a508353da Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski Date: Wed, 18 May 2022 15:15:25 +0200 Subject: [PATCH] ensure metaspace changes correctly notify listeners (#8611) --- src/stores/spaces/SpaceStore.ts | 6 ++++++ test/stores/SpaceStore-test.ts | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/stores/spaces/SpaceStore.ts b/src/stores/spaces/SpaceStore.ts index 4f455faee5..f00b527c1d 100644 --- a/src/stores/spaces/SpaceStore.ts +++ b/src/stores/spaces/SpaceStore.ts @@ -1072,6 +1072,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient { ?.["m.room_versions"]?.["org.matrix.msc3244.room_capabilities"]?.["restricted"]; }); + const oldMetaSpaces = this._enabledMetaSpaces; const enabledMetaSpaces = SettingsStore.getValue("Spaces.enabledMetaSpaces"); this._enabledMetaSpaces = metaSpaceOrder.filter(k => enabledMetaSpaces[k]); @@ -1079,6 +1080,11 @@ export class SpaceStoreClass extends AsyncStoreWithClient { this.sendUserProperties(); this.rebuildSpaceHierarchy(); // trigger an initial update + // rebuildSpaceHierarchy will only send an update if the spaces have changed. + // If only the meta spaces have changed, we need to send an update ourselves. + if (arrayHasDiff(oldMetaSpaces, this._enabledMetaSpaces)) { + this.emit(UPDATE_TOP_LEVEL_SPACES, this.spacePanelSpaces, this.enabledMetaSpaces); + } // restore selected state from last session if any and still valid const lastSpaceId = window.localStorage.getItem(ACTIVE_SPACE_LS_KEY); diff --git a/test/stores/SpaceStore-test.ts b/test/stores/SpaceStore-test.ts index 47c3f974b2..d83bc21f22 100644 --- a/test/stores/SpaceStore-test.ts +++ b/test/stores/SpaceStore-test.ts @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { EventEmitter } from "events"; import { mocked } from 'jest-mock'; import { EventType } from "matrix-js-sdk/src/@types/event"; import { RoomMember } from "matrix-js-sdk/src/models/room-member"; @@ -1221,4 +1222,26 @@ describe("SpaceStore", () => { expect(SpaceStore.instance.spacePanelSpaces.map(r => r.roomId)).toStrictEqual([rootSpace.roomId]); await prom; }); + + it("correctly emits events for metaspace changes during onReady", async () => { + // similar to useEventEmitterState, but for use inside of tests + function testEventEmitterState( + emitter: EventEmitter | undefined, + eventName: string | symbol, + callback: (...args: any[]) => void, + ): () => void { + callback(); + emitter.addListener(eventName, callback); + return () => emitter.removeListener(eventName, callback); + } + + let metaSpaces; + const removeListener = testEventEmitterState(store, UPDATE_TOP_LEVEL_SPACES, () => { + metaSpaces = store.enabledMetaSpaces; + }); + expect(metaSpaces).toEqual(store.enabledMetaSpaces); + await run(); + expect(metaSpaces).toEqual(store.enabledMetaSpaces); + removeListener(); + }); });