9edddce149
* chore: ignore jest-sonar.xml in gitconfig * chore: add missing rtl types to shared component * chore: add `symbol` to `Disposables.trackListener` * feat: add room list header view to shared components * fix: change `Space Settings` to `Space settings` * feat: add room list header view model * chore: remove old room list header * chore: update i18n * test: fix Room-test * test: update playwright screenshot * fix: remove extra margin at the top of Sort title in room options * test: fix room status bar test * fix: change for correct copyright * refactor: use `Disposables#track` instead of manually disposing the listener * refactor: avoid to recompute all the snapshot of `RoomListHeaderViewModel` * wip * fix: make header buttons the same size than figma * test: update shared component snapshots * test: update shared component screenshots * test: update EW screenshots
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
/*
|
|
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 { EventEmitter } from "events";
|
|
|
|
/**
|
|
* Something that needs to be eventually disposed. This can be:
|
|
* - A function that does the disposing
|
|
* - An object containing a dispose method which does the disposing
|
|
*/
|
|
export type DisposableItem = { dispose: () => void } | (() => void);
|
|
|
|
/**
|
|
* This class provides a way for the view-model to track any resource
|
|
* that it needs to eventually relinquish.
|
|
*/
|
|
export class Disposables {
|
|
private readonly disposables: DisposableItem[] = [];
|
|
private _isDisposed: boolean = false;
|
|
|
|
/**
|
|
* Relinquish all tracked disposable values
|
|
*/
|
|
public dispose(): void {
|
|
if (this.isDisposed) return;
|
|
this._isDisposed = true;
|
|
for (const disposable of this.disposables) {
|
|
if (typeof disposable === "function") {
|
|
disposable();
|
|
} else {
|
|
disposable.dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Track a value that needs to be eventually relinquished
|
|
*/
|
|
public track<T extends DisposableItem>(disposable: T): T {
|
|
this.throwIfDisposed();
|
|
this.disposables.push(disposable);
|
|
return disposable;
|
|
}
|
|
|
|
/**
|
|
* Add an event listener that will be removed on dispose
|
|
*/
|
|
public trackListener(emitter: EventEmitter, event: string | symbol, callback: (...args: unknown[]) => void): void {
|
|
this.throwIfDisposed();
|
|
emitter.on(event, callback);
|
|
this.track(() => {
|
|
emitter.off(event, callback);
|
|
});
|
|
}
|
|
|
|
private throwIfDisposed(): void {
|
|
if (this.isDisposed) throw new Error("Disposable is already disposed");
|
|
}
|
|
|
|
/**
|
|
* Whether this disposable has been disposed
|
|
*/
|
|
public get isDisposed(): boolean {
|
|
return this._isDisposed;
|
|
}
|
|
}
|