Skip to content

Commit 49a5fd3

Browse files
authored
enhance: Consolidate controller expiry logic in private getExpiryStatus() (#3450)
1 parent a571530 commit 49a5fd3

File tree

3 files changed

+63
-90
lines changed

3 files changed

+63
-90
lines changed

packages/core/src/controller/Controller.ts

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,11 @@ export default class Controller<
540540
} else if (!schema || !schemaHasEntity(schema)) {
541541
return {
542542
data: cacheEndpoints,
543-
expiryStatus:
544-
meta?.invalidated ? ExpiryStatus.Invalid
545-
: cacheEndpoints && !endpoint.invalidIfStale ? ExpiryStatus.Valid
546-
: ExpiryStatus.InvalidIfStale,
543+
expiryStatus: this.getExpiryStatus(
544+
!cacheEndpoints,
545+
!!endpoint.invalidIfStale,
546+
meta,
547+
),
547548
expiresAt: expiresAt || 0,
548549
countRef: this.gcPolicy.createCountRef({ key }),
549550
};
@@ -558,18 +559,23 @@ export default class Controller<
558559
args,
559560
) as { data: any; paths: EntityPath[] };
560561

561-
// note: isInvalid can only be true if shouldQuery is true
562-
if (!expiresAt && isInvalid) expiresAt = 1;
562+
if (!expiresAt) {
563+
// note: isInvalid can only be true if shouldQuery is true
564+
if (isInvalid) expiresAt = 1;
565+
// fallback to entity expiry time
566+
else expiresAt = entityExpiresAt(paths, state.entityMeta);
567+
}
563568

564-
return this.getSchemaResponse(
569+
return {
565570
data,
566-
key,
567-
paths,
568-
state.entityMeta,
571+
expiryStatus: this.getExpiryStatus(
572+
typeof data === 'symbol',
573+
!!endpoint.invalidIfStale || isInvalid,
574+
meta,
575+
),
569576
expiresAt,
570-
endpoint.invalidIfStale || isInvalid,
571-
meta,
572-
);
577+
countRef: this.gcPolicy.createCountRef({ key, paths }),
578+
};
573579
}
574580

