Instantiate message previewers lazily (#34065)

* fix(room-list): instantiate message previewers lazily

Removing the unused SettingsStore import from MessagePreviewStore (when
the
feature_new_room_list flag is dropped) changed module load order and
exposed a latent circular dependency: ReactionEventPreview imports
MessagePreviewStore, which eagerly did `new ReactionEventPreview()` at
module-eval — so importing ReactionEventPreview first (as its unit test
does) hit "ReactionEventPreview is not a constructor".

Construct the previewers lazily on first use (cached) instead of at
module
load, so nothing dereferences a mid-evaluation module. Fixes
ReactionEventPreview-test.

* refactor: pass MessagePreviewStore to ReactEventPreview
This commit is contained in:
Florian Duros
2026-07-01 10:14:41 +00:00
committed by GitHub
parent e836eb04e2
commit 27d97d3661
4 changed files with 104 additions and 47 deletions
@@ -11,6 +11,9 @@ import {
EventStatus,
EventTimeline,
EventType,
M_POLL_KIND_DISCLOSED,
M_POLL_START,
M_TEXT,
type MatrixClient,
type MatrixEvent,
PendingEventOrdering,
@@ -373,4 +376,82 @@ describe("MessagePreviewStore", () => {
// @ts-ignore private access
expect(store.previews.has(nonRenderedRoom.roomId)).toBeFalsy();
});
describe("generatePreviewForEvent", () => {
it("should generate a preview for a message event", () => {
const message = mkMessage({
user: "@sender:server",
event: true,
room: room.roomId,
msg: "Hello world",
});
expect(store.generatePreviewForEvent(message)).toBe("Hello world");
});
it("should generate a preview for a sticker event", () => {
const sticker = mkEvent({
event: true,
type: EventType.Sticker,
user: "@sender:server",
room: room.roomId,
content: { body: "A sticker" },
});
expect(store.generatePreviewForEvent(sticker)).toBe("A sticker");
});
// Poll start events have both a stable and an unstable event type, each with its own
// registry entry, so exercise both.
it.each([M_POLL_START.name, M_POLL_START.altName])(
"should generate a preview for a poll start event (%s)",
(type) => {
const poll = mkEvent({
event: true,
type: type!,
user: "@sender:server",
room: room.roomId,
content: {
[M_POLL_START.name]: {
question: { [M_TEXT.name]: "Where shall we eat?" },
kind: M_POLL_KIND_DISCLOSED.name,
answers: [
{ id: "pizza", [M_TEXT.name]: "Pizza" },
{ id: "sushi", [M_TEXT.name]: "Sushi" },
],
},
},
});
expect(store.generatePreviewForEvent(poll)).toBe("Where shall we eat?");
},
);
it.each([EventType.CallInvite, EventType.CallAnswer, EventType.CallHangup])(
"should generate a preview for a %s event",
(type) => {
const callEvent = mkEvent({
event: true,
type,
user: "@sender:server",
room: room.roomId,
content: { call_id: "1" },
});
expect(store.generatePreviewForEvent(callEvent)).toBeTruthy();
},
);
it("should return an empty string for an event type without a previewer", () => {
const topicEvent = mkEvent({
event: true,
type: EventType.RoomTopic,
user: "@sender:server",
room: room.roomId,
content: { topic: "A new topic" },
});
expect(store.generatePreviewForEvent(topicEvent)).toBe("");
});
});
});
@@ -10,12 +10,12 @@ import { RelationType, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { mkEvent, stubClient } from "../../../../test-utils";
// Import directly from the file to avoid circular dependencies with MessagePreviewStore
import { ReactionEventPreview } from "../../../../../src/stores/message-preview/previews/ReactionEventPreview";
import { MessagePreviewStore } from "../../../../../src/stores/message-preview";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("ReactionEventPreview", () => {
const preview = new ReactionEventPreview();
const preview = new ReactionEventPreview(MessagePreviewStore.instance);
const userId = "@user:example.com";
const roomId = "!room:example.com";