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