Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
15817cf
Very first pass at shared component views
dbkr Jun 26, 2025
ddbe098
Remove old TextualEvent
dbkr Jun 26, 2025
0179284
Pass showHiddenEvents
dbkr Jun 26, 2025
84d34e1
Factor out common view model stuff
dbkr Jun 26, 2025
b96da8d
Move ViewModel interface into the shared components
dbkr Jun 26, 2025
455ca44
Add tiny wrapper hook
dbkr Jun 26, 2025
73d78f4
Move showHiddenEvents into props fully
dbkr Jun 26, 2025
2125415
Fill in stories / test
dbkr Jun 30, 2025
a597221
chore: setup storybook
florianduros Jun 20, 2025
3d724ff
Add TextualEvent component to storybook
dbkr Jun 30, 2025
11f9849
Add mock view model & snapshot
dbkr Jun 30, 2025
4709656
Remove old style stories entry
dbkr Jun 30, 2025
5638dd7
Merge branch 'develop' into dbkr/textualevent_sharedcomponent
dbkr Jul 1, 2025
9318006
Change import
dbkr Jul 1, 2025
a936433
Change import
dbkr Jul 1, 2025
472de3b
Prettier
dbkr Jul 1, 2025
073d97e
Add paxckage patch to @types/mdx
dbkr Jul 1, 2025
f2fc88f
Merge remote-tracking branch 'origin/develop' into dbkr/textualevent_…
dbkr Jul 3, 2025
862aaff
Pass getSnapshot as getServerSnapshot too
dbkr Jul 3, 2025
82d08df
Maybe make sonar regognise tests as tests
dbkr Jul 3, 2025
b1edabe
Typo
dbkr Jul 3, 2025
3285007
Use storybook reacvt-vite
dbkr Jul 3, 2025
4fddd23
Change here too
dbkr Jul 3, 2025
703ba8d
Workaround for incomatible types in rollup
dbkr Jul 3, 2025
fbe6a06
Remove webpack styling addon
dbkr Jul 7, 2025
c8bd639
Merge remote-tracking branch 'origin/develop' into dbkr/textualevent_…
dbkr Jul 7, 2025
7e4ff89
Hopefully do screenshot testing...
dbkr Jul 7, 2025
d3300ac
need newer node
dbkr Jul 7, 2025
5f3fae2
quote issues
dbkr Jul 7, 2025
d3c5971
Make it an npm script
dbkr Jul 7, 2025
f47673c
colons
dbkr Jul 7, 2025
0c3830f
use right port
dbkr Jul 7, 2025
41612d3
Install playwright browsers
dbkr Jul 7, 2025
fd9253d
Try without the if
dbkr Jul 8, 2025
2f62c15
Oh right, we need the headless shell
dbkr Jul 8, 2025
d2c632f
Pass flag to store received screenshots
dbkr Jul 8, 2025
b632a61
Update snapshot from received
dbkr Jul 8, 2025
80ed90a
Include platform in snapshot / received dir
dbkr Jul 8, 2025
a56cf55
Suffix snapshots with platform instead
dbkr Jul 8, 2025
917f85e
Remove unnecessary env vars
dbkr Jul 8, 2025
6171d2a
Add some comments
dbkr Jul 8, 2025
94b7adc
Prettier
dbkr Jul 8, 2025
5a2cb98
Merge remote-tracking branch 'origin/develop' into dbkr/textualevent_…
dbkr Jul 8, 2025
f424599
Fix yarn.lock
dbkr Jul 8, 2025
e6dbe93
Memoise vm creation
dbkr Jul 9, 2025
7347d55
Add implements
dbkr Jul 9, 2025
b4ea530
Fix listener interface
dbkr Jul 9, 2025
3077be3
Add implements
dbkr Jul 9, 2025
9621f79
Merge remote-tracking branch 'origin/develop' into dbkr/textualevent_…
dbkr Jul 9, 2025
03ee035
Fix types
dbkr Jul 9, 2025
fbd9af6
Fix more types
dbkr Jul 9, 2025
056b06e
Add a superclass that simple view models can extend
dbkr Jul 9, 2025
8878c7b
Revert useMemo
dbkr Jul 9, 2025
626022e
Merge branch 'dbkr/textualevent_sharedcomponent' into dbkr/subscripti…
dbkr Jul 9, 2025
553724b
Unused import
dbkr Jul 9, 2025
117bba4
Merge branch 'dbkr/textualevent_sharedcomponent' into dbkr/subscripti…
dbkr Jul 9, 2025
86e7d99
Actually commit the file the branch is named after
dbkr Jul 9, 2025
ead893c
Merge remote-tracking branch 'origin/develop' into dbkr/textualevent_…
dbkr Jul 9, 2025
fa300af
Add missing playwright step
dbkr Jul 9, 2025
dc47abb
Add return type annotation
dbkr Jul 9, 2025
f62177a
Change to add / remove subscription callback
dbkr Jul 9, 2025
049a3ae
Change to 'add' rather than 'subs.subscribe'
dbkr Jul 9, 2025
b124415
Merge branch 'dbkr/textualevent_sharedcomponent' into dbkr/subscripti…
dbkr Jul 9, 2025
c16fd3d
Add cache specifier for only shell playwright browsers
dbkr Jul 9, 2025
c3e53aa
Add copyright headers
dbkr Jul 9, 2025
a373a68
Merge branch 'dbkr/textualevent_sharedcomponent' into dbkr/subscripti…
dbkr Jul 9, 2025
36940a4
Merge branch 'develop' into dbkr/subscriptionviewmodel
dbkr Jul 14, 2025
6cd968d
Better comment wording
dbkr Jul 17, 2025
fc83ec4
Make amit an arrow function
dbkr Jul 17, 2025
8c7529c
Add a test
dbkr Jul 17, 2025
8d8f9a7
Merge branch 'develop' into dbkr/subscriptionviewmodel
dbkr Jul 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/viewmodels/SubscriptionViewModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { type ViewModel } from "../shared-components/ViewModel";
import { ViewModelSubscriptions } from "./ViewModelSubscriptions";

