@lotics/app-sdk 0.127.3 → 0.127.5

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.
Files changed (2) hide show
  1. package/dist/index.js +65 -38
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -809,6 +809,8 @@ var MAX_RANDOM_LENGTH = 1e3;
809
809
  var MAX_RUN_BUILT_BYTES = 512 * 1024 * 1024;
810
810
  var MAX_IN_PLACE_BYTES = 1024 * 1024;
811
811
  var MAX_IN_PLACE_HELPER_BYTES = 8 * 1024;
812
+ var MAX_IN_PLACE_MS = 5;
813
+ var IN_PLACE_GAP_MS = 1;
812
814
  var MAX_PERSISTED_JSON_LENGTH = 64 * 1024 * 1024;
813
815
  var ExpressionArgumentError = class extends Error {
814
816
  constructor(message2) {
@@ -961,6 +963,20 @@ function getFormulaValue(value) {
961
963
  if (isFormulaError(value)) return null;
962
964
  return value;
963
965
  }
966
+ var NO_LINKED_ROWS = /* @__PURE__ */ new Map();
967
+ function prepareLinkedRows(records) {
968
+ return new Map(
969
+ [...records].map(([tableId, rows]) => {
970
+ const positions = /* @__PURE__ */ new Map();
971
+ for (let at2 = 0; at2 < rows.length; at2++) {
972
+ const held2 = positions.get(rows[at2].id);
973
+ if (held2 === void 0) positions.set(rows[at2].id, [at2]);
974
+ else held2.push(at2);
975
+ }
976
+ return [tableId, { rows, positions }];
977
+ })
978
+ );
979
+ }
964
980
 
965
981
  // ../shared/src/lookup_display.ts
966
982
  function flattenLookupValues(values2) {
@@ -8820,8 +8836,10 @@ var WorkflowTimeBudgetError = class extends Error {
8820
8836
  /** `waitedFor`: what the run's deadline passed waiting on, when that was not the run's own work. */
8821
8837
  constructor(subject, budgetMs, waitedFor) {
8822
8838
  super(`${subject} exceeded its time budget of ${budgetMs / 1e3}s${waitedFor === void 0 ? "" : ` waiting for ${waitedFor}`}.`);
8839
+ this.budgetMs = budgetMs;
8823
8840
  this.name = "WorkflowTimeBudgetError";
8824
8841
  }
8842
+ budgetMs;
8825
8843
  };
8826
8844
  var WorkflowExpressionBudgetError = class extends Error {
8827
8845
  constructor(budget) {
@@ -8858,11 +8876,11 @@ var turn = {
8858
8876
  function startRunBudget(subject, budgetMs, path) {
8859
8877
  return { host: "shared", deadline_ms: Date.now() + budgetMs, budget_ms: budgetMs, subject, path, built_bytes: 0 };
8860
8878
  }
8861
- function runClock(run2) {
8879
+ function runClock(run2, yields = true) {
8862
8880
  if (Date.now() > run2.deadline_ms) {
8863
8881
  throw new WorkflowTimeBudgetError(run2.subject, run2.budget_ms);
8864
8882
  }
8865
- if (run2.host === "isolated") return null;
8883
+ if (run2.host === "isolated" || !yields) return null;
8866
8884
  if (turn.pause === null && performance.now() - turn.slice_started_ms >= EVALUATION_SLICE_MS) {
8867
8885
  turn.pause = new Promise((resolve) => setTimeout(resolve, 0)).then(() => {
8868
8886
  turn.pause = null;
@@ -8958,13 +8976,13 @@ function parsedSize(text2) {
8958
8976
  return text2.length * CHAR_BYTES + nodes * PARSED_NODE_BYTES;
8959
8977
  }
8960
8978
  function assertCheap(budget, ...values2) {
8961
- assertWithin(budget, MAX_IN_PLACE_BYTES, values2);
8979
+ assertWithin(budget, MAX_IN_PLACE_BYTES, values2, writtenSize);
8962
8980
  }
8963
- function assertWithin(budget, limit, values2) {
8981
+ function assertWithin(budget, limit, values2, measure) {
8964
8982
  if (!budget.bounded) return;
8965
8983
  let bytes = 0;
8966
8984
  for (const value of values2) {
8967
- bytes += writtenSize(value, limit - bytes);
8985
+ bytes += measure(value, limit - bytes);
8968
8986
  if (bytes > limit) throw new ExpressionNotCheapError();
8969
8987
  }
8970
8988
  }
@@ -8990,9 +9008,21 @@ function callbackWorkers() {
8990
9008
  };
8991
9009
  }
8992
9010
  function evaluateWorkflowExpression(expr2, ctx, options = {}) {
8993
- const budget = { workers: callbackWorkers(), bounded: options.bounded === true, built: 0, clock_checks: 0, helpers: /* @__PURE__ */ new Map(), settled: false };
9011
+ const budget = {
9012
+ workers: callbackWorkers(),
9013
+ bounded: options.bounded === true,
9014
+ built: 0,
9015
+ clock_checks: 0,
9016
+ nodes: 0,
9017
+ last_node_ms: options.bounded === true ? performance.now() : 0,
9018
+ sync_ms: 0,
9019
+ over_time: false,
9020
+ helpers: /* @__PURE__ */ new Map(),
9021
+ settled: false
9022
+ };
8994
9023
  return evaluateRoot(expr2, { ...ctx, evaluation_budget: budget, anchor_context: evalAnchorContext(ctx) }).finally(() => {
8995
9024
  budget.settled = true;
9025
+ options.onNodes?.(budget.nodes);
8996
9026
  }).then((answer) => {
8997
9027
  if (budget.bounded && expandedSize(answer, MAX_IN_PLACE_BYTES) > MAX_IN_PLACE_BYTES) throw new ExpressionNotCheapError();
8998
9028
  return answer;
@@ -9013,7 +9043,10 @@ async function startBeside(expr2, ctx) {
9013
9043
  return evaluateExpression(expr2, ctx);
9014
9044
  }
9015
9045
  function evaluateExpression(expr2, ctx) {
9016
- assertUnsettled(ctx.evaluation_budget);
9046
+ const budget = ctx.evaluation_budget;
9047
+ assertUnsettled(budget);
9048
+ budget.nodes += 1;
9049
+ if (budget.bounded) assertTime(budget);
9017
9050
  switch (expr2.kind) {
9018
9051
  case "lit":
9019
9052
  return expr2.value;
@@ -9041,6 +9074,16 @@ function evaluateExpression(expr2, ctx) {
9041
9074
  return assertNever(expr2);
9042
9075
  }
9043
9076
  }
9077
+ function assertTime(budget) {
9078
+ if (budget.over_time) throw new ExpressionNotCheapError();
9079
+ const now = performance.now();
9080
+ const gap = now - budget.last_node_ms;
9081
+ budget.last_node_ms = now;
9082
+ if (gap < IN_PLACE_GAP_MS) budget.sync_ms += gap;
9083
+ if (budget.sync_ms <= MAX_IN_PLACE_MS) return;
9084
+ budget.over_time = true;
9085
+ throw new ExpressionNotCheapError();
9086
+ }
9044
9087
  async function branchAfter(cond, then, otherwise, ctx) {
9045
9088
  return evaluateExpression(await cond ? then : otherwise, ctx);
9046
9089
  }
@@ -9081,7 +9124,7 @@ async function fillObjectAfter(out, entries3, at2, value, ctx) {
9081
9124
  }
9082
9125
  function checkClock(ctx) {
9083
9126
  assertUnsettled(ctx.evaluation_budget);
9084
- return readsClock(ctx) ? runClock(ctx.run_budget) : null;
9127
+ return readsClock(ctx) ? runClock(ctx.run_budget, !ctx.evaluation_budget.bounded) : null;
9085
9128
  }
9086
9129
  var ISOLATED_CLOCK_STRIDE = 16;
9087
9130
  function readsClock(ctx) {
@@ -9598,7 +9641,7 @@ async function callAfter(helper, pending, ctx) {
9598
9641
  return callHelper(helper, values2, ctx);
9599
9642
  }
9600
9643
  function callHelper({ fn, call, cost }, values2, ctx) {
9601
- assertWithin(ctx.evaluation_budget, MAX_IN_PLACE_HELPER_BYTES, values2);
9644
+ assertWithin(ctx.evaluation_budget, MAX_IN_PLACE_HELPER_BYTES, values2, cost === "reads" ? writtenSize : expandedSize);
9602
9645
  if (cost === "parses") charge(ctx, parsedSize(values2[0]));
9603
9646
  let answer;
9604
9647
  try {
@@ -13277,14 +13320,14 @@ function prepareFormulaContext(fields) {
13277
13320
  names: optionNamesOf(fields)
13278
13321
  };
13279
13322
  }
13280
- function computeFormulaFieldsWithContext(row2, fields, context, linkedTableRecords = /* @__PURE__ */ new Map(), linkedTableFields = /* @__PURE__ */ new Map(), timezone, loadedLinkKeys, linkExpandedTableIds, fileFacts, memberNames) {
13323
+ function computeFormulaFieldsWithContext(row2, fields, context, linkedTableRecords = NO_LINKED_ROWS, linkedTableFields = /* @__PURE__ */ new Map(), timezone, loadedLinkKeys, linkExpandedTableIds, fileFacts, memberNames) {
13281
13324
  const result = { ...row2 };
13282
13325
  const mayRecompute = (linkKey) => loadedLinkKeys === void 0 || loadedLinkKeys.has(linkKey) || isEmptyFieldValue(row2[linkKey]);
13283
13326
  const computes = (key) => context.computedAggregates === void 0 || context.computedAggregates.has(key);
13284
13327
  for (const { field, sourceField } of context.rollupFields) {
13285
13328
  if (!computes(field.key) || !mayRecompute(sourceField.key)) continue;
13286
13329
  if (linkedTableRecords.has(sourceField.table_id) && linkedTableFields.has(sourceField.table_id)) {
13287
- const linkedRecords = linkedTableRecords.get(sourceField.table_id) ?? [];
13330
+ const linkedRecords = linkedTableRecords.get(sourceField.table_id);
13288
13331
  const linkedTableFieldsArray = linkedTableFields.get(sourceField.table_id) ?? [];
13289
13332
  const linkedFieldType = linkedTableFieldsArray.find(
13290
13333
  (f) => f.key === field.aggregate_option?.field_key
@@ -13480,7 +13523,7 @@ function computeFormulaFields(row2, fields, linkedTableRecords = /* @__PURE__ */
13480
13523
  row2,
13481
13524
  fields,
13482
13525
  context,
13483
- linkedTableRecords,
13526
+ prepareLinkedRows(linkedTableRecords),
13484
13527
  linkedTableFields,
13485
13528
  timezone
13486
13529
  );
@@ -13643,39 +13686,24 @@ function planFilterTraversal(filter2, aggregatedTableId, aggregatedFields, linke
13643
13686
  resolveLinkedRecords: (parent, linkFieldKey, parentFields) => {
13644
13687
  const hop = parentFields[linkFieldKey];
13645
13688
  if (!hop || hop.type !== "select_record_link") return null;
13646
- const rows = linkedTableRecords.get(hop.table_id);
13689
+ const table = linkedTableRecords.get(hop.table_id);
13647
13690
  const hopFields = linkedTableFields.get(hop.table_id);
13648
- if (!rows || !hopFields) return null;
13691
+ if (!table || !hopFields) return null;
13649
13692
  const ids = new Set(
13650
13693
  extractRecordLinkIds(parent.data[linkFieldKey])
13651
13694
  );
13652
13695
  if (ids.size === 0) return null;
13653
13696
  return {
13654
- records: linkedRowsNamed(rows, [...ids]),
13697
+ records: linkedRowsNamed(table, [...ids]),
13655
13698
  fields: fieldMapOf(hop.table_id, hopFields)
13656
13699
  };
13657
13700
  }
13658
13701
  }
13659
13702
  };
13660
13703
  }
13661
- var positionsOfFrozenRows = /* @__PURE__ */ new WeakMap();
13662
- function linkedRowPositions(rows) {
13663
- const frozen = Object.isFrozen(rows);
13664
- const held2 = frozen ? positionsOfFrozenRows.get(rows) : void 0;
13665
- if (held2 !== void 0) return held2;
13666
- const positions = /* @__PURE__ */ new Map();
13667
- for (let at2 = 0; at2 < rows.length; at2++) {
13668
- const of = positions.get(rows[at2].id);
13669
- if (of === void 0) positions.set(rows[at2].id, [at2]);
13670
- else of.push(at2);
13671
- }
13672
- if (frozen) positionsOfFrozenRows.set(rows, positions);
13673
- return positions;
13674
- }
13675
- function linkedRowsNamed(rows, ids) {
13676
- const positions = linkedRowPositions(rows);
13677
- const found = [...new Set(ids)].flatMap((id) => positions.get(id) ?? []);
13678
- return found.sort((a, b) => a - b).map((at2) => rows[at2]);
13704
+ function linkedRowsNamed(table, ids) {
13705
+ const found = [...new Set(ids)].flatMap((id) => table.positions.get(id) ?? []);
13706
+ return found.sort((a, b) => a - b).map((at2) => table.rows[at2]);
13679
13707
  }
13680
13708
  function rollupOverNothing(operation) {
13681
13709
  return operation !== void 0 && accumulates(operation) ? 0 : null;
@@ -13693,14 +13721,13 @@ function calculateRollupValue(value, fieldType, allLinkedTableRecords, aggregate
13693
13721
  if (operation === "count") return relevantRecords.length;
13694
13722
  return aggregateByField(relevantRecords, fieldType, aggregateOption, subject).result;
13695
13723
  }
13696
- function calculateLookupValue(value, linkedRecords, lookupFieldKey, linkedField, orderBy, linkedFields, memberNames) {
13697
- if (!linkedRecords.length || !lookupFieldKey) {
13724
+ function calculateLookupValue(value, linked, lookupFieldKey, linkedField, orderBy, linkedFields, memberNames) {
13725
+ if (!linked.rows.length || !lookupFieldKey) {
13698
13726
  return null;
13699
13727
  }
13700
- const positions = linkedRowPositions(linkedRecords);
13701
13728
  const relevantRecords = extractRecordLinkIds(value).flatMap((id) => {
13702
- const at2 = positions.get(id)?.at(-1);
13703
- return at2 === void 0 ? [] : [linkedRecords[at2]];
13729
+ const at2 = linked.positions.get(id)?.at(-1);
13730
+ return at2 === void 0 ? [] : [linked.rows[at2]];
13704
13731
  });
13705
13732
  if (relevantRecords.length === 0) {
13706
13733
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/app-sdk",
3
- "version": "0.127.3",
3
+ "version": "0.127.5",
4
4
  "description": "The SDK a Lotics custom-code app reads and writes through — typed hooks over the host bridge, cell readers, mount() and AppRouter",
5
5
  "type": "module",
6
6
  "exports": {