@next-core/brick-utils 2.45.14 → 2.45.16

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.
@@ -18157,10 +18157,14 @@
18157
18157
  usedTemplates
18158
18158
  } = scanStoryboard(storyboard, options);
18159
18159
  var customTemplates = (_storyboard$meta = storyboard.meta) === null || _storyboard$meta === void 0 ? void 0 : _storyboard$meta.customTemplates;
18160
+ var processors = scanProcessorsInAny([storyboard.routes, options !== null && options !== void 0 && options.ignoreBricksInUnusedCustomTemplates ? customTemplates === null || customTemplates === void 0 ? void 0 : customTemplates.filter(tpl => usedTemplates.includes(tpl.name)) : customTemplates]);
18160
18161
  return _objectSpread__default["default"](_objectSpread__default["default"]({}, getDllAndDepsByResource({
18161
18162
  bricks,
18162
- processors: scanProcessorsInAny([storyboard.routes, options !== null && options !== void 0 && options.ignoreBricksInUnusedCustomTemplates ? customTemplates === null || customTemplates === void 0 ? void 0 : customTemplates.filter(tpl => usedTemplates.includes(tpl.name)) : customTemplates])
18163
+ processors
18163
18164
  }, brickPackages)), {}, {
18165
+ byProcessors: getDllAndDepsByResource({
18166
+ processors
18167
+ }, brickPackages),
18164
18168
  bricks
18165
18169
  });
18166
18170
  }
@@ -19457,7 +19461,178 @@
19457
19461
  convertUnitValueByPrecision: convertValueByPrecision
19458
19462
  } = pipes.utils;
19459
19463
 
