shelving 1.200.0 → 1.200.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { AVOID_REFRESH } from "../../store/FetchStore.js";
1
2
  import { awaitDispose } from "../../util/dispose.js";
2
3
  import { setMapItem } from "../../util/map.js";
3
4
  import { EndpointCache } from "./EndpointCache.js";
@@ -23,7 +24,7 @@ export class APICache {
23
24
  * - Waits for the in-flight fetch if the store is loading.
24
25
  * - Throws if the fetch fails, matching `APIProvider.call` behaviour.
25
26
  */
26
- async call(endpoint, payload, maxAge, caller = this.call) {
27
+ async call(endpoint, payload, maxAge = AVOID_REFRESH, caller = this.call) {
27
28
  return this.get(endpoint).call(payload, maxAge, caller);
28
29
  }
29
30
  /** Invalidate a specific store for an endpoint. */
@@ -18,6 +18,8 @@ export declare class EndpointCache<P = unknown, R = unknown> implements AsyncDis
18
18
  * - Returns the cached value immediately if one exists.
19
19
  * - Waits for the in-flight fetch if the store is loading.
20
20
  * - Throws if the fetch fails, matching `APIProvider.call` behaviour.
21
+ *
22
+ * @param maxAge The maximum age (defaults to only refreshing if the value is still in a loading state).
21
23
  */
22
24
  call(payload: P, maxAge?: number, caller?: AnyCaller): Promise<R>;
23
25
  /** Invalidate a specific store. */
@@ -1,3 +1,4 @@
1
+ import { AVOID_REFRESH } from "../../store/FetchStore.js";
1
2
  import { awaitValues } from "../../util/async.js";
2
3
  import { awaitDispose } from "../../util/dispose.js";
3
4
  import { setMapItem } from "../../util/map.js";
@@ -24,8 +25,10 @@ export class EndpointCache {
24
25
  * - Returns the cached value immediately if one exists.
25
26
  * - Waits for the in-flight fetch if the store is loading.
26
27
  * - Throws if the fetch fails, matching `APIProvider.call` behaviour.
28
+ *
29
+ * @param maxAge The maximum age (defaults to only refreshing if the value is still in a loading state).
27
30
  */
28
- async call(payload, maxAge, caller = this.call) {
31
+ async call(payload, maxAge = AVOID_REFRESH, caller = this.call) {
29
32
  const store = this.get(payload, caller);
30
33
  await store.refresh(maxAge);
31
34
  return store.value;
@@ -1,4 +1,5 @@
1
1
  import type { AnyCaller } from "../../util/function.js";
2
+ import type { RequestOptions } from "../../util/http.js";
2
3
  import type { Endpoint } from "../endpoint/Endpoint.js";
3
4
  import type { APIProvider } from "./APIProvider.js";
4
5
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
@@ -7,12 +8,16 @@ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
7
8
  * - Constructor accepts a `source` provider and an optional default `maxAge`.
8
9
  * - On `call(...)`, triggers `cache.refresh(maxAge)` for the endpoint+payload before awaiting `cache.call(...)`.
9
10
  * - `invalidate`, `invalidateAll`, `refresh`, and `refreshAll` pass through to the underlying cache and use `this.maxAge` as the default refresh timing.
11
+ *
12
+ * @param `maxAge` The maximum age used when calling `call()` (defaults to `AVOID_REFRESH`, i.e. only refresh if the value is invalidated or still loading).
13
+ * - Note: This is not used for `refresh()` calls — when you call `refresh()` you likely mean "do it now".
14
+ * - When we are using `call()` on a cache, the entire point of the cache is to "cache", so the default isn't `0` like it is for `refresh()`
10
15
  */
11
16
  export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
12
17
  readonly maxAge: number | undefined;
13
18
  private readonly _cache;
14
19
  constructor(source: APIProvider<P, R>, maxAge?: number);
15
- call<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, maxAge?: number | undefined, caller?: AnyCaller): Promise<RR>;
20
+ call<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, _options?: RequestOptions, caller?: AnyCaller): Promise<RR>;
16
21
  invalidate<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP): void;
17
22
  invalidateAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
18
23
  refresh<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP): void;
@@ -1,3 +1,4 @@
1
+ import { AVOID_REFRESH } from "../../store/FetchStore.js";
1
2
  import { APICache } from "../cache/APICache.js";
2
3
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
3
4
  /**
@@ -5,19 +6,22 @@ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
5
6
  * - Constructor accepts a `source` provider and an optional default `maxAge`.
6
7
  * - On `call(...)`, triggers `cache.refresh(maxAge)` for the endpoint+payload before awaiting `cache.call(...)`.
7
8
  * - `invalidate`, `invalidateAll`, `refresh`, and `refreshAll` pass through to the underlying cache and use `this.maxAge` as the default refresh timing.
9
+ *
10
+ * @param `maxAge` The maximum age used when calling `call()` (defaults to `AVOID_REFRESH`, i.e. only refresh if the value is invalidated or still loading).
11
+ * - Note: This is not used for `refresh()` calls — when you call `refresh()` you likely mean "do it now".
12
+ * - When we are using `call()` on a cache, the entire point of the cache is to "cache", so the default isn't `0` like it is for `refresh()`
8
13
  */
9
14
  export class CachedAPIProvider extends ThroughAPIProvider {
10
15
  maxAge;
11
16
  _cache;
12
- constructor(source, maxAge) {
17
+ constructor(source, maxAge = AVOID_REFRESH) {
13
18
  super(source);
14
19
  this.maxAge = maxAge;
15
20
  this._cache = new APICache(source);
16
21
  }
17
- // @ts-expect-error TS2416: intentionally diverges from `APIProvider.call` `RequestOptions` are not used by the cache, so the third arg is repurposed as `maxAge`.
18
- call(endpoint, payload, maxAge = this.maxAge, caller = this.call) {
19
- this._cache.refresh(endpoint, payload, maxAge);
20
- return this._cache.call(endpoint, payload, maxAge, caller);
22
+ // Override call to use the cache racther than calling fresh each time.
23
+ call(endpoint, payload, _options, caller = this.call) {
24
+ return this._cache.call(endpoint, payload, this.maxAge, caller);
21
25
  }
22
26
  invalidate(endpoint, payload) {
23
27
  this._cache.invalidate(endpoint, payload);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.200.0",
3
+ "version": "1.200.1",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +1,10 @@
1
1
  import { type NONE } from "../util/constants.js";
2
2
  import { BusyStore } from "./BusyStore.js";
3
3
  import type { AsyncStoreInput, StoreInput } from "./Store.js";
4
+ /** Zero passed to `refresh()` means "always refresh this value" (this is the default). */
5
+ export declare const ALWAYS_REFRESH = 0;
6
+ /** Infinity passed to `refresh()` means "only refresh if this value is not loaded or invalid". */
7
+ export declare const AVOID_REFRESH: number;
4
8
  /** Callback for a callback fetch store. */
5
9
  export type FetchCallback<T> = (signal: AbortSignal) => StoreInput<T> | PromiseLike<StoreInput<T>>;
6
10
  /**
@@ -21,6 +25,11 @@ export declare class FetchStore<T, TT = T> extends BusyStore<T, TT> {
21
25
  * - Triggered automatically when someone reads `value` or `loading`.
22
26
  * - Refreshes are de-duplicated. Concurrent calls while a fetch is in-flight return the same promise.
23
27
  * - Never throws — errors are stored as `reason`.
28
+ *
29
+ * @param maxAge The maximum age for whether we refresh or not.
30
+ * - `0` zero means "always refresh" (this is the default).
31
+ * - `Infinity` means "refresh only if store is still in a loading state.
32
+ * - Any other value may or may not be stale based on `this.age`
24
33
  */
25
34
  refresh(maxAge?: number): Promise<boolean> | boolean;
26
35
  private _pendingRefresh;
@@ -2,6 +2,10 @@ import { RequiredError } from "../error/RequiredError.js";
2
2
  import { isAsync } from "../util/async.js";
3
3
  import { ABORT, SKIP } from "../util/constants.js";
4
4
  import { BusyStore } from "./BusyStore.js";
5
+ /** Zero passed to `refresh()` means "always refresh this value" (this is the default). */
6
+ export const ALWAYS_REFRESH = 0;
7
+ /** Infinity passed to `refresh()` means "only refresh if this value is not loaded or invalid". */
8
+ export const AVOID_REFRESH = Infinity;
5
9
  /**
6
10
  * Store that fetches its values from a remote source.
7
11
  *
@@ -47,8 +51,13 @@ export class FetchStore extends BusyStore {
47
51
  * - Triggered automatically when someone reads `value` or `loading`.
48
52
  * - Refreshes are de-duplicated. Concurrent calls while a fetch is in-flight return the same promise.
49
53
  * - Never throws — errors are stored as `reason`.
54
+ *
55
+ * @param maxAge The maximum age for whether we refresh or not.
56
+ * - `0` zero means "always refresh" (this is the default).
57
+ * - `Infinity` means "refresh only if store is still in a loading state.
58
+ * - Any other value may or may not be stale based on `this.age`
50
59
  */
51
- refresh(maxAge) {
60
+ refresh(maxAge = ALWAYS_REFRESH) {
52
61
  if (this._pendingRefresh)
53
62
  return this._pendingRefresh;
54
63
  if (!this.stale(maxAge))
package/store/Store.d.ts CHANGED
@@ -99,10 +99,10 @@ export declare class Store<T, TT = T> implements AsyncIterable<T, void, void>, A
99
99
  *
100
100
  * @param maxAge The maximum age for the stale check.
101
101
  * - `0` zero means "always refresh" (this is the default).
102
- * - `Infinity` means "refresh only if store is still in a loading state.
102
+ * - `Infinity` means "refresh only if store is still in a loading state"
103
103
  * - Any other value may or may not be stale based on `this.age`
104
104
  */
105
- stale(maxAge?: number): boolean;
105
+ stale(maxAge: number): boolean;
106
106
  /** Current error of this store, or `undefined` if there is no error. */
107
107
  get reason(): unknown;
108
108
  set reason(reason: unknown);
package/store/Store.js CHANGED
@@ -137,10 +137,10 @@ export class Store {
137
137
  *
138
138
  * @param maxAge The maximum age for the stale check.
139
139
  * - `0` zero means "always refresh" (this is the default).
140
- * - `Infinity` means "refresh only if store is still in a loading state.
140
+ * - `Infinity` means "refresh only if store is still in a loading state"
141
141
  * - Any other value may or may not be stale based on `this.age`
142
142
  */
143
- stale(maxAge = 0) {
143
+ stale(maxAge) {
144
144
  return this.age >= maxAge;
145
145
  }
146
146
  /** Current error of this store, or `undefined` if there is no error. */