@next-core/brick-utils 2.33.1 → 2.35.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,45 @@
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.35.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.34.2...@next-core/brick-utils@2.35.0) (2022-02-21)
7
+
8
+
9
+ ### Features
10
+
11
+ * record brick instance id in storyboard for development ([9d1193e](https://github.com/easyops-cn/next-core/commit/9d1193e11cf5cd35d10d36afe4f07a52a7f7841a))
12
+
13
+
14
+
15
+
16
+
17
+ ## [2.34.2](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.34.1...@next-core/brick-utils@2.34.2) (2022-02-17)
18
+
19
+ **Note:** Version bump only for package @next-core/brick-utils
20
+
21
+
22
+
23
+
24
+
25
+ ## [2.34.1](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.34.0...@next-core/brick-utils@2.34.1) (2022-02-15)
26
+
27
+ **Note:** Version bump only for package @next-core/brick-utils
28
+
29
+
30
+
31
+
32
+
33
+ # [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)
34
+
35
+
36
+ ### Features
37
+
38
+ * support template scoped state ([d736f85](https://github.com/easyops-cn/next-core/commit/d736f8550b4b645851e2b0a99bb2f99eee4378a2))
39
+ * support tpl-scoped context ([783b060](https://github.com/easyops-cn/next-core/commit/783b0605d3c74ac653120689d467384a71aaf79a))
40
+
41
+
42
+
43
+
44
+
6
45
  ## [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
46
 
8
47
  **Note:** Version bump only for package @next-core/brick-utils
@@ -21434,14 +21434,14 @@
21434
21434
  convertUnitValueByPrecision: convertValueByPrecision
21435
21435
  } = pipes.utils;
21436
21436
 
21437
- var CTX$1 = "CTX";
21438
21437
  function resolveContextConcurrently(_x, _x2) {
21439
21438
  return _resolveContextConcurrently.apply(this, arguments);
21440
21439
  }
21441
21440
 
21442
21441
  function _resolveContextConcurrently() {
21443
21442
  _resolveContextConcurrently = _asyncToGenerator__default["default"](function* (contextConfs, resolveContext) {
21444
- var dependencyMap = getDependencyMapOfContext(contextConfs);
21443
+ var keyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "CTX";
21444
+ var dependencyMap = getDependencyMapOfContext(contextConfs, keyword);
21445
21445
  var pendingDeps = new Set(Array.from(dependencyMap.keys()).map(contextConf => contextConf.name));
21446
21446
  var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
21447
21447
  var processed = new WeakSet();
@@ -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);
@@ -21492,7 +21489,7 @@
21492
21489
 
21493
21490
  if (dependencyMap.size > 0) {
21494
21491
  // This will throw if circular contexts detected.
21495
- detectCircularContexts(dependencyMap);
21492
+ detectCircularContexts(dependencyMap, keyword);
21496
21493
  scheduleAsSerial = true;
21497
21494
  yield scheduleNext();
21498
21495
  }
@@ -21500,28 +21497,76 @@
21500
21497
  return _resolveContextConcurrently.apply(this, arguments);
21501
21498
  }
21502
21499
 
21500
+ function syncResolveContextConcurrently(contextConfs, resolveContext) {
21501
+ var keyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "CTX";
21502
+ var dependencyMap = getDependencyMapOfContext(contextConfs, keyword);
21503
+ var pendingDeps = new Set(Array.from(dependencyMap.keys()).map(contextConf => contextConf.name));
21504
+ var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
21505
+ var scheduleAsSerial = includesComputed;
21506
+
21507
+ function scheduleNext() {
21508
+ var dep = Array.from(dependencyMap.entries()).find(predicateNextResolveFactory(pendingDeps, scheduleAsSerial));
21509
+
21510
+ if (dep) {
21511
+ var [_contextConf] = dep;
21512
+ var resolved = resolveContext(_contextConf);
21513
+ dependencyMap.delete(_contextConf);
21514
+
21515
+ if (resolved) {
21516
+ if (!pendingDeps.delete(_contextConf.name)) {
21517
+ throw new Error("Duplicated context defined: ".concat(_contextConf.name));
21518
+ }
21519
+ }
21520
+
21521
+ scheduleNext();
21522
+ }
21523
+ }
21524
+
21525
+ scheduleNext(); // If there are still contexts left, it implies one of these situations:
21526
+ // - Circular contexts.
21527
+ // Such as: a depends on b, while b depends on a.
21528
+ // - Related contexts are all ignored.
21529
+ // Such as: a depends on b,
21530
+ // while both them are ignore by a falsy result of `if`.
21531
+
21532
+ if (dependencyMap.size > 0) {
21533
+ // This will throw if circular contexts detected.
21534
+ detectCircularContexts(dependencyMap, keyword);
21535
+ scheduleAsSerial = true;
21536
+ scheduleNext();
21537
+ }
21538
+ }
21539
+
21540
+ function predicateNextResolveFactory(pendingDeps, scheduleAsSerial) {
21541
+ return (entry, index) => // When contexts contain computed CTX accesses, it implies a dynamic dependency map.
21542
+ // So make them process sequentially, keep the same behavior as before.
21543
+ scheduleAsSerial ? index === 0 : // A context is ready when it has no pending dependencies.
21544
+ !entry[1].dependencies.some(dep => pendingDeps.has(dep));
21545
+ }
21546
+
21503
21547
  function getDependencyMapOfContext(contextConfs) {
21548
+ var keyword = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "CTX";
21504
21549
  var depsMap = new Map();
21505
21550
 
21506
- for (var _contextConf of contextConfs) {
21551
+ for (var _contextConf2 of contextConfs) {
21507
21552
  var stats = {
21508
21553
  dependencies: [],
21509
21554
  includesComputed: false
21510
21555
  };
21511
21556
 
21512
- if (!_contextConf.property) {
21513
- visitStoryboardExpressions([_contextConf.if, _contextConf.value, _contextConf.resolve], beforeVisitContextFactory(stats), CTX$1);
21557
+ if (!_contextConf2.property) {
21558
+ visitStoryboardExpressions([_contextConf2.if, _contextConf2.value, _contextConf2.resolve], beforeVisitContextFactory(stats, keyword), keyword);
21514
21559
  }
21515
21560
 
21516
- depsMap.set(_contextConf, stats);
21561
+ depsMap.set(_contextConf2, stats);
21517
21562
  }
21518
21563
 
21519
21564
  return depsMap;
21520
21565
  }
21521
21566
 
21522
- function beforeVisitContextFactory(stats) {
21567
+ function beforeVisitContextFactory(stats, keyword) {
21523
21568
  return function beforeVisitContext(node, parent) {
21524
- if (node.name === CTX$1) {
21569
+ if (node.name === keyword) {
21525
21570
  var memberParent = parent[parent.length - 1];
21526
21571
 
21527
21572
  if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
@@ -21544,17 +21589,17 @@
21544
21589
  };
21545
21590
  }
21546
21591
 
21547
- function detectCircularContexts(dependencyMap) {
21592
+ function detectCircularContexts(dependencyMap, keyword) {
21548
21593
  var duplicatedMap = new Map(dependencyMap);
21549
21594
  var pendingDeps = new Set(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name));
21550
21595
 
21551
21596
  var next = () => {
21552
21597
  var processedAtLeastOne = false;
21553
21598
 
21554
- for (var [_contextConf2, stats] of duplicatedMap.entries()) {
21599
+ for (var [_contextConf3, stats] of duplicatedMap.entries()) {
21555
21600
  if (!stats.dependencies.some(dep => pendingDeps.has(dep))) {
21556
- duplicatedMap.delete(_contextConf2);
21557
- pendingDeps.delete(_contextConf2.name);
21601
+ duplicatedMap.delete(_contextConf3);
21602
+ pendingDeps.delete(_contextConf3.name);
21558
21603
  processedAtLeastOne = true;
21559
21604
  }
21560
21605
  }
@@ -21567,7 +21612,7 @@
21567
21612
  next();
21568
21613
 
21569
21614
  if (duplicatedMap.size > 0) {
21570
- throw new ReferenceError("Circular CTX detected: ".concat(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name).join(", ")));
21615
+ throw new ReferenceError("Circular ".concat(keyword, " detected: ").concat(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name).join(", ")));
21571
21616
  }
21572
21617
  }
21573
21618
 
@@ -21706,23 +21751,24 @@
21706
21751
 
21707
21752
  var yamlFieldsInBrick = ["permissionsPreCheck", "transformFrom"]; // Fields started with `_` will be removed by default.
21708
21753
 
21709
- var fieldsToRemoveInRoute = ["appId", "children", "creator", "ctime", "id", "instanceId", "graphInfo", "modifier", "mountPoint", "mtime", "org", "parent", "sort", "name", "providersBak", "providers_bak", "deleteAuthorizers", "readAuthorizers", "updateAuthorizers"];
21710
- var fieldsToRemoveInBrick = fieldsToRemoveInRoute.concat("type", "alias"); // Those fields can be disposed if value is null.
21754
+ var baseFieldsToRemove = ["appId", "children", "creator", "ctime", "id", "graphInfo", "modifier", "mountPoint", "mtime", "org", "parent", "sort", "name", "providersBak", "providers_bak", "deleteAuthorizers", "readAuthorizers", "updateAuthorizers"];
21755
+ var fieldsToRemoveInRoute = baseFieldsToRemove.concat("instanceId");
21756
+ var fieldsToRemoveInBrick = baseFieldsToRemove.concat("type", "alias"); // Those fields can be disposed if value is null.
21711
21757
 
21712
21758
  var disposableNullFields = ["alias", "documentId", "hybrid", "bg", "context", "exports", "ref", "portal", "analyticsData"];
21713
21759
  function normalizeBuilderNode(node) {
21714
21760
  if (isBrickNode(node)) {
21715
- return normalize(node, fieldsToRemoveInBrick, jsonFieldsInBrick, yamlFieldsInBrick, false);
21761
+ return normalize(node, fieldsToRemoveInBrick, jsonFieldsInBrick, yamlFieldsInBrick, "brick");
21716
21762
  }
21717
21763
 
21718
21764
  if (isRouteNode(node)) {
21719
- return normalize(node, fieldsToRemoveInRoute, jsonFieldsInRoute, yamlFieldsInRoute, true);
21765
+ return normalize(node, fieldsToRemoveInRoute, jsonFieldsInRoute, yamlFieldsInRoute, "route");
21720
21766
  }
21721
21767
 
21722
21768
  return null;
21723
21769
  }
21724
21770
 
21725
- function normalize(node, fieldsToRemove, jsonFields, yamlFields, cleanUpSegues) {
21771
+ function normalize(node, fieldsToRemove, jsonFields, yamlFields, type) {
21726
21772
  return Object.fromEntries(Object.entries(node) // Remove unused fields from CMDB.
21727
21773
  // Consider fields started with `_` as unused.
21728
21774
  .filter(_ref => {
@@ -21731,7 +21777,7 @@
21731
21777
  }) // Parse specific fields.
21732
21778
  .map(_ref2 => {
21733
21779
  var [key, value] = _ref2;
21734
- return [key, cleanUpSegues && key === "segues" ? getCleanSegues(value) : jsonFields.includes(key) ? safeJsonParse(value) : yamlFields.includes(key) ? safeYamlParse(value) : lodash.cloneDeep(value)];
21780
+ return [key === "instanceId" ? "iid" : key, type === "route" && key === "segues" ? getCleanSegues(value) : jsonFields.includes(key) ? safeJsonParse(value) : yamlFields.includes(key) ? safeYamlParse(value) : lodash.cloneDeep(value)];
21735
21781
  }));
21736
21782
  } // Clear `segue._view` which is for development only.
21737
21783
 
@@ -21813,8 +21859,6 @@
21813
21859
  return Object.freeze(object);
21814
21860
  }
21815
21861
 
21816
- var TRACK_CONTEXT = "track context";
21817
- var CTX = "CTX";
21818
21862
  /**
21819
21863
  * Get tracking CTX for an evaluable expression in `track context` mode.
21820
21864
  *
@@ -21843,7 +21887,14 @@
21843
21887
  */
21844
21888
 
21845
21889
  function trackContext(raw) {
21846
- if (raw.includes(TRACK_CONTEXT)) {
21890
+ return track(raw, "track context", "CTX");
21891
+ }
21892
+ function trackState(raw) {
21893
+ return track(raw, "track state", "STATE");
21894
+ }
21895
+
21896
+ function track(raw, trackText, variableName) {
21897
+ if (raw.includes(trackText)) {
21847
21898
  var contexts = new Set();
21848
21899
  var {
21849
21900
  expression
@@ -21851,7 +21902,7 @@
21851
21902
  withParent: true,
21852
21903
  hooks: {
21853
21904
  beforeVisitGlobal(node, parent) {
21854
- if (node.name === CTX) {
21905
+ if (node.name === variableName) {
21855
21906
  var memberParent = parent[parent.length - 1];
21856
21907
 
21857
21908
  if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
@@ -21870,12 +21921,12 @@
21870
21921
  });
21871
21922
  var trackCtxExp;
21872
21923
 
21873
- if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === TRACK_CONTEXT) {
21924
+ if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
21874
21925
  if (contexts.size > 0) {
21875
21926
  return Array.from(contexts);
21876
21927
  } else {
21877
21928
  // 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, "\""));
21929
+ console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
21879
21930
  }
21880
21931
  }
21881
21932
  }
@@ -21940,9 +21991,11 @@
21940
21991
  exports.scanTemplatesInStoryboard = scanTemplatesInStoryboard;
21941
21992
  exports.shouldAllowRecursiveEvaluations = shouldAllowRecursiveEvaluations;
21942
21993
  exports.smartDisplayForEvaluableString = smartDisplayForEvaluableString;
21994
+ exports.syncResolveContextConcurrently = syncResolveContextConcurrently;
21943
21995
  exports.toPath = toPath;
21944
21996
  exports.tokTypes = tokTypes_1;
21945
21997
  exports.trackContext = trackContext;
21998
+ exports.trackState = trackState;
21946
21999
  exports.transform = transform;
21947
22000
  exports.transformAndInject = transformAndInject;
21948
22001
  exports.visitStoryboardExpressions = visitStoryboardExpressions;