@player-ui/async-node-plugin 0.10.4-next.2 → 0.10.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 +159 -13
- package/dist/AsyncNodePlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +32 -7
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +29 -5
- package/dist/index.mjs +29 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
- package/src/__tests__/__snapshots__/transform.test.ts.snap +58 -0
- package/src/__tests__/index.test.ts +329 -0
- package/src/__tests__/transform.test.ts +25 -0
- package/src/index.ts +1 -0
- package/src/transform.ts +40 -0
- package/src/types.ts +7 -0
- package/types/index.d.ts +1 -0
- package/types/transform.d.ts +11 -0
- package/types/types.d.ts +1 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -31,13 +31,37 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
AsyncNodePlugin: () => AsyncNodePlugin,
|
|
34
|
-
AsyncNodePluginPlugin: () => AsyncNodePluginPlugin
|
|
34
|
+
AsyncNodePluginPlugin: () => AsyncNodePluginPlugin,
|
|
35
|
+
asyncTransform: () => asyncTransform
|
|
35
36
|
});
|
|
36
37
|
module.exports = __toCommonJS(src_exports);
|
|
37
|
-
var
|
|
38
|
+
var import_player2 = require("@player-ui/player");
|
|
38
39
|
var import_tapable_ts = require("tapable-ts");
|
|
39
40
|
var import_queue_microtask = __toESM(require("queue-microtask"));
|
|
40
41
|
var import_timm = require("timm");
|
|
42
|
+
|
|
43
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts
|
|
44
|
+
var import_player = require("@player-ui/player");
|
|
45
|
+
var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
46
|
+
const id = "async-" + assetId;
|
|
47
|
+
const asyncNode = import_player.Builder.asyncNode(id, flatten);
|
|
48
|
+
let multiNode;
|
|
49
|
+
let assetNode;
|
|
50
|
+
if (asset) {
|
|
51
|
+
assetNode = import_player.Builder.assetWrapper(asset);
|
|
52
|
+
multiNode = import_player.Builder.multiNode(assetNode, asyncNode);
|
|
53
|
+
} else {
|
|
54
|
+
multiNode = import_player.Builder.multiNode(asyncNode);
|
|
55
|
+
}
|
|
56
|
+
const wrapperAsset = import_player.Builder.asset({
|
|
57
|
+
id: wrapperAssetType + "-" + id,
|
|
58
|
+
type: wrapperAssetType
|
|
59
|
+
});
|
|
60
|
+
import_player.Builder.addChild(wrapperAsset, ["values"], multiNode);
|
|
61
|
+
return wrapperAsset;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
41
65
|
var AsyncNodePlugin = class {
|
|
42
66
|
constructor(options) {
|
|
43
67
|
this.hooks = {
|
|
@@ -102,7 +126,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
102
126
|
resolvedNode = null;
|
|
103
127
|
}
|
|
104
128
|
const newNode = resolvedNode || node;
|
|
105
|
-
if (!resolvedNode && node?.type ===
|
|
129
|
+
if (!resolvedNode && node?.type === import_player2.NodeType.Async) {
|
|
106
130
|
(0, import_queue_microtask.default)(async () => {
|
|
107
131
|
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
108
132
|
node,
|
|
@@ -118,7 +142,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
118
142
|
});
|
|
119
143
|
}
|
|
120
144
|
isAsync(node) {
|
|
121
|
-
return node?.type ===
|
|
145
|
+
return node?.type === import_player2.NodeType.Async;
|
|
122
146
|
}
|
|
123
147
|
isDeterminedAsync(obj) {
|
|
124
148
|
return obj && Object.prototype.hasOwnProperty.call(obj, "async");
|
|
@@ -133,14 +157,14 @@ var AsyncNodePluginPlugin = class {
|
|
|
133
157
|
nodeType,
|
|
134
158
|
options
|
|
135
159
|
);
|
|
136
|
-
const parsedNodeId = (0,
|
|
160
|
+
const parsedNodeId = (0, import_player2.getNodeID)(parsedAsync);
|
|
137
161
|
if (parsedAsync === null || !parsedNodeId) {
|
|
138
162
|
return childOptions ? [] : null;
|
|
139
163
|
}
|
|
140
164
|
const asyncAST = parser.createASTNode(
|
|
141
165
|
{
|
|
142
166
|
id: parsedNodeId,
|
|
143
|
-
type:
|
|
167
|
+
type: import_player2.NodeType.Async,
|
|
144
168
|
value: parsedAsync
|
|
145
169
|
},
|
|
146
170
|
obj
|
|
@@ -170,6 +194,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
170
194
|
// Annotate the CommonJS export names for ESM import in node:
|
|
171
195
|
0 && (module.exports = {
|
|
172
196
|
AsyncNodePlugin,
|
|
173
|
-
AsyncNodePluginPlugin
|
|
197
|
+
AsyncNodePluginPlugin,
|
|
198
|
+
asyncTransform
|
|
174
199
|
});
|
|
175
200
|
//# sourceMappingURL=index.cjs.map
|
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"],"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\";\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}\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) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\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 apply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ACE,IAAI;AAAA,cACJ,MAAM,uBAAS;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":["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}\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) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\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, NodeType } 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,oBAAkC;AAY3B,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;AAE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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,8 +1,31 @@
|
|
|
1
1
|
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
2
|
-
import { NodeType, getNodeID } from "@player-ui/player";
|
|
2
|
+
import { NodeType as NodeType2, getNodeID } from "@player-ui/player";
|
|
3
3
|
import { AsyncParallelBailHook } from "tapable-ts";
|
|
4
4
|
import queueMicrotask from "queue-microtask";
|
|
5
5
|
import { omit } from "timm";
|
|
6
|
+
|
|
7
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts
|
|
8
|
+
import { Builder } from "@player-ui/player";
|
|
9
|
+
var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
10
|
+
const id = "async-" + assetId;
|
|
11
|
+
const asyncNode = Builder.asyncNode(id, flatten);
|
|
12
|
+
let multiNode;
|
|
13
|
+
let assetNode;
|
|
14
|
+
if (asset) {
|
|
15
|
+
assetNode = Builder.assetWrapper(asset);
|
|
16
|
+
multiNode = Builder.multiNode(assetNode, asyncNode);
|
|
17
|
+
} else {
|
|
18
|
+
multiNode = Builder.multiNode(asyncNode);
|
|
19
|
+
}
|
|
20
|
+
const wrapperAsset = Builder.asset({
|
|
21
|
+
id: wrapperAssetType + "-" + id,
|
|
22
|
+
type: wrapperAssetType
|
|
23
|
+
});
|
|
24
|
+
Builder.addChild(wrapperAsset, ["values"], multiNode);
|
|
25
|
+
return wrapperAsset;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
6
29
|
var AsyncNodePlugin = class {
|
|
7
30
|
constructor(options) {
|
|
8
31
|
this.hooks = {
|
|
@@ -67,7 +90,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
67
90
|
resolvedNode = null;
|
|
68
91
|
}
|
|
69
92
|
const newNode = resolvedNode || node;
|
|
70
|
-
if (!resolvedNode && node?.type ===
|
|
93
|
+
if (!resolvedNode && node?.type === NodeType2.Async) {
|
|
71
94
|
queueMicrotask(async () => {
|
|
72
95
|
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
73
96
|
node,
|
|
@@ -83,7 +106,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
83
106
|
});
|
|
84
107
|
}
|
|
85
108
|
isAsync(node) {
|
|
86
|
-
return node?.type ===
|
|
109
|
+
return node?.type === NodeType2.Async;
|
|
87
110
|
}
|
|
88
111
|
isDeterminedAsync(obj) {
|
|
89
112
|
return obj && Object.prototype.hasOwnProperty.call(obj, "async");
|
|
@@ -105,7 +128,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
105
128
|
const asyncAST = parser.createASTNode(
|
|
106
129
|
{
|
|
107
130
|
id: parsedNodeId,
|
|
108
|
-
type:
|
|
131
|
+
type: NodeType2.Async,
|
|
109
132
|
value: parsedAsync
|
|
110
133
|
},
|
|
111
134
|
obj
|
|
@@ -134,6 +157,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
134
157
|
};
|
|
135
158
|
export {
|
|
136
159
|
AsyncNodePlugin,
|
|
137
|
-
AsyncNodePluginPlugin
|
|
160
|
+
AsyncNodePluginPlugin,
|
|
161
|
+
asyncTransform
|
|
138
162
|
};
|
|
139
163
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
2
|
-
import { NodeType, getNodeID } from "@player-ui/player";
|
|
2
|
+
import { NodeType as NodeType2, getNodeID } from "@player-ui/player";
|
|
3
3
|
import { AsyncParallelBailHook } from "tapable-ts";
|
|
4
4
|
import queueMicrotask from "queue-microtask";
|
|
5
5
|
import { omit } from "timm";
|
|
6
|
+
|
|
7
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/transform.ts
|
|
8
|
+
import { Builder } from "@player-ui/player";
|
|
9
|
+
var asyncTransform = (assetId, wrapperAssetType, asset, flatten) => {
|
|
10
|
+
const id = "async-" + assetId;
|
|
11
|
+
const asyncNode = Builder.asyncNode(id, flatten);
|
|
12
|
+
let multiNode;
|
|
13
|
+
let assetNode;
|
|
14
|
+
if (asset) {
|
|
15
|
+
assetNode = Builder.assetWrapper(asset);
|
|
16
|
+
multiNode = Builder.multiNode(assetNode, asyncNode);
|
|
17
|
+
} else {
|
|
18
|
+
multiNode = Builder.multiNode(asyncNode);
|
|
19
|
+
}
|
|
20
|
+
const wrapperAsset = Builder.asset({
|
|
21
|
+
id: wrapperAssetType + "-" + id,
|
|
22
|
+
type: wrapperAssetType
|
|
23
|
+
});
|
|
24
|
+
Builder.addChild(wrapperAsset, ["values"], multiNode);
|
|
25
|
+
return wrapperAsset;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
|
|
6
29
|
var AsyncNodePlugin = class {
|
|
7
30
|
constructor(options) {
|
|
8
31
|
this.hooks = {
|
|
@@ -67,7 +90,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
67
90
|
resolvedNode = null;
|
|
68
91
|
}
|
|
69
92
|
const newNode = resolvedNode || node;
|
|
70
|
-
if (!resolvedNode && node?.type ===
|
|
93
|
+
if (!resolvedNode && node?.type === NodeType2.Async) {
|
|
71
94
|
queueMicrotask(async () => {
|
|
72
95
|
const result = await this.basePlugin?.hooks.onAsyncNode.call(
|
|
73
96
|
node,
|
|
@@ -83,7 +106,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
83
106
|
});
|
|
84
107
|
}
|
|
85
108
|
isAsync(node) {
|
|
86
|
-
return node?.type ===
|
|
109
|
+
return node?.type === NodeType2.Async;
|
|
87
110
|
}
|
|
88
111
|
isDeterminedAsync(obj) {
|
|
89
112
|
return obj && Object.prototype.hasOwnProperty.call(obj, "async");
|
|
@@ -105,7 +128,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
105
128
|
const asyncAST = parser.createASTNode(
|
|
106
129
|
{
|
|
107
130
|
id: parsedNodeId,
|
|
108
|
-
type:
|
|
131
|
+
type: NodeType2.Async,
|
|
109
132
|
value: parsedAsync
|
|
110
133
|
},
|
|
111
134
|
obj
|
|
@@ -134,6 +157,7 @@ var AsyncNodePluginPlugin = class {
|
|
|
134
157
|
};
|
|
135
158
|
export {
|
|
136
159
|
AsyncNodePlugin,
|
|
137
|
-
AsyncNodePluginPlugin
|
|
160
|
+
AsyncNodePluginPlugin,
|
|
161
|
+
asyncTransform
|
|
138
162
|
};
|
|
139
163
|
//# sourceMappingURL=index.mjs.map
|
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"],"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\";\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}\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) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\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"],"mappings":";AAAA,SAAS,UAAU,iBAAiB;AAapC,SAAS,6BAA6B;AACtC,OAAO,oBAAoB;AAC3B,SAAS,YAAY;AAoBd,IAAM,kBAAN,MAA8C;AAAA,EAGnD,YAAY,SAAiC;AAS7C,SAAgB,QAAQ;AAAA,MACtB,aAAa,IAAI,sBAGf;AAAA,IACJ;AAEA,gBAAO;AAfL,QAAI,SAAS,SAAS;AACpB,WAAK,UAAU,QAAQ;AACvB,cAAQ,QAAQ,QAAQ,CAAC,WAAW;AAClC,eAAO,YAAY,IAAI;AAAA,MACzB,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAWA,MAAM,QAAgB;AACpB,WAAO,MAAM,eAAe,IAAI,KAAK,MAAM,CAAC,mBAAmB;AAC7D,qBAAe,MAAM,KAAK,IAAI,KAAK,MAAM,CAAC,SAAS;AACjD,aAAK,SAAS,QAAQ,CAAC,WAAW;AAChC,iBAAO,MAAM,IAAI;AAAA,QACnB,CAAC;AAAA,MACH,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;AAEO,IAAM,wBAAN,MAA2D;AAAA,EAA3D;AACL,SAAO,YAAY,IAAI,sBAGrB;AAGF,gBAAO;AAEP,SAAQ,kBAAkB,oBAAI,IAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcvC,kBACN,MACA,QACA,SACA,MACA;AACA,UAAM,aACJ,QAAQ,aAAa,SAAS,QAAQ,UAAU,MAAM,IAAI;AAE5D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UACF;AACA,gBAAM,eAAe,UAAU,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,SAAS;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":["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}\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) {\n if (options?.plugins) {\n this.plugins = options.plugins;\n options.plugins.forEach((plugin) => {\n plugin.applyPlugin(this);\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, NodeType } 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,YAAAA,WAAU,iBAAiB;AAapC,SAAS,6BAA6B;AACtC,OAAO,oBAAoB;AAC3B,SAAS,YAAY;;;ACfrB,SAAS,eAAyB;AAY3B,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,mBA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B,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":["NodeType","NodeType","result"]}
|
package/package.json
CHANGED
|
@@ -6,10 +6,13 @@
|
|
|
6
6
|
"types"
|
|
7
7
|
],
|
|
8
8
|
"name": "@player-ui/async-node-plugin",
|
|
9
|
-
"version": "0.10.4
|
|
9
|
+
"version": "0.10.4",
|
|
10
10
|
"main": "dist/cjs/index.cjs",
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@player-ui/player": "0.10.4
|
|
12
|
+
"@player-ui/player": "0.10.4"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@player-ui/reference-assets-plugin": "workspace:*"
|
|
13
16
|
},
|
|
14
17
|
"module": "dist/index.legacy-esm.js",
|
|
15
18
|
"types": "types/index.d.ts",
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`asyncTransform > generates wrapper asset with asset 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"children": [
|
|
6
|
+
{
|
|
7
|
+
"path": [
|
|
8
|
+
"values",
|
|
9
|
+
],
|
|
10
|
+
"value": {
|
|
11
|
+
"override": true,
|
|
12
|
+
"parent": [Circular],
|
|
13
|
+
"type": "multi-node",
|
|
14
|
+
"values": [
|
|
15
|
+
{
|
|
16
|
+
"children": [
|
|
17
|
+
{
|
|
18
|
+
"path": [
|
|
19
|
+
"asset",
|
|
20
|
+
],
|
|
21
|
+
"value": {
|
|
22
|
+
"parent": [Circular],
|
|
23
|
+
"type": "asset",
|
|
24
|
+
"value": {
|
|
25
|
+
"id": "2",
|
|
26
|
+
"type": "text",
|
|
27
|
+
"value": "chat message",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
"parent": [Circular],
|
|
33
|
+
"type": "value",
|
|
34
|
+
"value": undefined,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"flatten": true,
|
|
38
|
+
"id": "async-1",
|
|
39
|
+
"parent": [Circular],
|
|
40
|
+
"type": "async",
|
|
41
|
+
"value": {
|
|
42
|
+
"type": "value",
|
|
43
|
+
"value": {
|
|
44
|
+
"id": "async-1",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
"type": "asset",
|
|
53
|
+
"value": {
|
|
54
|
+
"id": "collection-async-1",
|
|
55
|
+
"type": "collection",
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
`;
|