@player-ui/async-node-plugin 0.8.0-next.3 → 0.8.0-next.4
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 +61 -78
- package/dist/AsyncNodePlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +53 -51
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +53 -51
- package/dist/index.mjs +53 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.test.ts +105 -9
- package/src/index.ts +74 -67
- package/types/index.d.ts +21 -4
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
Parser,
|
|
9
9
|
ViewPlugin,
|
|
10
10
|
Resolver,
|
|
11
|
+
Resolve,
|
|
11
12
|
} from "@player-ui/player";
|
|
12
13
|
import { AsyncParallelBailHook } from "tapable-ts";
|
|
13
14
|
import queueMicrotask from "queue-microtask";
|
|
@@ -24,7 +25,7 @@ export interface AsyncNodeViewPlugin extends ViewPlugin {
|
|
|
24
25
|
/** Use this to tap into the async node plugin hooks */
|
|
25
26
|
applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;
|
|
26
27
|
|
|
27
|
-
asyncNode: AsyncParallelBailHook<[Node.Async], any>;
|
|
28
|
+
asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
@@ -44,7 +45,10 @@ export class AsyncNodePlugin implements PlayerPlugin {
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
public readonly hooks = {
|
|
47
|
-
onAsyncNode: new AsyncParallelBailHook<
|
|
48
|
+
onAsyncNode: new AsyncParallelBailHook<
|
|
49
|
+
[Node.Async, (result: any) => void],
|
|
50
|
+
any
|
|
51
|
+
>(),
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
name = "AsyncNode";
|
|
@@ -61,7 +65,10 @@ export class AsyncNodePlugin implements PlayerPlugin {
|
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
64
|
-
public asyncNode = new AsyncParallelBailHook<
|
|
68
|
+
public asyncNode = new AsyncParallelBailHook<
|
|
69
|
+
[Node.Async, (result: any) => void],
|
|
70
|
+
any
|
|
71
|
+
>();
|
|
65
72
|
private basePlugin: AsyncNodePlugin | undefined;
|
|
66
73
|
|
|
67
74
|
name = "AsyncNode";
|
|
@@ -70,6 +77,67 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
|
70
77
|
|
|
71
78
|
private currentView: ViewInstance | undefined;
|
|
72
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Updates the node asynchronously based on the result provided.
|
|
82
|
+
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
83
|
+
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
84
|
+
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
85
|
+
* @param node The asynchronous node that might be updated.
|
|
86
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
87
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
88
|
+
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
89
|
+
*/
|
|
90
|
+
private handleAsyncUpdate(
|
|
91
|
+
node: Node.Async,
|
|
92
|
+
result: any,
|
|
93
|
+
options: Resolve.NodeResolveOptions,
|
|
94
|
+
view: ViewInstance | undefined,
|
|
95
|
+
) {
|
|
96
|
+
const parsedNode =
|
|
97
|
+
options.parseNode && result ? options.parseNode(result) : undefined;
|
|
98
|
+
|
|
99
|
+
if (this.resolvedMapping.get(node.id) !== parsedNode) {
|
|
100
|
+
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
|
|
101
|
+
view?.updateAsync();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Handles the asynchronous API integration for resolving nodes.
|
|
107
|
+
* This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.
|
|
108
|
+
* @param resolver The resolver instance to attach the hook to.
|
|
109
|
+
* @param view
|
|
110
|
+
*/
|
|
111
|
+
applyResolver(resolver: Resolver) {
|
|
112
|
+
resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
|
|
113
|
+
let resolvedNode;
|
|
114
|
+
if (this.isAsync(node)) {
|
|
115
|
+
const mappedValue = this.resolvedMapping.get(node.id);
|
|
116
|
+
if (mappedValue) {
|
|
117
|
+
resolvedNode = mappedValue;
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
resolvedNode = null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const newNode = resolvedNode || node;
|
|
124
|
+
if (!resolvedNode && node?.type === NodeType.Async) {
|
|
125
|
+
queueMicrotask(async () => {
|
|
126
|
+
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
127
|
+
node,
|
|
128
|
+
(result) => {
|
|
129
|
+
this.handleAsyncUpdate(node, result, options, this.currentView);
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
this.handleAsyncUpdate(node, result, options, this.currentView);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return node;
|
|
136
|
+
}
|
|
137
|
+
return newNode;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
73
141
|
private isAsync(node: Node.Node | null): node is Node.Async {
|
|
74
142
|
return node?.type === NodeType.Async;
|
|
75
143
|
}
|
|
@@ -112,71 +180,10 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
|
112
180
|
);
|
|
113
181
|
}
|
|
114
182
|
|
|
115
|
-
applyResolverHooks(resolver: Resolver) {
|
|
116
|
-
resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
|
|
117
|
-
let resolvedNode;
|
|
118
|
-
if (this.isAsync(node)) {
|
|
119
|
-
const mappedValue = this.resolvedMapping.get(node.id);
|
|
120
|
-
if (mappedValue) {
|
|
121
|
-
resolvedNode = mappedValue;
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
resolvedNode = null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const newNode = resolvedNode || node;
|
|
128
|
-
if (!resolvedNode && node?.type === NodeType.Async) {
|
|
129
|
-
queueMicrotask(async () => {
|
|
130
|
-
const result = await this.basePlugin?.hooks.onAsyncNode.call(node);
|
|
131
|
-
const parsedNode =
|
|
132
|
-
options.parseNode && result ? options.parseNode(result) : undefined;
|
|
133
|
-
|
|
134
|
-
if (parsedNode) {
|
|
135
|
-
this.resolvedMapping.set(node.id, parsedNode);
|
|
136
|
-
this.currentView?.updateAsync();
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
return node;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return newNode;
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
183
|
apply(view: ViewInstance): void {
|
|
148
|
-
|
|
149
|
-
view.hooks.
|
|
150
|
-
|
|
151
|
-
let resolvedNode;
|
|
152
|
-
if (this.isAsync(node)) {
|
|
153
|
-
const mappedValue = this.resolvedMapping.get(node.id);
|
|
154
|
-
if (mappedValue) {
|
|
155
|
-
resolvedNode = mappedValue;
|
|
156
|
-
}
|
|
157
|
-
} else {
|
|
158
|
-
resolvedNode = null;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const newNode = resolvedNode || node;
|
|
162
|
-
if (!resolvedNode && node?.type === NodeType.Async) {
|
|
163
|
-
queueMicrotask(async () => {
|
|
164
|
-
const result = await this.basePlugin?.hooks.onAsyncNode.call(node);
|
|
165
|
-
const parsedNode =
|
|
166
|
-
options.parseNode && result
|
|
167
|
-
? options.parseNode(result)
|
|
168
|
-
: undefined;
|
|
169
|
-
|
|
170
|
-
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
|
|
171
|
-
view.updateAsync();
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
return node;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return newNode;
|
|
178
|
-
});
|
|
179
|
-
});
|
|
184
|
+
this.currentView = view;
|
|
185
|
+
view.hooks.parser.tap("async", this.applyParser.bind(this));
|
|
186
|
+
view.hooks.resolver.tap("async", this.applyResolver.bind(this));
|
|
180
187
|
}
|
|
181
188
|
|
|
182
189
|
applyPlugin(asyncNodePlugin: AsyncNodePlugin): void {
|
package/types/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface AsyncNodePluginOptions {
|
|
|
8
8
|
export interface AsyncNodeViewPlugin extends ViewPlugin {
|
|
9
9
|
/** Use this to tap into the async node plugin hooks */
|
|
10
10
|
applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;
|
|
11
|
-
asyncNode: AsyncParallelBailHook<[Node.Async], any>;
|
|
11
|
+
asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Async node plugin used to resolve async nodes in the content
|
|
@@ -18,20 +18,37 @@ export declare class AsyncNodePlugin implements PlayerPlugin {
|
|
|
18
18
|
private plugins;
|
|
19
19
|
constructor(options: AsyncNodePluginOptions);
|
|
20
20
|
readonly hooks: {
|
|
21
|
-
onAsyncNode: AsyncParallelBailHook<[Node.Async], any, Record<string, any>>;
|
|
21
|
+
onAsyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any, Record<string, any>>;
|
|
22
22
|
};
|
|
23
23
|
name: string;
|
|
24
24
|
apply(player: Player): void;
|
|
25
25
|
}
|
|
26
26
|
export declare class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
|
|
27
|
-
asyncNode: AsyncParallelBailHook<[Node.Async], any, Record<string, any>>;
|
|
27
|
+
asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any, Record<string, any>>;
|
|
28
28
|
private basePlugin;
|
|
29
29
|
name: string;
|
|
30
30
|
private resolvedMapping;
|
|
31
31
|
private currentView;
|
|
32
|
+
/**
|
|
33
|
+
* Updates the node asynchronously based on the result provided.
|
|
34
|
+
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
35
|
+
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
36
|
+
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
37
|
+
* @param node The asynchronous node that might be updated.
|
|
38
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
39
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
40
|
+
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
41
|
+
*/
|
|
42
|
+
private handleAsyncUpdate;
|
|
43
|
+
/**
|
|
44
|
+
* Handles the asynchronous API integration for resolving nodes.
|
|
45
|
+
* This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.
|
|
46
|
+
* @param resolver The resolver instance to attach the hook to.
|
|
47
|
+
* @param view
|
|
48
|
+
*/
|
|
49
|
+
applyResolver(resolver: Resolver): void;
|
|
32
50
|
private isAsync;
|
|
33
51
|
applyParser(parser: Parser): void;
|
|
34
|
-
applyResolverHooks(resolver: Resolver): void;
|
|
35
52
|
apply(view: ViewInstance): void;
|
|
36
53
|
applyPlugin(asyncNodePlugin: AsyncNodePlugin): void;
|
|
37
54
|
}
|