@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.
package/dist/index.esm.js CHANGED
@@ -19456,7 +19456,178 @@ var {
19456
19456
  convertUnitValueByPrecision: convertValueByPrecision
19457
19457
  } = utils;
19458
19458
 
19459
- function resolveContextConcurrently(_x, _x2) {
19459
+ /**
19460
+ * Get tracking CTX for an evaluable expression in `track context` mode.
19461
+ *
19462
+ * A `track context` mode is an evaluable expression which is a sequence expression
19463
+ * starting with the exact literal string expression of "track context".
19464
+ *
19465
+ * @param raw - A raw string, which must be checked by `isEvaluable` first.
19466
+ *
19467
+ * @returns
19468
+ *
19469
+ * Returns used CTXs if `track context` mode is enabled, or returns false if not enabled or
19470
+ * no CTX found.
19471
+ *
19472
+ * @example
19473
+ *
19474
+ * ```js
19475
+ * trackContext('<% "track context", [CTX.hello, CTX.world] %>');
19476
+ * // => ["hello", "world"]
19477
+ *
19478
+ * trackContext('<% [CTX.hello, CTX.world] %>');
19479
+ * // => false
19480
+ *
19481
+ * trackContext('<% "track context", DATA.any %>');
19482
+ * // => false
19483
+ * ```
19484
+ */
19485
+ function trackContext(raw) {
19486
+ return track(raw, "track context", "CTX");
19487
+ }
19488
+ function trackState(raw) {
19489
+ return track(raw, "track state", "STATE");
19490
+ }
19491
+ function trackFormState(raw) {
19492
+ return track(raw, "track formstate", "FORM_STATE");
19493
+ }
19494
+ function trackUsedContext(data) {
19495
+ return collectContextUsage(data, "CTX").usedContexts;
19496
+ }
19497
+ function trackUsedState(data) {
19498
+ return collectContextUsage(data, "STATE").usedContexts;
19499
+ }
19500
+ function track(raw, trackText, variableName) {
19501
+ if (raw.includes(trackText)) {
19502
+ // const contexts = new Set<string>();
19503
+ var usage = {
19504
+ usedContexts: [],
19505
+ includesComputed: false
19506
+ };
19507
+ var {
19508
+ expression
19509
+ } = preevaluate(raw, {
19510
+ withParent: true,
19511
+ hooks: {
19512
+ beforeVisitGlobal: beforeVisitContextFactory(usage, variableName)
19513
+ }
19514
+ });
19515
+ // const contexts = usage
19516
+ var trackCtxExp;
19517
+ if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
19518
+ if (usage.usedContexts.length > 0) {
19519
+ return usage.usedContexts;
19520
+ } else {
19521
+ // eslint-disable-next-line no-console
19522
+ console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
19523
+ }
19524
+ }
19525
+ }
19526
+ return false;
19527
+ }
19528
+ function beforeVisitContextFactory(usage, variableName) {
19529
+ return function beforeVisitContext(node, parent) {
19530
+ if (node.name === variableName) {
19531
+ var memberParent = parent[parent.length - 1];
19532
+ if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19533
+ var memberNode = memberParent.node;
19534
+ var used;
19535
+ if (!memberNode.computed && memberNode.property.type === "Identifier") {
19536
+ used = memberNode.property.name;
19537
+ } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19538
+ used = memberNode.property.value;
19539
+ } else {
19540
+ usage.includesComputed = true;
19541
+ }
19542
+ if (used !== undefined && !usage.usedContexts.includes(used)) {
19543
+ usage.usedContexts.push(used);
19544
+ }
19545
+ }
19546
+ }
19547
+ };
19548
+ }
19549
+ function collectContextUsage(data, variableName) {
19550
+ var usage = {
19551
+ usedContexts: [],
19552
+ includesComputed: false
19553
+ };
19554
+ visitStoryboardExpressions(data, beforeVisitContextFactory(usage, variableName), variableName);
19555
+ return usage;
19556
+ }
19557
+
19558
+ function deferResolveContextConcurrently(contextConfs, resolveContext) {
19559
+ var keyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "CTX";
19560
+ var dependencyMap = getDependencyMapOfContext(contextConfs, keyword);
19561
+ var pendingDeps = new Set(Array.from(dependencyMap.keys()).map(contextConf => contextConf.name));
19562
+ var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
19563
+ var processed = new WeakSet();
19564
+ var deferredContexts = new Map();
19565
+ var pendingContexts = new Map([...new Set(contextConfs.map(contextConf => contextConf.name))].map(contextName => [contextName, new Promise((resolve, reject) => {
19566
+ deferredContexts.set(contextName, {
19567
+ resolve,
19568
+ reject
19569
+ });
19570
+ })]));
19571
+ var wrapResolve = /*#__PURE__*/function () {
19572
+ var _ref = _asyncToGenerator(function* (contextConf) {
19573
+ processed.add(contextConf);
19574
+ var resolved = yield resolveContext(contextConf);
19575
+ dependencyMap.delete(contextConf);
19576
+ if (resolved) {
19577
+ deferredContexts.get(contextConf.name).resolve();
19578
+ if (!pendingDeps.delete(contextConf.name)) {
19579
+ throw new Error("Duplicated context defined: ".concat(contextConf.name));
19580
+ }
19581
+ }
19582
+ yield scheduleNext();
19583
+ });
19584
+ return function wrapResolve(_x) {
19585
+ return _ref.apply(this, arguments);
19586
+ };
19587
+ }();
19588
+ var scheduleAsSerial = includesComputed;
19589
+ function scheduleNext() {
19590
+ return _scheduleNext.apply(this, arguments);
19591
+ }
19592
+ function _scheduleNext() {
19593
+ _scheduleNext = _asyncToGenerator(function* () {
19594
+ var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
19595
+ yield Promise.all(readyContexts.map(wrapResolve));
19596
+ });
19597
+ return _scheduleNext.apply(this, arguments);
19598
+ }
19599
+ var pendingResult = scheduleNext().then( /*#__PURE__*/_asyncToGenerator(function* () {
19600
+ // If there are still contexts left, it implies one of these situations:
19601
+ // - Circular contexts.
19602
+ // Such as: a depends on b, while b depends on a.
19603
+ // - Related contexts are all ignored.
19604
+ // Such as: a depends on b,
19605
+ // while both them are ignore by a falsy result of `if`.
19606
+ if (dependencyMap.size > 0) {
19607
+ // This will throw if circular contexts detected.
19608
+ detectCircularContexts(dependencyMap, keyword);
19609
+ scheduleAsSerial = true;
19610
+ yield scheduleNext();
19611
+ }
19612
+ // There maybe ignored contexts which are still not fulfilled.
19613
+ // We treat them as RESOLVED.
19614
+ for (var deferred of deferredContexts.values()) {
19615
+ deferred.resolve();
19616
+ }
19617
+ })).catch(error => {
19618
+ // There maybe contexts left not fulfilled, when an error occurred.
19619
+ // We treat them as REJECTED.
19620
+ for (var deferred of deferredContexts.values()) {
19621
+ deferred.reject(error);
19622
+ }
19623
+ throw error;
19624
+ });
19625
+ return {
19626
+ pendingResult,
19627
+ pendingContexts
19628
+ };
19629
+ }
19630
+ function resolveContextConcurrently(_x2, _x3) {
19460
19631
  return _resolveContextConcurrently.apply(this, arguments);
19461
19632
  }
