2020-07-29 16:53:26 -06:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-07-29 16:53:26 -06:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-07-29 16:53:26 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Whenable } from "../../utils/Whenable";
|
|
|
|
|
|
|
|
|
|
export type RunFn = () => Promise<void>;
|
|
|
|
|
|
|
|
|
|
export enum TransactionStatus {
|
|
|
|
|
Pending,
|
2020-07-31 10:00:02 -06:00
|
|
|
Success,
|
|
|
|
|
Error,
|
2020-07-29 16:53:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class EchoTransaction extends Whenable<TransactionStatus> {
|
|
|
|
|
private _status = TransactionStatus.Pending;
|
|
|
|
|
private didFail = false;
|
|
|
|
|
|
2020-07-29 20:36:04 -06:00
|
|
|
public readonly startTime = new Date();
|
|
|
|
|
|
2024-01-02 18:56:39 +00:00
|
|
|
public constructor(
|
|
|
|
|
public readonly auditName: string,
|
|
|
|
|
public runFn: RunFn,
|
|
|
|
|
) {
|
2020-07-29 16:53:26 -06:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get didPreviouslyFail(): boolean {
|
|
|
|
|
return this.didFail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get status(): TransactionStatus {
|
|
|
|
|
return this._status;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public run(): void {
|
2020-07-31 10:00:02 -06:00
|
|
|
if (this.status === TransactionStatus.Success) {
|
2020-07-29 16:53:26 -06:00
|
|
|
throw new Error("Cannot re-run a successful echo transaction");
|
|
|
|
|
}
|
|
|
|
|
this.setStatus(TransactionStatus.Pending);
|
|
|
|
|
this.runFn()
|
2020-07-31 10:00:02 -06:00
|
|
|
.then(() => this.setStatus(TransactionStatus.Success))
|
|
|
|
|
.catch(() => this.setStatus(TransactionStatus.Error));
|
2020-07-29 16:53:26 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public cancel(): void {
|
2020-07-29 19:11:24 -06:00
|
|
|
// Success basically means "done"
|
2020-07-31 10:00:02 -06:00
|
|
|
this.setStatus(TransactionStatus.Success);
|
2020-07-29 19:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private setStatus(status: TransactionStatus): void {
|
2020-07-29 16:53:26 -06:00
|
|
|
this._status = status;
|
2020-07-31 10:00:02 -06:00
|
|
|
if (status === TransactionStatus.Error) {
|
2020-07-29 16:53:26 -06:00
|
|
|
this.didFail = true;
|
2020-07-31 10:00:02 -06:00
|
|
|
} else if (status === TransactionStatus.Success) {
|
2020-07-29 16:53:26 -06:00
|
|
|
this.didFail = false;
|
|
|
|
|
}
|
|
|
|
|
this.notifyCondition(status);
|
|
|
|
|
}
|
|
|
|
|
}
|