shelving 1.200.0 → 1.201.0

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;
@@ -2,7 +2,7 @@ import type { AnyCaller } from "../../util/function.js";
2
2
  import type { RequestOptions } from "../../util/http.js";
3
3
  import type { Endpoint } from "../endpoint/Endpoint.js";
4
4
  /** Provider for API endpoints rooted at a common base URL. */
5
- export declare abstract class APIProvider<P = unknown, R = unknown> {
5
+ export declare abstract class APIProvider<P = unknown, R = unknown> implements AsyncDisposable {
6
6
  /** The base URL for this API. */
7
7
  abstract readonly url: URL;
8
8
  /**
@@ -43,4 +43,5 @@ export declare abstract class APIProvider<P = unknown, R = unknown> {
43
43
  abstract parseResponse<PP extends P, RR extends R>(_endpoint: Endpoint<PP, RR>, response: Response, caller?: AnyCaller): Promise<RR>;
44
44
  /** Send a payload to an `Endpoint` and retrieve the result. */
45
45
  call<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Promise<RR>;
46
+ [Symbol.asyncDispose](): Promise<void>;
46
47
  }
@@ -6,4 +6,8 @@ export class APIProvider {
6
6
  const response = await this.fetch(request);
7
7
  return this.parseResponse(endpoint, response, caller);
8
8
  }
9
+ // Implement `AsyncDisposable`
10
+ async [Symbol.asyncDispose]() {
11
+ // Empty by default.
12
+ }
9
13
  }
@@ -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,14 +8,19 @@ 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
- export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
16
+ export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> implements AsyncDisposable {
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;
19
24
  refreshAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
25
+ [Symbol.asyncDispose](): Promise<void>;
20
26
  }
@@ -1,3 +1,5 @@
1
+ import { AVOID_REFRESH } from "../../store/FetchStore.js";
2
+ import { awaitDispose } from "../../util/dispose.js";
1
3
  import { APICache } from "../cache/APICache.js";
2
4
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
3
5
  /**
@@ -5,19 +7,22 @@ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
5
7
  * - Constructor accepts a `source` provider and an optional default `maxAge`.
6
8
  * - On `call(...)`, triggers `cache.refresh(maxAge)` for the endpoint+payload before awaiting `cache.call(...)`.
7
9
  * - `invalidate`, `invalidateAll`, `refresh`, and `refreshAll` pass through to the underlying cache and use `this.maxAge` as the default refresh timing.
10
+ *
11
+ * @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).
12
+ * - Note: This is not used for `refresh()` calls — when you call `refresh()` you likely mean "do it now".
13
+ * - 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
14
  */
9
15
  export class CachedAPIProvider extends ThroughAPIProvider {
10
16
  maxAge;
11
17
  _cache;
12
- constructor(source, maxAge) {
18
+ constructor(source, maxAge = AVOID_REFRESH) {
13
19
  super(source);
14
20
  this.maxAge = maxAge;
15
21
  this._cache = new APICache(source);
16
22
  }
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);
23
+ // Override call to use the cache racther than calling fresh each time.
24
+ call(endpoint, payload, _options, caller = this.call) {
25
+ return this._cache.call(endpoint, payload, this.maxAge, caller);
21
26
  }
22
27
  invalidate(endpoint, payload) {
23
28
  this._cache.invalidate(endpoint, payload);
@@ -31,4 +36,9 @@ export class CachedAPIProvider extends ThroughAPIProvider {
31
36
  refreshAll(endpoint) {
32
37
  this._cache.refreshAll(endpoint, this.maxAge);
33
38
  }
39
+ // Implement `AsyncDisposable`
40
+ async [Symbol.asyncDispose]() {
41
+ await awaitDispose(this._cache, // Dispose the cache.
42
+ super[Symbol.asyncDispose]());
43
+ }
34
44
  }
@@ -15,4 +15,5 @@ export declare class ThroughAPIProvider<P, R> extends APIProvider<P, R> implemen
15
15
  getRequest<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Request;
16
16
  parseResponse<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, response: Response, caller?: AnyCaller): Promise<RR>;
17
17
  fetch(request: Request): Promise<Response>;
18
+ [Symbol.asyncDispose](): Promise<void>;
18
19
  }
