@next-core/brick-kit 2.135.0 → 2.136.1

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/index.esm.js CHANGED
@@ -1972,7 +1972,7 @@ class StoryboardContextWrapper {
1972
1972
  return (_this$data$get = this.data.get(name)) === null || _this$data$get === void 0 ? void 0 : _this$data$get.value;
1973
1973
  }
1974
1974
 
1975
- updateValue(name, value, method) {
1975
+ updateValue(name, value, method, callback) {
1976
1976
  var _item$eventTarget2;
1977
1977
 
1978
1978
  if (!this.data.has(name)) {
@@ -1995,36 +1995,58 @@ class StoryboardContextWrapper {
1995
1995
  // eslint-disable-next-line no-console
1996
1996
  console.error("Unexpected storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1997
1997
  return;
1998
- } // Todo(steve): support callback for refresh/load.
1999
-
1998
+ }
2000
1999
 
2001
2000
  if (method === "refresh" || method === "load") {
2002
2001
  if (!item.load) {
2003
2002
  throw new Error("You can not ".concat(method, " the storyboard context \"").concat(name, "\" which has no resolve."));
2004
2003
  }
2005
2004
 
2006
- if ((item.loaded || item.loading) && method === "load") {
2007
- // Do not load again.
2008
- return;
2005
+ var promise;
2006
+
2007
+ if (method === "load") {
2008
+ // Try to reuse previous request when calling `load`.
2009
+ if (item.loaded) {
2010
+ promise = Promise.resolve(item.value);
2011
+ } else if (item.loading) {
2012
+ promise = item.loading;
2013
+ }
2009
2014
  }
2010
2015
 
2011
- item.loading = true; // Defaults to ignore cache when refreshing.
2016
+ if (!promise) {
2017
+ promise = item.loading = item.load(_objectSpread({
2018
+ cache: method === "load" ? "default" : "reload"
2019
+ }, value)); // Do not use the chained promise, since the callbacks need the original promise.
2012
2020
 
2013
- item.load(_objectSpread({
2014
- cache: method === "load" ? "default" : "reload"
2015
- }, value)).then(val => {
2016
- var _item$eventTarget;
2021
+ promise.then(val => {
2022
+ var _item$eventTarget;
2023
+
2024
+ item.loaded = true;
2025
+ item.value = val;
2026
+ (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2027
+ detail: item.value
2028
+ }));
2029
+ }, err => {
2030
+ // Let users to override error handling.
2031
+ if (!(callback !== null && callback !== void 0 && callback.error)) {
2032
+ handleHttpError(err);
2033
+ }
2034
+ });
2035
+ }
2036
+
2037
+ if (callback) {
2038
+ var callbackFactory = eventCallbackFactory(callback, () => this.getResolveOptions(_internalApiGetCurrentContext()).mergedContext, null);
2039
+ promise.then(val => {
2040
+ callbackFactory("success")({
2041
+ value: val
2042
+ });
2043
+ callbackFactory("finally")();
2044
+ }, err => {
2045
+ callbackFactory("error")(err);
2046
+ callbackFactory("finally")();
2047
+ });
2048
+ }
2017
2049
 
2018
- item.loading = false;
2019
- item.loaded = true;
2020
- item.value = val;
2021
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2022
- detail: item.value
2023
- }));
2024
- }, err => {
2025
- item.loading = false;
2026
- handleHttpError(err);
2027
- });
2028
2050
  return;
2029
2051
  }
2030
2052
 
@@ -6311,12 +6333,12 @@ function listenerFactory(handler, context, runtimeBrick) {
6311
6333
  case "context.replace":
6312
6334
  case "context.refresh":
6313
6335
  case "context.load":
6314
- return builtinContextListenerFactory(method, handler.args, handler, context);
6336
+ return builtinContextListenerFactory(method, handler.args, handler, handler.callback, context);
6315
6337
 
6316
6338
  case "state.update":
6317
6339
  case "state.refresh":
6318
6340
  case "state.load":
6319
- return builtinStateListenerFactory(method, handler.args, handler, context);
6341
+ return builtinStateListenerFactory(method, handler.args, handler, handler.callback, context);
6320
6342
 
6321
6343
  case "tpl.dispatchEvent":
6322
6344
  return builtinTplDispatchEventFactory(handler.args, handler, context);
@@ -6445,7 +6467,7 @@ function builtinTplDispatchEventFactory(args, ifContainer, context) {
6445
6467
  };
6446
6468
  }
6447
6469
 
