shelving 1.200.1 → 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.
@@ -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
  }
@@ -13,7 +13,7 @@ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
13
13
  * - Note: This is not used for `refresh()` calls — when you call `refresh()` you likely mean "do it now".
14
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()`
15
15
  */
16
- export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
16
+ export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> implements AsyncDisposable {
17
17
  readonly maxAge: number | undefined;
18
18
  private readonly _cache;
19
19
  constructor(source: APIProvider<P, R>, maxAge?: number);
@@ -22,4 +22,5 @@ export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
22
22
  invalidateAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
23
23
  refresh<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP): void;
24
24
  refreshAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
25
+ [Symbol.asyncDispose](): Promise<void>;
25
26
  }
@@ -1,4 +1,5 @@
1
1
  import { AVOID_REFRESH } from "../../store/FetchStore.js";
2
+ import { awaitDispose } from "../../util/dispose.js";
2
3
  import { APICache } from "../cache/APICache.js";
3
4
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
4
5
  /**
@@ -35,4 +36,9 @@ export class CachedAPIProvider extends ThroughAPIProvider {
35
36
  refreshAll(endpoint) {
36
37
  this._cache.refreshAll(endpoint, this.maxAge);
37
38
  }
39
+ // Implement `AsyncDisposable`
40
+ async [Symbol.asyncDispose]() {
41
+ await awaitDispose(this._cache, // Dispose the cache.
42
+ super[Symbol.asyncDispose]());
43
+ }
38
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.1",
3
+ "version": "1.201.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",