@player-ui/async-node-plugin 0.11.3--canary.642.23780 → 0.11.3--canary.666.24070

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.
@@ -5596,6 +5596,178 @@ var AsyncNodePlugin = function() {
5596
5596
  ]);
5597
5597
  return AssetPlugin;
5598
5598
  }();
5599
+ var LocalStateStore = /*#__PURE__*/ function() {
5600
+ function LocalStateStore(onUpdate) {
5601
+ _class_call_check(this, LocalStateStore);
5602
+ this.updateCallback = onUpdate;
5603
+ this.state = /* @__PURE__ */ new Map();
5604
+ }
5605
+ _create_class(LocalStateStore, [
5606
+ {
5607
+ key: "removeKey",
5608
+ value: function removeKey(key) {
5609
+ this.state.delete(key);
5610
+ }
5611
+ },
5612
+ {
5613
+ key: "reset",
5614
+ value: function reset() {
5615
+ this.state.clear();
5616
+ }
5617
+ },
5618
+ {
5619
+ key: "useSharedState",
5620
+ value: function useSharedState(key) {
5621
+ var _this = this;
5622
+ return function(initialState) {
5623
+ if (!_this.state.has(key)) {
5624
+ _this.state.set(key, initialState);
5625
+ }
5626
+ return [
5627
+ _this.state.get(key),
5628
+ function(newState) {
5629
+ var current = _this.state.get(key);
5630
+ _this.state.set(key, newState);
5631
+ if (current !== newState) {
5632
+ var _this_updateCallback, _this1;
5633
+ (_this_updateCallback = (_this1 = _this).updateCallback) === null || _this_updateCallback === void 0 ? void 0 : _this_updateCallback.call(_this1);
5634
+ }
5635
+ }
5636
+ ];
5637
+ };
5638
+ }
5639
+ },
5640
+ {
5641
+ key: "getLocalStateFunction",
5642
+ value: function getLocalStateFunction(key, countKey) {
5643
+ var _this = this;
5644
+ return function(initialState) {
5645
+ if (!_this.state.has(key)) {
5646
+ _this.state.set(key, []);
5647
+ }
5648
+ if (!_this.state.has(countKey)) {
5649
+ _this.state.set(countKey, 0);
5650
+ }
5651
+ var localState = _this.state.get(key);
5652
+ var oldCount = _this.state.get(countKey);
5653
+ _this.state.set(countKey, oldCount + 1);
5654
+ if (localState.length <= oldCount) {
5655
+ localState.push(initialState);
5656
+ }
5657
+ var value = localState[oldCount];
5658
+ return [
5659
+ value,
5660
+ function(newState) {
5661
+ var oldValue = localState[oldCount];
5662
+ localState[oldCount] = newState;
5663
+ if (oldValue !== newState) {
5664
+ var _this_updateCallback, _this1;
5665
+ (_this_updateCallback = (_this1 = _this).updateCallback) === null || _this_updateCallback === void 0 ? void 0 : _this_updateCallback.call(_this1);
5666
+ }
5667
+ }
5668
+ ];
5669
+ };
5670
+ }
5671
+ }
5672
+ ]);
5673
+ return LocalStateStore;
5674
+ }();
5675
+ function findUp(node, target) {
5676
+ if (node === target) {
5677
+ return true;
5678
+ }
5679
+ if (node.parent) {
5680
+ return findUp(node.parent, target);
5681
+ }
5682
+ return false;
5683
+ }
5684
+ var AssetTransformCorePlugin = /*#__PURE__*/ function() {
5685
+ function AssetTransformCorePlugin(registry) {
5686
+ _class_call_check(this, AssetTransformCorePlugin);
5687
+ this.registry = registry;
5688
+ this.stateStore = /* @__PURE__ */ new Map();
5689
+ this.beforeResolveSymbol = Symbol("before resolve");
5690
+ this.resolveSymbol = Symbol("resolve");
5691
+ this.beforeResolveCountSymbol = Symbol("before resolve count");
5692
+ this.resolveCountSymbol = Symbol("resolve count");
5693
+ }
5694
+ _create_class(AssetTransformCorePlugin, [
5695
+ {
5696
+ key: "apply",
5697
+ value: function apply(view) {
5698
+ var _this = this;
5699
+ this.stateStore.clear();
5700
+ view.hooks.resolver.tap("asset-transform", function(resolver) {
5701
+ var lastUpdatedNode;
5702
+ var updateState = function(node) {
5703
+ lastUpdatedNode = node;
5704
+ view.update(/* @__PURE__ */ new Set());
5705
+ };
5706
+ var getStore = function(node, stepKey) {
5707
+ var store;
5708
+ var countKey = stepKey === _this.resolveSymbol ? _this.resolveCountSymbol : _this.beforeResolveCountSymbol;
5709
+ var storedState = _this.stateStore.get(node);
5710
+ if (storedState) {
5711
+ store = storedState;
5712
+ store.removeKey(countKey);
5713
+ } else {
5714
+ store = new LocalStateStore(function() {
5715
+ updateState(node);
5716
+ });
5717
+ _this.stateStore.set(node, store);
5718
+ }
5719
+ return {
5720
+ useSharedState: function(key) {
5721
+ return store.useSharedState(key);
5722
+ },
5723
+ useLocalState: function(initialState) {
5724
+ return store.getLocalStateFunction(stepKey, countKey)(initialState);
5725
+ }
5726
+ };
5727
+ };
5728
+ resolver.hooks.beforeResolve.tap("asset-transform", function(node, options) {
5729
+ if (node && (node.type === "asset" || node.type === "view")) {
5730
+ var transform = _this.registry.get(node.value);
5731
+ if (transform === null || transform === void 0 ? void 0 : transform.beforeResolve) {
5732
+ var _options_node;
5733
+ var store = getStore((_options_node = options.node) !== null && _options_node !== void 0 ? _options_node : node, _this.beforeResolveSymbol);
5734
+ return transform.beforeResolve(node, options, store);
5735
+ }
5736
+ }
5737
+ return node;
5738
+ });
5739
+ resolver.hooks.afterUpdate.tap("asset-transform", function() {
5740
+ lastUpdatedNode = void 0;
5741
+ });
5742
+ resolver.hooks.skipResolve.tap("asset-transform", function(skip, node) {
5743
+ if (!skip || !lastUpdatedNode) {
5744
+ return skip;
5745
+ }
5746
+ var isParentOfUpdated = findUp(lastUpdatedNode, node);
5747
+ var isChildOfUpdated = findUp(node, lastUpdatedNode);
5748
+ return !isParentOfUpdated && !isChildOfUpdated;
5749
+ });
5750
+ resolver.hooks.afterResolve.tap("asset-transform", function(value, node, options) {
5751
+ if (node.type !== "asset" && node.type !== "view") {
5752
+ return value;
5753
+ }
5754
+ var originalNode = resolver.getSourceNode(node);
5755
+ if (!originalNode) {
5756
+ return value;
5757
+ }
5758
+ var transform = _this.registry.get(value);
5759
+ if (transform === null || transform === void 0 ? void 0 : transform.resolve) {
5760
+ var store = getStore(originalNode, _this.resolveSymbol);
5761
+ return transform === null || transform === void 0 ? void 0 : transform.resolve(value, options, store);
5762
+ }
5763
+ return value;
5764
+ });
5765
+ });
5766
+ }
5767
+ }
5768
+ ]);
5769
+ return AssetTransformCorePlugin;
5770
+ }();
5599
5771
  var FlowInstance = /*#__PURE__*/ function() {
5600
5772
  function FlowInstance(id, flow, options) {
5601
5773
  _class_call_check(this, FlowInstance);
@@ -6625,180 +6797,6 @@ var AsyncNodePlugin = function() {
6625
6797
  ]);
6626
6798
  return ValidationController;
6627
6799
  }();
