@next-core/brick-kit 2.130.0 → 2.131.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,18 @@
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.131.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.130.0...@next-core/brick-kit@2.131.0) (2022-08-25)
7
+
8
+
9
+ ### Features
10
+
11
+ * context can track its deps now ([4993308](https://github.com/easyops-cn/next-core/commit/49933089e1ff6ec1ec5f01eeac5609dde07c1e2e))
12
+ * support lazy context ([39fdffb](https://github.com/easyops-cn/next-core/commit/39fdffb0500df36d5bb9447d63a448bd2b78df14))
13
+
14
+
15
+
16
+
17
+
6
18
  # [2.130.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.129.3...@next-core/brick-kit@2.130.0) (2022-08-23)
7
19
 
8
20
 
@@ -2040,7 +2040,7 @@
2040
2040
  }
2041
2041
 
2042
2042
  updateValue(name, value, method) {
2043
- var _item$eventTarget;
2043
+ var _item$eventTarget2;
2044
2044
 
2045
2045
  if (!this.data.has(name)) {
2046
2046
  if (this.tplContextId) {
@@ -2064,6 +2064,22 @@
2064
2064
  return;
2065
2065
  }
2066
2066
 
2067
+ if (method === "refresh") {
2068
+ if (!item.refresh) {
2069
+ throw new Error("You can not refresh the storyboard context \"".concat(name, "\" which has no resolve."));
2070
+ }
2071
+
2072
+ item.refresh().then(val => {
2073
+ var _item$eventTarget;
2074
+
2075
+ item.value = val;
2076
+ (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2077
+ detail: item.value
2078
+ }));
2079
+ }, handleHttpError);
2080
+ return;
2081
+ }
2082
+
2067
2083
  if (method === "replace") {
2068
2084
  item.value = value;
2069
2085
  } else {
@@ -2076,7 +2092,7 @@
2076
2092
  }
2077
2093
  }
2078
2094
 
2079
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2095
+ (_item$eventTarget2 = item.eventTarget) === null || _item$eventTarget2 === void 0 ? void 0 : _item$eventTarget2.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2080
2096
  detail: item.value
2081
2097
  }));
2082
2098
  }
@@ -2157,30 +2173,65 @@
2157
2173
  return false;
2158
2174
  }
2159
2175
 
2160
- var isResolve = false;
2161
- var value = getDefinedTemplateState(!!storyboardContextWrapper.tplContextId, contextConf, brick);
2176
+ var isTemplateState = !!storyboardContextWrapper.tplContextId;
2177
+ var value = getDefinedTemplateState(isTemplateState, contextConf, brick);
2178
+ var refresh = null;
2179
+ var isLazyResolve = false;
2162
2180
 
2163
2181
  if (value === undefined) {
2164
2182
  if (contextConf.resolve) {
2165
2183
  if (looseCheckIf(contextConf.resolve, mergedContext)) {
2166
- isResolve = true;
2167
- var valueConf = {};
2168
- yield _internalApiGetResolver().resolveOne("reference", _objectSpread__default["default"]({
2169
- transform: "value",
2170
- transformMapArray: false
2171
- }, contextConf.resolve), valueConf, null, mergedContext);
2172
- value = valueConf.value;
2184
+ refresh = /*#__PURE__*/function () {
2185
+ var _ref = _asyncToGenerator__default["default"](function* () {
2186
+ var valueConf = {};
2187
+ yield _internalApiGetResolver().resolveOne("reference", _objectSpread__default["default"]({
2188
+ transform: "value",
2189
+ transformMapArray: false
2190
+ }, contextConf.resolve), valueConf, null, mergedContext);
2191
+ return valueConf.value;
2192
+ });
2193
+
2194
+ return function refresh() {
2195
+ return _ref.apply(this, arguments);
2196
+ };
2197
+ }();
2198
+
2199
+ isLazyResolve = contextConf.resolve.lazy;
2200
+
2201
+ if (!isLazyResolve) {
2202
+ value = yield refresh();
2203
+ }
2173
2204
  } else if (!brickUtils.hasOwnProperty(contextConf, "value")) {
2174
2205
  return false;
2175
2206
  }
2176
2207
  }
2177
2208
 
2178
- if (!isResolve && contextConf.value !== undefined) {
2209
+ if ((!refresh || isLazyResolve) && contextConf.value !== undefined) {
2210
+ // If the context has no resolve, just use its `value`.
2211
+ // Or if the resolve is ignored or lazy, use its `value` as a fallback.
2179
2212
  value = computeRealValue(contextConf.value, mergedContext, true);
2180
2213
  }
2214
+
2215
+ if (contextConf.track) {
2216
+ // Track its dependencies and auto update when each of them changed.
2217
+ var deps = (isTemplateState ? brickUtils.trackUsedState : brickUtils.trackUsedContext)(refresh ? contextConf.resolve : contextConf.value);
2218
+
2219
+ for (var dep of deps) {
2220
+ var _eventTarget;
2221
+
2222
+ var ctx = storyboardContextWrapper.get().get(dep);
2223
+ ctx === null || ctx === void 0 ? void 0 : (_eventTarget = ctx.eventTarget) === null || _eventTarget === void 0 ? void 0 : _eventTarget.addEventListener(isTemplateState ? "state.change" : "context.change", () => {
2224
+ if (refresh) {
2225
+ storyboardContextWrapper.updateValue(contextConf.name, undefined, "refresh");
2226
+ } else {
2227
+ storyboardContextWrapper.updateValue(contextConf.name, computeRealValue(contextConf.value, mergedContext, true), "replace");
2228
+ }
2229
+ });
2230
+ }
2231
+ }
2181
2232
  }
2182
2233
 
2183
- resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick);
2234
+ resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh);
2184
2235
  return true;
