@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/CHANGELOG.md CHANGED
@@ -3,6 +3,42 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.136.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.135.1...@next-core/brick-kit@2.136.0) (2022-09-05)
7
+
8
+
9
+ ### Features
10
+
11
+ * support context/state load/refresh callback ([4dfdd8b](https://github.com/easyops-cn/next-core/commit/4dfdd8bc8e91430a99857154237bae290c685329))
12
+
13
+
14
+
15
+
16
+
17
+ ## [2.135.1](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.135.0...@next-core/brick-kit@2.135.1) (2022-09-02)
18
+
19
+ **Note:** Version bump only for package @next-core/brick-kit
20
+
21
+
22
+
23
+
24
+
25
+ # [2.135.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.134.2...@next-core/brick-kit@2.135.0) (2022-09-02)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * ignore cache when refresh context ([a7d5f86](https://github.com/easyops-cn/next-core/commit/a7d5f864475ebff6f6e7ebd1abbf09afb6a77473))
31
+
32
+
33
+ ### Features
34
+
35
+ * support context.load ([bf09ab1](https://github.com/easyops-cn/next-core/commit/bf09ab1aa650a4cd80b83650ad8c2173d8941d14))
36
+ * support context.load ([7056edd](https://github.com/easyops-cn/next-core/commit/7056eddb088e4fa3fd6aadafd8e027eaddcd63db))
37
+
38
+
39
+
40
+
41
+
6
42
  ## [2.134.2](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.134.1...@next-core/brick-kit@2.134.2) (2022-09-01)
7
43
 
8
44
  **Note:** Version bump only for package @next-core/brick-kit
@@ -1971,7 +1971,7 @@
1971
1971
  return (_this$data$get = this.data.get(name)) === null || _this$data$get === void 0 ? void 0 : _this$data$get.value;
1972
1972
  }
1973
1973
 
1974
- updateValue(name, value, method) {
1974
+ updateValue(name, value, method, callback) {
1975
1975
  var _item$eventTarget2;
1976
1976
 
1977
1977
  if (!this.data.has(name)) {
@@ -1992,23 +1992,60 @@
1992
1992
 
1993
1993
  if (item.type !== "free-variable") {
1994
1994
  // eslint-disable-next-line no-console
1995
- console.error("Conflict storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1995
+ console.error("Unexpected storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1996
1996
  return;
1997
1997
  }
1998
1998
 
1999
- if (method === "refresh") {
2000
- if (!item.refresh) {
2001
- throw new Error("You can not refresh the storyboard context \"".concat(name, "\" which has no resolve."));
1999
+ if (method === "refresh" || method === "load") {
2000
+ if (!item.load) {
2001
+ throw new Error("You can not ".concat(method, " the storyboard context \"").concat(name, "\" which has no resolve."));
2002
2002
  }
2003
2003
 
2004
- item.refresh().then(val => {
2005
- var _item$eventTarget;
2004
+ var promise;
2005
+
2006
+ if (method === "load") {
2007
+ // Try to reuse previous request when calling `load`.
2008
+ if (item.loaded) {
2009
+ promise = Promise.resolve(item.value);
2010
+ } else if (item.loading) {
2011
+ promise = item.loading;
2012
+ }
2013
+ }
2014
+
2015
+ if (!promise) {
2016
+ promise = item.loading = item.load(_objectSpread__default["default"]({
2017
+ cache: method === "load" ? "default" : "reload"
2018
+ }, value)); // Do not use the chained promise, since the callbacks need the original promise.
2019
+
2020
+ promise.then(val => {
2021
+ var _item$eventTarget;
2022
+
2023
+ item.loaded = true;
2024
+ item.value = val;
2025
+ (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2026
+ detail: item.value
2027
+ }));
2028
+ }, err => {
2029
+ // Let users to override error handling.
2030
+ if (!(callback !== null && callback !== void 0 && callback.error)) {
2031
+ handleHttpError(err);
2032
+ }
2033
+ });
2034
+ }
2035
+
2036
+ if (callback) {
2037
+ var callbackFactory = eventCallbackFactory(callback, () => this.getResolveOptions(_internalApiGetCurrentContext()).mergedContext);
2038
+ promise.then(val => {
2039
+ callbackFactory("success")({
2040
+ value: val
2041
+ });
2042
+ callbackFactory("finally")();
2043
+ }, err => {
2044
+ callbackFactory("error")(err);
2045
+ callbackFactory("finally")();
2046
+ });
2047
+ }
2006
2048
 
2007
- item.value = val;
2008
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2009
- detail: item.value
2010
- }));
2011
- }, handleHttpError);
2012
2049
  return;
2013
2050
  }
2014
2051
 
@@ -2107,23 +2144,23 @@
2107
2144
 
2108
2145
  var isTemplateState = !!storyboardContextWrapper.tplContextId;
2109
2146
  var value = getDefinedTemplateState(isTemplateState, contextConf, brick);
2110
- var refresh = null;
2147
+ var load = null;
2111
2148
  var isLazyResolve = false;
2112
2149
 
2113
2150
  if (value === undefined) {
2114
2151
  if (contextConf.resolve) {
2115
2152
  if (looseCheckIf(contextConf.resolve, mergedContext)) {
2116
- refresh = /*#__PURE__*/function () {
2117
- var _ref = _asyncToGenerator__default["default"](function* () {
2153
+ load = /*#__PURE__*/function () {
2154
+ var _ref = _asyncToGenerator__default["default"](function* (options) {
2118
2155
  var valueConf = {};
2119
2156
  yield _internalApiGetResolver().resolveOne("reference", _objectSpread__default["default"]({
2120
2157
  transform: "value",
2121
2158
  transformMapArray: false
2122
- }, contextConf.resolve), valueConf, null, mergedContext);
2159
+ }, contextConf.resolve), valueConf, null, mergedContext, options);
2123
2160
  return valueConf.value;
2124
2161
  });
2125
2162
 
2126
- return function refresh() {
2163
+ return function load(_x9) {
2127
2164
  return _ref.apply(this, arguments);
2128
2165
  };
2129
2166
  }();
@@ -2131,14 +2168,14 @@
2131
2168
  isLazyResolve = contextConf.resolve.lazy;
2132
2169
 
2133
2170
  if (!isLazyResolve) {
2134
- value = yield refresh();
2171
+ value = yield load();
2135
2172
  }
2136
2173
  } else if (!brickUtils.hasOwnProperty(contextConf, "value")) {
2137
2174
  return false;
2138
2175
  }
2139
2176
  }
2140
2177
 
2141
- if ((!refresh || isLazyResolve) && contextConf.value !== undefined) {
2178
+ if ((!load || isLazyResolve) && contextConf.value !== undefined) {
2142
2179
  // If the context has no resolve, just use its `value`.
2143
2180
  // Or if the resolve is ignored or lazy, use its `value` as a fallback.
2144
2181
  value = computeRealValue(contextConf.value, mergedContext, true);
@@ -2146,15 +2183,17 @@
2146
2183
 
2147
2184
  if (contextConf.track) {
2148
2185
  // Track its dependencies and auto update when each of them changed.
2149
- var deps = (isTemplateState ? brickUtils.trackUsedState : brickUtils.trackUsedContext)(refresh ? contextConf.resolve : contextConf.value);
2186
+ var deps = (isTemplateState ? brickUtils.trackUsedState : brickUtils.trackUsedContext)(load ? contextConf.resolve : contextConf.value);
2150
2187
 
2151
2188
  for (var dep of deps) {
2152
2189
  var _eventTarget;
2153
2190
 
2154
2191
  var ctx = storyboardContextWrapper.get().get(dep);
2155
2192
  ctx === null || ctx === void 0 ? void 0 : (_eventTarget = ctx.eventTarget) === null || _eventTarget === void 0 ? void 0 : _eventTarget.addEventListener(isTemplateState ? "state.change" : "context.change", () => {
2156
- if (refresh) {
2157
- storyboardContextWrapper.updateValue(contextConf.name, undefined, "refresh");
2193
+ if (load) {
2194
+ storyboardContextWrapper.updateValue(contextConf.name, {
2195
+ cache: "default"
2196
+ }, "refresh");
2158
2197
  } else {
2159
2198
  storyboardContextWrapper.updateValue(contextConf.name, computeRealValue(contextConf.value, mergedContext, true), "replace");
2160
2199
  }
@@ -2163,7 +2202,7 @@
2163
2202
  }
2164
2203
  }
2165
2204
 
2166
- resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh);
2205
+ resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, load, !isLazyResolve);
2167
2206
  return true;
2168
2207
  });
2169
2208
  return _resolveNormalStoryboardContext.apply(this, arguments);
@@ -2194,13 +2233,14 @@
2194
2233
  }
2195
2234
  }
2196
2235
 
2197
- function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh) {
2236
+ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, load, loaded) {
2198
2237
  var newContext = {
2199
2238
  type: "free-variable",
2200
2239
  value,
2201
2240
  // This is required for tracking context, even if no `onChange` is specified.
2202
2241
  eventTarget: new EventTarget$1(),
2203
- refresh
2242
+ load,
2243
+ loaded
2204
2244
  };
2205
2245
 
2206
2246
  if (contextConf.onChange) {
@@ -6291,11 +6331,13 @@
6291
6331
  case "context.assign":
6292
6332
  case "context.replace":
6293
6333
  case "context.refresh":
6294
- return builtinContextListenerFactory(method, handler.args, handler, context);
6334
+ case "context.load":
6335
+ return builtinContextListenerFactory(method, handler.args, handler, handler.callback, context);
6295
6336
 
6296
6337
  case "state.update":
6297
6338
  case "state.refresh":
6298
- return builtinStateListenerFactory(method, handler.args, handler, context);
6339
+ case "state.load":
6340
+ return builtinStateListenerFactory(method, handler.args, handler, handler.callback, context);
6299
6341
 
6300
6342
  case "tpl.dispatchEvent":
6301
6343
  return builtinTplDispatchEventFactory(handler.args, handler, context);
@@ -6424,7 +6466,7 @@
6424
6466
  };
6425
6467
  }
6426
6468
 
6427
- function builtinContextListenerFactory(method, args, ifContainer, context) {
6469
+ function builtinContextListenerFactory(method, args, ifContainer, callback, context) {
6428
6470
  return function (event) {
6429
6471
  if (!looseCheckIf(ifContainer, _objectSpread__default["default"](_objectSpread__default["default"]({}, context), {}, {
6430
6472
  event
@@ -6435,11 +6477,11 @@
6435
6477
  var storyboardContext = _internalApiGetStoryboardContextWrapper();
6436
6478
 
6437
6479
  var [name, value] = argsFactory(args, context, event);
6438
- storyboardContext.updateValue(name, value, method);
6480
+ storyboardContext.updateValue(name, value, method, callback);
6439
6481
  };
6440
6482
  }
6441
6483
 
6442
- function builtinStateListenerFactory(method, args, ifContainer, context) {
6484
+ function builtinStateListenerFactory(method, args, ifContainer, callback, context) {
6443
6485
  return function (event) {
6444
6486
  if (!looseCheckIf(ifContainer, _objectSpread__default["default"](_objectSpread__default["default"]({}, context), {}, {
6445
6487
  event
@@ -6449,7 +6491,7 @@
6449
6491
 
6450
6492
  var tplContext = getTplContext(context.tplContextId);
6451
6493
  var [name, value] = argsFactory(args, context, event);
6452
- tplContext.state.updateValue(name, value, method === "refresh" ? method : "replace");
6494
+ tplContext.state.updateValue(name, value, method === "update" ? "replace" : method, callback);
6453
6495
  };
6454
6496
  }
6455
6497
 
@@ -6650,13 +6692,39 @@
6650
6692
  };
6651
6693
  }
6652
6694
 
6695
+ function eventCallbackFactory(callback, getContext) {
6696
+ return function callbackFactory(type) {
6697
+ return function (result) {
6698
+ if (callback !== null && callback !== void 0 && callback[type]) {
6699
+ try {
6700
+ var event = new CustomEvent("callback.".concat(type), {
6701
+ detail: result
6702
+ });
6703
+ var context = getContext();
6704
+ [].concat(callback[type]).forEach(eachHandler => {
6705
+ listenerFactory(eachHandler, context, null)(event);
6706
+ });
6707
+ } catch (err) {
6708
+ // Do not throw errors in `callback.success` or `callback.progress`,
6709
+ // to avoid the following triggering of `callback.error`.
6710
+ // eslint-disable-next-line
6711
+ console.error(err);
6712
+ }
6713
+ } else if (type === "error") {
6714
+ // eslint-disable-next-line
6715
+ console.error("Unhandled callback error:", result);
6716
+ }
6717
+ };
6718
+ };
6719
+ }
6720
+
6653
6721
  function brickCallback(_x2, _x3, _x4, _x5, _x6, _x7, _x8) {
6654
6722
  return _brickCallback.apply(this, arguments);
6655
6723
  }
6656
6724
 
6657
6725
  function _brickCallback() {
6658
6726
  _brickCallback = _asyncToGenerator__default["default"](function* (target, handler, method, context, runtimeBrick, event, options) {
6659
- var _handler$callback, _poll;
6727
+ var _poll;
6660
6728
 
6661
6729
  if (typeof target[method] !== "function") {
6662
6730
  // eslint-disable-next-line no-console
@@ -6683,45 +6751,17 @@
6683
6751
  };
6684
6752
  }();
6685
6753
 
6686
- var {
6687
- success,
6688
- error,
6689
- finally: finallyHook,
6690
- progress
6691
- } = (_handler$callback = handler.callback) !== null && _handler$callback !== void 0 ? _handler$callback : {};
6692
-
6693
- if (!(success || error || finallyHook || progress)) {
6754
+ if (!handler.callback) {
6694
6755
  task();
6695
6756
  return;
6696
6757
  }
6697
6758
 
6698
- var callbackFactory = (eventType, specificHandler) => result => {
6699
- if (specificHandler) {
6700
- try {
6701
- var _event = new CustomEvent(eventType, {
6702
- detail: result
6703
- });
6704
-
6705
- [].concat(specificHandler).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 (eventType === "callback.error") {
6715
- // eslint-disable-next-line
6716
- console.error("Unhandled callback error:", result);
6717
- }
6718
- };
6719
-
6759
+ var callbackFactory = eventCallbackFactory(handler.callback, () => context);
6720
6760
  var pollableCallback = {
6721
- progress: callbackFactory("callback.progress", progress),
6722
- success: callbackFactory("callback.success", success),
6723
- error: callbackFactory("callback.error", error),
6724
- finally: callbackFactory("callback.finally", finallyHook)
6761
+ progress: callbackFactory("progress"),
6762
+ success: callbackFactory("success"),
6763
+ error: callbackFactory("error"),
6764
+ finally: callbackFactory("finally")
6725
6765
  };
6726
6766
  var poll;
6727
6767
 
@@ -11264,7 +11304,7 @@
11264
11304
  })();
11265
11305
  }
11266
11306
 
11267
- resolveOne(type, resolveConf, conf, brick, context) {
11307
+ resolveOne(type, resolveConf, conf, brick, context, options) {
11268
11308
  var _this2 = this;
11269
11309
 
11270
11310
  return _asyncToGenerator__default["default"](function* () {
@@ -11369,7 +11409,7 @@
11369
11409
 
11370
11410
  var promise;
11371
11411
 
11372
- if (_this2.cache.has(cacheKey)) {
11412
+ if ((options === null || options === void 0 ? void 0 : options.cache) !== "reload" && _this2.cache.has(cacheKey)) {
11373
11413
  promise = _this2.cache.get(cacheKey);
11374
11414
  } else {
11375
11415
  promise = _asyncToGenerator__default["default"](function* () {