@next-core/brick-utils 2.33.1 → 2.34.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.34.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.33.1...@next-core/brick-utils@2.34.0) (2022-02-14)
7
+
8
+
9
+ ### Features
10
+
11
+ * support template scoped state ([d736f85](https://github.com/easyops-cn/next-core/commit/d736f8550b4b645851e2b0a99bb2f99eee4378a2))
12
+ * support tpl-scoped context ([783b060](https://github.com/easyops-cn/next-core/commit/783b0605d3c74ac653120689d467384a71aaf79a))
13
+
14
+
15
+
16
+
17
+
6
18
  ## [2.33.1](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.33.0...@next-core/brick-utils@2.33.1) (2022-01-28)
7
19
 
8
20
  **Note:** Version bump only for package @next-core/brick-utils
@@ -21434,7 +21434,7 @@
21434
21434
  convertUnitValueByPrecision: convertValueByPrecision
21435
21435
  } = pipes.utils;
21436
21436
 
21437
- var CTX$1 = "CTX";
21437
+ var CTX = "CTX";
21438
21438
  function resolveContextConcurrently(_x, _x2) {
21439
21439
  return _resolveContextConcurrently.apply(this, arguments);
21440
21440
  }
@@ -21474,10 +21474,7 @@
21474
21474
 
21475
21475
  function _scheduleNext() {
21476
21476
  _scheduleNext = _asyncToGenerator__default["default"](function* () {
21477
- var readyContexts = Array.from(dependencyMap.entries()).filter((entry, index) => // When contexts contain computed CTX accesses, it implies a dynamic dependency map.
21478
- // So make them process sequentially, keep the same behavior as before.
21479
- scheduleAsSerial ? index === 0 : // A context is ready when it has no pending dependencies.
21480
- !entry[1].dependencies.some(dep => pendingDeps.has(dep))).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
21477
+ var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
21481
21478
  yield Promise.all(readyContexts.map(wrapResolve));
21482
21479
  });
21483
21480
  return _scheduleNext.apply(this, arguments);
@@ -21500,20 +21497,66 @@
21500
21497
  return _resolveContextConcurrently.apply(this, arguments);
21501
21498
  }
21502
21499
 
21500
+ function syncResolveContextConcurrently(contextConfs, resolveContext) {
21501
+ var dependencyMap = getDependencyMapOfContext(contextConfs);
21502
+ var pendingDeps = new Set(Array.from(dependencyMap.keys()).map(contextConf => contextConf.name));
21503
+ var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
21504
+ var scheduleAsSerial = includesComputed;
21505
+
21506
+ function scheduleNext() {
21507
+ var dep = Array.from(dependencyMap.entries()).find(predicateNextResolveFactory(pendingDeps, scheduleAsSerial));
21508
+
21509
+ if (dep) {
21510
+ var [_contextConf] = dep;
21511
+ var resolved = resolveContext(_contextConf);
21512
+ dependencyMap.delete(_contextConf);
21513
+
21514
+ if (resolved) {
21515
+ if (!pendingDeps.delete(_contextConf.name)) {
21516
+ throw new Error("Duplicated context defined: ".concat(_contextConf.name));
21517
+ }
21518
+ }
21519
+
21520
+ scheduleNext();
21521
+ }
21522
+ }
21523
+
21524
+ scheduleNext(); // If there are still contexts left, it implies one of these situations:
21525
+ // - Circular contexts.
21526
+ // Such as: a depends on b, while b depends on a.
21527
+ // - Related contexts are all ignored.
21528
+ // Such as: a depends on b,
21529
+ // while both them are ignore by a falsy result of `if`.
21530
+
21531
+ if (dependencyMap.size > 0) {
21532
+ // This will throw if circular contexts detected.
21533
+ detectCircularContexts(dependencyMap);
21534
+ scheduleAsSerial = true;
21535
+ scheduleNext();
21536
+ }
21537
+ }
21538
+
21539
+ function predicateNextResolveFactory(pendingDeps, scheduleAsSerial) {
21540
+ return (entry, index) => // When contexts contain computed CTX accesses, it implies a dynamic dependency map.
21541
+ // So make them process sequentially, keep the same behavior as before.
21542
+ scheduleAsSerial ? index === 0 : // A context is ready when it has no pending dependencies.
21543
+ !entry[1].dependencies.some(dep => pendingDeps.has(dep));
21544
+ }
21545
+
21503
21546
  function getDependencyMapOfContext(contextConfs) {
21504
21547
  var depsMap = new Map();
21505
21548
 
21506
- for (var _contextConf of contextConfs) {
21549
+ for (var _contextConf2 of contextConfs) {
21507
21550
  var stats = {
21508
21551
  dependencies: [],
21509
21552
  includesComputed: false
21510
21553
  };
21511
21554
 
21512
- if (!_contextConf.property) {
21513
- visitStoryboardExpressions([_contextConf.if, _contextConf.value, _contextConf.resolve], beforeVisitContextFactory(stats), CTX$1);
21555
+ if (!_contextConf2.property) {
21556
+ visitStoryboardExpressions([_contextConf2.if, _contextConf2.value, _contextConf2.resolve], beforeVisitContextFactory(stats), CTX);
21514
21557
  }
21515
21558
 
21516
- depsMap.set(_contextConf, stats);
21559
+ depsMap.set(_contextConf2, stats);
21517
21560
  }
21518
21561
 
21519
21562
  return depsMap;
@@ -21521,7 +21564,7 @@
21521
21564
 
21522
21565
  function beforeVisitContextFactory(stats) {
21523
21566
  return function beforeVisitContext(node, parent) {
21524
- if (node.name === CTX$1) {
21567
+ if (node.name === CTX) {
21525
21568
  var memberParent = parent[parent.length - 1];
21526
21569
 
21527
21570
  if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
@@ -21551,10 +21594,10 @@
21551
21594
  var next = () => {
21552
21595
  var processedAtLeastOne = false;
21553
21596
 
21554
- for (var [_contextConf2, stats] of duplicatedMap.entries()) {
21597
+ for (var [_contextConf3, stats] of duplicatedMap.entries()) {
21555
21598
  if (!stats.dependencies.some(dep => pendingDeps.has(dep))) {
21556
- duplicatedMap.delete(_contextConf2);
21557
- pendingDeps.delete(_contextConf2.name);
21599
+ duplicatedMap.delete(_contextConf3);
21600
+ pendingDeps.delete(_contextConf3.name);
21558
21601
  processedAtLeastOne = true;
21559
21602
  }
21560
21603
  }
@@ -21813,8 +21856,6 @@
21813
21856
  return Object.freeze(object);
21814
21857
  }
21815
21858
 
21816
- var TRACK_CONTEXT = "track context";
21817
- var CTX = "CTX";
21818
21859
  /**
21819
21860
  * Get tracking CTX for an evaluable expression in `track context` mode.
21820
21861
  *
@@ -21843,7 +21884,14 @@
21843
21884
  */
21844
21885
 
21845
21886
  function trackContext(raw) {
21846
- if (raw.includes(TRACK_CONTEXT)) {
21887
+ return track(raw, "track context", "CTX");
21888
+ }
21889
+ function trackState(raw) {
21890
+ return track(raw, "track state", "STATE");
21891
+ }
21892
+
21893
+ function track(raw, trackText, variableName) {
21894
+ if (raw.includes(trackText)) {
21847
21895
  var contexts = new Set();
21848
21896
  var {
21849
21897
  expression
@@ -21851,7 +21899,7 @@
21851
21899
  withParent: true,
21852
21900
  hooks: {
21853
21901
  beforeVisitGlobal(node, parent) {
21854
- if (node.name === CTX) {
21902
+ if (node.name === variableName) {
21855
21903
  var memberParent = parent[parent.length - 1];
21856
21904
 
21857
21905
  if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
@@ -21870,12 +21918,12 @@
21870
21918
  });
21871
21919
  var trackCtxExp;
21872
21920
 
21873
- if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === TRACK_CONTEXT) {
21921
+ if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
21874
21922
  if (contexts.size > 0) {
21875
21923
  return Array.from(contexts);
21876
21924
  } else {
21877
21925
  // eslint-disable-next-line no-console
21878
- console.warn("You are using \"".concat(TRACK_CONTEXT, "\" but no CTX usage found in your expression: \"").concat(raw, "\""));
21926
+ console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
21879
21927
  }
21880
21928
  }
21881
21929
  }
@@ -21940,9 +21988,11 @@
21940
21988
  exports.scanTemplatesInStoryboard = scanTemplatesInStoryboard;
21941
21989
  exports.shouldAllowRecursiveEvaluations = shouldAllowRecursiveEvaluations;
21942
21990
  exports.smartDisplayForEvaluableString = smartDisplayForEvaluableString;
21991
+ exports.syncResolveContextConcurrently = syncResolveContextConcurrently;
21943
21992
  exports.toPath = toPath;
21944
21993
  exports.tokTypes = tokTypes_1;
21945
21994
  exports.trackContext = trackContext;
21995
+ exports.trackState = trackState;
21946
21996
  exports.transform = transform;
21947
21997
  exports.transformAndInject = transformAndInject;
21948
21998
  exports.visitStoryboardExpressions = visitStoryboardExpressions;