Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
ensure metaspace changes correctly notify listeners (#8611)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne authored May 18, 2022
1 parent 9b92eca commit 2d38655
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/stores/spaces/SpaceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,13 +1072,19 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
?.["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]);

this._allRoomsInHome = SettingsStore.getValue("Spaces.allRoomsInHome");
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);
Expand Down
23 changes: 23 additions & 0 deletions test/stores/SpaceStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 2d38655

Please sign in to comment.