@player-ui/async-node-plugin 0.11.3--canary.670.24078 → 0.11.3--canary.666.24188

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.
@@ -7729,7 +7729,8 @@ var AsyncNodePlugin = function() {
7729
7729
  var _this = this;
7730
7730
  _class_call_check(this, AsyncNodePlugin);
7731
7731
  this.hooks = {
7732
- onAsyncNode: new AsyncParallelBailHook()
7732
+ onAsyncNode: new AsyncParallelBailHook(),
7733
+ onAsyncNodeError: new SyncBailHook()
7733
7734
  };
7734
7735
  this.name = "AsyncNode";
7735
7736
  if (options === null || options === void 0 ? void 0 : options.plugins) {
@@ -7763,10 +7764,17 @@ var AsyncNodePlugin = function() {
7763
7764
  }
7764
7765
  }
7765
7766
  _create_class(AsyncNodePlugin, [
7767
+ {
7768
+ key: "getPlayerInstance",
7769
+ value: function getPlayerInstance() {
7770
+ return this.playerInstance;
7771
+ }
7772
+ },
7766
7773
  {
7767
7774
  key: "apply",
7768
7775
  value: function apply(player) {
7769
7776
  var _this = this;
7777
+ this.playerInstance = player;
7770
7778
  player.hooks.viewController.tap(this.name, function(viewController) {
7771
7779
  viewController.hooks.view.tap(_this.name, function(view) {
7772
7780
  var _this_plugins;
@@ -7785,24 +7793,35 @@ var AsyncNodePlugin = function() {
7785
7793
  _class_call_check(this, AsyncNodePluginPlugin);
7786
7794
  this.asyncNode = new AsyncParallelBailHook();
7787
7795
  this.name = "AsyncNode";
7788
- this.resolvedMapping = /* @__PURE__ */ new Map();
7789
7796
  }
7790
7797
  _create_class(AsyncNodePluginPlugin, [
7791
7798
  {
7792
7799
  /**
7800
+ * Parses the node from the result and triggers an asynchronous view update if necessary.
7801
+ * @param node The asynchronous node that might be updated.
7802
+ * @param result The result obtained from resolving the async node. This could be any data structure or value.
7803
+ * @param options Options provided for node resolution, including a potential parseNode function to process the result.
7804
+ * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
7805
+ */ key: "parseNodeAndUpdate",
7806
+ value: function parseNodeAndUpdate(node, context, result, options, view) {
7807
+ var parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
7808
+ this.handleAsyncUpdate(node, context, parsedNode, view);
7809
+ }
7810
+ },
7811
+ {
7812
+ /**
7793
7813
  * Updates the node asynchronously based on the result provided.
7794
7814
  * This method is responsible for handling the update logic of asynchronous nodes.
7795
7815
  * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
7796
7816
  * If an update is necessary, it triggers an asynchronous update on the view.
7797
7817
  * @param node The asynchronous node that might be updated.
7798
- * @param result The result obtained from resolving the async node. This could be any data structure or value.
7799
- * @param options Options provided for node resolution, including a potential parseNode function to process the result.
7818
+ * @param newNode The new node to replace the async node.
7800
7819
  * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
7801
7820
  */ key: "handleAsyncUpdate",
7802
- value: function handleAsyncUpdate(node, result, options, view) {
7803
- var parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
7804
- if (this.resolvedMapping.get(node.id) !== parsedNode) {
7805
- this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
7821
+ value: function handleAsyncUpdate(node, context, newNode, view) {
7822
+ var nodeResolveCache = context.nodeResolveCache;
7823
+ if (nodeResolveCache.get(node.id) !== newNode) {
7824
+ nodeResolveCache.set(node.id, newNode ? newNode : node);
7806
7825
  view === null || view === void 0 ? void 0 : view.updateAsync();
7807
7826
  }
7808
7827
  }
@@ -7814,47 +7833,80 @@ var AsyncNodePlugin = function() {
7814
7833
  * @param resolver The resolver instance to attach the hook to.
7815
7834
  * @param view
7816
7835
  */ key: "applyResolver",
7817
- value: function applyResolver(resolver) {
7836
+ value: function applyResolver(resolver, context) {
7818
7837
  var _this = this;
7819
7838
  resolver.hooks.beforeResolve.tap(this.name, function(node, options) {
7820
- var resolvedNode;
7821
- if (_this.isAsync(node)) {
7822
- var mappedValue = _this.resolvedMapping.get(node.id);
7823
- if (mappedValue) {
7824
- resolvedNode = mappedValue;
7825
- }
7826
- } else {
7827
- resolvedNode = null;
7828
- }
7829
- var newNode = resolvedNode || node;
7830
- if (!resolvedNode && (node === null || node === void 0 ? void 0 : node.type) === NodeType.Async) {
7831
- var _this1 = _this;
7832
- (0, import_queue_microtask2.default)(/*#__PURE__*/ _async_to_generator(function() {
7833
- var _this_basePlugin, result;
7834
- return _ts_generator(this, function(_state) {
7835
- switch(_state.label){
7836
- case 0:
7837
- return [
7838
- 4,
7839
- (_this_basePlugin = _this1.basePlugin) === null || _this_basePlugin === void 0 ? void 0 : _this_basePlugin.hooks.onAsyncNode.call(node, function(result2) {
7840
- _this1.handleAsyncUpdate(node, result2, options, _this1.currentView);
7841
- })
7842
- ];
7843
- case 1:
7844
- result = _state.sent();
7845
- _this1.handleAsyncUpdate(node, result, options, _this1.currentView);
7846
- return [
7847
- 2
7848
- ];
7849
- }
7850
- });
7851
- }));
7839
+ if (!_this.isAsync(node)) {
7852
7840
  return node;
7853
7841
  }
7854
- return newNode;
7842
+ var resolvedNode = context.nodeResolveCache.get(node.id);
7843
+ if (resolvedNode !== void 0) {
7844
+ return resolvedNode;
7845
+ }
7846
+ (0, import_queue_microtask2.default)(function() {
7847
+ return _this.runAsyncNode(node, context, options);
7848
+ });
7849
+ return node;
7855
7850
  });
7856
7851
  }
7857
7852
  },
7853
+ {
7854
+ key: "runAsyncNode",
7855
+ value: function runAsyncNode(node, context, options) {
7856
+ var _this = this;
7857
+ return _async_to_generator(function() {
7858
+ var _this_basePlugin, result, e, _this_basePlugin1, _options_logger, error, result1, _this_basePlugin_getPlayerInstance, _this_basePlugin2, playerState;
7859
+ return _ts_generator(this, function(_state) {
7860
+ switch(_state.label){
7861
+ case 0:
7862
+ _state.trys.push([
7863
+ 0,
7864
+ 2,
7865
+ ,
7866
+ 3
7867
+ ]);
7868
+ return [
7869
+ 4,
7870
+ (_this_basePlugin = _this.basePlugin) === null || _this_basePlugin === void 0 ? void 0 : _this_basePlugin.hooks.onAsyncNode.call(node, function(result2) {
7871
+ _this.parseNodeAndUpdate(node, context, result2, options, _this.currentView);
7872
+ })
7873
+ ];
7874
+ case 1:
7875
+ result = _state.sent();
7876
+ _this.parseNodeAndUpdate(node, context, result, options, _this.currentView);
7877
+ return [
7878
+ 3,
7879
+ 3
7880
+ ];
7881
+ case 2:
7882
+ e = _state.sent();
7883
+ error = _instanceof(e, Error) ? e : new Error(String(e));
7884
+ result1 = (_this_basePlugin1 = _this.basePlugin) === null || _this_basePlugin1 === void 0 ? void 0 : _this_basePlugin1.hooks.onAsyncNodeError.call(error, node);
7885
+ if (result1 === void 0) {
7886
+ ;
7887
+ playerState = (_this_basePlugin2 = _this.basePlugin) === null || _this_basePlugin2 === void 0 ? void 0 : (_this_basePlugin_getPlayerInstance = _this_basePlugin2.getPlayerInstance()) === null || _this_basePlugin_getPlayerInstance === void 0 ? void 0 : _this_basePlugin_getPlayerInstance.getState();
7888
+ if ((playerState === null || playerState === void 0 ? void 0 : playerState.status) === "in-progress") {
7889
+ playerState.fail(error);
7890
+ }
7891
+ return [
7892
+ 2
7893
+ ];
7894
+ }
7895
+ (_options_logger = options.logger) === null || _options_logger === void 0 ? void 0 : _options_logger.error("Async node handling failed and resolved with a fallback. Error:", error);
7896
+ _this.parseNodeAndUpdate(node, context, result1, options, _this.currentView);
7897
+ return [
7898
+ 3,
7899
+ 3
7900
+ ];
7901
+ case 3:
7902
+ return [
7903
+ 2
7904
+ ];
7905
+ }
7906
+ });
7907
+ })();
7908
+ }
7909
+ },
7858
7910
  {
7859
7911
  key: "isAsync",
7860
7912
  value: function isAsync(node) {
@@ -7901,9 +7953,15 @@ var AsyncNodePlugin = function() {
7901
7953
  {
7902
7954
  key: "apply",
7903
7955
  value: function apply(view) {
7956
+ var _this = this;
7904
7957
  this.currentView = view;
7958
+ var context = {
7959
+ nodeResolveCache: /* @__PURE__ */ new Map()
7960
+ };
7905
7961
  view.hooks.parser.tap("async", this.applyParser.bind(this));
7906
- view.hooks.resolver.tap("async", this.applyResolver.bind(this));
7962
+ view.hooks.resolver.tap("async", function(resolver) {
7963
+ _this.applyResolver(resolver, context);
7964
+ });
7907
7965
  }
7908
7966
  },
7909
7967
  {