@player-ui/async-node-plugin 0.7.5--canary.449.15808 → 0.7.5--canary.443.15842

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/src/index.ts CHANGED
@@ -8,7 +8,6 @@ import type {
8
8
  Parser,
9
9
  ViewPlugin,
10
10
  Resolver,
11
- Resolve,
12
11
  } from "@player-ui/player";
13
12
  import { AsyncParallelBailHook } from "tapable-ts";
14
13
  import queueMicrotask from "queue-microtask";
@@ -25,7 +24,7 @@ export interface AsyncNodeViewPlugin extends ViewPlugin {
25
24
  /** Use this to tap into the async node plugin hooks */
26
25
  applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;
27
26
 
28
- asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;
27
+ asyncNode: AsyncParallelBailHook<[Node.Async], any>;
29
28
  }
30
29
 
31
30
  /**
@@ -45,10 +44,7 @@ export class AsyncNodePlugin implements PlayerPlugin {
45
44
  }
46
45
 
47
46
  public readonly hooks = {
48
- onAsyncNode: new AsyncParallelBailHook<
49
- [Node.Async, (result: any) => void],
50
- any
51
- >(),
47
+ onAsyncNode: new AsyncParallelBailHook<[Node.Async], any>(),
52
48
  };
53
49
 
54
50
  name = "AsyncNode";
@@ -65,10 +61,7 @@ export class AsyncNodePlugin implements PlayerPlugin {
65
61
  }
66
62
 
67
63
  export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
68
- public asyncNode = new AsyncParallelBailHook<
69
- [Node.Async, (result: any) => void],
70
- any
71
- >();
64
+ public asyncNode = new AsyncParallelBailHook<[Node.Async], any>();
72
65
  private basePlugin: AsyncNodePlugin | undefined;
73
66
 
74
67
  name = "AsyncNode";
@@ -77,67 +70,6 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
77
70
 
78
71
  private currentView: ViewInstance | undefined;
79
72
 
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
-
141
73
  private isAsync(node: Node.Node | null): node is Node.Async {
142
74
  return node?.type === NodeType.Async;
143
75
  }
@@ -180,10 +112,71 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
180
112
  );
181
113
  }
182
114
 
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
+
183
147
  apply(view: ViewInstance): void {
184
- this.currentView = view;
185
- view.hooks.parser.tap("async", this.applyParser.bind(this));
186
- view.hooks.resolver.tap("async", this.applyResolver.bind(this));
148
+ view.hooks.parser.tap("template", this.applyParser.bind(this));
149
+ view.hooks.resolver.tap("template", (resolver) => {
150
+ resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
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
+ });
187
180
  }
188
181
 
189
182
  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, (result: any) => void], any>;
11
+ asyncNode: AsyncParallelBailHook<[Node.Async], any>;
12
12
  }
13
13
  /**
14
14
  * Async node plugin used to resolve async nodes in the content
@@ -18,37 +18,20 @@ export declare class AsyncNodePlugin implements PlayerPlugin {
18
18
  private plugins;
19
19
  constructor(options: AsyncNodePluginOptions);
20
20
  readonly hooks: {
21
- onAsyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any, Record<string, any>>;
21
+ onAsyncNode: AsyncParallelBailHook<[Node.Async], 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, (result: any) => void], any, Record<string, any>>;
27
+ asyncNode: AsyncParallelBailHook<[Node.Async], 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;
50
32
  private isAsync;
51
33
  applyParser(parser: Parser): void;
34
+ applyResolverHooks(resolver: Resolver): void;
52
35
  apply(view: ViewInstance): void;
53
36
  applyPlugin(asyncNodePlugin: AsyncNodePlugin): void;
54
37
  }