@player-ui/async-node-plugin 0.8.0--canary.307.9621 → 0.8.0--canary.410.15865

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.
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ AsyncNodePlugin: () => AsyncNodePlugin,
34
+ AsyncNodePluginPlugin: () => AsyncNodePluginPlugin
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+ var import_player = require("@player-ui/player");
38
+ var import_tapable_ts = require("tapable-ts");
39
+ var import_queue_microtask = __toESM(require("queue-microtask"));
40
+ var import_timm = require("timm");
41
+ var AsyncNodePlugin = class {
42
+ constructor(options) {
43
+ this.hooks = {
44
+ onAsyncNode: new import_tapable_ts.AsyncParallelBailHook()
45
+ };
46
+ this.name = "AsyncNode";
47
+ if (options?.plugins) {
48
+ this.plugins = options.plugins;
49
+ options.plugins.forEach((plugin) => {
50
+ plugin.applyPlugin(this);
51
+ });
52
+ }
53
+ }
54
+ apply(player) {
55
+ player.hooks.viewController.tap(this.name, (viewController) => {
56
+ viewController.hooks.view.tap(this.name, (view) => {
57
+ this.plugins?.forEach((plugin) => {
58
+ plugin.apply(view);
59
+ });
60
+ });
61
+ });
62
+ }
63
+ };
64
+ var AsyncNodePluginPlugin = class {
65
+ constructor() {
66
+ this.asyncNode = new import_tapable_ts.AsyncParallelBailHook();
67
+ this.name = "AsyncNode";
68
+ this.resolvedMapping = /* @__PURE__ */ new Map();
69
+ }
70
+ /**
71
+ * Updates the node asynchronously based on the result provided.
72
+ * This method is responsible for handling the update logic of asynchronous nodes.
73
+ * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
74
+ * If an update is necessary, it triggers an asynchronous update on the view.
75
+ * @param node The asynchronous node that might be updated.
76
+ * @param result The result obtained from resolving the async node. This could be any data structure or value.
77
+ * @param options Options provided for node resolution, including a potential parseNode function to process the result.
78
+ * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
79
+ */
80
+ handleAsyncUpdate(node, result, options, view) {
81
+ const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
82
+ if (this.resolvedMapping.get(node.id) !== parsedNode) {
83
+ this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
84
+ view?.updateAsync();
85
+ }
86
+ }
87
+ /**
88
+ * Handles the asynchronous API integration for resolving nodes.
89
+ * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.
90
+ * @param resolver The resolver instance to attach the hook to.
91
+ * @param view
92
+ */
93
+ applyResolver(resolver) {
94
+ resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
95
+ let resolvedNode;
96
+ if (this.isAsync(node)) {
97
+ const mappedValue = this.resolvedMapping.get(node.id);
98
+ if (mappedValue) {
99
+ resolvedNode = mappedValue;
100
+ }
101
+ } else {
102
+ resolvedNode = null;
103
+ }
104
+ const newNode = resolvedNode || node;
105
+ if (!resolvedNode && node?.type === import_player.NodeType.Async) {
106
+ (0, import_queue_microtask.default)(async () => {
107
+ const result = await this.basePlugin?.hooks.onAsyncNode.call(
108
+ node,
109
+ (result2) => {
110
+ this.handleAsyncUpdate(node, result2, options, this.currentView);
111
+ }
112
+ );
113
+ this.handleAsyncUpdate(node, result, options, this.currentView);
114
+ });
115
+ return node;
116
+ }
117
+ return newNode;
118
+ });
119
+ }
120
+ isAsync(node) {
121
+ return node?.type === import_player.NodeType.Async;
122
+ }
123
+ applyParser(parser) {
124
+ parser.hooks.determineNodeType.tap(this.name, (obj) => {
125
+ if (Object.prototype.hasOwnProperty.call(obj, "async")) {
126
+ return import_player.NodeType.Async;
127
+ }
128
+ });
129
+ parser.hooks.parseNode.tap(
130
+ this.name,
131
+ (obj, nodeType, options, determinedNodeType) => {
132
+ if (determinedNodeType === import_player.NodeType.Async) {
133
+ const parsedAsync = parser.parseObject(
134
+ (0, import_timm.omit)(obj, "async"),
135
+ nodeType,
136
+ options
137
+ );
138
+ const parsedNodeId = (0, import_player.getNodeID)(parsedAsync);
139
+ if (parsedAsync !== null && parsedNodeId) {
140
+ return parser.createASTNode(
141
+ {
142
+ id: parsedNodeId,
143
+ type: import_player.NodeType.Async,
144
+ value: parsedAsync
145
+ },
146
+ obj
147
+ );
148
+ }
149
+ return null;
150
+ }
151
+ }
152
+ );
153
+ }
154
+ apply(view) {
155
+ this.currentView = view;
156
+ view.hooks.parser.tap("async", this.applyParser.bind(this));
157
+ view.hooks.resolver.tap("async", this.applyResolver.bind(this));
158
+ }
159
+ applyPlugin(asyncNodePlugin) {
160
+ this.basePlugin = asyncNodePlugin;
161
+ }
162
+ };
163
+ // Annotate the CommonJS export names for ESM import in node:
164
+ 0 && (module.exports = {
165
+ AsyncNodePlugin,
166
+ AsyncNodePluginPlugin
167
+ });
168
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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 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 applyParser(parser: Parser) {\n parser.hooks.determineNodeType.tap(this.name, (obj) => {\n if (Object.prototype.hasOwnProperty.call(obj, \"async\")) {\n return NodeType.Async;\n }\n });\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n determinedNodeType: null | NodeType,\n ) => {\n if (determinedNodeType === NodeType.Async) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n if (parsedAsync !== null && parsedNodeId) {\n return parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n }\n\n return null;\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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoC;AAYpC,wBAAsC;AACtC,6BAA2B;AAC3B,kBAAqB;AAoBd,IAAM,kBAAN,MAA8C;AAAA,EAGnD,YAAY,SAAiC;AAS7C,SAAgB,QAAQ;AAAA,MACtB,aAAa,IAAI,wCAGf;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,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ACT,CAAC;AAAA,EACH;AAAA,EAEQ,QAAQ,MAA4C;AAC1D,WAAO,MAAM,SAAS,uBAAS;AAAA,EACjC;AAAA,EAEA,YAAY,QAAgB;AAC1B,WAAO,MAAM,kBAAkB,IAAI,KAAK,MAAM,CAAC,QAAQ;AACrD,UAAI,OAAO,UAAU,eAAe,KAAK,KAAK,OAAO,GAAG;AACtD,eAAO,uBAAS;AAAA,MAClB;AAAA,IACF,CAAC;AACD,WAAO,MAAM,UAAU;AAAA,MACrB,KAAK;AAAA,MACL,CACE,KACA,UACA,SACA,uBACG;AACH,YAAI,uBAAuB,uBAAS,OAAO;AACzC,gBAAM,cAAc,OAAO;AAAA,gBACzB,kBAAK,KAAK,OAAO;AAAA,YACjB;AAAA,YACA;AAAA,UACF;AACA,gBAAM,mBAAe,yBAAU,WAAW;AAC1C,cAAI,gBAAgB,QAAQ,cAAc;AACxC,mBAAO,OAAO;AAAA,cACZ;AAAA,gBACE,IAAI;AAAA,gBACJ,MAAM,uBAAS;AAAA,gBACf,OAAO;AAAA,cACT;AAAA,cACA;AAAA,YACF;AAAA,UACF;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"]}
@@ -0,0 +1,132 @@
1
+ // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
2
+ import { NodeType, getNodeID } from "@player-ui/player";
3
+ import { AsyncParallelBailHook } from "tapable-ts";
4
+ import queueMicrotask from "queue-microtask";
5
+ import { omit } from "timm";
6
+ var AsyncNodePlugin = class {
7
+ constructor(options) {
8
+ this.hooks = {
9
+ onAsyncNode: new AsyncParallelBailHook()
10
+ };
11
+ this.name = "AsyncNode";
12
+ if (options?.plugins) {
13
+ this.plugins = options.plugins;
14
+ options.plugins.forEach((plugin) => {
15
+ plugin.applyPlugin(this);
16
+ });
17
+ }
18
+ }
19
+ apply(player) {
20
+ player.hooks.viewController.tap(this.name, (viewController) => {
21
+ viewController.hooks.view.tap(this.name, (view) => {
22
+ this.plugins?.forEach((plugin) => {
23
+ plugin.apply(view);
24
+ });
25
+ });
26
+ });
27
+ }
28
+ };
29
+ var AsyncNodePluginPlugin = class {
30
+ constructor() {
31
+ this.asyncNode = new AsyncParallelBailHook();
32
+ this.name = "AsyncNode";
33
+ this.resolvedMapping = /* @__PURE__ */ new Map();
34
+ }
35
+ /**
36
+ * Updates the node asynchronously based on the result provided.
37
+ * This method is responsible for handling the update logic of asynchronous nodes.
38
+ * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
39
+ * If an update is necessary, it triggers an asynchronous update on the view.
40
+ * @param node The asynchronous node that might be updated.
41
+ * @param result The result obtained from resolving the async node. This could be any data structure or value.
42
+ * @param options Options provided for node resolution, including a potential parseNode function to process the result.
43
+ * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
44
+ */
45
+ handleAsyncUpdate(node, result, options, view) {
46
+ const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
47
+ if (this.resolvedMapping.get(node.id) !== parsedNode) {
48
+ this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
49
+ view?.updateAsync();
50
+ }
51
+ }
52
+ /**
53
+ * Handles the asynchronous API integration for resolving nodes.
54
+ * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.
55
+ * @param resolver The resolver instance to attach the hook to.
56
+ * @param view
57
+ */
58
+ applyResolver(resolver) {
59
+ resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
60
+ let resolvedNode;
61
+ if (this.isAsync(node)) {
62
+ const mappedValue = this.resolvedMapping.get(node.id);
63
+ if (mappedValue) {
64
+ resolvedNode = mappedValue;
65
+ }
66
+ } else {
67
+ resolvedNode = null;
68
+ }
69
+ const newNode = resolvedNode || node;
70
+ if (!resolvedNode && node?.type === NodeType.Async) {
71
+ queueMicrotask(async () => {
72
+ const result = await this.basePlugin?.hooks.onAsyncNode.call(
73
+ node,
74
+ (result2) => {
75
+ this.handleAsyncUpdate(node, result2, options, this.currentView);
76
+ }
77
+ );
78
+ this.handleAsyncUpdate(node, result, options, this.currentView);
79
+ });
80
+ return node;
81
+ }
82
+ return newNode;
83
+ });
84
+ }
85
+ isAsync(node) {
86
+ return node?.type === NodeType.Async;
87
+ }
88
+ applyParser(parser) {
89
+ parser.hooks.determineNodeType.tap(this.name, (obj) => {
90
+ if (Object.prototype.hasOwnProperty.call(obj, "async")) {
91
+ return NodeType.Async;
92
+ }
93
+ });
94
+ parser.hooks.parseNode.tap(
95
+ this.name,
96
+ (obj, nodeType, options, determinedNodeType) => {
97
+ if (determinedNodeType === NodeType.Async) {
98
+ const parsedAsync = parser.parseObject(
99
+ omit(obj, "async"),
100
+ nodeType,
101
+ options
102
+ );
103
+ const parsedNodeId = getNodeID(parsedAsync);
104
+ if (parsedAsync !== null && parsedNodeId) {
105
+ return parser.createASTNode(
106
+ {
107
+ id: parsedNodeId,
108
+ type: NodeType.Async,
109
+ value: parsedAsync
110
+ },
111
+ obj
112
+ );
113
+ }
114
+ return null;
115
+ }
116
+ }
117
+ );
118
+ }
119
+ apply(view) {
120
+ this.currentView = view;
121
+ view.hooks.parser.tap("async", this.applyParser.bind(this));
122
+ view.hooks.resolver.tap("async", this.applyResolver.bind(this));
123
+ }
124
+ applyPlugin(asyncNodePlugin) {
125
+ this.basePlugin = asyncNodePlugin;
126
+ }
127
+ };
128
+ export {
129
+ AsyncNodePlugin,
130
+ AsyncNodePluginPlugin
131
+ };
132
+ //# sourceMappingURL=index.mjs.map
package/dist/index.mjs ADDED
@@ -0,0 +1,132 @@
1
+ // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/async-node/core/src/index.ts
2
+ import { NodeType, getNodeID } from "@player-ui/player";
3
+ import { AsyncParallelBailHook } from "tapable-ts";
4
+ import queueMicrotask from "queue-microtask";
5
+ import { omit } from "timm";
6
+ var AsyncNodePlugin = class {
7
+ constructor(options) {
8
+ this.hooks = {
9
+ onAsyncNode: new AsyncParallelBailHook()
10
+ };
11
+ this.name = "AsyncNode";
12
+ if (options?.plugins) {
13
+ this.plugins = options.plugins;
14
+ options.plugins.forEach((plugin) => {
15
+ plugin.applyPlugin(this);
16
+ });
17
+ }
18
+ }
19
+ apply(player) {
20
+ player.hooks.viewController.tap(this.name, (viewController) => {
21
+ viewController.hooks.view.tap(this.name, (view) => {
22
+ this.plugins?.forEach((plugin) => {
23
+ plugin.apply(view);
24
+ });
25
+ });
26
+ });
27
+ }
28
+ };
29
+ var AsyncNodePluginPlugin = class {
30
+ constructor() {
31
+ this.asyncNode = new AsyncParallelBailHook();
32
+ this.name = "AsyncNode";
33
+ this.resolvedMapping = /* @__PURE__ */ new Map();
34
+ }
35
+ /**
36
+ * Updates the node asynchronously based on the result provided.
37
+ * This method is responsible for handling the update logic of asynchronous nodes.
38
+ * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
39
+ * If an update is necessary, it triggers an asynchronous update on the view.
40
+ * @param node The asynchronous node that might be updated.
41
+ * @param result The result obtained from resolving the async node. This could be any data structure or value.
42
+ * @param options Options provided for node resolution, including a potential parseNode function to process the result.
43
+ * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
44
+ */
45
+ handleAsyncUpdate(node, result, options, view) {
46
+ const parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
47
+ if (this.resolvedMapping.get(node.id) !== parsedNode) {
48
+ this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
49
+ view?.updateAsync();
50
+ }
51
+ }
52
+ /**
53
+ * Handles the asynchronous API integration for resolving nodes.
54
+ * This method sets up a hook on the resolver's `beforeResolve` event to process async nodes.
55
+ * @param resolver The resolver instance to attach the hook to.
56
+ * @param view
57
+ */
58
+ applyResolver(resolver) {
59
+ resolver.hooks.beforeResolve.tap(this.name, (node, options) => {
60
+ let resolvedNode;
61
+ if (this.isAsync(node)) {
62
+ const mappedValue = this.resolvedMapping.get(node.id);
63
+ if (mappedValue) {
64
+ resolvedNode = mappedValue;
65
+ }
66
+ } else {
67
+ resolvedNode = null;
68
+ }
69
+ const newNode = resolvedNode || node;
70
+ if (!resolvedNode && node?.type === NodeType.Async) {
71
+ queueMicrotask(async () => {
72
+ const result = await this.basePlugin?.hooks.onAsyncNode.call(
73
+ node,
74
+ (result2) => {
75
+ this.handleAsyncUpdate(node, result2, options, this.currentView);
76
+ }
77
+ );
78
+ this.handleAsyncUpdate(node, result, options, this.currentView);
79
+ });
80
+ return node;
81
+ }
82
+ return newNode;
83
+ });
84
+ }
85
+ isAsync(node) {
86
+ return node?.type === NodeType.Async;
87
+ }
88
+ applyParser(parser) {
89
+ parser.hooks.determineNodeType.tap(this.name, (obj) => {
90
+ if (Object.prototype.hasOwnProperty.call(obj, "async")) {
91
+ return NodeType.Async;
92
+ }
93
+ });
94
+ parser.hooks.parseNode.tap(
95
+ this.name,
96
+ (obj, nodeType, options, determinedNodeType) => {
97
+ if (determinedNodeType === NodeType.Async) {
98
+ const parsedAsync = parser.parseObject(
99
+ omit(obj, "async"),
100
+ nodeType,
101
+ options
102
+ );
103
+ const parsedNodeId = getNodeID(parsedAsync);
104
+ if (parsedAsync !== null && parsedNodeId) {
105
+ return parser.createASTNode(
106
+ {
107
+ id: parsedNodeId,
108
+ type: NodeType.Async,
109
+ value: parsedAsync
110
+ },
111
+ obj
112
+ );
113
+ }
114
+ return null;
115
+ }
116
+ }
117
+ );
118
+ }
119
+ apply(view) {
120
+ this.currentView = view;
121
+ view.hooks.parser.tap("async", this.applyParser.bind(this));
122
+ view.hooks.resolver.tap("async", this.applyResolver.bind(this));
123
+ }
124
+ applyPlugin(asyncNodePlugin) {
125
+ this.basePlugin = asyncNodePlugin;
126
+ }
127
+ };
128
+ export {
129
+ AsyncNodePlugin,
130
+ AsyncNodePluginPlugin
131
+ };
132
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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 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 applyParser(parser: Parser) {\n parser.hooks.determineNodeType.tap(this.name, (obj) => {\n if (Object.prototype.hasOwnProperty.call(obj, \"async\")) {\n return NodeType.Async;\n }\n });\n parser.hooks.parseNode.tap(\n this.name,\n (\n obj: any,\n nodeType: Node.ChildrenTypes,\n options: ParseObjectOptions,\n determinedNodeType: null | NodeType,\n ) => {\n if (determinedNodeType === NodeType.Async) {\n const parsedAsync = parser.parseObject(\n omit(obj, \"async\"),\n nodeType,\n options,\n );\n const parsedNodeId = getNodeID(parsedAsync);\n if (parsedAsync !== null && parsedNodeId) {\n return parser.createASTNode(\n {\n id: parsedNodeId,\n type: NodeType.Async,\n value: parsedAsync,\n },\n obj,\n );\n }\n\n return null;\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;AAYpC,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,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AA,EACjC;AAAA,EAEA,YAAY,QAAgB;AAC1B,WAAO,MAAM,kBAAkB,IAAI,KAAK,MAAM,CAAC,QAAQ;AACrD,UAAI,OAAO,UAAU,eAAe,KAAK,KAAK,OAAO,GAAG;AACtD,eAAO,SAAS;AAAA,MAClB;AAAA,IACF,CAAC;AACD,WAAO,MAAM,UAAU;AAAA,MACrB,KAAK;AAAA,MACL,CACE,KACA,UACA,SACA,uBACG;AACH,YAAI,uBAAuB,SAAS,OAAO;AACzC,gBAAM,cAAc,OAAO;AAAA,YACzB,KAAK,KAAK,OAAO;AAAA,YACjB;AAAA,YACA;AAAA,UACF;AACA,gBAAM,eAAe,UAAU,WAAW;AAC1C,cAAI,gBAAgB,QAAQ,cAAc;AACxC,mBAAO,OAAO;AAAA,cACZ;AAAA,gBACE,IAAI;AAAA,gBACJ,MAAM,SAAS;AAAA,gBACf,OAAO;AAAA,cACT;AAAA,cACA;AAAA,YACF;AAAA,UACF;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"]}
package/package.json CHANGED
@@ -1,68 +1,32 @@
1
1
  {
2
2
  "name": "@player-ui/async-node-plugin",
3
- "version": "0.8.0--canary.307.9621",
4
- "private": false,
5
- "publishConfig": {
6
- "registry": "https://registry.npmjs.org"
7
- },
3
+ "version": "0.8.0--canary.410.15865",
4
+ "main": "dist/cjs/index.cjs",
8
5
  "peerDependencies": {
9
- "@player-ui/player": "0.8.0--canary.307.9621"
10
- },
11
- "dependencies": {
12
- "tapable-ts": "^0.2.3",
13
- "queue-microtask": "^1.2.3",
14
- "@babel/runtime": "7.15.4"
6
+ "@player-ui/player": "0.8.0--canary.410.15865"
15
7
  },
16
- "main": "dist/index.cjs.js",
17
- "module": "dist/index.esm.js",
18
- "typings": "dist/index.d.ts",
8
+ "module": "dist/index.legacy-esm.js",
9
+ "types": "types/index.d.ts",
10
+ "bundle": "dist/AsyncNodePlugin.native.js",
19
11
  "sideEffects": false,
20
- "license": "MIT",
21
- "repository": {
22
- "type": "git",
23
- "url": "https://github.com/player-ui/player-ui"
24
- },
25
- "bugs": {
26
- "url": "https://github.com/player-ui/player-ui/issues"
27
- },
28
- "homepage": "https://player-ui.github.io",
29
- "contributors": [
30
- {
31
- "name": "Adam Dierkens",
32
- "url": "https://github.com/adierkens"
33
- },
34
- {
35
- "name": "Spencer Hamm",
36
- "url": "https://github.com/spentacular"
37
- },
38
- {
39
- "name": "Harris Borawski",
40
- "url": "https://github.com/hborawski"
41
- },
42
- {
43
- "name": "Jeremiah Zucker",
44
- "url": "https://github.com/sugarmanz"
45
- },
46
- {
47
- "name": "Ketan Reddy",
48
- "url": "https://github.com/KetanReddy"
49
- },
50
- {
51
- "name": "Brocollie08",
52
- "url": "https://github.com/brocollie08"
53
- },
54
- {
55
- "name": "Kelly Harrop",
56
- "url": "https://github.com/kharrop"
57
- },
58
- {
59
- "name": "Alejandro Fimbres",
60
- "url": "https://github.com/lexfm"
61
- },
62
- {
63
- "name": "Rafael Campos",
64
- "url": "https://github.com/rafbcampos"
12
+ "exports": {
13
+ "./package.json": "./package.json",
14
+ "./dist/index.css": "./dist/index.css",
15
+ ".": {
16
+ "types": "./types/index.d.ts",
17
+ "import": "./dist/index.mjs",
18
+ "default": "./dist/cjs/index.cjs"
65
19
  }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "src",
24
+ "types"
66
25
  ],
67
- "bundle": "./dist/async-node-plugin.prod.js"
26
+ "dependencies": {
27
+ "queue-microtask": "^1.2.3",
28
+ "tapable-ts": "^0.2.3",
29
+ "timm": "^1.6.2",
30
+ "tslib": "^2.6.2"
31
+ }
68
32
  }