Files
ThreadNet-Web/apps/web/src/stores/local-echo/EchoTransaction.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.7 KiB
TypeScript
Raw Normal View History

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.
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;
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;
}
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
}
public cancel(): void {
// Success basically means "done"
2020-07-31 10:00:02 -06:00
this.setStatus(TransactionStatus.Success);
}
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);
}
}