mirror of
https://github.com/element-hq/element-web.git
synced 2025-12-07 01:21:02 +00:00
Compare commits
67 Commits
midhun/mod
...
dbkr/subsc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36940a4223 | ||
|
|
a373a68423 | ||
|
|
c3e53aa389 | ||
|
|
c16fd3dca6 | ||
|
|
b124415373 | ||
|
|
049a3ae8d2 | ||
|
|
f62177ad4f | ||
|
|
dc47abb82d | ||
|
|
fa300af2ff | ||
|
|
ead893c2ec | ||
|
|
86e7d99c9b | ||
|
|
117bba4521 | ||
|
|
553724b567 | ||
|
|
626022e805 | ||
|
|
8878c7b5c7 | ||
|
|
056b06e614 | ||
|
|
fbd9af69bc | ||
|
|
03ee035a28 | ||
|
|
9621f791d1 | ||
|
|
3077be3e4f | ||
|
|
b4ea530bd0 | ||
|
|
7347d55479 | ||
|
|
e6dbe93877 | ||
|
|
f424599295 | ||
|
|
5a2cb98670 | ||
|
|
94b7adcb49 | ||
|
|
6171d2aedd | ||
|
|
917f85ea60 | ||
|
|
a56cf55f34 | ||
|
|
80ed90a975 | ||
|
|
b632a61be7 | ||
|
|
d2c632f93d | ||
|
|
2f62c15fec | ||
|
|
fd9253d958 | ||
|
|
41612d3db6 | ||
|
|
0c3830f0a9 | ||
|
|
f47673cce9 | ||
|
|
d3c5971dfe | ||
|
|
5f3fae2412 | ||
|
|
d3300acca3 | ||
|
|
7e4ff89597 | ||
|
|
c8bd639d4f | ||
|
|
fbe6a06774 | ||
|
|
703ba8d9fb | ||
|
|
4fddd23b60 | ||
|
|
3285007224 | ||
|
|
b1edabe384 | ||
|
|
82d08df297 | ||
|
|
862aaff468 | ||
|
|
f2fc88fb7e | ||
|
|
073d97e261 | ||
|
|
472de3bd14 | ||
|
|
a9364332b3 | ||
|
|
9318006b21 | ||
|
|
5638dd7c5f | ||
|
|
4709656510 | ||
|
|
11f9849e51 | ||
|
|
3d724ffa84 | ||
|
|
a597221d05 | ||
|
|
2125415654 | ||
|
|
73d78f4be6 | ||
|
|
455ca447ea | ||
|
|
b96da8de83 | ||
|
|
84d34e1332 | ||
|
|
017928455e | ||
|
|
ddbe0989ed | ||
|
|
15817cffc5 |
56
src/viewmodels/SubscriptionViewModel.ts
Normal file
56
src/viewmodels/SubscriptionViewModel.ts
Normal file
@@ -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 call pass an abstract method directly
|
||||
* in the constructor.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@@ -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,
|
||||
@@ -46,12 +47,4 @@ export class ViewModelSubscriptions {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of listeners currently subscribed to updates.
|
||||
* @returns The number of listeners.
|
||||
*/
|
||||
public listenerCount(): number {
|
||||
return this.listeners.size;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
protected addDownstreamSubscription = (): void => {
|
||||
this.eventTileProps.mxEvent.on(MatrixEventEvent.SentinelUpdated, this.subs.emit);
|
||||
};
|
||||
|
||||
private removeSubscription = (): void => {
|
||||
this.eventTileProps.mxEvent.off(MatrixEventEvent.SentinelUpdated, this.onEventSentinelUpdated);
|
||||
};
|
||||
|
||||
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 => {
|
||||
@@ -42,8 +35,4 @@ export class TextualEventViewModel implements ViewModel<TextualEventViewSnapshot
|
||||
);
|
||||
return text;
|
||||
};
|
||||
|
||||
private onEventSentinelUpdated = (): void => {
|
||||
this.subs.emit();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user