Merge pull request #5076 from matrix-org/travis/cleanup/1

Clean up documentation of Whenable + fix other code concerns
pull/21833/head
Travis Ralston 2020-07-31 11:27:19 -06:00 committed by GitHub
commit e8d1024c45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 20 deletions

View File

@ -56,10 +56,10 @@ export default class ServerOfflineDialog extends React.PureComponent<IProps> {
</div>
);
const entries = c.transactions
.filter(t => t.status === TransactionStatus.DoneError || t.didPreviouslyFail)
.filter(t => t.status === TransactionStatus.Error || t.didPreviouslyFail)
.map((t, j) => {
let button = <Spinner w={19} h={19} />;
if (t.status === TransactionStatus.DoneError) {
if (t.status === TransactionStatus.Error) {
button = (
<AccessibleButton kind="link" onClick={() => t.run()}>{_t("Resend")}</AccessibleButton>
);

View File

@ -38,7 +38,7 @@ export abstract class EchoContext extends Whenable<ContextTransactionState> impl
}
public get firstFailedTime(): Date {
const failedTxn = this.transactions.find(t => t.didPreviouslyFail || t.status === TransactionStatus.DoneError);
const failedTxn = this.transactions.find(t => t.didPreviouslyFail || t.status === TransactionStatus.Error);
if (failedTxn) return failedTxn.startTime;
return null;
}
@ -57,7 +57,7 @@ export abstract class EchoContext extends Whenable<ContextTransactionState> impl
// We have no intent to call the transaction again if it succeeds (in fact, it'll
// be really angry at us if we do), so call that the end of the road for the events.
txn.when(TransactionStatus.DoneSuccess, () => txn.destroy());
txn.when(TransactionStatus.Success, () => txn.destroy());
return txn;
}
@ -65,7 +65,7 @@ export abstract class EchoContext extends Whenable<ContextTransactionState> impl
private checkTransactions = () => {
let status = ContextTransactionState.AllSuccessful;
for (const txn of this.transactions) {
if (txn.status === TransactionStatus.DoneError || txn.didPreviouslyFail) {
if (txn.status === TransactionStatus.Error || txn.didPreviouslyFail) {
status = ContextTransactionState.PendingErrors;
break;
} else if (txn.status === TransactionStatus.Pending) {

View File

@ -20,8 +20,8 @@ export type RunFn = () => Promise<void>;
export enum TransactionStatus {
Pending,
DoneSuccess,
DoneError,
Success,
Error,
}
export class EchoTransaction extends Whenable<TransactionStatus> {
@ -46,25 +46,25 @@ export class EchoTransaction extends Whenable<TransactionStatus> {
}
public run() {
if (this.status === TransactionStatus.DoneSuccess) {
if (this.status === TransactionStatus.Success) {
throw new Error("Cannot re-run a successful echo transaction");
}
this.setStatus(TransactionStatus.Pending);
this.runFn()
.then(() => this.setStatus(TransactionStatus.DoneSuccess))
.catch(() => this.setStatus(TransactionStatus.DoneError));
.then(() => this.setStatus(TransactionStatus.Success))
.catch(() => this.setStatus(TransactionStatus.Error));
}
public cancel() {
// Success basically means "done"
this.setStatus(TransactionStatus.DoneSuccess);
this.setStatus(TransactionStatus.Success);
}
private setStatus(status: TransactionStatus) {
this._status = status;
if (status === TransactionStatus.DoneError) {
if (status === TransactionStatus.Error) {
this.didFail = true;
} else if (status === TransactionStatus.DoneSuccess) {
} else if (status === TransactionStatus.Success) {
this.didFail = false;
}
this.notifyCondition(status);

View File

@ -80,12 +80,12 @@ export abstract class GenericEchoChamber<C extends EchoContext, K, V> extends Ev
this.cache.get(key).txn.cancel();
}
const txn = this.context.beginTransaction(auditName, runFn);
this.cacheVal(key, targetVal, txn); // set the cache now as it won't be updated by the .when() ladder below.
const ctxn = this.context.beginTransaction(auditName, runFn);
this.cacheVal(key, targetVal, ctxn); // set the cache now as it won't be updated by the .when() ladder below.
txn.when(TransactionStatus.Pending, () => this.cacheVal(key, targetVal, txn))
.when(TransactionStatus.DoneError, () => revertFn());
ctxn.when(TransactionStatus.Pending, () => this.cacheVal(key, targetVal, ctxn))
.when(TransactionStatus.Error, () => revertFn());
txn.run();
ctxn.run();
}
}

View File

@ -41,7 +41,7 @@ export abstract class Whenable<T> implements IDestroyable {
}
/**
* Sets up a fall to `fn` *when* any of the `conditions` are met.
* Sets up a call to `fn` *when* any of the `conditions` are met.
* @param conditions The conditions to match.
* @param fn The function to call.
* @returns This.
@ -64,7 +64,7 @@ export abstract class Whenable<T> implements IDestroyable {
}
/**
* Notifies all the whenables of a given condition.
* Notifies all the listeners of a given condition.
* @param condition The new condition that has been met.
*/
protected notifyCondition(condition: T) {