ensure metaspace changes correctly notify listeners (#8611)

This commit is contained in:
Janne Mareike Koschinski 2022-05-18 15:15:25 +02:00 committed by GitHub
parent 9b92eca73d
commit 2d386556a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View file

@ -1072,6 +1072,7 @@ 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]);
@ -1079,6 +1080,11 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
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);

View file

@ -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();
});
});