19460
- function resolveContextConcurrently(_x, _x2) {
19464
+ /**
19465
+ * Get tracking CTX for an evaluable expression in `track context` mode.
19466
+ *
19467
+ * A `track context` mode is an evaluable expression which is a sequence expression
19468
+ * starting with the exact literal string expression of "track context".
19469
+ *
19470
+ * @param raw - A raw string, which must be checked by `isEvaluable` first.
19471
+ *
19472
+ * @returns
19473
+ *
19474
+ * Returns used CTXs if `track context` mode is enabled, or returns false if not enabled or
19475
+ * no CTX found.
19476
+ *
19477
+ * @example
19478
+ *
19479
+ * ```js
19480
+ * trackContext('<% "track context", [CTX.hello, CTX.world] %>');
19481
+ * // => ["hello", "world"]
19482
+ *
19483
+ * trackContext('<% [CTX.hello, CTX.world] %>');
19484
+ * // => false
19485
+ *
19486
+ * trackContext('<% "track context", DATA.any %>');
19487
+ * // => false
19488
+ * ```
19489
+ */
19490
+ function trackContext(raw) {
19491
+ return track(raw, "track context", "CTX");
19492
+ }
19493
+ function trackState(raw) {
19494
+ return track(raw, "track state", "STATE");
19495
+ }
19496
+ function trackFormState(raw) {
19497
+ return track(raw, "track formstate", "FORM_STATE");
19498
+ }
19499
+ function trackUsedContext(data) {
19500
+ return collectContextUsage(data, "CTX").usedContexts;
19501
+ }
19502
+ function trackUsedState(data) {
19503
+ return collectContextUsage(data, "STATE").usedContexts;
19504
+ }
19505
+ function track(raw, trackText, variableName) {
19506
+ if (raw.includes(trackText)) {
19507
+ // const contexts = new Set<string>();
19508
+ var usage = {
19509
+ usedContexts: [],
19510
+ includesComputed: false
19511
+ };
19512
+ var {
19513
+ expression
19514
+ } = preevaluate(raw, {
19515
+ withParent: true,
19516
+ hooks: {
19517
+ beforeVisitGlobal: beforeVisitContextFactory(usage, variableName)
19518
+ }
19519
+ });
19520
+ // const contexts = usage
19521
+ var trackCtxExp;
19522
+ if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
19523
+ if (usage.usedContexts.length > 0) {
19524
+ return usage.usedContexts;
19525
+ } else {
19526
+ // eslint-disable-next-line no-console
19527
+ console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
19528
+ }
19529
+ }
19530
+ }
19531
+ return false;
19532
+ }
19533
+ function beforeVisitContextFactory(usage, variableName) {
19534
+ return function beforeVisitContext(node, parent) {
19535
+ if (node.name === variableName) {
19536
+ var memberParent = parent[parent.length - 1];
19537
+ if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19538
+ var memberNode = memberParent.node;
19539
+ var used;
19540
+ if (!memberNode.computed && memberNode.property.type === "Identifier") {
19541
+ used = memberNode.property.name;
19542
+ } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19543
+ used = memberNode.property.value;
19544
+ } else {
19545
+ usage.includesComputed = true;
19546
+ }
19547
+ if (used !== undefined && !usage.usedContexts.includes(used)) {
19548
+ usage.usedContexts.push(used);
19549
+ }
19550
+ }
19551
+ }
19552
+ };
19553
+ }
19554
+ function collectContextUsage(data, variableName) {
19555
+ var usage = {
19556
+ usedContexts: [],
19557
+ includesComputed: false
19558
+ };
19559
+ visitStoryboardExpressions(data, beforeVisitContextFactory(usage, variableName), variableName);
19560
+ return usage;
19561
+ }
19562
+
19563
+ function deferResolveContextConcurrently(contextConfs, resolveContext) {
19564
+ var keyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "CTX";
19565
+ var dependencyMap = getDependencyMapOfContext(contextConfs, keyword);
19566
+ var pendingDeps = new Set(Array.from(dependencyMap.keys()).map(contextConf => contextConf.name));
19567
+ var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
19568
+ var processed = new WeakSet();
19569
+ var deferredContexts = new Map();
19570
+ var pendingContexts = new Map([...new Set(contextConfs.map(contextConf => contextConf.name))].map(contextName => [contextName, new Promise((resolve, reject) => {
19571
+ deferredContexts.set(contextName, {
19572
+ resolve,
19573
+ reject
19574
+ });
19575
+ })]));
19576
+ var wrapResolve = /*#__PURE__*/function () {
19577
+ var _ref = _asyncToGenerator__default["default"](function* (contextConf) {
19578
+ processed.add(contextConf);
19579
+ var resolved = yield resolveContext(contextConf);
19580
+ dependencyMap.delete(contextConf);
19581
+ if (resolved) {
19582
+ deferredContexts.get(contextConf.name).resolve();
19583
+ if (!pendingDeps.delete(contextConf.name)) {
19584
+ throw new Error("Duplicated context defined: ".concat(contextConf.name));
19585
+ }
19586
+ }
19587
+ yield scheduleNext();
19588
+ });
19589
+ return function wrapResolve(_x) {
19590
+ return _ref.apply(this, arguments);
19591
+ };
19592
+ }();
19593
+ var scheduleAsSerial = includesComputed;
19594
+ function scheduleNext() {
19595
+ return _scheduleNext.apply(this, arguments);
19596
+ }
19597
+ function _scheduleNext() {
19598
+ _scheduleNext = _asyncToGenerator__default["default"](function* () {
19599
+ var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
19600
+ yield Promise.all(readyContexts.map(wrapResolve));
19601
+ });
19602
+ return _scheduleNext.apply(this, arguments);
19603
+ }
19604
+ var pendingResult = scheduleNext().then( /*#__PURE__*/_asyncToGenerator__default["default"](function* () {
19605
+ // If there are still contexts left, it implies one of these situations:
19606
+ // - Circular contexts.
19607
+ // Such as: a depends on b, while b depends on a.
19608
+ // - Related contexts are all ignored.
19609
+ // Such as: a depends on b,
19610
+ // while both them are ignore by a falsy result of `if`.
19611
+ if (dependencyMap.size > 0) {
19612
+ // This will throw if circular contexts detected.
19613
+ detectCircularContexts(dependencyMap, keyword);
19614
+ scheduleAsSerial = true;
19615
+ yield scheduleNext();
19616
+ }
19617
+ // There maybe ignored contexts which are still not fulfilled.
19618
+ // We treat them as RESOLVED.
19619
+ for (var deferred of deferredContexts.values()) {
19620
+ deferred.resolve();
19621
+ }
19622
+ })).catch(error => {
19623
+ // There maybe contexts left not fulfilled, when an error occurred.
19624
+ // We treat them as REJECTED.
19625
+ for (var deferred of deferredContexts.values()) {
19626
+ deferred.reject(error);
19627
+ }
19628
+ throw error;
19629
+ });
19630
+ return {
19631
+ pendingResult,
19632
+ pendingContexts
19633
+ };
19634
+ }
19635
+ function resolveContextConcurrently(_x2, _x3) {
19461
19636
  return _resolveContextConcurrently.apply(this, arguments);
19462
19637
  }
