@player-ui/async-node-plugin 0.11.3--canary.666.23588 → 0.11.3--canary.660.23595
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 +40 -92
- package/dist/AsyncNodePlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +28 -54
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +29 -55
- package/dist/index.mjs +29 -55
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/index.test.ts +2 -79
- package/src/index.ts +40 -88
- package/types/index.d.ts +7 -25
package/dist/cjs/index.cjs
CHANGED
|
@@ -65,8 +65,7 @@ var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
|
65
65
|
var AsyncNodePlugin = class {
|
|
66
66
|
constructor(options, asyncHandler) {
|
|
67
67
|
this.hooks = {
|
|
68
|
-
onAsyncNode: new import_tapable_ts.AsyncParallelBailHook()
|
|
69
|
-
onAsyncNodeError: new import_tapable_ts.SyncBailHook()
|
|
68
|
+
onAsyncNode: new import_tapable_ts.AsyncParallelBailHook()
|
|
70
69
|
};
|
|
71
70
|
this.name = "AsyncNode";
|
|
72
71
|
if (options?.plugins) {
|
|
@@ -84,11 +83,7 @@ var AsyncNodePlugin = class {
|
|
|
84
83
|
);
|
|
85
84
|
}
|
|
86
85
|
}
|
|
87
|
-
getPlayerInstance() {
|
|
88
|
-
return this.playerInstance;
|
|
89
|
-
}
|
|
90
86
|
apply(player) {
|
|
91
|
-
this.playerInstance = player;
|
|
92
87
|
player.hooks.viewController.tap(this.name, (viewController) => {
|
|
93
88
|
viewController.hooks.view.tap(this.name, (view) => {
|
|
94
89
|
this.plugins?.forEach((plugin) => {
|
|
@@ -104,29 +99,20 @@ var AsyncNodePluginPlugin = class {
|
|
|
104
99
|
this.name = "AsyncNode";
|
|
105
100
|
this.resolvedMapping = /* @__PURE__ */ new Map();
|
|
106
101
|
}
|
|
107
|
-
/**
|
|
108
|
-
* Parses the node from the result and triggers an asynchronous view update if necessary.
|
|
109
|
-
* @param node The asynchronous node that might be updated.
|
|
110
|
-
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
111
|
-
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
112
|
-
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
113
|
-
*/
|
|
114
|
-
parseNodeAndUpdate(node, result, options, view) {
|
|
115
|
-
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
116
|
-
this.handleAsyncUpdate(node, parsedNode, view);
|
|
117
|
-
}
|
|
118
102
|
/**
|
|
119
103
|
* Updates the node asynchronously based on the result provided.
|
|
120
104
|
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
121
105
|
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
122
106
|
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
123
107
|
* @param node The asynchronous node that might be updated.
|
|
124
|
-
* @param
|
|
108
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
109
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
125
110
|
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
126
111
|
*/
|
|
127
|
-
handleAsyncUpdate(node,
|
|
128
|
-
|
|
129
|
-
|
|
112
|
+
handleAsyncUpdate(node, result, options, view) {
|
|
113
|
+
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
114
|
+
if (this.resolvedMapping.get(node.id) !== parsedNode) {
|
|
115
|
+
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
|
|
130
116
|
view?.updateAsync();
|
|
131
117
|
}
|
|
132
118
|
}
|
|
@@ -138,43 +124,31 @@ var AsyncNodePluginPlugin = class {
|
|
|
138
124
|
*/
|
|
139
125
|
applyResolver(resolver) {
|
|
140
126
|
resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
|
|
141
|
-
|
|
142
|
-
|
|
127
|
+
let resolvedNode;
|
|
128
|
+
if (this.isAsync(node)) {
|
|
129
|
+
const mappedValue = this.resolvedMapping.get(node.id);
|
|
130
|
+
if (mappedValue) {
|
|
131
|
+
resolvedNode = mappedValue;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
resolvedNode = null;
|
|
143
135
|
}
|
|
144
|
-
const
|
|
145
|
-
if (resolvedNode
|
|
146
|
-
|
|
136
|
+
const newNode = resolvedNode || node;
|
|
137
|
+
if (!resolvedNode && node?.type === import_player2.NodeType.Async) {
|
|
138
|
+
(0, import_queue_microtask.default)(async () => {
|
|
139
|
+
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
140
|
+
node,
|
|
141
|
+
(result2) => {
|
|
142
|
+
this.handleAsyncUpdate(node, result2, options, this.currentView);
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
this.handleAsyncUpdate(node, result, options, this.currentView);
|
|
146
|
+
});
|
|
147
|
+
return node;
|
|
147
148
|
}
|
|
148
|
-
|
|
149
|
-
return node;
|
|
149
|
+
return newNode;
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
|
-
async runAsyncNode(node, options) {
|
|
153
|
-
try {
|
|
154
|
-
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
155
|
-
node,
|
|
156
|
-
(result2) => {
|
|
157
|
-
this.parseNodeAndUpdate(node, result2, options, this.currentView);
|
|
158
|
-
}
|
|
159
|
-
);
|
|
160
|
-
this.parseNodeAndUpdate(node, result, options, this.currentView);
|
|
161
|
-
} catch (e) {
|
|
162
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
163
|
-
const result = this.basePlugin?.hooks.onAsyncNodeError.call(error, node);
|
|
164
|
-
if (result === void 0) {
|
|
165
|
-
const playerState = this.basePlugin?.getPlayerInstance()?.getState();
|
|
166
|
-
if (playerState?.status === "in-progress") {
|
|
167
|
-
playerState.fail(error);
|
|
168
|
-
}
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
options.logger?.error(
|
|
172
|
-
"Async node handling failed and resolved with a fallback. Error:",
|
|
173
|
-
error
|
|
174
|
-
);
|
|
175
|
-
this.handleAsyncUpdate(node, result, this.currentView);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
152
|
isAsync(node) {
|
|
179
153
|
return node?.type === import_player2.NodeType.Async;
|
|
180
154
|
}
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts"],"sourcesContent":["import { NodeType, getNodeID } from \"@player-ui/player\";\nimport type {\n Player,\n PlayerPlugin,\n Node,\n ParseObjectOptions,\n ParseObjectChildOptions,\n ViewInstance,\n Parser,\n ViewPlugin,\n Resolver,\n Resolve,\n} from \"@player-ui/player\";\nimport { AsyncParallelBailHook, SyncBailHook } from \"tapable-ts\";\nimport queueMicrotask from \"queue-microtask\";\nimport { omit } from \"timm\";\n\nexport * from \"./types\";\nexport * from \"./transform\";\n\nexport interface AsyncNodePluginOptions {\n /** A set of plugins to load */\n plugins?: AsyncNodeViewPlugin[];\n}\n\nexport interface AsyncNodeViewPlugin extends ViewPlugin {\n /** Use this to tap into the async node plugin hooks */\n applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;\n\n asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n}\nexport type AsyncHandler = (\n node: Node.Async,\n callback?: (result: any) => void,\n) => Promise<any>;\n\n/** Hook declaration for the AsyncNodePlugin */\ntype AsyncNodeHooks = {\n /** Async hook to get content for an async node */\n onAsyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n /** 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. */\n onAsyncNodeError: SyncBailHook<[Error, Node.Async], Node.Node | null>;\n};\n\n/**\n * Async node plugin used to resolve async nodes in the content\n * If an async node is present, allow users to provide a replacement node to be rendered when ready\n */\nexport class AsyncNodePlugin implements PlayerPlugin {\n private plugins: AsyncNodeViewPlugin[] | undefined;\n private playerInstance: Player | undefined;\n\n constructor(options: AsyncNodePluginOptions, asyncHandler?: AsyncHandler) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\n });\n }\n\n if (asyncHandler) {\n this.hooks.onAsyncNode.tap(\n \"async\",\n async (node: Node.Async, callback) => {\n return await asyncHandler(node, callback);\n },\n );\n }\n }\n\n public readonly hooks: AsyncNodeHooks = {\n onAsyncNode: new AsyncParallelBailHook(),\n onAsyncNodeError: new SyncBailHook(),\n };\n\n getPlayerInstance(): Player | undefined {\n return this.playerInstance;\n }\n\n name = \"AsyncNode\";\n\n apply(player: Player): void {\n this.playerInstance = player;\n\n player.hooks.viewController.tap(this.name, (viewController) => {\n viewController.hooks.view.tap(this.name, (view) => {\n this.plugins?.forEach((plugin) => {\n plugin.apply(view);\n });\n });\n });\n }\n}\n\nexport class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {\n public asyncNode: AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n > = new AsyncParallelBailHook();\n private basePlugin: AsyncNodePlugin | undefined;\n\n name = \"AsyncNode\";\n\n private resolvedMapping = new Map<string, any>();\n\n private currentView: ViewInstance | undefined;\n\n /**\n * Parses the node from the result and triggers an asynchronous view update if necessary.\n * @param node The asynchronous node that might be updated.\n * @param result The result obtained from resolving the async node. This could be any data structure or value.\n * @param options Options provided for node resolution, including a potential parseNode function to process the result.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private parseNodeAndUpdate(\n node: Node.Async,\n result: any,\n options: Resolve.NodeResolveOptions,\n view: ViewInstance | undefined,\n ) {\n const parsedNode =\n options.parseNode && result ? options.parseNode(result) : undefined;\n\n this.handleAsyncUpdate(node, parsedNode, view);\n }\n\n /**\n * Updates the node asynchronously based on the result provided.\n * This method is responsible for handling the update logic of asynchronous nodes.\n * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.\n * If an update is necessary, it triggers an asynchronous update on the view.\n * @param node The asynchronous node that might be updated.\n * @param newNode The new node to replace the async node.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private handleAsyncUpdate(\n node: Node.Async,\n newNode?: Node.Node | null,\n view?: ViewInstance,\n ) {\n if (this.resolvedMapping.get(node.id) !== newNode) {\n this.resolvedMapping.set(node.id, newNode ? newNode : node);\n view?.updateAsync();\n }\n }\n\n /**\n * Handles the asynchronous API integration for resolving nodes.\n * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.\n * @param resolver The resolver instance to attach the hook to.\n * @param view\n */\n applyResolver(resolver: Resolver): void {\n resolver.hooks.beforeResolve.tap(this.name, (node, options) => {\n if (!this.isAsync(node)) {\n return node;\n }\n\n const resolvedNode = this.resolvedMapping.get(node.id);\n if (resolvedNode !== undefined) {\n return resolvedNode;\n }\n\n queueMicrotask(() => this.runAsyncNode(node, options));\n\n return node;\n });\n }\n\n private async runAsyncNode(\n node: Node.Async,\n options: Resolve.NodeResolveOptions,\n ) {\n try {\n const result = await this.basePlugin?.hooks.onAsyncNode.call(\n node,\n (result) => {\n this.parseNodeAndUpdate(node, result, options, this.currentView);\n },\n );\n this.parseNodeAndUpdate(node, result, options, this.currentView);\n } catch (e: unknown) {\n const error = e instanceof Error ? e : new Error(String(e));\n const result = this.basePlugin?.hooks.onAsyncNodeError.call(error, node);\n\n if (result === undefined) {\n const playerState = this.basePlugin?.getPlayerInstance()?.getState();\n\n if (playerState?.status === \"in-progress\") {\n playerState.fail(error);\n }\n\n return;\n }\n\n options.logger?.error(\n \"Async node handling failed and resolved with a fallback. Error:\",\n error,\n );\n this.handleAsyncUpdate(node, result, this.currentView);\n }\n }\n\n private isAsync(node: Node.Node | null): node is Node.Async {\n return node?.type === NodeType.Async;\n }\n\n private isDeterminedAsync(obj: any) {\n return obj && Object.prototype.hasOwnProperty.call(obj, \"async\");\n }\n\n applyParser(parser: Parser): void {\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n childOptions?: ParseObjectChildOptions,\n ) => {\n if (this.isDeterminedAsync(obj)) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n\n if (parsedAsync === null || !parsedNodeId) {\n return childOptions ? [] : null;\n }\n\n const asyncAST = parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n\n if (childOptions) {\n return asyncAST\n ? [\n {\n path: [...childOptions.path, childOptions.key],\n value: asyncAST,\n },\n ]\n : [];\n }\n\n return asyncAST;\n }\n },\n );\n }\n\n apply(view: ViewInstance): void {\n this.currentView = view;\n view.hooks.parser.tap(\"async\", this.applyParser.bind(this));\n view.hooks.resolver.tap(\"async\", this.applyResolver.bind(this));\n }\n\n applyPlugin(asyncNodePlugin: AsyncNodePlugin): void {\n this.basePlugin = asyncNodePlugin;\n }\n}\n","import { Builder } from \"@player-ui/player\";\nimport type { AsyncTransformFunc } from \"./types\";\n\n/**\n * Util function to generate transform function for async asset\n * @param asset - async asset to apply beforeResolve transform\n * @param transformedAssetType: transformed asset type for rendering\n * @param wrapperAssetType: container asset type\n * @param flatten: flatten the streamed in content\n * @returns - wrapper asset with children of transformed asset and async node\n */\n\nexport const asyncTransform: AsyncTransformFunc = (\n assetId,\n wrapperAssetType,\n asset,\n flatten,\n) => {\n const id = \"async-\" + assetId;\n\n const asyncNode = Builder.asyncNode(id, flatten);\n let multiNode;\n let assetNode;\n\n if (asset) {\n assetNode = Builder.assetWrapper(asset);\n multiNode = Builder.multiNode(assetNode, asyncNode);\n } else {\n multiNode = Builder.multiNode(asyncNode);\n }\n\n const wrapperAsset = Builder.asset({\n id: wrapperAssetType + \"-\" + id,\n type: wrapperAssetType,\n });\n\n Builder.addChild(wrapperAsset, [\"values\"], multiNode);\n\n return wrapperAsset;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAAoC;AAapC,wBAAoD;AACpD,6BAA2B;AAC3B,kBAAqB;;;ACfrB,oBAAwB;AAYjB,IAAM,iBAAqC,CAChD,SACA,kBACA,OACA,YACG;AACH,QAAM,KAAK,WAAW;AAEtB,QAAM,YAAY,sBAAQ,UAAU,IAAI,OAAO;AAC/C,MAAI;AACJ,MAAI;AAEJ,MAAI,OAAO;AACT,gBAAY,sBAAQ,aAAa,KAAK;AACtC,gBAAY,sBAAQ,UAAU,WAAW,SAAS;AAAA,EACpD,OAAO;AACL,gBAAY,sBAAQ,UAAU,SAAS;AAAA,EACzC;AAEA,QAAM,eAAe,sBAAQ,MAAM;AAAA,IACjC,IAAI,mBAAmB,MAAM;AAAA,IAC7B,MAAM;AAAA,EACR,CAAC;AAED,wBAAQ,SAAS,cAAc,CAAC,QAAQ,GAAG,SAAS;AAEpD,SAAO;AACT;;;ADSO,IAAM,kBAAN,MAA8C;AAAA,EAInD,YAAY,SAAiC,cAA6B;AAkB1E,SAAgB,QAAwB;AAAA,MACtC,aAAa,IAAI,wCAAsB;AAAA,MACvC,kBAAkB,IAAI,+BAAa;AAAA,IACrC;AAMA,gBAAO;AA1BL,QAAI,SAAS,SAAS;AACpB,WAAK,UAAU,QAAQ;AACvB,cAAQ,QAAQ,QAAQ,CAAC,WAAW;AAClC,eAAO,YAAY,IAAI;AAAA,MACzB,CAAC;AAAA,IACH;AAEA,QAAI,cAAc;AAChB,WAAK,MAAM,YAAY;AAAA,QACrB;AAAA,QACA,OAAO,MAAkB,aAAa;AACpC,iBAAO,MAAM,aAAa,MAAM,QAAQ;AAAA,QAC1C;AAAA,MACF;AAAA,IACF;A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ACzC,mBAAO,eAAe,CAAC,IAAI;AAAA,UAC7B;AAEA,gBAAM,WAAW,OAAO;AAAA,YACtB;AAAA,cACE,IAAI;AAAA,cACJ,MAAM,wBAAS;AAAA,cACf,OAAO;AAAA,YACT;AAAA,YACA;AAAA,UACF;AAEA,cAAI,cAAc;AAChB,mBAAO,WACH;AAAA,cACE;AAAA,gBACE,MAAM,CAAC,GAAG,aAAa,MAAM,aAAa,GAAG;AAAA,gBAC7C,OAAO;AAAA,cACT;AAAA,YACF,IACA,CAAC;AAAA,UACP;AAEA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,MAA0B;AAC9B,SAAK,cAAc;AACnB,SAAK,MAAM,OAAO,IAAI,SAAS,KAAK,YAAY,KAAK,IAAI,CAAC;AAC1D,SAAK,MAAM,SAAS,IAAI,SAAS,KAAK,cAAc,KAAK,IAAI,CAAC;AAAA,EAChE;AAAA,EAEA,YAAY,iBAAwC;AAClD,SAAK,aAAa;AAAA,EACpB;AACF;","names":["import_player","queueMicrotask","result"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts"],"sourcesContent":["import { NodeType, getNodeID } from \"@player-ui/player\";\nimport type {\n Player,\n PlayerPlugin,\n Node,\n ParseObjectOptions,\n ParseObjectChildOptions,\n ViewInstance,\n Parser,\n ViewPlugin,\n Resolver,\n Resolve,\n} from \"@player-ui/player\";\nimport { AsyncParallelBailHook } from \"tapable-ts\";\nimport queueMicrotask from \"queue-microtask\";\nimport { omit } from \"timm\";\n\nexport * from \"./types\";\nexport * from \"./transform\";\n\nexport interface AsyncNodePluginOptions {\n /** A set of plugins to load */\n plugins?: AsyncNodeViewPlugin[];\n}\n\nexport interface AsyncNodeViewPlugin extends ViewPlugin {\n /** Use this to tap into the async node plugin hooks */\n applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;\n\n asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n}\nexport type AsyncHandler = (\n node: Node.Async,\n callback?: (result: any) => void,\n) => Promise<any>;\n\n/**\n * Async node plugin used to resolve async nodes in the content\n * If an async node is present, allow users to provide a replacement node to be rendered when ready\n */\nexport class AsyncNodePlugin implements PlayerPlugin {\n private plugins: AsyncNodeViewPlugin[] | undefined;\n\n constructor(options: AsyncNodePluginOptions, asyncHandler?: AsyncHandler) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\n });\n }\n\n if (asyncHandler) {\n this.hooks.onAsyncNode.tap(\n \"async\",\n async (node: Node.Async, callback) => {\n return await asyncHandler(node, callback);\n },\n );\n }\n }\n\n public readonly hooks = {\n onAsyncNode: new AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n >(),\n };\n\n name = \"AsyncNode\";\n\n apply(player: Player) {\n player.hooks.viewController.tap(this.name, (viewController) => {\n viewController.hooks.view.tap(this.name, (view) => {\n this.plugins?.forEach((plugin) => {\n plugin.apply(view);\n });\n });\n });\n }\n}\n\nexport class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {\n public asyncNode = new AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n >();\n private basePlugin: AsyncNodePlugin | undefined;\n\n name = \"AsyncNode\";\n\n private resolvedMapping = new Map<string, any>();\n\n private currentView: ViewInstance | undefined;\n\n /**\n * Updates the node asynchronously based on the result provided.\n * This method is responsible for handling the update logic of asynchronous nodes.\n * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.\n * If an update is necessary, it triggers an asynchronous update on the view.\n * @param node The asynchronous node that might be updated.\n * @param result The result obtained from resolving the async node. This could be any data structure or value.\n * @param options Options provided for node resolution, including a potential parseNode function to process the result.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private handleAsyncUpdate(\n node: Node.Async,\n result: any,\n options: Resolve.NodeResolveOptions,\n view: ViewInstance | undefined,\n ) {\n const parsedNode =\n options.parseNode && result ? options.parseNode(result) : undefined;\n\n if (this.resolvedMapping.get(node.id) !== parsedNode) {\n this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);\n view?.updateAsync();\n }\n }\n\n /**\n * Handles the asynchronous API integration for resolving nodes.\n * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.\n * @param resolver The resolver instance to attach the hook to.\n * @param view\n */\n applyResolver(resolver: Resolver) {\n resolver.hooks.beforeResolve.tap(this.name, (node, options) => {\n let resolvedNode;\n if (this.isAsync(node)) {\n const mappedValue = this.resolvedMapping.get(node.id);\n if (mappedValue) {\n resolvedNode = mappedValue;\n }\n } else {\n resolvedNode = null;\n }\n\n const newNode = resolvedNode || node;\n if (!resolvedNode && node?.type === NodeType.Async) {\n queueMicrotask(async () => {\n const result = await this.basePlugin?.hooks.onAsyncNode.call(\n node,\n (result) => {\n this.handleAsyncUpdate(node, result, options, this.currentView);\n },\n );\n this.handleAsyncUpdate(node, result, options, this.currentView);\n });\n\n return node;\n }\n return newNode;\n });\n }\n\n private isAsync(node: Node.Node | null): node is Node.Async {\n return node?.type === NodeType.Async;\n }\n\n private isDeterminedAsync(obj: any) {\n return obj && Object.prototype.hasOwnProperty.call(obj, \"async\");\n }\n\n applyParser(parser: Parser) {\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n childOptions?: ParseObjectChildOptions,\n ) => {\n if (this.isDeterminedAsync(obj)) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n\n if (parsedAsync === null || !parsedNodeId) {\n return childOptions ? [] : null;\n }\n\n const asyncAST = parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n\n if (childOptions) {\n return asyncAST\n ? [\n {\n path: [...childOptions.path, childOptions.key],\n value: asyncAST,\n },\n ]\n : [];\n }\n\n return asyncAST;\n }\n },\n );\n }\n\n apply(view: ViewInstance): void {\n this.currentView = view;\n view.hooks.parser.tap(\"async\", this.applyParser.bind(this));\n view.hooks.resolver.tap(\"async\", this.applyResolver.bind(this));\n }\n\n applyPlugin(asyncNodePlugin: AsyncNodePlugin): void {\n this.basePlugin = asyncNodePlugin;\n }\n}\n","import { Builder } from \"@player-ui/player\";\nimport type { AsyncTransformFunc } from \"./types\";\n\n/**\n * Util function to generate transform function for async asset\n * @param asset - async asset to apply beforeResolve transform\n * @param transformedAssetType: transformed asset type for rendering\n * @param wrapperAssetType: container asset type\n * @param flatten: flatten the streamed in content\n * @returns - wrapper asset with children of transformed asset and async node\n */\n\nexport const asyncTransform: AsyncTransformFunc = (\n assetId,\n wrapperAssetType,\n asset,\n flatten,\n) => {\n const id = \"async-\" + assetId;\n\n const asyncNode = Builder.asyncNode(id, flatten);\n let multiNode;\n let assetNode;\n\n if (asset) {\n assetNode = Builder.assetWrapper(asset);\n multiNode = Builder.multiNode(assetNode, asyncNode);\n } else {\n multiNode = Builder.multiNode(asyncNode);\n }\n\n const wrapperAsset = Builder.asset({\n id: wrapperAssetType + \"-\" + id,\n type: wrapperAssetType,\n });\n\n Builder.addChild(wrapperAsset, [\"values\"], multiNode);\n\n return wrapperAsset;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAAoC;AAapC,wBAAsC;AACtC,6BAA2B;AAC3B,kBAAqB;;;ACfrB,oBAAwB;AAYjB,IAAM,iBAAqC,CAChD,SACA,kBACA,OACA,YACG;AACH,QAAM,KAAK,WAAW;AAEtB,QAAM,YAAY,sBAAQ,UAAU,IAAI,OAAO;AAC/C,MAAI;AACJ,MAAI;AAEJ,MAAI,OAAO;AACT,gBAAY,sBAAQ,aAAa,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AAc,OAAO;AAAA,gBACzB,kBAAK,KAAK,OAAO;AAAA,YACjB;AAAA,YACA;AAAA,UACF;AACA,gBAAM,mBAAe,0BAAU,WAAW;AAE1C,cAAI,gBAAgB,QAAQ,CAAC,cAAc;AACzC,mBAAO,eAAe,CAAC,IAAI;AAAA,UAC7B;AAEA,gBAAM,WAAW,OAAO;AAAA,YACtB;AAAA,cACE,IAAI;AAAA,cACJ,MAAM,wBAAS;AAAA,cACf,OAAO;AAAA,YACT;AAAA,YACA;AAAA,UACF;AAEA,cAAI,cAAc;AAChB,mBAAO,WACH;AAAA,cACE;AAAA,gBACE,MAAM,CAAC,GAAG,aAAa,MAAM,aAAa,GAAG;AAAA,gBAC7C,OAAO;AAAA,cACT;AAAA,YACF,IACA,CAAC;AAAA,UACP;AAEA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,MAA0B;AAC9B,SAAK,cAAc;AACnB,SAAK,MAAM,OAAO,IAAI,SAAS,KAAK,YAAY,KAAK,IAAI,CAAC;AAC1D,SAAK,MAAM,SAAS,IAAI,SAAS,KAAK,cAAc,KAAK,IAAI,CAAC;AAAA,EAChE;AAAA,EAEA,YAAY,iBAAwC;AAClD,SAAK,aAAa;AAAA,EACpB;AACF;","names":["import_player","queueMicrotask","result"]}
|
package/dist/index.legacy-esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
2
2
|
import { NodeType, getNodeID } from "@player-ui/player";
|
|
3
|
-
import { AsyncParallelBailHook
|
|
3
|
+
import { AsyncParallelBailHook } from "tapable-ts";
|
|
4
4
|
import queueMicrotask from "queue-microtask";
|
|
5
5
|
import { omit } from "timm";
|
|
6
6
|
|
|
@@ -29,8 +29,7 @@ var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
|
29
29
|
var AsyncNodePlugin = class {
|
|
30
30
|
constructor(options, asyncHandler) {
|
|
31
31
|
this.hooks = {
|
|
32
|
-
onAsyncNode: new AsyncParallelBailHook()
|
|
33
|
-
onAsyncNodeError: new SyncBailHook()
|
|
32
|
+
onAsyncNode: new AsyncParallelBailHook()
|
|
34
33
|
};
|
|
35
34
|
this.name = "AsyncNode";
|
|
36
35
|
if (options?.plugins) {
|
|
@@ -48,11 +47,7 @@ var AsyncNodePlugin = class {
|
|
|
48
47
|
);
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
|
-
getPlayerInstance() {
|
|
52
|
-
return this.playerInstance;
|
|
53
|
-
}
|
|
54
50
|
apply(player) {
|
|
55
|
-
this.playerInstance = player;
|
|
56
51
|
player.hooks.viewController.tap(this.name, (viewController) => {
|
|
57
52
|
viewController.hooks.view.tap(this.name, (view) => {
|
|
58
53
|
this.plugins?.forEach((plugin) => {
|
|
@@ -68,29 +63,20 @@ var AsyncNodePluginPlugin = class {
|
|
|
68
63
|
this.name = "AsyncNode";
|
|
69
64
|
this.resolvedMapping = /* @__PURE__ */ new Map();
|
|
70
65
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Parses the node from the result and triggers an asynchronous view update if necessary.
|
|
73
|
-
* @param node The asynchronous node that might be updated.
|
|
74
|
-
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
75
|
-
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
76
|
-
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
77
|
-
*/
|
|
78
|
-
parseNodeAndUpdate(node, result, options, view) {
|
|
79
|
-
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
80
|
-
this.handleAsyncUpdate(node, parsedNode, view);
|
|
81
|
-
}
|
|
82
66
|
/**
|
|
83
67
|
* Updates the node asynchronously based on the result provided.
|
|
84
68
|
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
85
69
|
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
86
70
|
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
87
71
|
* @param node The asynchronous node that might be updated.
|
|
88
|
-
* @param
|
|
72
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
73
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
89
74
|
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
90
75
|
*/
|
|
91
|
-
handleAsyncUpdate(node,
|
|
92
|
-
|
|
93
|
-
|
|
76
|
+
handleAsyncUpdate(node, result, options, view) {
|
|
77
|
+
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
78
|
+
if (this.resolvedMapping.get(node.id) !== parsedNode) {
|
|
79
|
+
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
|
|
94
80
|
view?.updateAsync();
|
|
95
81
|
}
|
|
96
82
|
}
|
|
@@ -102,43 +88,31 @@ var AsyncNodePluginPlugin = class {
|
|
|
102
88
|
*/
|
|
103
89
|
applyResolver(resolver) {
|
|
104
90
|
resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
|
|
105
|
-
|
|
106
|
-
|
|
91
|
+
let resolvedNode;
|
|
92
|
+
if (this.isAsync(node)) {
|
|
93
|
+
const mappedValue = this.resolvedMapping.get(node.id);
|
|
94
|
+
if (mappedValue) {
|
|
95
|
+
resolvedNode = mappedValue;
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
resolvedNode = null;
|
|
107
99
|
}
|
|
108
|
-
const
|
|
109
|
-
if (resolvedNode
|
|
110
|
-
|
|
100
|
+
const newNode = resolvedNode || node;
|
|
101
|
+
if (!resolvedNode && node?.type === NodeType.Async) {
|
|
102
|
+
queueMicrotask(async () => {
|
|
103
|
+
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
104
|
+
node,
|
|
105
|
+
(result2) => {
|
|
106
|
+
this.handleAsyncUpdate(node, result2, options, this.currentView);
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
this.handleAsyncUpdate(node, result, options, this.currentView);
|
|
110
|
+
});
|
|
111
|
+
return node;
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
-
return node;
|
|
113
|
+
return newNode;
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
|
-
async runAsyncNode(node, options) {
|
|
117
|
-
try {
|
|
118
|
-
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
119
|
-
node,
|
|
120
|
-
(result2) => {
|
|
121
|
-
this.parseNodeAndUpdate(node, result2, options, this.currentView);
|
|
122
|
-
}
|
|
123
|
-
);
|
|
124
|
-
this.parseNodeAndUpdate(node, result, options, this.currentView);
|
|
125
|
-
} catch (e) {
|
|
126
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
127
|
-
const result = this.basePlugin?.hooks.onAsyncNodeError.call(error, node);
|
|
128
|
-
if (result === void 0) {
|
|
129
|
-
const playerState = this.basePlugin?.getPlayerInstance()?.getState();
|
|
130
|
-
if (playerState?.status === "in-progress") {
|
|
131
|
-
playerState.fail(error);
|
|
132
|
-
}
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
options.logger?.error(
|
|
136
|
-
"Async node handling failed and resolved with a fallback. Error:",
|
|
137
|
-
error
|
|
138
|
-
);
|
|
139
|
-
this.handleAsyncUpdate(node, result, this.currentView);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
116
|
isAsync(node) {
|
|
143
117
|
return node?.type === NodeType.Async;
|
|
144
118
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
2
2
|
import { NodeType, getNodeID } from "@player-ui/player";
|
|
3
|
-
import { AsyncParallelBailHook
|
|
3
|
+
import { AsyncParallelBailHook } from "tapable-ts";
|
|
4
4
|
import queueMicrotask from "queue-microtask";
|
|
5
5
|
import { omit } from "timm";
|
|
6
6
|
|
|
@@ -29,8 +29,7 @@ var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
|
29
29
|
var AsyncNodePlugin = class {
|
|
30
30
|
constructor(options, asyncHandler) {
|
|
31
31
|
this.hooks = {
|
|
32
|
-
onAsyncNode: new AsyncParallelBailHook()
|
|
33
|
-
onAsyncNodeError: new SyncBailHook()
|
|
32
|
+
onAsyncNode: new AsyncParallelBailHook()
|
|
34
33
|
};
|
|
35
34
|
this.name = "AsyncNode";
|
|
36
35
|
if (options?.plugins) {
|
|
@@ -48,11 +47,7 @@ var AsyncNodePlugin = class {
|
|
|
48
47
|
);
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
|
-
getPlayerInstance() {
|
|
52
|
-
return this.playerInstance;
|
|
53
|
-
}
|
|
54
50
|
apply(player) {
|
|
55
|
-
this.playerInstance = player;
|
|
56
51
|
player.hooks.viewController.tap(this.name, (viewController) => {
|
|
57
52
|
viewController.hooks.view.tap(this.name, (view) => {
|
|
58
53
|
this.plugins?.forEach((plugin) => {
|
|
@@ -68,29 +63,20 @@ var AsyncNodePluginPlugin = class {
|
|
|
68
63
|
this.name = "AsyncNode";
|
|
69
64
|
this.resolvedMapping = /* @__PURE__ */ new Map();
|
|
70
65
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Parses the node from the result and triggers an asynchronous view update if necessary.
|
|
73
|
-
* @param node The asynchronous node that might be updated.
|
|
74
|
-
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
75
|
-
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
76
|
-
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
77
|
-
*/
|
|
78
|
-
parseNodeAndUpdate(node, result, options, view) {
|
|
79
|
-
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
80
|
-
this.handleAsyncUpdate(node, parsedNode, view);
|
|
81
|
-
}
|
|
82
66
|
/**
|
|
83
67
|
* Updates the node asynchronously based on the result provided.
|
|
84
68
|
* This method is responsible for handling the update logic of asynchronous nodes.
|
|
85
69
|
* It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
|
|
86
70
|
* If an update is necessary, it triggers an asynchronous update on the view.
|
|
87
71
|
* @param node The asynchronous node that might be updated.
|
|
88
|
-
* @param
|
|
72
|
+
* @param result The result obtained from resolving the async node. This could be any data structure or value.
|
|
73
|
+
* @param options Options provided for node resolution, including a potential parseNode function to process the result.
|
|
89
74
|
* @param view The view instance where the node resides. This can be undefined if the view is not currently active.
|
|
90
75
|
*/
|
|
91
|
-
handleAsyncUpdate(node,
|
|
92
|
-
|
|
93
|
-
|
|
76
|
+
handleAsyncUpdate(node, result, options, view) {
|
|
77
|
+
const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
|
|
78
|
+
if (this.resolvedMapping.get(node.id) !== parsedNode) {
|
|
79
|
+
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
|
|
94
80
|
view?.updateAsync();
|
|
95
81
|
}
|
|
96
82
|
}
|
|
@@ -102,43 +88,31 @@ var AsyncNodePluginPlugin = class {
|
|
|
102
88
|
*/
|
|
103
89
|
applyResolver(resolver) {
|
|
104
90
|
resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
|
|
105
|
-
|
|
106
|
-
|
|
91
|
+
let resolvedNode;
|
|
92
|
+
if (this.isAsync(node)) {
|
|
93
|
+
const mappedValue = this.resolvedMapping.get(node.id);
|
|
94
|
+
if (mappedValue) {
|
|
95
|
+
resolvedNode = mappedValue;
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
resolvedNode = null;
|
|
107
99
|
}
|
|
108
|
-
const
|
|
109
|
-
if (resolvedNode
|
|
110
|
-
|
|
100
|
+
const newNode = resolvedNode || node;
|
|
101
|
+
if (!resolvedNode && node?.type === NodeType.Async) {
|
|
102
|
+
queueMicrotask(async () => {
|
|
103
|
+
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
104
|
+
node,
|
|
105
|
+
(result2) => {
|
|
106
|
+
this.handleAsyncUpdate(node, result2, options, this.currentView);
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
this.handleAsyncUpdate(node, result, options, this.currentView);
|
|
110
|
+
});
|
|
111
|
+
return node;
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
-
return node;
|
|
113
|
+
return newNode;
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
|
-
async runAsyncNode(node, options) {
|
|
117
|
-
try {
|
|
118
|
-
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
119
|
-
node,
|
|
120
|
-
(result2) => {
|
|
121
|
-
this.parseNodeAndUpdate(node, result2, options, this.currentView);
|
|
122
|
-
}
|
|
123
|
-
);
|
|
124
|
-
this.parseNodeAndUpdate(node, result, options, this.currentView);
|
|
125
|
-
} catch (e) {
|
|
126
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
127
|
-
const result = this.basePlugin?.hooks.onAsyncNodeError.call(error, node);
|
|
128
|
-
if (result === void 0) {
|
|
129
|
-
const playerState = this.basePlugin?.getPlayerInstance()?.getState();
|
|
130
|
-
if (playerState?.status === "in-progress") {
|
|
131
|
-
playerState.fail(error);
|
|
132
|
-
}
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
options.logger?.error(
|
|
136
|
-
"Async node handling failed and resolved with a fallback. Error:",
|
|
137
|
-
error
|
|
138
|
-
);
|
|
139
|
-
this.handleAsyncUpdate(node, result, this.currentView);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
116
|
isAsync(node) {
|
|
143
117
|
return node?.type === NodeType.Async;
|
|
144
118
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts"],"sourcesContent":["import { NodeType, getNodeID } from \"@player-ui/player\";\nimport type {\n Player,\n PlayerPlugin,\n Node,\n ParseObjectOptions,\n ParseObjectChildOptions,\n ViewInstance,\n Parser,\n ViewPlugin,\n Resolver,\n Resolve,\n} from \"@player-ui/player\";\nimport { AsyncParallelBailHook, SyncBailHook } from \"tapable-ts\";\nimport queueMicrotask from \"queue-microtask\";\nimport { omit } from \"timm\";\n\nexport * from \"./types\";\nexport * from \"./transform\";\n\nexport interface AsyncNodePluginOptions {\n /** A set of plugins to load */\n plugins?: AsyncNodeViewPlugin[];\n}\n\nexport interface AsyncNodeViewPlugin extends ViewPlugin {\n /** Use this to tap into the async node plugin hooks */\n applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;\n\n asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n}\nexport type AsyncHandler = (\n node: Node.Async,\n callback?: (result: any) => void,\n) => Promise<any>;\n\n/** Hook declaration for the AsyncNodePlugin */\ntype AsyncNodeHooks = {\n /** Async hook to get content for an async node */\n onAsyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n /** 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. */\n onAsyncNodeError: SyncBailHook<[Error, Node.Async], Node.Node | null>;\n};\n\n/**\n * Async node plugin used to resolve async nodes in the content\n * If an async node is present, allow users to provide a replacement node to be rendered when ready\n */\nexport class AsyncNodePlugin implements PlayerPlugin {\n private plugins: AsyncNodeViewPlugin[] | undefined;\n private playerInstance: Player | undefined;\n\n constructor(options: AsyncNodePluginOptions, asyncHandler?: AsyncHandler) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\n });\n }\n\n if (asyncHandler) {\n this.hooks.onAsyncNode.tap(\n \"async\",\n async (node: Node.Async, callback) => {\n return await asyncHandler(node, callback);\n },\n );\n }\n }\n\n public readonly hooks: AsyncNodeHooks = {\n onAsyncNode: new AsyncParallelBailHook(),\n onAsyncNodeError: new SyncBailHook(),\n };\n\n getPlayerInstance(): Player | undefined {\n return this.playerInstance;\n }\n\n name = \"AsyncNode\";\n\n apply(player: Player): void {\n this.playerInstance = player;\n\n player.hooks.viewController.tap(this.name, (viewController) => {\n viewController.hooks.view.tap(this.name, (view) => {\n this.plugins?.forEach((plugin) => {\n plugin.apply(view);\n });\n });\n });\n }\n}\n\nexport class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {\n public asyncNode: AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n > = new AsyncParallelBailHook();\n private basePlugin: AsyncNodePlugin | undefined;\n\n name = \"AsyncNode\";\n\n private resolvedMapping = new Map<string, any>();\n\n private currentView: ViewInstance | undefined;\n\n /**\n * Parses the node from the result and triggers an asynchronous view update if necessary.\n * @param node The asynchronous node that might be updated.\n * @param result The result obtained from resolving the async node. This could be any data structure or value.\n * @param options Options provided for node resolution, including a potential parseNode function to process the result.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private parseNodeAndUpdate(\n node: Node.Async,\n result: any,\n options: Resolve.NodeResolveOptions,\n view: ViewInstance | undefined,\n ) {\n const parsedNode =\n options.parseNode && result ? options.parseNode(result) : undefined;\n\n this.handleAsyncUpdate(node, parsedNode, view);\n }\n\n /**\n * Updates the node asynchronously based on the result provided.\n * This method is responsible for handling the update logic of asynchronous nodes.\n * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.\n * If an update is necessary, it triggers an asynchronous update on the view.\n * @param node The asynchronous node that might be updated.\n * @param newNode The new node to replace the async node.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private handleAsyncUpdate(\n node: Node.Async,\n newNode?: Node.Node | null,\n view?: ViewInstance,\n ) {\n if (this.resolvedMapping.get(node.id) !== newNode) {\n this.resolvedMapping.set(node.id, newNode ? newNode : node);\n view?.updateAsync();\n }\n }\n\n /**\n * Handles the asynchronous API integration for resolving nodes.\n * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.\n * @param resolver The resolver instance to attach the hook to.\n * @param view\n */\n applyResolver(resolver: Resolver): void {\n resolver.hooks.beforeResolve.tap(this.name, (node, options) => {\n if (!this.isAsync(node)) {\n return node;\n }\n\n const resolvedNode = this.resolvedMapping.get(node.id);\n if (resolvedNode !== undefined) {\n return resolvedNode;\n }\n\n queueMicrotask(() => this.runAsyncNode(node, options));\n\n return node;\n });\n }\n\n private async runAsyncNode(\n node: Node.Async,\n options: Resolve.NodeResolveOptions,\n ) {\n try {\n const result = await this.basePlugin?.hooks.onAsyncNode.call(\n node,\n (result) => {\n this.parseNodeAndUpdate(node, result, options, this.currentView);\n },\n );\n this.parseNodeAndUpdate(node, result, options, this.currentView);\n } catch (e: unknown) {\n const error = e instanceof Error ? e : new Error(String(e));\n const result = this.basePlugin?.hooks.onAsyncNodeError.call(error, node);\n\n if (result === undefined) {\n const playerState = this.basePlugin?.getPlayerInstance()?.getState();\n\n if (playerState?.status === \"in-progress\") {\n playerState.fail(error);\n }\n\n return;\n }\n\n options.logger?.error(\n \"Async node handling failed and resolved with a fallback. Error:\",\n error,\n );\n this.handleAsyncUpdate(node, result, this.currentView);\n }\n }\n\n private isAsync(node: Node.Node | null): node is Node.Async {\n return node?.type === NodeType.Async;\n }\n\n private isDeterminedAsync(obj: any) {\n return obj && Object.prototype.hasOwnProperty.call(obj, \"async\");\n }\n\n applyParser(parser: Parser): void {\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n childOptions?: ParseObjectChildOptions,\n ) => {\n if (this.isDeterminedAsync(obj)) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n\n if (parsedAsync === null || !parsedNodeId) {\n return childOptions ? [] : null;\n }\n\n const asyncAST = parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n\n if (childOptions) {\n return asyncAST\n ? [\n {\n path: [...childOptions.path, childOptions.key],\n value: asyncAST,\n },\n ]\n : [];\n }\n\n return asyncAST;\n }\n },\n );\n }\n\n apply(view: ViewInstance): void {\n this.currentView = view;\n view.hooks.parser.tap(\"async\", this.applyParser.bind(this));\n view.hooks.resolver.tap(\"async\", this.applyResolver.bind(this));\n }\n\n applyPlugin(asyncNodePlugin: AsyncNodePlugin): void {\n this.basePlugin = asyncNodePlugin;\n }\n}\n","import { Builder } from \"@player-ui/player\";\nimport type { AsyncTransformFunc } from \"./types\";\n\n/**\n * Util function to generate transform function for async asset\n * @param asset - async asset to apply beforeResolve transform\n * @param transformedAssetType: transformed asset type for rendering\n * @param wrapperAssetType: container asset type\n * @param flatten: flatten the streamed in content\n * @returns - wrapper asset with children of transformed asset and async node\n */\n\nexport const asyncTransform: AsyncTransformFunc = (\n assetId,\n wrapperAssetType,\n asset,\n flatten,\n) => {\n const id = \"async-\" + assetId;\n\n const asyncNode = Builder.asyncNode(id, flatten);\n let multiNode;\n let assetNode;\n\n if (asset) {\n assetNode = Builder.assetWrapper(asset);\n multiNode = Builder.multiNode(assetNode, asyncNode);\n } else {\n multiNode = Builder.multiNode(asyncNode);\n }\n\n const wrapperAsset = Builder.asset({\n id: wrapperAssetType + \"-\" + id,\n type: wrapperAssetType,\n });\n\n Builder.addChild(wrapperAsset, [\"values\"], multiNode);\n\n return wrapperAsset;\n};\n"],"mappings":";AAAA,SAAS,UAAU,iBAAiB;AAapC,SAAS,uBAAuB,oBAAoB;AACpD,OAAO,oBAAoB;AAC3B,SAAS,YAAY;;;ACfrB,SAAS,eAAe;AAYjB,IAAM,iBAAqC,CAChD,SACA,kBACA,OACA,YACG;AACH,QAAM,KAAK,WAAW;AAEtB,QAAM,YAAY,QAAQ,UAAU,IAAI,OAAO;AAC/C,MAAI;AACJ,MAAI;AAEJ,MAAI,OAAO;AACT,gBAAY,QAAQ,aAAa,KAAK;AACtC,gBAAY,QAAQ,UAAU,WAAW,SAAS;AAAA,EACpD,OAAO;AACL,gBAAY,QAAQ,UAA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;AAAA,EAEA,YAAY,iBAAwC;AAClD,SAAK,aAAa;AAAA,EACpB;AACF;","names":["result"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts"],"sourcesContent":["import { NodeType, getNodeID } from \"@player-ui/player\";\nimport type {\n Player,\n PlayerPlugin,\n Node,\n ParseObjectOptions,\n ParseObjectChildOptions,\n ViewInstance,\n Parser,\n ViewPlugin,\n Resolver,\n Resolve,\n} from \"@player-ui/player\";\nimport { AsyncParallelBailHook } from \"tapable-ts\";\nimport queueMicrotask from \"queue-microtask\";\nimport { omit } from \"timm\";\n\nexport * from \"./types\";\nexport * from \"./transform\";\n\nexport interface AsyncNodePluginOptions {\n /** A set of plugins to load */\n plugins?: AsyncNodeViewPlugin[];\n}\n\nexport interface AsyncNodeViewPlugin extends ViewPlugin {\n /** Use this to tap into the async node plugin hooks */\n applyPlugin: (asyncNodePlugin: AsyncNodePlugin) => void;\n\n asyncNode: AsyncParallelBailHook<[Node.Async, (result: any) => void], any>;\n}\nexport type AsyncHandler = (\n node: Node.Async,\n callback?: (result: any) => void,\n) => Promise<any>;\n\n/**\n * Async node plugin used to resolve async nodes in the content\n * If an async node is present, allow users to provide a replacement node to be rendered when ready\n */\nexport class AsyncNodePlugin implements PlayerPlugin {\n private plugins: AsyncNodeViewPlugin[] | undefined;\n\n constructor(options: AsyncNodePluginOptions, asyncHandler?: AsyncHandler) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\n });\n }\n\n if (asyncHandler) {\n this.hooks.onAsyncNode.tap(\n \"async\",\n async (node: Node.Async, callback) => {\n return await asyncHandler(node, callback);\n },\n );\n }\n }\n\n public readonly hooks = {\n onAsyncNode: new AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n >(),\n };\n\n name = \"AsyncNode\";\n\n apply(player: Player) {\n player.hooks.viewController.tap(this.name, (viewController) => {\n viewController.hooks.view.tap(this.name, (view) => {\n this.plugins?.forEach((plugin) => {\n plugin.apply(view);\n });\n });\n });\n }\n}\n\nexport class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {\n public asyncNode = new AsyncParallelBailHook<\n [Node.Async, (result: any) => void],\n any\n >();\n private basePlugin: AsyncNodePlugin | undefined;\n\n name = \"AsyncNode\";\n\n private resolvedMapping = new Map<string, any>();\n\n private currentView: ViewInstance | undefined;\n\n /**\n * Updates the node asynchronously based on the result provided.\n * This method is responsible for handling the update logic of asynchronous nodes.\n * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.\n * If an update is necessary, it triggers an asynchronous update on the view.\n * @param node The asynchronous node that might be updated.\n * @param result The result obtained from resolving the async node. This could be any data structure or value.\n * @param options Options provided for node resolution, including a potential parseNode function to process the result.\n * @param view The view instance where the node resides. This can be undefined if the view is not currently active.\n */\n private handleAsyncUpdate(\n node: Node.Async,\n result: any,\n options: Resolve.NodeResolveOptions,\n view: ViewInstance | undefined,\n ) {\n const parsedNode =\n options.parseNode && result ? options.parseNode(result) : undefined;\n\n if (this.resolvedMapping.get(node.id) !== parsedNode) {\n this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);\n view?.updateAsync();\n }\n }\n\n /**\n * Handles the asynchronous API integration for resolving nodes.\n * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.\n * @param resolver The resolver instance to attach the hook to.\n * @param view\n */\n applyResolver(resolver: Resolver) {\n resolver.hooks.beforeResolve.tap(this.name, (node, options) => {\n let resolvedNode;\n if (this.isAsync(node)) {\n const mappedValue = this.resolvedMapping.get(node.id);\n if (mappedValue) {\n resolvedNode = mappedValue;\n }\n } else {\n resolvedNode = null;\n }\n\n const newNode = resolvedNode || node;\n if (!resolvedNode && node?.type === NodeType.Async) {\n queueMicrotask(async () => {\n const result = await this.basePlugin?.hooks.onAsyncNode.call(\n node,\n (result) => {\n this.handleAsyncUpdate(node, result, options, this.currentView);\n },\n );\n this.handleAsyncUpdate(node, result, options, this.currentView);\n });\n\n return node;\n }\n return newNode;\n });\n }\n\n private isAsync(node: Node.Node | null): node is Node.Async {\n return node?.type === NodeType.Async;\n }\n\n private isDeterminedAsync(obj: any) {\n return obj && Object.prototype.hasOwnProperty.call(obj, \"async\");\n }\n\n applyParser(parser: Parser) {\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n childOptions?: ParseObjectChildOptions,\n ) => {\n if (this.isDeterminedAsync(obj)) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n\n if (parsedAsync === null || !parsedNodeId) {\n return childOptions ? [] : null;\n }\n\n const asyncAST = parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n\n if (childOptions) {\n return asyncAST\n ? [\n {\n path: [...childOptions.path, childOptions.key],\n value: asyncAST,\n },\n ]\n : [];\n }\n\n return asyncAST;\n }\n },\n );\n }\n\n apply(view: ViewInstance): void {\n this.currentView = view;\n view.hooks.parser.tap(\"async\", this.applyParser.bind(this));\n view.hooks.resolver.tap(\"async\", this.applyResolver.bind(this));\n }\n\n applyPlugin(asyncNodePlugin: AsyncNodePlugin): void {\n this.basePlugin = asyncNodePlugin;\n }\n}\n","import { Builder } from \"@player-ui/player\";\nimport type { AsyncTransformFunc } from \"./types\";\n\n/**\n * Util function to generate transform function for async asset\n * @param asset - async asset to apply beforeResolve transform\n * @param transformedAssetType: transformed asset type for rendering\n * @param wrapperAssetType: container asset type\n * @param flatten: flatten the streamed in content\n * @returns - wrapper asset with children of transformed asset and async node\n */\n\nexport const asyncTransform: AsyncTransformFunc = (\n assetId,\n wrapperAssetType,\n asset,\n flatten,\n) => {\n const id = \"async-\" + assetId;\n\n const asyncNode = Builder.asyncNode(id, flatten);\n let multiNode;\n let assetNode;\n\n if (asset) {\n assetNode = Builder.assetWrapper(asset);\n multiNode = Builder.multiNode(assetNode, asyncNode);\n } else {\n multiNode = Builder.multiNode(asyncNode);\n }\n\n const wrapperAsset = Builder.asset({\n id: wrapperAssetType + \"-\" + id,\n type: wrapperAssetType,\n });\n\n Builder.addChild(wrapperAsset, [\"values\"], multiNode);\n\n return wrapperAsset;\n};\n"],"mappings":";AAAA,SAAS,UAAU,iBAAiB;AAapC,SAAS,6BAA6B;AACtC,OAAO,oBAAoB;AAC3B,SAAS,YAAY;;;ACfrB,SAAS,eAAe;AAYjB,IAAM,iBAAqC,CAChD,SACA,kBACA,OACA,YACG;AACH,QAAM,KAAK,WAAW;AAEtB,QAAM,YAAY,QAAQ,UAAU,IAAI,OAAO;AAC/C,MAAI;AACJ,MAAI;AAEJ,MAAI,OAAO;AACT,gBAAY,QAAQ,aAAa,KAAK;AACtC,gBAAY,QAAQ,UAAU,WAAW,SAAS;AAAA,EACpD,OAAO;AACL,gBAAY,QAAQ,UAAU,SAAS;AAAA,EACzC;AAEA,QAAM,eAAe,QAAQ,MAAM;AAAA,IACjC,IAAI,mBAAmB,MAAM;AAAA,IAC7B,MAAM;AAAA,EACR,CAAC;AAED,UAAQ,SAAS,cAAc,CAAC,QAAQ,GAAG,SAAS;AAEpD,SAAO;AACT;;;ADCO,IAAM,kBAAN,MAA8C;AAAA,EAGnD,YAAY,SAAiC,cAA6B;AAkB1E,SAAgB,QAAQ;AAAA,MACtB,aAAa,IAAI,sBAGf;AAAA,IACJ;AAEA,gBAAO;AAxBL,QAAI,SAAS,SAAS;AACpB,WAAK,UAAU,QAAQ;AACvB,cAAQ,QAAQ,QAAQ,CAAC,WAAW;AAClC,eAAO,YAAY,IAAI;AAAA,MACzB,CAAC;AAAA,IACH;AAEA,QAAI,cAAc;AAChB,WAAK,MAAM,YAAY;AAAA,QACrB;AAAA,QACA,OAAO,MAAkB,aAAa;AACpC,iBAAO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SAAS,IAAI,SAAS,KAAK,cAAc,KAAK,IAAI,CAAC;AAAA,EAChE;AAAA,EAEA,YAAY,iBAAwC;AAClD,SAAK,aAAa;AAAA,EACpB;AACF;","names":["result"]}
|
package/package.json
CHANGED
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
"types"
|
|
7
7
|
],
|
|
8
8
|
"name": "@player-ui/async-node-plugin",
|
|
9
|
-
"version": "0.11.3--canary.
|
|
9
|
+
"version": "0.11.3--canary.660.23595",
|
|
10
10
|
"main": "dist/cjs/index.cjs",
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@player-ui/player": "0.11.3--canary.
|
|
12
|
+
"@player-ui/player": "0.11.3--canary.660.23595"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@player-ui/reference-assets-plugin": "workspace:*",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { expect, test, describe
|
|
2
|
-
import { Node, InProgressState
|
|
1
|
+
import { expect, test, describe } from "vitest";
|
|
2
|
+
import { Node, InProgressState } from "@player-ui/player";
|
|
3
3
|
import { Player, Parser } from "@player-ui/player";
|
|
4
4
|
import { waitFor } from "@testing-library/react";
|
|
5
5
|
import { AsyncNodePlugin, AsyncNodePluginPlugin } from "../index";
|
|
@@ -734,83 +734,6 @@ describe("view", () => {
|
|
|
734
734
|
});
|
|
735
735
|
});
|
|
736
736
|
|
|
737
|
-
describe("Async Node Error Handling", () => {
|
|
738
|
-
let failingAsyncNodePlugin: AsyncNodePlugin = new AsyncNodePlugin({});
|
|
739
|
-
const onAsyncNodeErrorCallback = vi.fn();
|
|
740
|
-
|
|
741
|
-
beforeEach(() => {
|
|
742
|
-
onAsyncNodeErrorCallback.mockReset();
|
|
743
|
-
const failingHandler = vi.fn();
|
|
744
|
-
failingHandler.mockRejectedValue("Promise Rejected");
|
|
745
|
-
|
|
746
|
-
failingAsyncNodePlugin = new AsyncNodePlugin(
|
|
747
|
-
{
|
|
748
|
-
plugins: [new AsyncNodePluginPlugin()],
|
|
749
|
-
},
|
|
750
|
-
failingHandler,
|
|
751
|
-
);
|
|
752
|
-
|
|
753
|
-
failingAsyncNodePlugin.hooks.onAsyncNodeError.tap(
|
|
754
|
-
"test",
|
|
755
|
-
onAsyncNodeErrorCallback,
|
|
756
|
-
);
|
|
757
|
-
});
|
|
758
|
-
|
|
759
|
-
test("should replace the async node with the result from the onAsyncNodeError hook when there is an error handling the async node", async () => {
|
|
760
|
-
onAsyncNodeErrorCallback.mockReturnValue({
|
|
761
|
-
type: "asset",
|
|
762
|
-
value: {
|
|
763
|
-
id: "async-text",
|
|
764
|
-
type: "text",
|
|
765
|
-
value: "Fallback Text",
|
|
766
|
-
},
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
const player = new Player({ plugins: [failingAsyncNodePlugin] });
|
|
770
|
-
player.start(basicFRFWithActions as any);
|
|
771
|
-
|
|
772
|
-
await waitFor(() => {
|
|
773
|
-
expect(onAsyncNodeErrorCallback).toHaveBeenCalledWith(
|
|
774
|
-
new Error("Promise Rejected"),
|
|
775
|
-
expect.anything(),
|
|
776
|
-
);
|
|
777
|
-
|
|
778
|
-
const playerState = player.getState();
|
|
779
|
-
expect(playerState.status).toBe("in-progress");
|
|
780
|
-
const inProgressState = playerState as InProgressState;
|
|
781
|
-
const lastViewUpdate =
|
|
782
|
-
inProgressState.controllers.view.currentView?.lastUpdate;
|
|
783
|
-
|
|
784
|
-
expect(lastViewUpdate?.actions[1]).toStrictEqual({
|
|
785
|
-
id: "async-text",
|
|
786
|
-
type: "text",
|
|
787
|
-
value: "Fallback Text",
|
|
788
|
-
});
|
|
789
|
-
});
|
|
790
|
-
});
|
|
791
|
-
|
|
792
|
-
test("should bubble up the error and cause player to fail when there is an error handling the async node and the onAsyncNodeError hook does not produce a fallback", async () => {
|
|
793
|
-
onAsyncNodeErrorCallback.mockReturnValue(undefined);
|
|
794
|
-
|
|
795
|
-
const player = new Player({ plugins: [failingAsyncNodePlugin] });
|
|
796
|
-
player.start(basicFRFWithActions as any).catch(() => {
|
|
797
|
-
/** Purposefully failing player in this test so catching the unresolved exception suppresses warnings from vitest */
|
|
798
|
-
});
|
|
799
|
-
|
|
800
|
-
await waitFor(() => {
|
|
801
|
-
expect(onAsyncNodeErrorCallback).toHaveBeenCalledWith(
|
|
802
|
-
new Error("Promise Rejected"),
|
|
803
|
-
expect.anything(),
|
|
804
|
-
);
|
|
805
|
-
|
|
806
|
-
const playerState = player.getState();
|
|
807
|
-
expect(playerState.status).toBe("error");
|
|
808
|
-
const errorState = playerState as ErrorState;
|
|
809
|
-
expect(errorState.error.message).toBe("Promise Rejected");
|
|
810
|
-
});
|
|
811
|
-
});
|
|
812
|
-
});
|
|
813
|
-
|
|
814
737
|
test("chat-message asset - replaces async nodes with multi node flattened", async () => {
|
|
815
738
|
const plugin = new AsyncNodePlugin({
|
|
816
739
|
plugins: [new AsyncNodePluginPlugin()],
|