6448
- function builtinContextListenerFactory(method, args, ifContainer, context) {
6470
+ function builtinContextListenerFactory(method, args, ifContainer, callback, context) {
6449
6471
  return function (event) {
6450
6472
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6451
6473
  event
@@ -6456,11 +6478,11 @@ function builtinContextListenerFactory(method, args, ifContainer, context) {
6456
6478
  var storyboardContext = _internalApiGetStoryboardContextWrapper();
6457
6479
 
6458
6480
  var [name, value] = argsFactory(args, context, event);
6459
- storyboardContext.updateValue(name, value, method);
6481
+ storyboardContext.updateValue(name, value, method, callback);
6460
6482
  };
6461
6483
  }
6462
6484
 
6463
- function builtinStateListenerFactory(method, args, ifContainer, context) {
6485
+ function builtinStateListenerFactory(method, args, ifContainer, callback, context) {
6464
6486
  return function (event) {
6465
6487
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6466
6488
  event
@@ -6470,7 +6492,7 @@ function builtinStateListenerFactory(method, args, ifContainer, context) {
6470
6492
 
6471
6493
  var tplContext = getTplContext(context.tplContextId);
6472
6494
  var [name, value] = argsFactory(args, context, event);
6473
- tplContext.state.updateValue(name, value, method === "update" ? "replace" : method);
6495
+ tplContext.state.updateValue(name, value, method === "update" ? "replace" : method, callback);
6474
6496
  };
6475
6497
  }
6476
6498
 
@@ -6671,13 +6693,39 @@ function customListenerFactory(handler, ifContainer, context, runtimeBrick) {
6671
6693
  };
6672
6694
  }
6673
6695
 
6696
+ function eventCallbackFactory(callback, getContext, runtimeBrick) {
6697
+ return function callbackFactory(type) {
6698
+ return function (result) {
6699
+ if (callback !== null && callback !== void 0 && callback[type]) {
6700
+ try {
6701
+ var event = new CustomEvent("callback.".concat(type), {
6702
+ detail: result
6703
+ });
6704
+ var context = getContext();
6705
+ [].concat(callback[type]).forEach(eachHandler => {
6706
+ listenerFactory(eachHandler, context, runtimeBrick)(event);
6707
+ });
6708
+ } catch (err) {
6709
+ // Do not throw errors in `callback.success` or `callback.progress`,
6710
+ // to avoid the following triggering of `callback.error`.
6711
+ // eslint-disable-next-line
6712
+ console.error(err);
6713
+ }
6714
+ } else if (type === "error") {
6715
+ // eslint-disable-next-line
6716
+ console.error("Unhandled callback error:", result);
6717
+ }
6718
+ };
6719
+ };
6720
+ }
6721
+
6674
6722
  function brickCallback(_x2, _x3, _x4, _x5, _x6, _x7, _x8) {
6675
6723
  return _brickCallback.apply(this, arguments);
6676
6724
  }
6677
6725
 
6678
6726
  function _brickCallback() {
6679
6727
  _brickCallback = _asyncToGenerator$3(function* (target, handler, method, context, runtimeBrick, event, options) {
6680
- var _handler$callback, _poll;
6728
+ var _poll;
6681
6729
 
6682
6730
  if (typeof target[method] !== "function") {
6683
6731
  // eslint-disable-next-line no-console
@@ -6704,45 +6752,17 @@ function _brickCallback() {
6704
6752
  };
6705
6753
  }();
6706
6754
 
6707
- var {
6708
- success,
6709
- error,
6710
- finally: finallyHook,
6711
- progress
6712
- } = (_handler$callback = handler.callback) !== null && _handler$callback !== void 0 ? _handler$callback : {};
6713
-
6714
- if (!(success || error || finallyHook || progress)) {
6755
+ if (!handler.callback) {
6715
6756
  task();
6716
6757
  return;
6717
6758
  }
6718
6759
 
6719
- var callbackFactory = (eventType, specificHandler) => result => {
6720
- if (specificHandler) {
6721
- try {
6722
- var _event = new CustomEvent(eventType, {
6723
- detail: result
6724
- });
6725
-
6726
- [].concat(specificHandler).forEach(eachHandler => {
6727
- listenerFactory(eachHandler, context, runtimeBrick)(_event);
6728
- });
6729
- } catch (err) {
6730
- // Do not throw errors in `callback.success` or `callback.progress`,
6731
- // to avoid the following triggering of `callback.error`.
6732
- // eslint-disable-next-line
6733
- console.error(err);
6734
- }
6735
- } else if (eventType === "callback.error") {
6736
- // eslint-disable-next-line
6737
- console.error("Unhandled callback error:", result);
6738
- }
6739
- };
6740
-
6760
+ var callbackFactory = eventCallbackFactory(handler.callback, () => context, runtimeBrick);
6741
6761
  var pollableCallback = {
6742
- progress: callbackFactory("callback.progress", progress),
6743
- success: callbackFactory("callback.success", success),
6744
- error: callbackFactory("callback.error", error),
6745
- finally: callbackFactory("callback.finally", finallyHook)
6762
+ progress: callbackFactory("progress"),
6763
+ success: callbackFactory("success"),
6764
+ error: callbackFactory("error"),
6765
+ finally: callbackFactory("finally")
6746
6766
  };
6747
6767
  var poll;
6748
6768