19463
19638
  function _resolveContextConcurrently() {
@@ -19468,7 +19643,7 @@
19468
19643
  var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
19469
19644
  var processed = new WeakSet();
19470
19645
  var wrapResolve = /*#__PURE__*/function () {
19471
- var _ref = _asyncToGenerator__default["default"](function* (contextConf) {
19646
+ var _ref3 = _asyncToGenerator__default["default"](function* (contextConf) {
19472
19647
  processed.add(contextConf);
19473
19648
  var resolved = yield resolveContext(contextConf);
19474
19649
  dependencyMap.delete(contextConf);
@@ -19479,20 +19654,20 @@
19479
19654
  }
19480
19655
  yield scheduleNext();
19481
19656
  });
19482
- return function wrapResolve(_x3) {
19483
- return _ref.apply(this, arguments);
19657
+ return function wrapResolve(_x4) {
19658
+ return _ref3.apply(this, arguments);
19484
19659
  };
19485
19660
  }();
19486
19661
  var scheduleAsSerial = includesComputed;
19487
19662
  function scheduleNext() {
19488
- return _scheduleNext.apply(this, arguments);
19663
+ return _scheduleNext2.apply(this, arguments);
19489
19664
  }
19490
- function _scheduleNext() {
19491
- _scheduleNext = _asyncToGenerator__default["default"](function* () {
19665
+ function _scheduleNext2() {
19666
+ _scheduleNext2 = _asyncToGenerator__default["default"](function* () {
19492
19667
  var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
19493
19668
  yield Promise.all(readyContexts.map(wrapResolve));
19494
19669
  });
19495
- return _scheduleNext.apply(this, arguments);
19670
+ return _scheduleNext2.apply(this, arguments);
19496
19671
  }
19497
19672
  yield scheduleNext();
19498
19673
 
@@ -19552,51 +19727,24 @@
19552
19727
  // So make them process sequentially, keep the same behavior as before.
19553
19728
  scheduleAsSerial ? index === 0 :
19554
19729
  // A context is ready when it has no pending dependencies.
19555
- !entry[1].dependencies.some(dep => pendingDeps.has(dep));
19730
+ !entry[1].usedContexts.some(dep => pendingDeps.has(dep));
19556
19731
  }
19557
19732
  function getDependencyMapOfContext(contextConfs) {
19558
19733
  var keyword = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "CTX";
19559
19734
  var depsMap = new Map();
19560
19735
  for (var _contextConf2 of contextConfs) {
19561
- var stats = {
19562
- dependencies: [],
19563
- includesComputed: false
19564
- };
19565
- if (!_contextConf2.property) {
19566
- visitStoryboardExpressions([_contextConf2.if, _contextConf2.value, _contextConf2.resolve], beforeVisitContextFactory$1(stats, keyword), keyword);
19567
- }
19736
+ var stats = collectContextUsage(_contextConf2.property ? null : [_contextConf2.if, _contextConf2.value, _contextConf2.resolve], keyword);
19568
19737
  depsMap.set(_contextConf2, stats);
19569
19738
  }
19570
19739
  return depsMap;
19571
19740
  }
19572
- function beforeVisitContextFactory$1(stats, keyword) {
19573
- return function beforeVisitContext(node, parent) {
19574
- if (node.name === keyword) {
19575
- var memberParent = parent[parent.length - 1];
19576
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19577
- var memberNode = memberParent.node;
19578
- var dep;
19579
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19580
- dep = memberNode.property.name;
19581
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19582
- dep = memberNode.property.value;
19583
- } else {
19584
- stats.includesComputed = true;
19585
- }
19586
- if (dep !== undefined && !stats.dependencies.includes(dep)) {
19587
- stats.dependencies.push(dep);
19588
- }
19589
- }
19590
- }
19591
- };
19592
- }
19593
19741
  function detectCircularContexts(dependencyMap, keyword) {
19594
19742
  var duplicatedMap = new Map(dependencyMap);
19595
19743
  var pendingDeps = new Set(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name));
19596
19744
  var next = () => {
19597
19745
  var processedAtLeastOne = false;
19598
19746
  for (var [_contextConf3, stats] of duplicatedMap.entries()) {
19599
- if (!stats.dependencies.some(dep => pendingDeps.has(dep))) {
19747
+ if (!stats.usedContexts.some(dep => pendingDeps.has(dep))) {
19600
19748
  duplicatedMap.delete(_contextConf3);
19601
19749
  pendingDeps.delete(_contextConf3.name);
19602
19750
  processedAtLeastOne = true;
@@ -19839,91 +19987,6 @@
19839
19987
  return Object.freeze(object);
19840
19988
  }
19841
19989
 
19842
- /**
19843
- * Get tracking CTX for an evaluable expression in `track context` mode.
19844
- *
19845
- * A `track context` mode is an evaluable expression which is a sequence expression
19846
- * starting with the exact literal string expression of "track context".
19847
- *
19848
- * @param raw - A raw string, which must be checked by `isEvaluable` first.
19849
- *
19850
- * @returns
19851
- *
19852
- * Returns used CTXs if `track context` mode is enabled, or returns false if not enabled or
19853
- * no CTX found.
19854
- *
19855
- * @example
19856
- *
19857
- * ```js
19858
- * trackContext('<% "track context", [CTX.hello, CTX.world] %>');
19859
- * // => ["hello", "world"]
19860
- *
19861
- * trackContext('<% [CTX.hello, CTX.world] %>');
19862
- * // => false
19863
- *
19864
- * trackContext('<% "track context", DATA.any %>');
19865
- * // => false
19866
- * ```
19867
- */
19868
- function trackContext(raw) {
19869
- return track(raw, "track context", "CTX");
19870
- }
19871
- function trackState(raw) {
19872
- return track(raw, "track state", "STATE");
19873
- }
19874
- function trackFormState(raw) {
19875
- return track(raw, "track formstate", "FORM_STATE");
19876
- }
19877
- function trackUsedContext(data) {
19878
- return trackUsed(data, "CTX");
19879
- }
19880
- function trackUsedState(data) {
19881
- return trackUsed(data, "STATE");
19882
- }
19883
- function track(raw, trackText, variableName) {
19884
- if (raw.includes(trackText)) {
19885
- var contexts = new Set();
19886
- var {
19887
- expression
19888
- } = preevaluate(raw, {
19889
- withParent: true,
19890
- hooks: {
19891
- beforeVisitGlobal: beforeVisitContextFactory(contexts, variableName)
19892
- }
19893
- });
19894
- var trackCtxExp;
19895
- if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
19896
- if (contexts.size > 0) {
19897
- return Array.from(contexts);
19898
- } else {
19899
- // eslint-disable-next-line no-console
19900
- console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
19901
- }
19902
- }
19903
- }
19904
- return false;
19905
- }
19906
- function trackUsed(data, variableName) {
19907
- var contexts = new Set();
19908
- visitStoryboardExpressions(data, beforeVisitContextFactory(contexts, variableName), variableName);
19909
- return Array.from(contexts);
19910
- }
19911
- function beforeVisitContextFactory(contexts, variableName) {
19912
- return function beforeVisitContext(node, parent) {
19913
- if (node.name === variableName) {
19914
- var memberParent = parent[parent.length - 1];
19915
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19916
- var memberNode = memberParent.node;
19917
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19918
- contexts.add(memberNode.property.name);
19919
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19920
- contexts.add(memberNode.property.value);
19921
- }
19922
- }
19923
- }
19924
- };
19925
- }
19926
-
19927
19990
  // The debounce function receives our function as a parameter
19928
19991
  function debounceByAnimationFrame(fn) {
19929
19992
  // This holds the requestAnimationFrame reference, so we can cancel it if we wish
@@ -20238,6 +20301,7 @@
20238
20301
  exports.asyncProcessBrick = asyncProcessBrick;
20239
20302
  exports.asyncProcessStoryboard = asyncProcessStoryboard;
20240
20303
  exports.collectBricksByCustomTemplates = collectBricksByCustomTemplates;
20304
+ exports.collectContextUsage = collectContextUsage;
20241
20305
  exports.computeConstantCondition = computeConstantCondition;
20242
20306
  exports.computeRealRoutePath = computeRealRoutePath;
20243
20307
  exports.convertValueByPrecision = convertValueByPrecision;
@@ -20245,6 +20309,7 @@
20245
20309
  exports.createProviderClass = createProviderClass;
20246
20310
  exports.debounceByAnimationFrame = debounceByAnimationFrame;
20247
20311
  exports.deepFreeze = deepFreeze;
20312
+ exports.deferResolveContextConcurrently = deferResolveContextConcurrently;
20248
20313
  exports.formatValue = formatValue;
20249
20314
  exports.getDependencyMapOfContext = getDependencyMapOfContext;
20250
20315
  exports.getDepsOfTemplates = getDepsOfTemplates;