@tempots/ui 1.2.0 → 1.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempots/ui",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -1,12 +1,53 @@
1
1
  import { Signal, TNode } from '@tempots/dom';
2
2
  import { MakeResourceOptions, AsyncResource } from '../utils/resource';
3
+ /**
4
+ * Options for displaying the different states of an asynchronous resource.
5
+ *
6
+ * @template V - The type of the value when the resource is successfully loaded.
7
+ * @template E - The type of the error when the resource fails to load.
8
+ * @public
9
+ */
3
10
  export interface ResourceDisplayOptions<V, E> {
11
+ /** Function to render when the resource has not been requested yet. */
4
12
  notAsked?: (reload: () => void) => TNode;
13
+ /** Function to render when the resource is loading. */
5
14
  loading?: (previous: Signal<V | undefined>, reload: () => void) => TNode;
15
+ /** Function to render when the resource has failed to load. */
6
16
  error?: (error: Signal<E>, reload: () => void) => TNode;
17
+ /** Function to render when the resource has successfully loaded. */
7
18
  success: (value: Signal<V>, reload: () => void) => TNode;
8
19
  }
20
+ /**
21
+ * Options for creating and displaying an asynchronous resource.
22
+ *
23
+ * @template R - The type of the request.
24
+ * @template V - The type of the value when the resource is successfully loaded.
25
+ * @template E - The type of the error when the resource fails to load.
26
+ * @public
27
+ */
9
28
  export interface ResourceOptions<R, V, E> extends MakeResourceOptions<R, V, E>, ResourceDisplayOptions<V, E> {
10
29
  }
30
+ /**
31
+ * Component to display an asynchronous resource based on its current status.
32
+ *
33
+ * @template V - The type of the value when the resource is successfully loaded.
34
+ * @template E - The type of the error when the resource fails to load.
35
+ *
36
+ * @param {AsyncResource<V, E>} resource - The asynchronous resource to display.
37
+ * @param {ResourceDisplayOptions<V, E>} options - The display options for the resource.
38
+ * @returns {TNode} A node representing the current state of the resource.
39
+ * @public
40
+ */
11
41
  export declare const ResourceDisplay: <V, E>(resource: AsyncResource<V, E>, options: ResourceDisplayOptions<V, E>) => import('@tempots/dom').Renderable;
42
+ /**
43
+ * Creates and displays an asynchronous resource.
44
+ *
45
+ * @template R - The type of the request.
46
+ * @template V - The type of the value when the resource is successfully loaded.
47
+ * @template E - The type of the error when the resource fails to load.
48
+ *
49
+ * @param {ResourceOptions<R, V, E>} options - The options for creating and displaying the resource.
50
+ * @returns {TNode} A node representing the current state of the resource.
51
+ * @public
52
+ */
12
53
  export declare const Resource: <R, V, E>(options: ResourceOptions<R, V, E>) => import('@tempots/dom').Renderable;
@@ -1,21 +1,68 @@
1
1
  import { Signal } from '@tempots/dom';
2
2
  import { AsyncResult } from '@tempots/std';
3
+ /**
4
+ * Represents an asynchronous resource with its current status, value, error, and loading state.
5
+ * Provides methods to reload the resource and dispose of it.
6
+ *
7
+ * @template V - The type of the value when the resource is successfully loaded.
8
+ * @template E - The type of the error when the resource fails to load.
9
+ * @public
10
+ */
3
11
  export interface AsyncResource<V, E> {
12
+ /** The current status of the resource as an AsyncResult. */
4
13
  readonly status: Signal<AsyncResult<V, E>>;
14
+ /** Disposes of the resource, aborting any ongoing requests and cleaning up. */
5
15
  readonly dispose: () => void;
16
+ /** The current value of the resource, or undefined if not loaded or failed. */
6
17
  readonly value: Signal<V | undefined>;
18
+ /** The current error of the resource, or undefined if not failed. */
7
19
  readonly error: Signal<E | undefined>;
20
+ /** Whether the resource is currently loading. */
8
21
  readonly loading: Signal<boolean>;
22
+ /** Reloads the resource using the current request. */
9
23
  readonly reload: () => void;
10
24
  }
25
+ /**
26
+ * Options for loading a resource, including the request, abort signal, and previous result.
27
+ *
28
+ * @template R - The type of the request.
29
+ * @template V - The type of the value when the resource is successfully loaded.
30
+ * @template E - The type of the error when the resource fails to load.
31
+ * @public
32
+ */
11
33
  export interface ResourceLoadOptions<R, V, E> {
34
+ /** The request to load the resource. */
12
35
  readonly request: R;
36
+ /** The signal to abort the loading process if needed. */
13
37
  readonly abortSignal: AbortSignal;
38
+ /** The previous result of the resource loading, if any. */
14
39
  readonly previous: AsyncResult<V, E>;
15
40
  }
41
+ /**
42
+ * Options for creating a resource, including the request signal, load function, and error converter.
43
+ *
44
+ * @template R - The type of the request.
45
+ * @template V - The type of the value when the resource is successfully loaded.
46
+ * @template E - The type of the error when the resource fails to load.
47
+ * @public
48
+ */
16
49
  export interface MakeResourceOptions<R, V, E> {
50
+ /** A signal representing the request to load the resource. */
17
51
  readonly request: Signal<R>;
52
+ /** A function to load the resource, returning a promise of the value. */
18
53
  readonly load: (options: ResourceLoadOptions<R, V, E>) => Promise<V>;
54
+ /** A function to convert an unknown error into a specific error type. */
19
55
  readonly convertError: (error: unknown) => E;
20
56
  }
57
+ /**
58
+ * Creates an asynchronous resource that can be loaded, reloaded, and disposed of.
59
+ *
60
+ * @template R - The type of the request.
61
+ * @template V - The type of the value when the resource is successfully loaded.
62
+ * @template E - The type of the error when the resource fails to load.
63
+ *
64
+ * @param {MakeResourceOptions<R, V, E>} options - The options for creating the resource.
65
+ * @returns {AsyncResource<V, E>} The created asynchronous resource.
66
+ * @public
67
+ */
21
68
  export declare const makeResource: <R, V, E>({ request, load, convertError, }: MakeResourceOptions<R, V, E>) => AsyncResource<V, E>;