6628
- var LocalStateStore = /*#__PURE__*/ function() {
6629
- function LocalStateStore(onUpdate) {
6630
- _class_call_check(this, LocalStateStore);
6631
- this.updateCallback = onUpdate;
6632
- this.state = /* @__PURE__ */ new Map();
6633
- }
6634
- _create_class(LocalStateStore, [
6635
- {
6636
- key: "removeKey",
6637
- value: function removeKey(key) {
6638
- this.state.delete(key);
6639
- }
6640
- },
6641
- {
6642
- key: "reset",
6643
- value: function reset() {
6644
- this.state.clear();
6645
- }
6646
- },
6647
- {
6648
- key: "useSharedState",
6649
- value: function useSharedState(key) {
6650
- var _this = this;
6651
- return function(initialState) {
6652
- if (!_this.state.has(key)) {
6653
- _this.state.set(key, initialState);
6654
- }
6655
- return [
6656
- _this.state.get(key),
6657
- function(newState) {
6658
- var current = _this.state.get(key);
6659
- _this.state.set(key, newState);
6660
- if (current !== newState) {
6661
- var _this_updateCallback, _this1;
6662
- (_this_updateCallback = (_this1 = _this).updateCallback) === null || _this_updateCallback === void 0 ? void 0 : _this_updateCallback.call(_this1);
6663
- }
6664
- }
6665
- ];
6666
- };
6667
- }
6668
- },
6669
- {
6670
- key: "getLocalStateFunction",
6671
- value: function getLocalStateFunction(key, countKey) {
6672
- var _this = this;
6673
- return function(initialState) {
6674
- if (!_this.state.has(key)) {
6675
- _this.state.set(key, []);
6676
- }
6677
- if (!_this.state.has(countKey)) {
6678
- _this.state.set(countKey, 0);
6679
- }
6680
- var localState = _this.state.get(key);
6681
- var oldCount = _this.state.get(countKey);
6682
- _this.state.set(countKey, oldCount + 1);
6683
- if (localState.length <= oldCount) {
6684
- localState.push(initialState);
6685
- }
6686
- var value = localState[oldCount];
6687
- return [
6688
- value,
6689
- function(newState) {
6690
- var oldValue = localState[oldCount];
6691
- localState[oldCount] = newState;
6692
- if (oldValue !== newState) {
6693
- var _this_updateCallback, _this1;
6694
- (_this_updateCallback = (_this1 = _this).updateCallback) === null || _this_updateCallback === void 0 ? void 0 : _this_updateCallback.call(_this1);
6695
- }
6696
- }
6697
- ];
6698
- };
6699
- }
6700
- }
6701
- ]);
6702
- return LocalStateStore;
6703
- }();
6704
- function findUp(node, target) {
6705
- if (node === target) {
6706
- return true;
6707
- }
6708
- if (node.parent) {
6709
- return findUp(node.parent, target);
6710
- }
6711
- return false;
6712
- }
6713
- var AssetTransformCorePlugin = /*#__PURE__*/ function() {
6714
- function AssetTransformCorePlugin(registry) {
6715
- _class_call_check(this, AssetTransformCorePlugin);
6716
- this.registry = registry;
6717
- this.stateStore = /* @__PURE__ */ new Map();
6718
- this.beforeResolveSymbol = Symbol("before resolve");
6719
- this.resolveSymbol = Symbol("resolve");
6720
- this.beforeResolveCountSymbol = Symbol("before resolve count");
6721
- this.resolveCountSymbol = Symbol("resolve count");
6722
- }
6723
- _create_class(AssetTransformCorePlugin, [
6724
- {
6725
- key: "apply",
6726
- value: function apply(viewController) {
6727
- var _this = this;
6728
- viewController.hooks.view.tap("asset-transform", function(view) {
6729
- _this.stateStore.clear();
6730
- view.hooks.resolver.tap("asset-transform", function(resolver) {
6731
- var lastUpdatedNode;
6732
- var updateState = function(node) {
6733
- lastUpdatedNode = node;
6734
- view.update(/* @__PURE__ */ new Set());
6735
- };
6736
- var getStore = function(node, stepKey) {
6737
- var store;
6738
- var countKey = stepKey === _this.resolveSymbol ? _this.resolveCountSymbol : _this.beforeResolveCountSymbol;
6739
- var storedState = _this.stateStore.get(node);
6740
- if (storedState) {
6741
- store = storedState;
6742
- store.removeKey(countKey);
6743
- } else {
6744
- store = new LocalStateStore(function() {
6745
- updateState(node);
6746
- });
6747
- _this.stateStore.set(node, store);
6748
- }
6749
- return {
6750
- useSharedState: function(key) {
6751
- return store.useSharedState(key);
6752
- },
6753
- useLocalState: function(initialState) {
6754
- return store.getLocalStateFunction(stepKey, countKey)(initialState);
6755
- }
6756
- };
6757
- };
6758
- resolver.hooks.beforeResolve.tap("asset-transform", function(node, options) {
6759
- if (node && (node.type === "asset" || node.type === "view")) {
6760
- var transform = _this.registry.get(node.value);
6761
- if (transform === null || transform === void 0 ? void 0 : transform.beforeResolve) {
6762
- var _options_node;
6763
- var store = getStore((_options_node = options.node) !== null && _options_node !== void 0 ? _options_node : node, _this.beforeResolveSymbol);
6764
- return transform.beforeResolve(node, options, store);
6765
- }
6766
- }
6767
- return node;
6768
- });
6769
- resolver.hooks.afterUpdate.tap("asset-transform", function() {
6770
- lastUpdatedNode = void 0;
6771
- });
6772
- resolver.hooks.skipResolve.tap("asset-transform", function(skip, node) {
6773
- if (!skip || !lastUpdatedNode) {
6774
- return skip;
6775
- }
6776
- var isParentOfUpdated = findUp(lastUpdatedNode, node);
6777
- var isChildOfUpdated = findUp(node, lastUpdatedNode);
6778
- return !isParentOfUpdated && !isChildOfUpdated;
6779
- });
6780
- resolver.hooks.afterResolve.tap("asset-transform", function(value, node, options) {
6781
- if (node.type !== "asset" && node.type !== "view") {
6782
- return value;
6783
- }
6784
- var originalNode = resolver.getSourceNode(node);
6785
- if (!originalNode) {
6786
- return value;
6787
- }
6788
- var transform = _this.registry.get(value);
6789
- if (transform === null || transform === void 0 ? void 0 : transform.resolve) {
6790
- var store = getStore(originalNode, _this.resolveSymbol);
6791
- return transform === null || transform === void 0 ? void 0 : transform.resolve(value, options, store);
6792
- }
6793
- return value;
6794
- });
6795
- });
6796
- });
6797
- }
6798
- }
6799
- ]);
6800
- return AssetTransformCorePlugin;
6801
- }();
6802
6800
  var ViewController = /*#__PURE__*/ function() {
6803
6801
  function ViewController(initialViews, options) {
6804
6802
  var _this = this;
@@ -6816,7 +6814,6 @@ var AsyncNodePlugin = function() {
6816
6814
  viewMap[view.id] = view;
6817
6815
  return viewMap;
6818
6816
  }, {});
6819
- new AssetTransformCorePlugin(this.transformRegistry).apply(this);
6820
6817
  options.flowController.hooks.flow.tap("viewController", function(flow) {
6821
6818
  flow.hooks.transition.tap("viewController", function(_oldState, newState) {
6822
6819
  if (newState.value.state_type === "VIEW") {
@@ -7301,6 +7298,7 @@ var AsyncNodePlugin = function() {
7301
7298
  new AssetPlugin().apply(view);
7302
7299
  new SwitchPlugin(pluginOptions).apply(view);
7303
7300
  new ApplicabilityPlugin().apply(view);
7301
+ new AssetTransformCorePlugin(viewController.transformRegistry).apply(view);
7304
7302
  new StringResolverPlugin().apply(view);
7305
7303
  var templatePlugin = new TemplatePlugin(pluginOptions);
7306
7304
  templatePlugin.apply(view);
@@ -7731,7 +7729,8 @@ var AsyncNodePlugin = function() {
7731
7729
  var _this = this;
7732
7730
  _class_call_check(this, AsyncNodePlugin);
7733
7731
  this.hooks = {
7734
- onAsyncNode: new AsyncParallelBailHook()
7732
+ onAsyncNode: new AsyncParallelBailHook(),
7733
+ onAsyncNodeError: new SyncBailHook()
7735
7734
  };
7736
7735
  this.name = "AsyncNode";
7737
7736
  if (options === null || options === void 0 ? void 0 : options.plugins) {
@@ -7765,10 +7764,17 @@ var AsyncNodePlugin = function() {
7765
7764
  }
7766
7765
  }
7767
7766
  _create_class(AsyncNodePlugin, [
7767
+ {
7768
+ key: "getPlayerInstance",
7769
+ value: function getPlayerInstance() {
7770
+ return this.playerInstance;
7771
+ }
7772
+ },
7768
7773
  {
7769
7774
  key: "apply",
7770
7775
  value: function apply(player) {
7771
7776
  var _this = this;
7777
+ this.playerInstance = player;
7772
7778
  player.hooks.viewController.tap(this.name, function(viewController) {
7773
7779
  viewController.hooks.view.tap(_this.name, function(view) {
7774
7780
  var _this_plugins;
@@ -7792,19 +7798,30 @@ var AsyncNodePlugin = function() {
7792
7798
  _create_class(AsyncNodePluginPlugin, [
7793
7799
  {
7794
7800
  /**
7801
+ * Parses the node from the result and triggers an asynchronous view update if necessary.
7802
+ * @param node The asynchronous node that might be updated.
7803
+ * @param result The result obtained from resolving the async node. This could be any data structure or value.
7804
+ * @param options Options provided for node resolution, including a potential parseNode function to process the result.
7805
+ * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
7806
+ */ key: "parseNodeAndUpdate",
7807
+ value: function parseNodeAndUpdate(node, result, options, view) {
7808
+ var parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
7809
+ this.handleAsyncUpdate(node, parsedNode, view);
7810
+ }
7811
+ },
7812
+ {
7813
+ /**
7795
7814
  * Updates the node asynchronously based on the result provided.
7796
7815
  * This method is responsible for handling the update logic of asynchronous nodes.
7797
7816
  * It checks if the node needs to be updated based on the new result and updates the mapping accordingly.
7798
7817
  * If an update is necessary, it triggers an asynchronous update on the view.
7799
7818
  * @param node The asynchronous node that might be updated.
7800
- * @param result The result obtained from resolving the async node. This could be any data structure or value.
7801
- * @param options Options provided for node resolution, including a potential parseNode function to process the result.
7819
+ * @param newNode The new node to replace the async node.
7802
7820
  * @param view The view instance where the node resides. This can be undefined if the view is not currently active.
7803
7821
  */ key: "handleAsyncUpdate",
7804
- value: function handleAsyncUpdate(node, result, options, view) {
7805
- var parsedNode = options.parseNode && result ? options.parseNode(result) : void 0;
7806
- if (this.resolvedMapping.get(node.id) !== parsedNode) {
7807
- this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
7822
+ value: function handleAsyncUpdate(node, newNode, view) {
7823
+ if (this.resolvedMapping.get(node.id) !== newNode) {
7824
+ this.resolvedMapping.set(node.id, newNode ? newNode : node);
7808
7825
  view === null || view === void 0 ? void 0 : view.updateAsync();
7809
7826
  }
7810
7827
  }
@@ -7819,44 +7836,77 @@ var AsyncNodePlugin = function() {
7819
7836
  value: function applyResolver(resolver) {
7820
7837
  var _this = this;
7821
7838
  resolver.hooks.beforeResolve.tap(this.name, function(node, options) {
7822
- var resolvedNode;
7823
- if (_this.isAsync(node)) {
7824
- var mappedValue = _this.resolvedMapping.get(node.id);
7825
- if (mappedValue) {
7826
- resolvedNode = mappedValue;
7827
- }
7828
- } else {
7829
- resolvedNode = null;
7830
- }
7831
- var newNode = resolvedNode || node;
7832
- if (!resolvedNode && (node === null || node === void 0 ? void 0 : node.type) === NodeType.Async) {
7833
- var _this1 = _this;
7834
- (0, import_queue_microtask2.default)(/*#__PURE__*/ _async_to_generator(function() {
7835
- var _this_basePlugin, result;
7836
- return _ts_generator(this, function(_state) {
7837
- switch(_state.label){
7838
- case 0:
7839
- return [
7840
- 4,
7841
- (_this_basePlugin = _this1.basePlugin) === null || _this_basePlugin === void 0 ? void 0 : _this_basePlugin.hooks.onAsyncNode.call(node, function(result2) {
7842
- _this1.handleAsyncUpdate(node, result2, options, _this1.currentView);
7843
- })
7844
- ];
7845
- case 1:
7846
- result = _state.sent();
7847
- _this1.handleAsyncUpdate(node, result, options, _this1.currentView);
7848
- return [
7849
- 2
7850
- ];
7851
- }
7852
- });
7853
- }));
7839
+ if (!_this.isAsync(node)) {
7854
7840
  return node;
7855
7841
  }
7856
- return newNode;
7842
+ var resolvedNode = _this.resolvedMapping.get(node.id);
7843
+ if (resolvedNode !== void 0) {
7844
+ return resolvedNode;
7845
+ }
7846
+ (0, import_queue_microtask2.default)(function() {
7847
+ return _this.runAsyncNode(node, options);
7848
+ });
7849
+ return node;
7857
7850
  });
7858
7851
  }
7859
7852
  },
7853
+ {
7854
+ key: "runAsyncNode",
7855
+ value: function runAsyncNode(node, 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, result2, options, _this.currentView);
7872
+ })
7873
+ ];
7874
+ case 1:
7875
+ result = _state.sent();
7876
+ _this.parseNodeAndUpdate(node, 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, result1, options, _this.currentView);
7897
+ return [
7898
+ 3,
7899
+ 3
7900
+ ];
7901
+ case 3:
7902
+ return [
7903
+ 2
7904
+ ];
7905
+ }
7906
+ });
7907
+ })();
7908
+ }
7909
+ },
7860
7910
  {
7861
7911
  key: "isAsync",
7862
7912
  value: function isAsync(node) {