@next-core/brick-kit 2.134.2 → 2.136.0

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)) {
@@ -1993,23 +1993,60 @@ class StoryboardContextWrapper {
1993
1993
 
1994
1994
  if (item.type !== "free-variable") {
1995
1995
  // eslint-disable-next-line no-console
1996
- console.error("Conflict storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1996
+ console.error("Unexpected storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1997
1997
  return;
1998
1998
  }
1999
1999
 
2000
- if (method === "refresh") {
2001
- if (!item.refresh) {
2002
- throw new Error("You can not refresh the storyboard context \"".concat(name, "\" which has no resolve."));
2000
+ if (method === "refresh" || method === "load") {
2001
+ if (!item.load) {
2002
+ throw new Error("You can not ".concat(method, " the storyboard context \"").concat(name, "\" which has no resolve."));
2003
2003
  }
2004
2004
 
2005
- item.refresh().then(val => {
2006
- var _item$eventTarget;
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
+ }
2014
+ }
2015
+
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.
2020
+
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);
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
+ }
2007
2049
 
2008
- item.value = val;
2009
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2010
- detail: item.value
2011
- }));
2012
- }, handleHttpError);
2013
2050
  return;
2014
2051
  }
2015
2052
 
@@ -2108,23 +2145,23 @@ function _resolveNormalStoryboardContext() {
2108
2145
 
2109
2146
  var isTemplateState = !!storyboardContextWrapper.tplContextId;
2110
2147
  var value = getDefinedTemplateState(isTemplateState, contextConf, brick);
2111
- var refresh = null;
2148
+ var load = null;
2112
2149
  var isLazyResolve = false;
2113
2150
 
2114
2151
  if (value === undefined) {
2115
2152
  if (contextConf.resolve) {
2116
2153
  if (looseCheckIf(contextConf.resolve, mergedContext)) {
2117
- refresh = /*#__PURE__*/function () {
2118
- var _ref = _asyncToGenerator$3(function* () {
2154
+ load = /*#__PURE__*/function () {
2155
+ var _ref = _asyncToGenerator$3(function* (options) {
2119
2156
  var valueConf = {};
2120
2157
  yield _internalApiGetResolver().resolveOne("reference", _objectSpread({
2121
2158
  transform: "value",
2122
2159
  transformMapArray: false
2123
- }, contextConf.resolve), valueConf, null, mergedContext);
2160
+ }, contextConf.resolve), valueConf, null, mergedContext, options);
2124
2161
  return valueConf.value;
2125
2162
  });
2126
2163
 
2127
- return function refresh() {
2164
+ return function load(_x9) {
2128
2165
  return _ref.apply(this, arguments);
2129
2166
  };
2130
2167
  }();
@@ -2132,14 +2169,14 @@ function _resolveNormalStoryboardContext() {
2132
2169
  isLazyResolve = contextConf.resolve.lazy;
2133
2170
 
2134
2171
  if (!isLazyResolve) {
2135
- value = yield refresh();
2172
+ value = yield load();
2136
2173
  }
2137
2174
  } else if (!hasOwnProperty(contextConf, "value")) {
2138
2175
  return false;
2139
2176
  }
2140
2177
  }
2141
2178
 
2142
- if ((!refresh || isLazyResolve) && contextConf.value !== undefined) {
2179
+ if ((!load || isLazyResolve) && contextConf.value !== undefined) {
2143
2180
  // If the context has no resolve, just use its `value`.
2144
2181
  // Or if the resolve is ignored or lazy, use its `value` as a fallback.
2145
2182
  value = computeRealValue(contextConf.value, mergedContext, true);
@@ -2147,15 +2184,17 @@ function _resolveNormalStoryboardContext() {
2147
2184
 
2148
2185
  if (contextConf.track) {
2149
2186
  // Track its dependencies and auto update when each of them changed.
2150
- var deps = (isTemplateState ? trackUsedState : trackUsedContext)(refresh ? contextConf.resolve : contextConf.value);
2187
+ var deps = (isTemplateState ? trackUsedState : trackUsedContext)(load ? contextConf.resolve : contextConf.value);
2151
2188
 
2152
2189
  for (var dep of deps) {
2153
2190
  var _eventTarget;
2154
2191
 
2155
2192
  var ctx = storyboardContextWrapper.get().get(dep);
2156
2193
  ctx === null || ctx === void 0 ? void 0 : (_eventTarget = ctx.eventTarget) === null || _eventTarget === void 0 ? void 0 : _eventTarget.addEventListener(isTemplateState ? "state.change" : "context.change", () => {
2157
- if (refresh) {
2158
- storyboardContextWrapper.updateValue(contextConf.name, undefined, "refresh");
2194
+ if (load) {
2195
+ storyboardContextWrapper.updateValue(contextConf.name, {
2196
+ cache: "default"
2197
+ }, "refresh");
2159
2198
  } else {
2160
2199
  storyboardContextWrapper.updateValue(contextConf.name, computeRealValue(contextConf.value, mergedContext, true), "replace");
2161
2200
  }
@@ -2164,7 +2203,7 @@ function _resolveNormalStoryboardContext() {
2164
2203
  }
2165
2204
  }
2166
2205
 
2167
- resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh);
2206
+ resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, load, !isLazyResolve);
2168
2207
  return true;
2169
2208
  });
2170
2209
  return _resolveNormalStoryboardContext.apply(this, arguments);
@@ -2195,13 +2234,14 @@ function getDefinedTemplateState(isTemplateState, contextConf, brick) {
2195
2234
  }
2196
2235
  }
2197
2236
 
2198
- function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh) {
2237
+ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, load, loaded) {
2199
2238
  var newContext = {
2200
2239
  type: "free-variable",
2201
2240
  value,
2202
2241
  // This is required for tracking context, even if no `onChange` is specified.
2203
2242
  eventTarget: new EventTarget$1(),
2204
- refresh
2243
+ load,
2244
+ loaded
2205
2245
  };
2206
2246
 
2207
2247
  if (contextConf.onChange) {
@@ -6292,11 +6332,13 @@ function listenerFactory(handler, context, runtimeBrick) {
6292
6332
  case "context.assign":
6293
6333
  case "context.replace":
6294
6334
  case "context.refresh":
6295
- return builtinContextListenerFactory(method, handler.args, handler, context);
6335
+ case "context.load":
6336
+ return builtinContextListenerFactory(method, handler.args, handler, handler.callback, context);
6296
6337
 
6297
6338
  case "state.update":
6298
6339
  case "state.refresh":
6299
- return builtinStateListenerFactory(method, handler.args, handler, context);
6340
+ case "state.load":
6341
+ return builtinStateListenerFactory(method, handler.args, handler, handler.callback, context);
6300
6342
 
6301
6343
  case "tpl.dispatchEvent":
6302
6344
  return builtinTplDispatchEventFactory(handler.args, handler, context);
@@ -6425,7 +6467,7 @@ function builtinTplDispatchEventFactory(args, ifContainer, context) {
6425
6467
  };
6426
6468
  }
6427
6469
 
6428
- function builtinContextListenerFactory(method, args, ifContainer, context) {
6470
+ function builtinContextListenerFactory(method, args, ifContainer, callback, context) {
6429
6471
  return function (event) {
6430
6472
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6431
6473
  event
@@ -6436,11 +6478,11 @@ function builtinContextListenerFactory(method, args, ifContainer, context) {
6436
6478
  var storyboardContext = _internalApiGetStoryboardContextWrapper();
6437
6479
 
6438
6480
  var [name, value] = argsFactory(args, context, event);
6439
- storyboardContext.updateValue(name, value, method);
6481
+ storyboardContext.updateValue(name, value, method, callback);
6440
6482
  };
6441
6483
  }
6442
6484
 
6443
- function builtinStateListenerFactory(method, args, ifContainer, context) {
6485
+ function builtinStateListenerFactory(method, args, ifContainer, callback, context) {
6444
6486
  return function (event) {
6445
6487
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6446
6488
  event
@@ -6450,7 +6492,7 @@ function builtinStateListenerFactory(method, args, ifContainer, context) {
6450
6492
 
6451
6493
  var tplContext = getTplContext(context.tplContextId);
6452
6494
  var [name, value] = argsFactory(args, context, event);
6453
- tplContext.state.updateValue(name, value, method === "refresh" ? method : "replace");
6495
+ tplContext.state.updateValue(name, value, method === "update" ? "replace" : method, callback);
6454
6496
  };
6455
6497
  }
6456
6498
 
@@ -6651,13 +6693,39 @@ function customListenerFactory(handler, ifContainer, context, runtimeBrick) {
6651
6693
  };
6652
6694
  }
6653
6695
 
6696
+ function eventCallbackFactory(callback, getContext) {
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, null)(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
+
6654
6722
  function brickCallback(_x2, _x3, _x4, _x5, _x6, _x7, _x8) {
6655
6723
  return _brickCallback.apply(this, arguments);
6656
6724
  }
6657
6725
 
6658
6726
  function _brickCallback() {
6659
6727
  _brickCallback = _asyncToGenerator$3(function* (target, handler, method, context, runtimeBrick, event, options) {
6660
- var _handler$callback, _poll;
6728
+ var _poll;
6661
6729
 
6662
6730
  if (typeof target[method] !== "function") {
6663
6731
  // eslint-disable-next-line no-console
@@ -6684,45 +6752,17 @@ function _brickCallback() {
6684
6752
  };
6685
6753
  }();
6686
6754
 
6687
- var {
6688
- success,
6689
- error,
6690
- finally: finallyHook,
6691
- progress
6692
- } = (_handler$callback = handler.callback) !== null && _handler$callback !== void 0 ? _handler$callback : {};
6693
-
6694
- if (!(success || error || finallyHook || progress)) {
6755
+ if (!handler.callback) {
6695
6756
  task();
6696
6757
  return;
6697
6758
  }
6698
6759
 
6699
- var callbackFactory = (eventType, specificHandler) => result => {
6700
- if (specificHandler) {
6701
- try {
6702
- var _event = new CustomEvent(eventType, {
6703
- detail: result
6704
- });
6705
-
6706
- [].concat(specificHandler).forEach(eachHandler => {
6707
- listenerFactory(eachHandler, context, runtimeBrick)(_event);
6708
- });
6709
- } catch (err) {
6710
- // Do not throw errors in `callback.success` or `callback.progress`,
6711
- // to avoid the following triggering of `callback.error`.
6712
- // eslint-disable-next-line
6713
- console.error(err);
6714
- }
6715
- } else if (eventType === "callback.error") {
6716
- // eslint-disable-next-line
6717
- console.error("Unhandled callback error:", result);
6718
- }
6719
- };
6720
-
6760
+ var callbackFactory = eventCallbackFactory(handler.callback, () => context);
6721
6761
  var pollableCallback = {
6722
- progress: callbackFactory("callback.progress", progress),
6723
- success: callbackFactory("callback.success", success),
6724
- error: callbackFactory("callback.error", error),
6725
- finally: callbackFactory("callback.finally", finallyHook)
6762
+ progress: callbackFactory("progress"),
6763
+ success: callbackFactory("success"),
6764
+ error: callbackFactory("error"),
6765
+ finally: callbackFactory("finally")
6726
6766
  };
6727
6767
  var poll;
6728
6768
 
@@ -11265,7 +11305,7 @@ class Resolver {
11265
11305
  })();
11266
11306
  }
11267
11307
 
11268
- resolveOne(type, resolveConf, conf, brick, context) {
11308
+ resolveOne(type, resolveConf, conf, brick, context, options) {
11269
11309
  var _this2 = this;
11270
11310
 
11271
11311
  return _asyncToGenerator$3(function* () {
@@ -11370,7 +11410,7 @@ class Resolver {
11370
11410
 
11371
11411
  var promise;
11372
11412
 
11373
- if (_this2.cache.has(cacheKey)) {
11413
+ if ((options === null || options === void 0 ? void 0 : options.cache) !== "reload" && _this2.cache.has(cacheKey)) {
11374
11414
  promise = _this2.cache.get(cacheKey);
11375
11415
  } else {
11376
11416
  promise = _asyncToGenerator$3(function* () {