export abstract class SubscriptionViewModel<T> implements ViewModel<T> {
protected subs: ViewModelSubscriptions;

protected constructor() {
this.subs = new ViewModelSubscriptions(
this.addDownstreamSubscriptionWrapper,
this.removeDownstreamSubscriptionWrapper,
);
}

public subscribe = (listener: () => void): (() => void) => {
return this.subs.add(listener);
};

/**
* Wrapper around the abstract subscribe callback as we can't assume that the subclassed method
* has a bound `this` context.
*/
private addDownstreamSubscriptionWrapper = (): void => {
this.addDownstreamSubscription();
};

/**
* Wrapper around the abstract unsubscribe callback as we can't call pass an abstract method directly
* in the constructor.
*/
private removeDownstreamSubscriptionWrapper = (): void => {
this.removeDownstreamSubscription();
};

/**
* Called when the first listener subscribes: the subclass should set up any necessary subscriptions
* to call this.subs.emit() when the snapshot changes.
*/
protected abstract addDownstreamSubscription(): void;

/**
* Called when the last listener unsubscribes: the subclass should clean up any subscriptions.
*/
protected abstract removeDownstreamSubscription(): void;

/**
* Returns the current snapshot of the view model.
*/
public abstract getSnapshot: () => T;
}
15 changes: 4 additions & 11 deletions src/viewmodels/ViewModelSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export class ViewModelSubscriptions {
private listeners = new Set<() => void>();

/**
* @param updateSubscription A function called whenever a listener is added or removed.
* @param subscribeCallback Called when the first listener subscribes.
* @param unsubscribeCallback Called when the last listener unsubscribes.
*/
public constructor(
private subscribeCallback: () => void,
Expand Down Expand Up @@ -41,17 +42,9 @@ export class ViewModelSubscriptions {
/**
* Emit an update to all subscribed listeners.
*/
public emit(): void {
public emit = (): void => {
for (const listener of this.listeners) {
listener();
}
}

/**
* Get the number of listeners currently subscribed to updates.
* @returns The number of listeners.
*/
public listenerCount(): number {
return this.listeners.size;
}
};
}
25 changes: 7 additions & 18 deletions src/viewmodels/event-tiles/TextualEventViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@ import { MatrixEventEvent } from "matrix-js-sdk/src/matrix";
import { type EventTileTypeProps } from "../../events/EventTileFactory";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { textForEvent } from "../../TextForEvent";
import { ViewModelSubscriptions } from "../ViewModelSubscriptions";
import { type TextualEventViewSnapshot } from "../../shared-components/event-tiles/TextualEvent/TextualEvent";
import { type ViewModel } from "../../shared-components/ViewModel";

export class TextualEventViewModel implements ViewModel<TextualEventViewSnapshot> {
private subs: ViewModelSubscriptions;
import { SubscriptionViewModel } from "../SubscriptionViewModel";

export class TextualEventViewModel extends SubscriptionViewModel<TextualEventViewSnapshot> {
public constructor(private eventTileProps: EventTileTypeProps) {
this.subs = new ViewModelSubscriptions(this.addSubscription, this.removeSubscription);
super();
}

private addSubscription = (): void => {
this.eventTileProps.mxEvent.on(MatrixEventEvent.SentinelUpdated, this.onEventSentinelUpdated);
};

private removeSubscription = (): void => {
this.eventTileProps.mxEvent.off(MatrixEventEvent.SentinelUpdated, this.onEventSentinelUpdated);
protected addDownstreamSubscription = (): void => {
this.eventTileProps.mxEvent.on(MatrixEventEvent.SentinelUpdated, this.subs.emit);
Copy link
Member

@t3chguy t3chguy Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emit called in this way will have no this reference as it is not an arrow-function or bound

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point: I've made emit an arrow function and added a test to asset it actually works.

};

public subscribe = (listener: () => void): (() => void) => {
return this.subs.add(listener);
protected removeDownstreamSubscription = (): void => {
this.eventTileProps.mxEvent.off(MatrixEventEvent.SentinelUpdated, this.subs.emit);
};

public getSnapshot = (): TextualEventViewSnapshot => {
Expand All @@ -42,8 +35,4 @@ export class TextualEventViewModel implements ViewModel<TextualEventViewSnapshot
);
return text;
};

private onEventSentinelUpdated = (): void => {
this.subs.emit();
};
}
29 changes: 29 additions & 0 deletions test/viewmodels/event-tiles/TextualEventViewModel-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";

import { TextualEventViewModel } from "../../../src/viewmodels/event-tiles/TextualEventViewModel";

describe("TextualEventViewModel", () => {
it("should update when the sentinel updates", () => {
const fakeEvent = new MatrixEvent({});

const vm = new TextualEventViewModel({
showHiddenEvents: false,
mxEvent: fakeEvent,
});

const cb = jest.fn();

vm.subscribe(cb);

fakeEvent.emit(MatrixEventEvent.SentinelUpdated);

expect(cb).toHaveBeenCalledTimes(1);
});
});
Loading