575581
/**
@@ -638,42 +644,19 @@ export default class Controller<
638644
};
639645
}
640646

641-
private getSchemaResponse<T>(
642-
data: T,
643-
key: string,
644-
paths: EntityPath[],
645-
entityMeta: State<unknown>['entityMeta'],
646-
expiresAt: number,
647+
private getExpiryStatus(
648+
invalidData: boolean,
647649
invalidIfStale: boolean,
648650
meta: { error?: unknown; invalidated?: unknown } = {},
649-
): {
650-
data: T;
651-
expiryStatus: ExpiryStatus;
652-
expiresAt: number;
653-
countRef: () => () => void;
654-
} {
655-
const invalidDenormalize = typeof data === 'symbol';
656-
657-
// fallback to entity expiry time
658-
if (!expiresAt) {
659-
expiresAt = entityExpiresAt(paths, entityMeta);
660-
}
661-
651+
) {
662652
// https://dataclient.io/docs/concepts/expiry-policy#expiry-status
663653
// we don't track the difference between stale or fresh because that is tied to triggering
664654
// conditions
665-
const expiryStatus =
666-
meta?.invalidated || (invalidDenormalize && !meta?.error) ?
667-
ExpiryStatus.Invalid
668-
: invalidDenormalize || invalidIfStale ? ExpiryStatus.InvalidIfStale
669-
: ExpiryStatus.Valid;
670-
671-
return {
672-
data,
673-
expiryStatus,
674-
expiresAt,
675-
countRef: this.gcPolicy.createCountRef({ key, paths }),
676-
};
655+
return (
656+
meta.invalidated || (invalidData && !meta.error) ? ExpiryStatus.Invalid
657+
: invalidData || invalidIfStale ? ExpiryStatus.InvalidIfStale
658+
: ExpiryStatus.Valid
659+
);
677660
}
678661
}
679662

website/src/components/Playground/editor-types/@data-client/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ declare class Controller<D extends GenericDispatch = DataClientDispatch> {
807807
data: DenormalizeNullable<S> | undefined;
808808
countRef: () => () => void;
809809
};
810-
private getSchemaResponse;
810+
private getExpiryStatus;
811811
}
812812

813813
declare class Snapshot<T = unknown> implements SnapshotInterface {

website/src/components/Playground/editor-types/bignumber.d.ts

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
// Documentation: http://mikemcl.github.io/bignumber.js/
77
//
8-
// Exports:
9-
//
10-
// class BigNumber (default export)
8+
// class BigNumber
119
// type BigNumber.Constructor
1210
// type BigNumber.ModuloMode
1311
// type BigNumber.RoundingMode
@@ -31,9 +29,7 @@
3129
//
3230
// The use of compiler option `--strictNullChecks` is recommended.
3331

34-
export default BigNumber;
35-
36-
export namespace BigNumber {
32+
declare namespace BigNumber {
3733

3834
/** See `BigNumber.config` (alias `BigNumber.set`) and `BigNumber.clone`. */
3935
interface Config {
@@ -324,10 +320,10 @@ export namespace BigNumber {
324320
type Constructor = typeof BigNumber;
325321
type ModuloMode = 0 | 1 | 3 | 6 | 9;
326322
type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
327-
type Value = string | number | Instance;
323+
type Value = string | number | bigint | Instance;
328324
}
329325

330-
export declare class BigNumber implements BigNumber.Instance {
326+
declare class BigNumber implements BigNumber.Instance {
331327

332328
/** Used internally to identify a BigNumber instance. */
333329
private readonly _isBigNumber: true;
@@ -343,7 +339,7 @@ export declare class BigNumber implements BigNumber.Instance {
343339

344340
/**
345341
* Returns a new instance of a BigNumber object with value `n`, where `n` is a numeric value in
346-
* the specified `base`, or base 10 if `base` is omitted or is `null` or `undefined`.
342+
* the specified `base`, or base 10 if `base` is omitted.
347343
*
348344
* ```ts
349345
* x = new BigNumber(123.4567) // '123.4567'
@@ -485,17 +481,16 @@ export declare class BigNumber implements BigNumber.Instance {
485481
* @param n A numeric value.
486482
* @param [base] The base of n.
487483
*/
488-
comparedTo(n: BigNumber.Value, base?: number): number;
484+
comparedTo(n: BigNumber.Value, base?: number): 1 | -1 | 0 | null;
489485

490486
/**
491487
* Returns a BigNumber whose value is the value of this BigNumber rounded by rounding mode
492488
* `roundingMode` to a maximum of `decimalPlaces` decimal places.
493489
*
494-
* If `decimalPlaces` is omitted, or is `null` or `undefined`, the return value is the number of
495-
* decimal places of the value of this BigNumber, or `null` if the value of this BigNumber is
496-
* ±`Infinity` or `NaN`.
490+
* If `decimalPlaces` is omitted, the return value is the number of decimal places of the value of
491+
* this BigNumber, or `null` if the value of this BigNumber is ±`Infinity` or `NaN`.
497492
*
498-
* If `roundingMode` is omitted, or is `null` or `undefined`, `ROUNDING_MODE` is used.
493+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
499494
*
500495
* Throws if `decimalPlaces` or `roundingMode` is invalid.
501496
*
@@ -524,11 +519,10 @@ export declare class BigNumber implements BigNumber.Instance {
524519
* Returns a BigNumber whose value is the value of this BigNumber rounded by rounding mode
525520
* `roundingMode` to a maximum of `decimalPlaces` decimal places.
526521
*
527-
* If `decimalPlaces` is omitted, or is `null` or `undefined`, the return value is the number of
528-
* decimal places of the value of this BigNumber, or `null` if the value of this BigNumber is
529-
* ±`Infinity` or `NaN`.
522+
* If `decimalPlaces` is omitted, the return value is the number of decimal places of the value of
523+
* this BigNumber, or `null` if the value of this BigNumber is ±`Infinity` or `NaN`.
530524
*
531-
* If `roundingMode` is omitted, or is `null` or `undefined`, `ROUNDING_MODE` is used.
525+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
532526
*
533527
* Throws if `decimalPlaces` or `roundingMode` is invalid.
534528
*
@@ -693,7 +687,7 @@ export declare class BigNumber implements BigNumber.Instance {
693687
* Returns a BigNumber whose value is the value of this BigNumber rounded to an integer using
694688
* rounding mode `rm`.
695689
*
696-
* If `rm` is omitted, or is `null` or `undefined`, `ROUNDING_MODE` is used.
690+
* If `rm` is omitted, `ROUNDING_MODE` is used.
697691
*
698692
* Throws if `rm` is invalid.
699693
*
@@ -790,7 +784,7 @@ export declare class BigNumber implements BigNumber.Instance {
790784
* returns `false`.
791785
*
792786
* ```ts
793-
* 0.1 > (0.3 - 0 // true
787+
* 0.1 > (0.3 - 0.2) // true
794788
* x = new BigNumber(0.1)
795789
* x.gt(BigNumber(0.3).minus(0.2)) // false
796790
* BigNumber(0).gt(x) // false
@@ -1122,7 +1116,7 @@ export declare class BigNumber implements BigNumber.Instance {
11221116
* Returns a BigNumber whose value is the value of this BigNumber rounded to a precision of
11231117
* `significantDigits` significant digits using rounding mode `roundingMode`.
11241118
*
1125-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` will be used.
1119+
* If `roundingMode` is omitted, `ROUNDING_MODE` will be used.
11261120
*
11271121
* Throws if `significantDigits` or `roundingMode` is invalid.
11281122
*
@@ -1166,7 +1160,7 @@ export declare class BigNumber implements BigNumber.Instance {
11661160
* Returns a BigNumber whose value is the value of this BigNumber rounded to a precision of
11671161
* `significantDigits` significant digits using rounding mode `roundingMode`.
11681162
*
1169-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` will be used.
1163+
* If `roundingMode` is omitted, `ROUNDING_MODE` will be used.
11701164
*
11711165
* Throws if `significantDigits` or `roundingMode` is invalid.
11721166
*
@@ -1244,11 +1238,10 @@ export declare class BigNumber implements BigNumber.Instance {
12441238
* If the value of this BigNumber in exponential notation has fewer than `decimalPlaces` fraction
12451239
* digits, the return value will be appended with zeros accordingly.
12461240
*
1247-
* If `decimalPlaces` is omitted, or is `null` or `undefined`, the number of digits after the
1248-
* decimal point defaults to the minimum number of digits necessary to represent the value
1249-
* exactly.
1241+
* If `decimalPlaces` is omitted, the number of digits after the decimal point defaults to the
1242+
* minimum number of digits necessary to represent the value exactly.
12501243
*
1251-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` is used.
1244+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
12521245
*
12531246
* Throws if `decimalPlaces` or `roundingMode` is invalid.
12541247
*
@@ -1282,12 +1275,12 @@ export declare class BigNumber implements BigNumber.Instance {
12821275
* Unlike `Number.prototype.toFixed`, which returns exponential notation if a number is greater or
12831276
* equal to 10**21, this method will always return normal notation.
12841277
*
1285-
* If `decimalPlaces` is omitted or is `null` or `undefined`, the return value will be unrounded
1286-
* and in normal notation. This is also unlike `Number.prototype.toFixed`, which returns the value
1287-
* to zero decimal places. It is useful when normal notation is required and the current
1288-
* `EXPONENTIAL_AT` setting causes `toString` to return exponential notation.
1278+
* If `decimalPlaces` is omitted, the return value will be unrounded and in normal notation.
1279+
* This is also unlike `Number.prototype.toFixed`, which returns the value to zero decimal places.
1280+
* It is useful when normal notation is required and the current `EXPONENTIAL_AT` setting causes
1281+
* `toString` to return exponential notation.
12891282
*
1290-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` is used.
1283+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
12911284
*
12921285
* Throws if `decimalPlaces` or `roundingMode` is invalid.
12931286
*
@@ -1317,12 +1310,12 @@ export declare class BigNumber implements BigNumber.Instance {
13171310
*
13181311
* The formatting object may contain some or all of the properties shown in the examples below.
13191312
*
1320-
* If `decimalPlaces` is omitted or is `null` or `undefined`, then the return value is not
1321-
* rounded to a fixed number of decimal places.
1313+
* If `decimalPlaces` is omitted, then the return value is not rounded to a fixed number of
1314+
* decimal places.
13221315
*
1323-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` is used.
1316+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
13241317
*
1325-
* If `format` is omitted or is `null` or `undefined`, `FORMAT` is used.
1318+
* If `format` is omitted, `FORMAT` is used.
13261319
*
13271320
* Throws if `decimalPlaces`, `roundingMode`, or `format` is invalid.
13281321
*
@@ -1378,8 +1371,8 @@ export declare class BigNumber implements BigNumber.Instance {
13781371
* Returns an array of two BigNumbers representing the value of this BigNumber as a simple
13791372
* fraction with an integer numerator and an integer denominator.
13801373
* The denominator will be a positive non-zero value less than or equal to `max_denominator`.
1381-
* If a maximum denominator, `max_denominator`, is not specified, or is `null` or `undefined`, the
1382-
* denominator will be the lowest value necessary to represent the number exactly.
1374+
* If a maximum denominator, `max_denominator`, is not specified, the denominator will be the
1375+
* lowest value necessary to represent the number exactly.
13831376
*
13841377
* Throws if `max_denominator` is invalid.
13851378
*
@@ -1430,10 +1423,9 @@ export declare class BigNumber implements BigNumber.Instance {
14301423
* If `significantDigits` is less than the number of digits necessary to represent the integer
14311424
* part of the value in normal (fixed-point) notation, then exponential notation is used.
14321425
*
1433-
* If `significantDigits` is omitted, or is `null` or `undefined`, then the return value is the
1434-
* same as `n.toString()`.
1426+
* If `significantDigits` is omitted, then the return value is the same as `n.toString()`.
14351427
*
1436-
* If `roundingMode` is omitted or is `null` or `undefined`, `ROUNDING_MODE` is used.
1428+
* If `roundingMode` is omitted, `ROUNDING_MODE` is used.
14371429
*
14381430
* Throws if `significantDigits` or `roundingMode` is invalid.
14391431
*
@@ -1458,7 +1450,7 @@ export declare class BigNumber implements BigNumber.Instance {
14581450

14591451
/**
14601452
* Returns a string representing the value of this BigNumber in base `base`, or base 10 if `base`
1461-
* is omitted or is `null` or `undefined`.
1453+
* is omitted.
14621454
*
14631455
* For bases above 10, and using the default base conversion alphabet (see `ALPHABET`), values
14641456
* from 10 to 35 are represented by a-z (the same as `Number.prototype.toString`).
@@ -1471,8 +1463,6 @@ export declare class BigNumber implements BigNumber.Instance {
14711463
* exponent equal to or less than the negative component of the setting, then exponential notation
14721464
* is returned.
14731465
*
1474-
* If `base` is `null` or `undefined` it is ignored.
1475-
*
14761466
* Throws if `base` is invalid.
14771467
*
14781468
* ```ts
@@ -1592,7 +1582,7 @@ export declare class BigNumber implements BigNumber.Instance {
15921582

15931583
/**
15941584
* Returns a new independent BigNumber constructor with configuration as described by `object`, or
1595-
* with the default configuration if object is `null` or `undefined`.
1585+
* with the default configuration if object is omitted.
15961586
*
15971587
* Throws if `object` is not an object.
15981588
*
@@ -1828,4 +1818,4 @@ export declare class BigNumber implements BigNumber.Instance {
18281818
static set(object?: BigNumber.Config): BigNumber.Config;
18291819
}
18301820

1831-
export function BigNumber(n: BigNumber.Value, base?: number): BigNumber;
1821+
declare function BigNumber(n: BigNumber.Value, base?: number): BigNumber;

0 commit comments

Comments
 (0)