19462
19633
  function _resolveContextConcurrently() {
@@ -19467,7 +19638,7 @@ function _resolveContextConcurrently() {
19467
19638
  var includesComputed = Array.from(dependencyMap.values()).some(stats => stats.includesComputed);
19468
19639
  var processed = new WeakSet();
19469
19640
  var wrapResolve = /*#__PURE__*/function () {
19470
- var _ref = _asyncToGenerator(function* (contextConf) {
19641
+ var _ref3 = _asyncToGenerator(function* (contextConf) {
19471
19642
  processed.add(contextConf);
19472
19643
  var resolved = yield resolveContext(contextConf);
19473
19644
  dependencyMap.delete(contextConf);
@@ -19478,20 +19649,20 @@ function _resolveContextConcurrently() {
19478
19649
  }
19479
19650
  yield scheduleNext();
19480
19651
  });
19481
- return function wrapResolve(_x3) {
19482
- return _ref.apply(this, arguments);
19652
+ return function wrapResolve(_x4) {
19653
+ return _ref3.apply(this, arguments);
19483
19654
  };
19484
19655
  }();
19485
19656
  var scheduleAsSerial = includesComputed;
19486
19657
  function scheduleNext() {
19487
- return _scheduleNext.apply(this, arguments);
19658
+ return _scheduleNext2.apply(this, arguments);
19488
19659
  }
19489
- function _scheduleNext() {
19490
- _scheduleNext = _asyncToGenerator(function* () {
19660
+ function _scheduleNext2() {
19661
+ _scheduleNext2 = _asyncToGenerator(function* () {
19491
19662
  var readyContexts = Array.from(dependencyMap.entries()).filter(predicateNextResolveFactory(pendingDeps, scheduleAsSerial)).map(entry => entry[0]).filter(contextConf => !processed.has(contextConf));
19492
19663
  yield Promise.all(readyContexts.map(wrapResolve));
19493
19664
  });
19494
- return _scheduleNext.apply(this, arguments);
19665
+ return _scheduleNext2.apply(this, arguments);
19495
19666
  }
19496
19667
  yield scheduleNext();
19497
19668
 
@@ -19551,51 +19722,24 @@ function predicateNextResolveFactory(pendingDeps, scheduleAsSerial) {
19551
19722
  // So make them process sequentially, keep the same behavior as before.
19552
19723
  scheduleAsSerial ? index === 0 :
19553
19724
  // A context is ready when it has no pending dependencies.
19554
- !entry[1].dependencies.some(dep => pendingDeps.has(dep));
19725
+ !entry[1].usedContexts.some(dep => pendingDeps.has(dep));
19555
19726
  }
19556
19727
  function getDependencyMapOfContext(contextConfs) {
19557
19728
  var keyword = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "CTX";
19558
19729
  var depsMap = new Map();
19559
19730
  for (var _contextConf2 of contextConfs) {
19560
- var stats = {
19561
- dependencies: [],
19562
- includesComputed: false
19563
- };
19564
- if (!_contextConf2.property) {
19565
- visitStoryboardExpressions([_contextConf2.if, _contextConf2.value, _contextConf2.resolve], beforeVisitContextFactory$1(stats, keyword), keyword);
19566
- }
19731
+ var stats = collectContextUsage(_contextConf2.property ? null : [_contextConf2.if, _contextConf2.value, _contextConf2.resolve], keyword);
19567
19732
  depsMap.set(_contextConf2, stats);
19568
19733
  }
19569
19734
  return depsMap;
19570
19735
  }
19571
- function beforeVisitContextFactory$1(stats, keyword) {
19572
- return function beforeVisitContext(node, parent) {
19573
- if (node.name === keyword) {
19574
- var memberParent = parent[parent.length - 1];
19575
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19576
- var memberNode = memberParent.node;
19577
- var dep;
19578
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19579
- dep = memberNode.property.name;
19580
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19581
- dep = memberNode.property.value;
19582
- } else {
19583
- stats.includesComputed = true;
19584
- }
19585
- if (dep !== undefined && !stats.dependencies.includes(dep)) {
19586
- stats.dependencies.push(dep);
19587
- }
19588
- }
19589
- }
19590
- };
19591
- }
19592
19736
  function detectCircularContexts(dependencyMap, keyword) {
19593
19737
  var duplicatedMap = new Map(dependencyMap);
19594
19738
  var pendingDeps = new Set(Array.from(duplicatedMap.keys()).map(contextConf => contextConf.name));
19595
19739
  var next = () => {
19596
19740
  var processedAtLeastOne = false;
19597
19741
  for (var [_contextConf3, stats] of duplicatedMap.entries()) {
19598
- if (!stats.dependencies.some(dep => pendingDeps.has(dep))) {
19742
+ if (!stats.usedContexts.some(dep => pendingDeps.has(dep))) {
19599
19743
  duplicatedMap.delete(_contextConf3);
19600
19744
  pendingDeps.delete(_contextConf3.name);
19601
19745
  processedAtLeastOne = true;
@@ -19838,91 +19982,6 @@ function deepFreeze(object) {
19838
19982
  return Object.freeze(object);
19839
19983
  }
19840
19984
 
19841
- /**
19842
- * Get tracking CTX for an evaluable expression in `track context` mode.
19843
- *
19844
- * A `track context` mode is an evaluable expression which is a sequence expression
19845
- * starting with the exact literal string expression of "track context".
19846
- *
19847
- * @param raw - A raw string, which must be checked by `isEvaluable` first.
19848
- *
19849
- * @returns
19850
- *
19851
- * Returns used CTXs if `track context` mode is enabled, or returns false if not enabled or
19852
- * no CTX found.
19853
- *
19854
- * @example
19855
- *
19856
- * ```js
19857
- * trackContext('<% "track context", [CTX.hello, CTX.world] %>');
19858
- * // => ["hello", "world"]
19859
- *
19860
- * trackContext('<% [CTX.hello, CTX.world] %>');
19861
- * // => false
19862
- *
19863
- * trackContext('<% "track context", DATA.any %>');
19864
- * // => false
19865
- * ```
19866
- */
19867
- function trackContext(raw) {
19868
- return track(raw, "track context", "CTX");
19869
- }
19870
- function trackState(raw) {
19871
- return track(raw, "track state", "STATE");
19872
- }
19873
- function trackFormState(raw) {
19874
- return track(raw, "track formstate", "FORM_STATE");
19875
- }
19876
- function trackUsedContext(data) {
19877
- return trackUsed(data, "CTX");
19878
- }
19879
- function trackUsedState(data) {
19880
- return trackUsed(data, "STATE");
19881
- }
19882
- function track(raw, trackText, variableName) {
19883
- if (raw.includes(trackText)) {
19884
- var contexts = new Set();
19885
- var {
19886
- expression
19887
- } = preevaluate(raw, {
19888
- withParent: true,
19889
- hooks: {
19890
- beforeVisitGlobal: beforeVisitContextFactory(contexts, variableName)
19891
- }
19892
- });
19893
- var trackCtxExp;
19894
- if (expression.type === "SequenceExpression" && (trackCtxExp = expression.expressions[0]) && trackCtxExp.type === "Literal" && trackCtxExp.value === trackText) {
19895
- if (contexts.size > 0) {
19896
- return Array.from(contexts);
19897
- } else {
19898
- // eslint-disable-next-line no-console
19899
- console.warn("You are using \"".concat(trackText, "\" but no `").concat(variableName, "` usage found in your expression: ").concat(JSON.stringify(raw)));
19900
- }
19901
- }
19902
- }
19903
- return false;
19904
- }
19905
- function trackUsed(data, variableName) {
19906
- var contexts = new Set();
19907
- visitStoryboardExpressions(data, beforeVisitContextFactory(contexts, variableName), variableName);
19908
- return Array.from(contexts);
19909
- }
19910
- function beforeVisitContextFactory(contexts, variableName) {
19911
- return function beforeVisitContext(node, parent) {
19912
- if (node.name === variableName) {
19913
- var memberParent = parent[parent.length - 1];
19914
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
19915
- var memberNode = memberParent.node;
19916
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
19917
- contexts.add(memberNode.property.name);
19918
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
19919
- contexts.add(memberNode.property.value);
19920
- }
19921
- }
19922
- }
19923
- };
19924
- }
19925
-
19926
19985
  // The debounce function receives our function as a parameter
19927
19986
  function debounceByAnimationFrame(fn) {
19928
19987
  // This holds the requestAnimationFrame reference, so we can cancel it if we wish
@@ -20227,5 +20286,5 @@ function isConstantLogical(node, expect, options) {
20227
20286
  return node.type === "LogicalExpression" ? node.operator === (expect ? "||" : "&&") && [node.left, node.right].some(item => isConstantLogical(item, expect, options)) : node.type === "UnaryExpression" ? node.operator === "!" && isConstantLogical(node.argument, !expect, options) : node.type === "Literal" ? !!node.value === expect : node.type === "Identifier" ? node.name === "undefined" ? !expect : false : constantFeatureFlags && node.type === "MemberExpression" && node.object.type === "Identifier" && node.object.name === "FLAGS" && (node.computed ? node.property.type === "Literal" && typeof node.property.value === "string" && !!featureFlags[node.property.value] === expect : node.property.type === "Identifier" && !!featureFlags[node.property.name] === expect);
20228
20287
  }
20229
20288
 
20230
- export { JsonStorage, PrecookFunctionVisitor, PrecookVisitor, asyncProcessBrick, asyncProcessStoryboard, collectBricksByCustomTemplates, computeConstantCondition, computeRealRoutePath, convertValueByPrecision, cook, createProviderClass, debounceByAnimationFrame, deepFreeze, formatValue, getDependencyMapOfContext, getDepsOfTemplates, getDllAndDepsByResource, getDllAndDepsOfBricks, getDllAndDepsOfStoryboard, getTemplateDepsOfStoryboard, hasOwnProperty$1 as hasOwnProperty, inject, isBrickNode, isCustomTemplateNode, isEvaluable, isObject$1 as isObject, isRouteNode, isSnippetNode, lint, loadScript, makeThrottledAggregation, mapCustomApisToNameAndNamespace, matchPath, normalizeBuilderNode, normalizeMenu, parseBrick, parseForAnalysis, parseRoutes, parseStoryboard, parseTemplate, parseTemplates, precook, precookFunction, preevaluate, prefetchScript, removeDeadConditions, removeDeadConditionsInTpl, resolveContextConcurrently, restoreDynamicTemplates, scanAppGetMenuInAny, scanAppGetMenuInStoryboard, scanBricksInBrickConf, scanBricksInStoryboard, scanCustomApisInStoryboard, scanI18NInAny, scanI18NInStoryboard, scanInstalledAppsInStoryboard, scanPermissionActionsInAny, scanPermissionActionsInStoryboard, scanProcessorsInAny, scanProcessorsInStoryboard, scanRouteAliasInStoryboard, scanStoryboard, scanStoryboardAst, scanTemplatesInBrick, scanTemplatesInStoryboard, shouldAllowRecursiveEvaluations, smartDisplayForEvaluableString, syncResolveContextConcurrently, toPath, tokTypes_1 as tokTypes, trackContext, trackFormState, trackState, trackUsedContext, trackUsedState, transform, transformAndInject, traverse, traverseStoryboard, visitStoryboardExpressions, visitStoryboardFunctions };
20289
+ export { JsonStorage, PrecookFunctionVisitor, PrecookVisitor, asyncProcessBrick, asyncProcessStoryboard, collectBricksByCustomTemplates, collectContextUsage, computeConstantCondition, computeRealRoutePath, convertValueByPrecision, cook, createProviderClass, debounceByAnimationFrame, deepFreeze, deferResolveContextConcurrently, formatValue, getDependencyMapOfContext, getDepsOfTemplates, getDllAndDepsByResource, getDllAndDepsOfBricks, getDllAndDepsOfStoryboard, getTemplateDepsOfStoryboard, hasOwnProperty$1 as hasOwnProperty, inject, isBrickNode, isCustomTemplateNode, isEvaluable, isObject$1 as isObject, isRouteNode, isSnippetNode, lint, loadScript, makeThrottledAggregation, mapCustomApisToNameAndNamespace, matchPath, normalizeBuilderNode, normalizeMenu, parseBrick, parseForAnalysis, parseRoutes, parseStoryboard, parseTemplate, parseTemplates, precook, precookFunction, preevaluate, prefetchScript, removeDeadConditions, removeDeadConditionsInTpl, resolveContextConcurrently, restoreDynamicTemplates, scanAppGetMenuInAny, scanAppGetMenuInStoryboard, scanBricksInBrickConf, scanBricksInStoryboard, scanCustomApisInStoryboard, scanI18NInAny, scanI18NInStoryboard, scanInstalledAppsInStoryboard, scanPermissionActionsInAny, scanPermissionActionsInStoryboard, scanProcessorsInAny, scanProcessorsInStoryboard, scanRouteAliasInStoryboard, scanStoryboard, scanStoryboardAst, scanTemplatesInBrick, scanTemplatesInStoryboard, shouldAllowRecursiveEvaluations, smartDisplayForEvaluableString, syncResolveContextConcurrently, toPath, tokTypes_1 as tokTypes, trackContext, trackFormState, trackState, trackUsedContext, trackUsedState, transform, transformAndInject, traverse, traverseStoryboard, visitStoryboardExpressions, visitStoryboardFunctions };
20231
20290
  //# sourceMappingURL=index.esm.js.map