@next-core/brick-utils 2.45.15 → 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.
@@ -19461,7 +19461,178 @@
19461
19461
  convertUnitValueByPrecision: convertValueByPrecision
19462
19462
  } = pipes.utils;
19463
19463
 
19464
- 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) {
19465
19636
  return _resolveContextConcurrently.apply(this, arguments);
19466
19637
  }
19467
19638
  function _resolveContextConcurrently() {
@@ -19472,7 +19643,7 @@
19472
19643
  var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
19473
19644
  var processed = new WeakSet();
19474
19645
  var wrapResolve = /*#__PURE__*/function () {
19475
- var _ref = _asyncToGenerator__default["default"](function* (contextConf) {
19646
+ var _ref3 = _asyncToGenerator__default["default"](function* (contextConf) {
19476
19647
  processed.add(contextConf);
19477
19648
  var resolved = yield resolveContext(contextConf);
19478
19649
  dependencyMap.delete(contextConf);
@@ -19483,20 +19654,20 @@
19483
19654
  }
19484
19655
  yield scheduleNext();
19485
19656
  });
19486
- return function wrapResolve(_x3) {
19487
- return _ref.apply(this, arguments);
19657
+ return function wrapResolve(_x4) {
19658
+ return _ref3.apply(this, arguments);
19488
19659
  };
19489
19660
  }();
19490
19661
  var scheduleAsSerial = includesComputed;
19491
19662
  function scheduleNext() {
19492
- return _scheduleNext.apply(this, arguments);
19663
+ return _scheduleNext2.apply(this, arguments);
19493
19664
  }
19494
- function _scheduleNext() {
19495
- _scheduleNext = _asyncToGenerator__default["default"](function* () {
19665
+ function _scheduleNext2() {
19666
+ _scheduleNext2 = _asyncToGenerator__default["default"](function* () {
19496
19667
  var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
19497
19668
  yield Promise.all(readyContexts.map(wrapResolve));
19498
19669
  });
19499
- return _scheduleNext.apply(this, arguments);
19670
+ return _scheduleNext2.apply(this, arguments);
19500
19671
  }
19501
19672
  yield scheduleNext();
19502
19673
 
@@ -19556,51 +19727,24 @@
19556
19727
  // So make them process sequentially, keep the same behavior as before.
19557
19728
  scheduleAsSerial ? index === 0 :
19558
19729
  // A context is ready when it has no pending dependencies.
19559
- !entry[1].dependencies.some(dep => pendingDeps.has(dep));
19730
+ !entry[1].usedContexts.some(dep => pendingDeps.has(dep));
19560
19731
  }
19561
19732
  function getDependencyMapOfContext(contextConfs) {
19562
19733
  var keyword = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "CTX";
19563
19734
  var depsMap = new Map();
19564
19735
  for (var _contextConf2 of contextConfs) {
19565
- var stats = {
19566
- dependencies: [],
19567
- includesComputed: false
19568
- };
19569
- if (!_contextConf2.property) {
19570
- visitStoryboardExpressions([_contextConf2.if, _contextConf2.value, _contextConf2.resolve], beforeVisitContextFactory$1(stats, keyword), keyword);
19571
- }
19736
+ var stats = collectContextUsage(_contextConf2.property ? null : [_contextConf2.if, _contextConf2.value, _contextConf2.resolve], keyword);
19572
19737
  depsMap.set(_contextConf2, stats);
19573
19738
  }
19574
19739
  return depsMap;
19575
19740
  }
19576
- function beforeVisitContextFactory$1(stats, keyword) {
19577
- return function beforeVisitContext(node, parent) {
19578
- if (node.name === keyword) {
19579
- var memberParent = parent[parent.length - 1];
19580
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19581
- var memberNode = memberParent.node;
19582
- var dep;
19583
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19584
- dep = memberNode.property.name;
19585
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19586
- dep = memberNode.property.value;
19587
- } else {
19588
- stats.includesComputed = true;
19589
- }
19590
- if (dep !== undefined && !stats.dependencies.includes(dep)) {
19591
- stats.dependencies.push(dep);
19592
- }
19593
- }
19594
- }
19595
- };
19596
- }
19597
19741
  function detectCircularContexts(dependencyMap, keyword) {
19598
19742
  var duplicatedMap = new Map(dependencyMap);
19599
19743
  var pendingDeps = new Set(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name));
19600
19744
  var next = () => {
19601
19745
  var processedAtLeastOne = false;
19602
19746
  for (var [_contextConf3, stats] of duplicatedMap.entries()) {
19603
- if (!stats.dependencies.some(dep => pendingDeps.has(dep))) {
19747
+ if (!stats.usedContexts.some(dep => pendingDeps.has(dep))) {
19604
19748
  duplicatedMap.delete(_contextConf3);
19605
19749
  pendingDeps.delete(_contextConf3.name);
19606
19750
  processedAtLeastOne = true;
@@ -19843,91 +19987,6 @@
19843
19987
  return Object.freeze(object);
19844
19988
  }
19845
19989
 
19846
- /**
19847
- * Get tracking CTX for an evaluable expression in `track context` mode.
19848
- *
19849
- * A `track context` mode is an evaluable expression which is a sequence expression
19850
- * starting with the exact literal string expression of "track context".
19851
- *
19852
- * @param raw - A raw string, which must be checked by `isEvaluable` first.
19853
- *
19854
- * @returns
19855
- *
19856
- * Returns used CTXs if `track context` mode is enabled, or returns false if not enabled or
19857
- * no CTX found.
19858
- *
19859
- * @example
19860
- *
19861
- * ```js
19862
- * trackContext('<% "track context", [CTX.hello, CTX.world] %>');
19863
- * // => ["hello", "world"]
19864
- *
19865
- * trackContext('<% [CTX.hello, CTX.world] %>');
19866
- * // => false
19867
- *
19868
- * trackContext('<% "track context", DATA.any %>');
19869
- * // => false
19870
- * ```
19871
- */
19872
- function trackContext(raw) {
19873
- return track(raw, "track context", "CTX");
19874
- }
19875
- function trackState(raw) {
19876
- return track(raw, "track state", "STATE");
19877
- }
19878
- function trackFormState(raw) {
19879
- return track(raw, "track formstate", "FORM_STATE");
19880
- }
19881
- function trackUsedContext(data) {
19882
- return trackUsed(data, "CTX");
19883
- }
19884
- function trackUsedState(data) {
19885
- return trackUsed(data, "STATE");
19886
- }
19887
- function track(raw, trackText, variableName) {
19888
- if (raw.includes(trackText)) {
19889
- var contexts = new Set();
19890
- var {
19891
- expression
19892
- } = preevaluate(raw, {
19893
- withParent: true,
19894
- hooks: {
19895
- beforeVisitGlobal: beforeVisitContextFactory(contexts, variableName)
19896
- }
19897
- });
19898
- var trackCtxExp;
19899
- if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
19900
- if (contexts.size > 0) {
19901
- return Array.from(contexts);
19902
- } else {
19903
- // eslint-disable-next-line no-console
19904
- console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
19905
- }
19906
- }
19907
- }
19908
- return false;
19909
- }
19910
- function trackUsed(data, variableName) {
19911
- var contexts = new Set();
19912
- visitStoryboardExpressions(data, beforeVisitContextFactory(contexts, variableName), variableName);
19913
- return Array.from(contexts);
19914
- }
19915
- function beforeVisitContextFactory(contexts, variableName) {
19916
- return function beforeVisitContext(node, parent) {
19917
- if (node.name === variableName) {
19918
- var memberParent = parent[parent.length - 1];
19919
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19920
- var memberNode = memberParent.node;
19921
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19922
- contexts.add(memberNode.property.name);
19923
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19924
- contexts.add(memberNode.property.value);
19925
- }
19926
- }
19927
- }
19928
- };
19929
- }
19930
-
19931
19990
  // The debounce function receives our function as a parameter
19932
19991
  function debounceByAnimationFrame(fn) {
19933
19992
  // This holds the requestAnimationFrame reference, so we can cancel it if we wish
@@ -20242,6 +20301,7 @@
20242
20301
  exports.asyncProcessBrick = asyncProcessBrick;
20243
20302
  exports.asyncProcessStoryboard = asyncProcessStoryboard;
20244
20303
  exports.collectBricksByCustomTemplates = collectBricksByCustomTemplates;
20304
+ exports.collectContextUsage = collectContextUsage;
20245
20305
  exports.computeConstantCondition = computeConstantCondition;
20246
20306
  exports.computeRealRoutePath = computeRealRoutePath;
20247
20307
  exports.convertValueByPrecision = convertValueByPrecision;
@@ -20249,6 +20309,7 @@
20249
20309
  exports.createProviderClass = createProviderClass;
20250
20310
  exports.debounceByAnimationFrame = debounceByAnimationFrame;
20251
20311
  exports.deepFreeze = deepFreeze;
20312
+ exports.deferResolveContextConcurrently = deferResolveContextConcurrently;
20252
20313
  exports.formatValue = formatValue;
20253
20314
  exports.getDependencyMapOfContext = getDependencyMapOfContext;
20254
20315
  exports.getDepsOfTemplates = getDepsOfTemplates;