@player-ui/async-node-plugin 0.13.0-next.1 → 0.13.0-next.3
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/dist/AsyncNodePlugin.native.js +141 -136
- package/dist/AsyncNodePlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +64 -33
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +65 -34
- package/dist/index.mjs +65 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/index.bench.ts +135 -0
- package/src/__tests__/index.test.ts +207 -9
- package/src/index.ts +111 -48
- package/types/index.d.ts +35 -10
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import type { Player, PlayerPlugin, Node, ViewInstance, Parser, ViewPlugin, Resolver } from "@player-ui/player";
|
|
2
|
-
import { AsyncParallelBailHook } from "tapable-ts";
|
|
2
|
+
import { AsyncParallelBailHook, SyncBailHook } from "tapable-ts";
|
|
3
3
|
export * from "./types";
|
|
4
4
|
export * from "./transform";
|
|
5
|
+
/** Object type for storing data related to a single `apply` of the `AsyncNodePluginPlugin`
|
|
6
|
+
* This object should be setup once per ViewInstance to keep any cached info just for that view to avoid conflicts of shared async node ids across different view states.
|
|
7
|
+
*/
|
|
8
|
+
type AsyncPluginContext = {
|
|
9
|
+
/** Map of async node id to resolved content */
|
|
10
|
+
nodeResolveCache: Map<string, any>;
|
|
11
|
+
/** The view instance this context is attached to. */
|
|
12
|
+
view: ViewInstance;
|
|
13
|
+
};
|
|
5
14
|
export interface AsyncNodePluginOptions {
|
|
6
15
|
/** A set of plugins to load */
|
|
7
16
|
plugins?: AsyncNodeViewPlugin[];
|
|
@@ -12,33 +21,48 @@ export interface AsyncNodeViewPlugin extends ViewPlugin {
|
|
|
12
21
|
asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;
|
|
13
22
|
}
|
|
14
23
|
export type AsyncHandler = (node: Node.Async, callback?: (result: any) => void) => Promise<any>;
|
|
24
|
+
/** Hook declaration for the AsyncNodePlugin */
|
|
25
|
+
type AsyncNodeHooks = {
|
|
26
|
+
/** Async hook to get content for an async node */
|
|
27
|
+
onAsyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;
|
|
28
|
+
/** Sync hook to manage errors coming from the onAsyncNode hook. Return a fallback node or null to render a fallback. The first argument of passed in the call is the error thrown. */
|
|
29
|
+
onAsyncNodeError: SyncBailHook<[Error, Node.Async], any>;
|
|
30
|
+
};
|
|
15
31
|
/**
|
|
16
32
|
* Async node plugin used to resolve async nodes in the content
|
|
17
33
|
* If an async node is present, allow users to provide a replacement node to be rendered when ready
|
|
18
34
|
*/
|
|
19
35
|
export declare class AsyncNodePlugin implements PlayerPlugin {
|
|
20
36
|
private plugins;
|
|
37
|
+
private playerInstance;
|
|
21
38
|
constructor(options: AsyncNodePluginOptions, asyncHandler?: AsyncHandler);
|
|
22
|
-
readonly hooks:
|
|
23
|
-
|
|
24
|
-
};
|
|
39
|
+
readonly hooks: AsyncNodeHooks;
|
|
40
|
+
getPlayerInstance(): Player | undefined;
|
|
25
41
|
name: string;
|
|
26
42
|
apply(player: Player): void;
|
|
27
43
|
}
|
|
28
44
|
export declare class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
29
|
-
asyncNode: AsyncParallelBailHook<[
|
|
45
|
+
asyncNode: AsyncParallelBailHook<[
|
|
46
|
+
Node.Async,
|
|
47
|
+
(result: any) => void
|
|
48
|
+
], any>;
|
|
30
49
|
private basePlugin;
|
|
31
50
|
name: string;
|
|
32
|
-
|
|
33
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Parses the node from the result and triggers an asynchronous view update if necessary.
|
|
53
|
+
* @param node The asynchronous node that might be updated.
|
|
54
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
55
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
56
|
+
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
57
|
+
*/
|
|
58
|
+
private parseNodeAndUpdate;
|
|
34
59
|
/**
|
|
35
60
|
* Updates the node asynchronously based on the result provided.
|
|
36
61
|
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
37
62
|
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
38
63
|
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
39
64
|
* @param node The asynchronous node that might be updated.
|
|
40
|
-
* @param
|
|
41
|
-
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
65
|
+
* @param newNode The new node to replace the async node.
|
|
42
66
|
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
43
67
|
*/
|
|
44
68
|
private handleAsyncUpdate;
|
|
@@ -48,7 +72,8 @@ export declare class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
|
48
72
|
* @param resolver The resolver instance to attach the hook to.
|
|
49
73
|
* @param view
|
|
50
74
|
*/
|
|
51
|
-
applyResolver(resolver: Resolver): void;
|
|
75
|
+
applyResolver(resolver: Resolver, context: AsyncPluginContext): void;
|
|
76
|
+
private runAsyncNode;
|
|
52
77
|
private isAsync;
|
|
53
78
|
private isDeterminedAsync;
|
|
54
79
|
applyParser(parser: Parser): void;
|