retab auto-verifier.ts

This commit is contained in:
Michael Peters 2022-10-06 23:00:36 -07:00
parent 5f1e9fddc0
commit cc8b528bb4

View File

@ -26,7 +26,7 @@ export enum AutoVerifierChangesType {
export class AutoVerifier<T> {
public primaryPromise: Promise<T | null> | null = null;
public trustedPromise: Promise<T | null> | null = null;
public verifyPromise: Promise<boolean> | null = null;
public verifyPromise: Promise<boolean> | null = null;
public trustedStatus: 'none' | 'fetching' | 'verifying' | 'verified' = 'none';
private verifierId: string;
@ -175,7 +175,7 @@ export class AutoVerifier<T> {
}
this.primaryPromise = null;
this.trustedPromise = null;
this.verifyPromise = null;
this.verifyPromise = null;
this.trustedStatus = 'none';
}
@ -250,7 +250,7 @@ export class AutoVerifier<T> {
// make sure to verify BEFORE potentially resolving
// this way the conflicts can be resolved before the result is returned
this.verifyPromise = this.verifyFunc(primaryResult, trustedResult);
this.verifyPromise = this.verifyFunc(primaryResult, trustedResult);
const primaryUpToDate = await this.verifyPromise;
if (this.trustedPromise === origTrustedPromise) {
@ -285,22 +285,22 @@ export class AutoVerifier<T> {
if (this.trustedPromise !== origTrustedPromise) {
// we've been invalidated while we were waiting for the trusted result!
console.warn(
'ULTRA RARE ALERT: we got unverified while awaiting a trusted promise another path was verifying!',
);
console.warn(
'ULTRA RARE ALERT: we got unverified while awaiting a trusted promise another path was verifying!',
);
await tryResolveTrustedPromise();
return;
}
const origVerifyPromise: Promise<boolean> | null = this.verifyPromise;
await origVerifyPromise; // we don't care about the result, just that we wait for the verification to finish
if (this.verifyPromise !== origVerifyPromise) {
const origVerifyPromise: Promise<boolean> | null = this.verifyPromise;
await origVerifyPromise; // we don't care about the result, just that we wait for the verification to finish
if (this.verifyPromise !== origVerifyPromise) {
// we've been invalidated while we were waiting for the trusted result!
console.warn(
'ULTRA RARE ALERT: we got unverified while awaiting a verify promise another path was calling!',
origVerifyPromise,
this.verifyPromise,
);
console.warn(
'ULTRA RARE ALERT: we got unverified while awaiting a verify promise another path was calling!',
origVerifyPromise,
this.verifyPromise,
);
await tryResolveTrustedPromise();
return;
}
@ -321,15 +321,15 @@ export class AutoVerifier<T> {
await tryResolveTrustedPromise();
} catch (e: unknown) {
if (!resolved) {
this.trustedPromise = null; // suppress warnings
this.primaryPromise = null; // suppress warnings
this.verifyPromise = null; // suppress warnings
this.unverify();
this.trustedPromise = null; // suppress warnings
this.primaryPromise = null; // suppress warnings
this.verifyPromise = null; // suppress warnings
this.unverify();
// eslint-disable-next-line prefer-promise-reject-errors
reject(e as Error);
resolved = true;
} else {
this.unverify()
this.unverify()
console.warn('server request failed after returning cache value (or when already rejected)', e);
}
}