@kosdev-code/kos-ui-sdk 0.1.0-next.958 → 0.1.0-next.962

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.
@@ -3599,7 +3599,7 @@ function createOnceHandler(originalHandler) {
3599
3599
  }
3600
3600
  const isPrintable = (model) => typeof model.toJSON === "function";
3601
3601
  const isKosDataModel = (model) => model[DependencyModels] !== void 0;
3602
- const isKosModelContainer = (model) => model.revision !== void 0 && model.index !== void 0;
3602
+ const isKosModelContainer = (model) => typeof model === "object" && model !== null && model.revision !== void 0 && model.index !== void 0;
3603
3603
  const resolveChild = (attribute) => {
3604
3604
  if (!attribute) return void 0;
3605
3605
  if (isKosDataModel(attribute)) return attribute;
@@ -17960,6 +17960,56 @@ Parser.prototype.isOperatorEnabled = function(op) {
17960
17960
  var operators = this.options.operators || {};
17961
17961
  return !(optionName in operators) || !!operators[optionName];
17962
17962
  };
17963
+ class CompiledExpression {
17964
+ expression;
17965
+ expr;
17966
+ constructor(expression, expr) {
17967
+ this.expression = expression;
17968
+ this.expr = expr;
17969
+ }
17970
+ evaluate(contextData) {
17971
+ try {
17972
+ return { value: this.expr.evaluate(contextData), error: void 0 };
17973
+ } catch (error) {
17974
+ return {
17975
+ value: void 0,
17976
+ error: error instanceof Error ? error.message : String(error)
17977
+ };
17978
+ }
17979
+ }
17980
+ }
17981
+ const PARSE_CACHE_LIMIT = 500;
17982
+ class ExpressionParseCache {
17983
+ constructor(parser, limit = PARSE_CACHE_LIMIT) {
17984
+ this.parser = parser;
17985
+ this.limit = limit;
17986
+ }
17987
+ parser;
17988
+ limit;
17989
+ entries = /* @__PURE__ */ new Map();
17990
+ parse(expression) {
17991
+ const cached = this.entries.get(expression);
17992
+ if (cached) {
17993
+ this.entries.delete(expression);
17994
+ this.entries.set(expression, cached);
17995
+ return cached;
17996
+ }
17997
+ const parsed = new CompiledExpression(
17998
+ expression,
17999
+ this.parser.parse(expression)
18000
+ );
18001
+ this.entries.set(expression, parsed);
18002
+ if (this.entries.size > this.limit) {
18003
+ const oldest = this.entries.keys().next().value;
18004
+ if (oldest !== void 0) this.entries.delete(oldest);
18005
+ }
18006
+ return parsed;
18007
+ }
18008
+ }
18009
+ const layerContext = (base, local) => new Proxy(local, {
18010
+ get: (target, key) => typeof key === "string" && !Object.prototype.hasOwnProperty.call(target, key) ? base[key] : Reflect.get(target, key),
18011
+ has: (target, key) => Object.prototype.hasOwnProperty.call(target, key) || typeof key === "string" && base.has(key)
18012
+ });
17963
18013
  var __defProp$i = Object.defineProperty;
17964
18014
  var __getOwnPropDesc$m = Object.getOwnPropertyDescriptor;
17965
18015
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$i(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -17972,60 +18022,67 @@ var __decorateClass$m = (decorators, target, key, kind) => {
17972
18022
  };
17973
18023
  var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, key + "", value);
17974
18024
  const MODEL_TYPE$l = "kos-expression-evaluator-model";
18025
+ const sameResult = (a, b) => Object.is(a.value, b.value) && a.error === b.error;
18026
+ const DISPOSED_RESULT = {
18027
+ value: void 0,
18028
+ error: void 0
18029
+ };
17975
18030
  class ReactiveExpression {
17976
18031
  name;
17977
18032
  expression;
17978
- expr;
17979
- getContextData = null;
17980
- // Cache the last result to allow comparison without re-evaluation
17981
- _lastResult = { value: void 0 };
17982
- constructor(name, expression, expr, getContextData) {
18033
+ parsed;
18034
+ disposed;
18035
+ computedResult;
18036
+ constructor(name, parsed, getContextData) {
17983
18037
  this.name = name;
17984
- this.expression = expression;
17985
- this.expr = expr;
17986
- this.getContextData = getContextData;
17987
- this._lastResult = { value: void 0, error: void 0 };
18038
+ this.expression = parsed.expression;
18039
+ this.parsed = parsed;
18040
+ this.disposed = kosObservable.box(false);
18041
+ this.computedResult = kosComputed(
18042
+ () => this.disposed.get() ? DISPOSED_RESULT : this.evaluate(getContextData()),
18043
+ { keepAlive: true, equals: sameResult, name: `expression:${name}` }
18044
+ );
17988
18045
  }
17989
- /** The cached result, returned without evaluating. */
18046
+ /** The current result, evaluated only if a context value it read changed since the last evaluation. */
17990
18047
  get result() {
17991
- return this._lastResult;
18048
+ return this.computedResult.get();
17992
18049
  }
17993
18050
  /**
17994
- * Update the cached result. Called by the parent model after evaluation.
18051
+ * @deprecated The result is derived from the context data; this has no effect.
17995
18052
  */
17996
- updateResult(result) {
17997
- this._lastResult = result;
18053
+ updateResult(_result) {
18054
+ return;
17998
18055
  }
17999
18056
  /**
18000
- * Evaluate the expression against the given context data. The cached result is unchanged.
18057
+ * Evaluate the expression against the given context data. The result is unchanged.
18001
18058
  */
18002
18059
  evaluate(contextData) {
18003
- try {
18004
- const value = this.expr.evaluate(contextData);
18005
- return { value, error: void 0 };
18006
- } catch (error) {
18007
- return {
18008
- value: void 0,
18009
- error: error instanceof Error ? error.message : String(error)
18010
- };
18011
- }
18060
+ return this.parsed.evaluate(contextData);
18061
+ }
18062
+ /** Release every context value the result tracks. */
18063
+ dispose() {
18064
+ kosAction(() => this.disposed.set(true));
18065
+ this.computedResult.get();
18012
18066
  }
18013
18067
  }
18068
+ class ExpressionResultsView {
18069
+ }
18014
18070
  let KosExpressionEvaluatorModelImpl = class {
18015
18071
  id;
18016
18072
  logger;
18017
18073
  parser;
18018
18074
  _data;
18019
18075
  _expressions = /* @__PURE__ */ new Map();
18020
- // Use ObservableData for results to handle dynamic properties properly
18021
- _results;
18076
+ _parseCache;
18077
+ _resultsView;
18022
18078
  _contextRevision = 0;
18023
18079
  constructor(modelId, options, context) {
18024
18080
  this.id = modelId;
18025
18081
  this.logger = context.logger;
18026
18082
  this._data = ObservableData();
18027
- this._results = ObservableData();
18028
18083
  this.parser = new Parser();
18084
+ this._parseCache = new ExpressionParseCache(this.parser);
18085
+ this._resultsView = this.createResultsView();
18029
18086
  this.parser.functions.exists = (value) => value !== void 0 && value !== null;
18030
18087
  this.parser.functions.isEmpty = (value) => {
18031
18088
  if (value === void 0 || value === null) return true;
@@ -18056,7 +18113,8 @@ let KosExpressionEvaluatorModelImpl = class {
18056
18113
  return this._contextRevision;
18057
18114
  }
18058
18115
  /**
18059
- * Replace the context data. Every expression re-evaluates against it.
18116
+ * Replace the context data. An expression re-evaluates only if a value it
18117
+ * read changed.
18060
18118
  *
18061
18119
  * @param newData - Complete new context data to replace existing data
18062
18120
  */
@@ -18114,7 +18172,8 @@ let KosExpressionEvaluatorModelImpl = class {
18114
18172
  this._data[key] = model;
18115
18173
  }
18116
18174
  /**
18117
- * Add a named expression and evaluate it once.
18175
+ * Add a named expression. It evaluates on first read. Adding a name that
18176
+ * exists replaces that expression.
18118
18177
  *
18119
18178
  * @param name - Unique identifier for this expression
18120
18179
  * @param expressionString - Expression in `expr-eval` syntax
@@ -18122,25 +18181,31 @@ let KosExpressionEvaluatorModelImpl = class {
18122
18181
  */
18123
18182
  addExpression(name, expressionString) {
18124
18183
  try {
18125
- const expr = this.parser.parse(expressionString);
18184
+ const parsed = this._parseCache.parse(expressionString);
18126
18185
  const reactiveExpression = new ReactiveExpression(
18127
18186
  name,
18128
- expressionString,
18129
- expr,
18130
- () => this.data
18187
+ parsed,
18188
+ () => this._data
18131
18189
  );
18190
+ this._expressions.get(name)?.dispose();
18132
18191
  this._expressions.set(name, reactiveExpression);
18133
- this.evaluateExpression(name);
18134
18192
  this.logger.debug(`Added expression "${name}": ${expressionString}`);
18135
18193
  } catch (error) {
18136
18194
  this.logger.error(`Failed to parse expression "${name}"`, error);
18137
18195
  throw error;
18138
18196
  }
18139
18197
  }
18198
+ /**
18199
+ * Remove a named expression and release the context values it tracks.
18200
+ *
18201
+ * @param name - The expression name
18202
+ * @returns Whether an expression was removed
18203
+ */
18140
18204
  removeExpression(name) {
18205
+ const expression = this._expressions.get(name);
18141
18206
  const removed = this._expressions.delete(name);
18142
18207
  if (removed) {
18143
- delete this._results[name];
18208
+ expression?.dispose();
18144
18209
  this.logger.debug(`Removed expression "${name}"`);
18145
18210
  } else {
18146
18211
  this.logger.warn(
@@ -18175,11 +18240,12 @@ let KosExpressionEvaluatorModelImpl = class {
18175
18240
  return expression.result;
18176
18241
  }
18177
18242
  /**
18178
- * The last cached result of an expression, read without re-evaluating. Use it
18179
- * in effects that should not depend on the expression's inputs.
18243
+ * The result of an expression without a warning when it does not exist. It
18244
+ * re-evaluates only if a context value it read changed since the last
18245
+ * evaluation, and a reader tracks the result, not the expression's inputs.
18180
18246
  *
18181
- * @param name - The expression name to get the cached result for
18182
- * @returns The last cached result, or undefined if expression doesn't exist
18247
+ * @param name - The expression name to get the result for
18248
+ * @returns The result, or undefined if expression doesn't exist
18183
18249
  */
18184
18250
  getCachedExpressionValue(name) {
18185
18251
  const expression = this._expressions.get(name);
@@ -18194,53 +18260,64 @@ let KosExpressionEvaluatorModelImpl = class {
18194
18260
  get expressionNames() {
18195
18261
  return Array.from(this._expressions.keys());
18196
18262
  }
18197
- // Evaluates on access so readers track the context data each expression uses
18263
+ /**
18264
+ * Every expression's result by name. Reading `results[name]` evaluates only
18265
+ * that expression, and only if a context value it read changed.
18266
+ */
18198
18267
  get results() {
18199
- this.evaluateAllExpressions();
18200
- return this._results;
18268
+ return this._resultsView;
18201
18269
  }
18202
- /** Re-evaluate every expression and update the cached results. */
18203
- evaluateAllExpressions() {
18204
- const keys = Array.from(this._expressions.keys());
18205
- keys.forEach((key) => {
18206
- const expression = this._expressions.get(key);
18207
- if (expression) {
18208
- this.evaluateExpression(key);
18209
- }
18210
- });
18270
+ /**
18271
+ * Parse an expression, or return the cached parse of the same string, to
18272
+ * evaluate against any context object.
18273
+ *
18274
+ * @param expressionString - Expression in `expr-eval` syntax
18275
+ * @returns The parsed expression
18276
+ * @throws {Error} If the expression has invalid syntax
18277
+ */
18278
+ parseExpression(expressionString) {
18279
+ return this._parseCache.parse(expressionString);
18211
18280
  }
18212
18281
  /**
18213
- * Evaluate a specific expression and update the cached result if it changed.
18282
+ * Evaluate an expression against the context data with `localContext`
18283
+ * layered over it, without copying either. A key in `localContext` wins over
18284
+ * the same context key. Called from a reactive render, the reader tracks only
18285
+ * the values the expression read.
18286
+ *
18287
+ * @param expressionString - Expression in `expr-eval` syntax
18288
+ * @param localContext - Values that apply to this evaluation only, such as a row's item
18289
+ * @returns The result; a syntax or evaluation failure is returned as `error`
18214
18290
  */
18215
- evaluateExpression(name) {
18216
- this.logger.debug(`Evaluating expression: ${String(name)}`);
18217
- const expression = this._expressions.get(name);
18218
- if (!expression) {
18219
- this.logger.warn(`Expression "${String(name)}" not found`);
18220
- return;
18221
- }
18222
- const newResult = expression.evaluate(this.data);
18223
- this.logger.debug(`Expression "${String(name)}" evaluated to:`, newResult);
18224
- const currentResult = expression.result;
18225
- if (newResult.value !== currentResult.value || newResult.error !== currentResult.error) {
18226
- this.logger.debug(
18227
- `Expression "${String(name)}" result changed from`,
18228
- currentResult,
18229
- "to",
18230
- newResult
18231
- );
18232
- expression.updateResult(newResult);
18233
- this.updateResult(name, newResult);
18291
+ evaluateWithLocalContext(expressionString, localContext = {}) {
18292
+ let parsed;
18293
+ try {
18294
+ parsed = this._parseCache.parse(expressionString);
18295
+ } catch (error) {
18296
+ return {
18297
+ value: void 0,
18298
+ error: error instanceof Error ? error.message : String(error)
18299
+ };
18234
18300
  }
18301
+ return parsed.evaluate(layerContext(this._data, localContext));
18235
18302
  }
18236
- /** Store a changed result in the reactive results object. */
18237
- updateResult(name, result) {
18238
- this.logger.debug(
18239
- `ExpressionEvaluator.updateResult called for "${name}":`,
18240
- result
18241
- );
18242
- kosAction(() => {
18243
- this._results[name] = result;
18303
+ createResultsView() {
18304
+ const lookup = (key) => typeof key === "string" ? this._expressions.get(key) : void 0;
18305
+ return new Proxy(new ExpressionResultsView(), {
18306
+ get: (target, key, receiver) => {
18307
+ const expression = lookup(key);
18308
+ return expression ? expression.result : Reflect.get(target, key, receiver);
18309
+ },
18310
+ has: (target, key) => lookup(key) !== void 0 || Reflect.has(target, key),
18311
+ ownKeys: () => Array.from(this._expressions.keys()),
18312
+ getOwnPropertyDescriptor: (_target, key) => {
18313
+ const expression = lookup(key);
18314
+ return expression ? {
18315
+ value: expression.result,
18316
+ enumerable: true,
18317
+ configurable: true,
18318
+ writable: false
18319
+ } : void 0;
18320
+ }
18244
18321
  });
18245
18322
  }
18246
18323
  /**
@@ -24672,4 +24749,4 @@ export {
24672
24749
  getStateBeanData as y,
24673
24750
  StateBean as z
24674
24751
  };
24675
- //# sourceMappingURL=software-info-registration-C8kf-pBB.js.map
24752
+ //# sourceMappingURL=software-info-registration-CefJgWke.js.map