@@ -1,3 +1,4 @@
1
+ import { awaitDispose } from "../../util/dispose.js";
1
2
  import { APIProvider } from "./APIProvider.js";
2
3
  /**
3
4
  * Provider wrapper that delegates API operations to a source provider.
@@ -24,4 +25,8 @@ export class ThroughAPIProvider extends APIProvider {
24
25
  fetch(request) {
25
26
  return this.source.fetch(request);
26
27
  }
28
+ // Implement `AsyncDisposable`
29
+ async [Symbol.asyncDispose]() {
30
+ await awaitDispose(this.source);
31
+ }
27
32
  }
@@ -23,4 +23,5 @@ export declare class CacheDBProvider<I extends Identifier, T extends Data> exten
23
23
  setQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>, data: TT): Promise<void>;
24
24
  updateQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>, updates: Updates<TT>): Promise<void>;
25
25
  deleteQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<void>;
26
+ [Symbol.asyncDispose](): Promise<void>;
26
27
  }
@@ -1,3 +1,4 @@
1
+ import { awaitDispose } from "../../util/dispose.js";
1
2
  import { DBProvider } from "./DBProvider.js";
2
3
  import { MemoryDBProvider } from "./MemoryDBProvider.js";
3
4
  /** Keep a copy of asynchronous remote data in a local synchronous cache. */
@@ -58,4 +59,10 @@ export class CacheDBProvider extends DBProvider {
58
59
  await this.source.deleteQuery(collection, query);
59
60
  this.memory.getTable(collection).deleteQuery(query);
60
61
  }
62
+ // Implement `AsyncDisposable`
63
+ async [Symbol.asyncDispose]() {
64
+ await awaitDispose(this.source, // Dispose the source API provider.
65
+ this.memory, // Dispose the source API provider.
66
+ super[Symbol.asyncDispose]());
67
+ }
61
68
  }
@@ -14,7 +14,10 @@ export type DBChange<I extends Identifier> = {
14
14
  readonly data?: unknown;
15
15
  readonly updates?: unknown;
16
16
  };
