Skip to content

Commit

Permalink
async: Write BackoffMachine's methods properly.
Browse files Browse the repository at this point in the history
As Greg points out [1], about the current (subtly incorrect) form:

"""
I believe the semantics of this is actually that that
[`methodName = …`] becomes part of the constructor! Not a method on
the class.
"""

We want methods on the class, so, write them that way (as "method
definitions") [2].

In particular, in an upcoming commit where we test a new `tryFetch`
implementation that retries a network request until a timeout, we'll
want to mock `BackoffMachine.prototype.wait` so we don't have to
think about it within those tests. (BackoffMachine has its own unit
tests, after all.) Making `wait` an actual method on the class lets
us accomplish this straightforwardly.

[1] https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/user_avatar_url_field_optional/near/909006
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions
  • Loading branch information
chrisbobbe committed Jul 16, 2020
1 parent 8b2d6ee commit 664497b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/utils/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class BackoffMachine {
* Use this to implement "give up" logic by breaking out of the loop after a
* threshold number of waits.
*/
waitsCompleted = (): number => this._waitsCompleted;
waitsCompleted(): number {
return this._waitsCompleted;
}

/**
* Promise to resolve after the appropriate duration.
Expand All @@ -58,7 +60,7 @@ export class BackoffMachine {
* a capped exponential shape on the expected value. Greg discusses this in more
* detail in #3841.
*/
wait = async (): Promise<void> => {
async wait(): Promise<void> {
if (this._startTime === undefined) {
this._startTime = Date.now();
}
Expand All @@ -74,7 +76,7 @@ export class BackoffMachine {
await sleep(duration);

this._waitsCompleted++;
};
}
}

/**
Expand Down

0 comments on commit 664497b

Please sign in to comment.