2185
2236
  });
2186
2237
  return _resolveNormalStoryboardContext.apply(this, arguments);
@@ -2211,12 +2262,13 @@
2211
2262
  }
2212
2263
  }
2213
2264
 
2214
- function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick) {
2265
+ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardContextWrapper, brick, refresh) {
2215
2266
  var newContext = {
2216
2267
  type: "free-variable",
2217
2268
  value,
2218
2269
  // This is required for tracking context, even if no `onChange` is specified.
2219
- eventTarget: new EventTarget$1()
2270
+ eventTarget: new EventTarget$1(),
2271
+ refresh
2220
2272
  };
2221
2273
 
2222
2274
  if (contextConf.onChange) {
@@ -6287,10 +6339,12 @@
6287
6339
 
6288
6340
  case "context.assign":
6289
6341
  case "context.replace":
6342
+ case "context.refresh":
6290
6343
  return builtinContextListenerFactory(method, handler.args, handler, context);
6291
6344
 
6292
6345
  case "state.update":
6293
- return builtinStateListenerFactory(handler.args, handler, context);
6346
+ case "state.refresh":
6347
+ return builtinStateListenerFactory(method, handler.args, handler, context);
6294
6348
 
6295
6349
  case "tpl.dispatchEvent":
6296
6350
  return builtinTplDispatchEventFactory(handler.args, handler, context);
@@ -6434,7 +6488,7 @@
6434
6488
  };
6435
6489
  }
6436
6490
 
6437
- function builtinStateListenerFactory(args, ifContainer, context) {
6491
+ function builtinStateListenerFactory(method, args, ifContainer, context) {
6438
6492
  return function (event) {
6439
6493
  if (!looseCheckIf(ifContainer, _objectSpread__default["default"](_objectSpread__default["default"]({}, context), {}, {
6440
6494
  event
@@ -6444,7 +6498,7 @@
6444
6498
 
6445
6499
  var tplContext = getTplContext(context.tplContextId);
6446
6500
  var [name, value] = argsFactory(args, context, event);
6447
- tplContext.state.updateValue(name, value, "replace");
6501
+ tplContext.state.updateValue(name, value, method === "refresh" ? method : "replace");
6448
6502
  };
6449
6503
  }
6450
6504
 
@@ -10801,7 +10855,7 @@
10801
10855
  if (expandedBrickConf.exports) {
10802
10856
  for (var [prop, ctxName] of Object.entries(expandedBrickConf.exports)) {
10803
10857
  if (typeof ctxName === "string" && ctxName.startsWith("CTX.")) {
10804
- _this6.storyboardContextWrapper.set(ctxName.substr(4), {
10858
+ _this6.storyboardContextWrapper.set(ctxName.substring(4), {
10805
10859
  type: "brick-property",
10806
10860
  brick,
10807
10861
  prop
@@ -11380,19 +11434,33 @@
11380
11434
  }
11381
11435
  }
11382
11436
 
11383
- var cacheKey = JSON.stringify({
11384
- provider,
11385
- useProvider,
11386
- method,
11387
- args
11388
- });
11437
+ var actualArgs = args ? ref ? args // `args` are already computed for `defineResolves`
11438
+ : context ? computeRealValue(args, context, true) : args : providerBrick.args || [];
11439
+ var cacheKey;
11440
+
11441
+ try {
11442
+ // `actualArgs` may contain circular references, which makes
11443
+ // JSON stringify failed, thus we fallback to original args.
11444
+ cacheKey = JSON.stringify({
11445
+ provider,
11446
+ useProvider,
11447
+ method,
11448
+ actualArgs
11449
+ });
11450
+ } catch (e) {
11451
+ cacheKey = JSON.stringify({
11452
+ provider,
11453
+ useProvider,
11454
+ method,
11455
+ args
11456
+ });
11457
+ }
11458
+
11389
11459
  var promise;
11390
11460
 
11391
11461
  if (_this2.cache.has(cacheKey)) {
11392
11462
  promise = _this2.cache.get(cacheKey);
11393
11463
  } else {
11394
- var actualArgs = args ? ref ? args // `args` are already computed for `defineResolves`
11395
- : context ? computeRealValue(args, context, true) : args : providerBrick.args || [];
11396
11464
  promise = _asyncToGenerator__default["default"](function* () {
11397
11465
  if (useProvider) {
11398
11466
  actualArgs = yield getArgsOfCustomApi(useProvider, actualArgs);