17
- /** Asynchronous provider that keeps a log of any written changes to its `.changes` property. */
17
+ /**
18
+ * Database provider that keeps a log of any written changes to its `.changes` property.
19
+ * - This is useful if you want to hook into a database to build out audit logging functionality.
20
+ */
18
21
  export declare class ChangesDBProvider<I extends Identifier, T extends Data> extends ThroughDBProvider<I, T> {
19
22
  get changes(): ReadonlyArray<DBChange<I>>;
20
23
  readonly _changes: MutableArray<DBChange<I>>;
@@ -1,5 +1,8 @@
1
1
  import { ThroughDBProvider } from "./ThroughDBProvider.js";
2
- /** Asynchronous provider that keeps a log of any written changes to its `.changes` property. */
2
+ /**
3
+ * Database provider that keeps a log of any written changes to its `.changes` property.
4
+ * - This is useful if you want to hook into a database to build out audit logging functionality.
5
+ */
3
6
  export class ChangesDBProvider extends ThroughDBProvider {
4
7
  get changes() {
5
8
  return this._changes;
@@ -4,7 +4,7 @@ import type { Query } from "../../util/query.js";
4
4
  import type { Updates } from "../../util/update.js";
5
5
  import type { Collection } from "../collection/Collection.js";
6
6
  /** Provider with a fully asynchronous interface for database access. */
7
- export declare abstract class DBProvider<I extends Identifier = Identifier, T extends Data = Data> {
7
+ export declare abstract class DBProvider<I extends Identifier = Identifier, T extends Data = Data> implements AsyncDisposable {
8
8
  abstract getItem<II extends I, TT extends T>(collection: Collection<string, II, TT>, id: II): Promise<OptionalItem<II, TT>>;
9
9
  requireItem<II extends I, TT extends T>(collection: Collection<string, II, TT>, id: II): Promise<Item<II, TT>>;
10
10
  abstract getItemSequence<II extends I, TT extends T>(collection: Collection<string, II, TT>, id: II): OptionalItemSequence<II, TT>;
@@ -20,4 +20,5 @@ export declare abstract class DBProvider<I extends Identifier = Identifier, T ex
20
20
  abstract deleteQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<void>;
21
21
  getFirst<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<OptionalItem<II, TT>>;
22
22
  requireFirst<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<Item<II, TT>>;
23
+ [Symbol.asyncDispose](): Promise<void>;
23
24
  }
@@ -1,5 +1,6 @@
1
1
  import { RequiredError } from "../../error/RequiredError.js";
2
2
  import { countArray, getFirst } from "../../util/array.js";
3
+ import { awaitDispose } from "../../util/dispose.js";
3
4
  /** Provider with a fully asynchronous interface for database access. */
4
5
  export class DBProvider {
5
6
  async requireItem(collection, id) {
@@ -30,4 +31,10 @@ export class DBProvider {
30
31
  });
31
32
  return first;
32
33
  }
34
+ // Implement `AsyncDisposable`
35
+ async [Symbol.asyncDispose]() {
36
+ await awaitDispose(
37
+ // Empty by default.
38
+ );
39
+ }
33
40
  }
@@ -8,7 +8,7 @@ import { DBProvider } from "./DBProvider.js";
8
8
  /**
9
9
  * Fast in-memory store for data.
10
10
  * - Extremely fast (ideal for caching!), but does not persist data after the browser window is closed.
11
- * - `get()` etc return the exact same instance of an object that's passed into `set()`
11
+ * - `getItem()` etc return the exact same instance of an object that's passed into `setItem()`
12
12
  */
13
13
  export declare class MemoryDBProvider<I extends Identifier = Identifier, T extends Data = Data> extends DBProvider<I, T> {
14
14
  /** List of tables in `{ name: MemoryTable }` format. */
@@ -11,7 +11,7 @@ import { DBProvider } from "./DBProvider.js";
11
11
  /**
12
12
  * Fast in-memory store for data.
13
13
  * - Extremely fast (ideal for caching!), but does not persist data after the browser window is closed.
14
- * - `get()` etc return the exact same instance of an object that's passed into `set()`
14
+ * - `getItem()` etc return the exact same instance of an object that's passed into `setItem()`
15
15
  */
16
16
  export class MemoryDBProvider extends DBProvider {
17
17
  /** List of tables in `{ name: MemoryTable }` format. */
@@ -24,4 +24,5 @@ export declare class ThroughDBProvider<I extends Identifier, T extends Data> imp
24
24
  deleteQuery<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<void>;
25
25
  getFirst<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<OptionalItem<II, TT>>;
26
26
  requireFirst<II extends I, TT extends T>(collection: Collection<string, II, TT>, query: Query<Item<II, TT>>): Promise<Item<II, TT>>;
27
+ [Symbol.asyncDispose](): Promise<void>;
27
28
  }
@@ -1,3 +1,4 @@
1
+ import { awaitDispose } from "../../util/dispose.js";
1
2
  /** A provider that passes through to an asynchronous source. */
2
3
  export class ThroughDBProvider {
3
4
  source;
@@ -49,4 +50,8 @@ export class ThroughDBProvider {
49
50
  requireFirst(collection, query) {
50
51
  return this.source.requireFirst(collection, query);
51
52
  }
53
+ // Implement `AsyncDisposable`
54
+ async [Symbol.asyncDispose]() {
55
+ await awaitDispose(this.source);
56
+ }
52
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.200.0",
3
+ "version": "1.201.0",
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. */