@almadar/evaluator 2.5.5 → 2.6.0

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.
@@ -82,6 +82,15 @@ interface EvaluationContext {
82
82
  renderUI?: (slot: string, pattern: unknown, props?: Record<string, unknown>, priority?: number) => void;
83
83
  /** Register an OS trigger (server-side only) */
84
84
  registerOsTrigger?: (type: string, config: Record<string, unknown>) => void;
85
+ /** Effect handlers for resource operators (grouped to avoid top-level pollution) */
86
+ effectHandlers?: {
87
+ ref?: (entityType: string, options?: unknown) => unknown;
88
+ deref?: (entityType: string, id?: unknown) => unknown;
89
+ swap?: (entityType: string, id: unknown, transformExpr: unknown, evaluate: unknown, ctx: unknown) => unknown;
90
+ watch?: (entityType: string, effects: unknown[], evaluate: unknown, ctx: unknown) => void;
91
+ atomic?: (effects: unknown[], evaluate: unknown, ctx: unknown) => unknown;
92
+ fetch?: (entityType: string, options?: unknown) => unknown;
93
+ };
85
94
  }
86
95
  /**
87
96
  * Create a minimal evaluation context for testing/guards.
@@ -383,5 +392,30 @@ declare function evalIncrement(args: SExpr[], evaluate: Evaluator, ctx: Evaluati
383
392
  * Evaluate decrement: ["decrement", "@entity.field"] or ["decrement", "@entity.field", amount]
384
393
  */
385
394
  declare function evalDecrement(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
395
+ /**
396
+ * Evaluate ref: ["ref", "EntityType"] or ["ref", "EntityType", { filter, include }]
397
+ * Server-side: queries entity data. Client-side: subscribes to EntityStore.
398
+ */
399
+ declare function evalRef(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
400
+ /**
401
+ * Evaluate deref: ["deref", "EntityType"] or ["deref", "EntityType", idExpr]
402
+ * Pure snapshot read. Returns current entity data from store.
403
+ */
404
+ declare function evalDeref(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
405
+ /**
406
+ * Evaluate swap!: ["swap!", "EntityType", idExpr, transformExpr]
407
+ * Atomic read-modify-write with CAS retry.
408
+ */
409
+ declare function evalSwap(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
410
+ /**
411
+ * Evaluate watch: ["watch", "EntityType", [effect1, effect2, ...]]
412
+ * Client-only. Registers a callback on the EntityStore.
413
+ */
414
+ declare function evalWatch(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
415
+ /**
416
+ * Evaluate atomic: ["atomic", [effect1, effect2, ...]]
417
+ * Groups effects into a transaction. All succeed or all roll back.
418
+ */
419
+ declare function evalAtomic(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
386
420
 
387
- export { evalWhen as $, evalIncludes as A, evalIncrement as B, evalLast as C, evalLessThan as D, type EvaluationContext as E, evalLessThanOrEqual as F, evalLet as G, evalMap as H, evalMatches as I, evalMax as J, evalMin as K, evalModulo as L, evalMultiply as M, evalNavigate as N, evalNot as O, evalNotEqual as P, evalNotify as Q, evalNth as R, evalOr as S, evalPersist as T, evalRenderUI as U, evalRound as V, evalSet as W, evalSetDynamic as X, evalSpawn as Y, evalSubtract as Z, evalSum as _, createEffectContext as a, resolveBinding as a0, createMinimalContext as b, createChildContext as c, evalAdd as d, evalAbs as e, evalAnd as f, evalCallService as g, evalCeil as h, evalClamp as i, evalConcat as j, evalCount as k, evalDecrement as l, evalDespawn as m, evalDivide as n, evalDo as o, evalEmit as p, evalEmpty as q, evalEqual as r, evalFilter as s, evalFind as t, evalFirst as u, evalFloor as v, evalFn as w, evalGreaterThan as x, evalGreaterThanOrEqual as y, evalIf as z };
421
+ export { evalSpawn as $, evalGreaterThanOrEqual as A, evalIf as B, evalIncludes as C, evalIncrement as D, type EvaluationContext as E, evalLast as F, evalLessThan as G, evalLessThanOrEqual as H, evalLet as I, evalMap as J, evalMatches as K, evalMax as L, evalMin as M, evalModulo as N, evalMultiply as O, evalNavigate as P, evalNot as Q, evalNotEqual as R, evalNotify as S, evalNth as T, evalOr as U, evalPersist as V, evalRef as W, evalRenderUI as X, evalRound as Y, evalSet as Z, evalSetDynamic as _, createEffectContext as a, evalSubtract as a0, evalSum as a1, evalSwap as a2, evalWatch as a3, evalWhen as a4, resolveBinding as a5, createMinimalContext as b, createChildContext as c, evalAdd as d, evalAbs as e, evalAnd as f, evalAtomic as g, evalCallService as h, evalCeil as i, evalClamp as j, evalConcat as k, evalCount as l, evalDecrement as m, evalDeref as n, evalDespawn as o, evalDivide as p, evalDo as q, evalEmit as r, evalEmpty as s, evalEqual as t, evalFilter as u, evalFind as v, evalFirst as w, evalFloor as x, evalFn as y, evalGreaterThan as z };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SExpr } from '@almadar/core';
2
2
  export { CORE_BINDINGS, CoreBinding, Expression, ExpressionSchema, ParsedBinding, SExpr, SExprAtom, SExprSchema, collectBindings, getArgs, getOperator, isBinding, isSExpr, isSExprAtom, isSExprCall, isValidBinding, parseBinding, sexpr, walkSExpr } from '@almadar/core';
3
- import { E as EvaluationContext } from './index-Cwku8aP4.js';
4
- export { c as createChildContext, a as createEffectContext, b as createMinimalContext, e as evalAbs, d as evalAdd, f as evalAnd, g as evalCallService, h as evalCeil, i as evalClamp, j as evalConcat, k as evalCount, l as evalDecrement, m as evalDespawn, n as evalDivide, o as evalDo, p as evalEmit, q as evalEmpty, r as evalEqual, s as evalFilter, t as evalFind, u as evalFirst, v as evalFloor, w as evalFn, x as evalGreaterThan, y as evalGreaterThanOrEqual, z as evalIf, A as evalIncludes, B as evalIncrement, C as evalLast, D as evalLessThan, F as evalLessThanOrEqual, G as evalLet, H as evalMap, I as evalMatches, J as evalMax, K as evalMin, L as evalModulo, M as evalMultiply, N as evalNavigate, O as evalNot, P as evalNotEqual, Q as evalNotify, R as evalNth, S as evalOr, T as evalPersist, U as evalRenderUI, V as evalRound, W as evalSet, X as evalSetDynamic, Y as evalSpawn, Z as evalSubtract, _ as evalSum, $ as evalWhen, a0 as resolveBinding } from './index-Cwku8aP4.js';
3
+ import { E as EvaluationContext } from './index-Cf4WAVBs.js';
4
+ export { c as createChildContext, a as createEffectContext, b as createMinimalContext, e as evalAbs, d as evalAdd, f as evalAnd, g as evalAtomic, h as evalCallService, i as evalCeil, j as evalClamp, k as evalConcat, l as evalCount, m as evalDecrement, n as evalDeref, o as evalDespawn, p as evalDivide, q as evalDo, r as evalEmit, s as evalEmpty, t as evalEqual, u as evalFilter, v as evalFind, w as evalFirst, x as evalFloor, y as evalFn, z as evalGreaterThan, A as evalGreaterThanOrEqual, B as evalIf, C as evalIncludes, D as evalIncrement, F as evalLast, G as evalLessThan, H as evalLessThanOrEqual, I as evalLet, J as evalMap, K as evalMatches, L as evalMax, M as evalMin, N as evalModulo, O as evalMultiply, P as evalNavigate, Q as evalNot, R as evalNotEqual, S as evalNotify, T as evalNth, U as evalOr, V as evalPersist, W as evalRef, X as evalRenderUI, Y as evalRound, Z as evalSet, _ as evalSetDynamic, $ as evalSpawn, a0 as evalSubtract, a1 as evalSum, a2 as evalSwap, a3 as evalWatch, a4 as evalWhen, a5 as resolveBinding } from './index-Cf4WAVBs.js';
5
5
 
6
6
  /**
7
7
  * S-Expression Evaluator
package/dist/index.js CHANGED
@@ -574,6 +574,56 @@ function evalDecrement(args, evaluate2, ctx) {
574
574
  const newValue = toNumber3(currentValue) - amount;
575
575
  ctx.mutateEntity({ [fieldPath]: newValue });
576
576
  }
577
+ function evalRef(args, evaluate2, ctx) {
578
+ const entityType = args[0];
579
+ if (ctx.effectHandlers?.ref) {
580
+ return ctx.effectHandlers.ref(entityType, args.length > 1 ? evaluate2(args[1], ctx) : void 0);
581
+ }
582
+ if (ctx.effectHandlers?.fetch) {
583
+ return ctx.effectHandlers.fetch(entityType, args.length > 1 ? evaluate2(args[1], ctx) : void 0);
584
+ }
585
+ return void 0;
586
+ }
587
+ function evalDeref(args, evaluate2, ctx) {
588
+ const entityType = args[0];
589
+ const id = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
590
+ if (ctx.effectHandlers?.deref) {
591
+ return ctx.effectHandlers.deref(entityType, id);
592
+ }
593
+ if (id && ctx.effectHandlers?.fetch) {
594
+ return ctx.effectHandlers.fetch(entityType, id);
595
+ }
596
+ return ctx.entity?.[entityType] ?? [];
597
+ }
598
+ function evalSwap(args, evaluate2, ctx) {
599
+ const entityType = args[0];
600
+ const id = evaluate2(args[1], ctx);
601
+ const transformExpr = args[2];
602
+ if (ctx.effectHandlers?.swap) {
603
+ return ctx.effectHandlers.swap(entityType, id, transformExpr, evaluate2, ctx);
604
+ }
605
+ console.warn("No swap handler in context");
606
+ return void 0;
607
+ }
608
+ function evalWatch(args, evaluate2, ctx) {
609
+ const entityType = args[0];
610
+ const effects = args[1];
611
+ if (ctx.effectHandlers?.watch) {
612
+ ctx.effectHandlers.watch(entityType, effects, evaluate2, ctx);
613
+ }
614
+ }
615
+ function evalAtomic(args, evaluate2, ctx) {
616
+ const effects = args[0];
617
+ if (ctx.effectHandlers?.atomic) {
618
+ return ctx.effectHandlers.atomic(effects, evaluate2, ctx);
619
+ }
620
+ if (Array.isArray(effects)) {
621
+ for (const eff of effects) {
622
+ evaluate2(eff, ctx);
623
+ }
624
+ }
625
+ return void 0;
626
+ }
577
627
  function toNumber3(value) {
578
628
  if (typeof value === "number") return value;
579
629
  if (typeof value === "string") {
@@ -887,7 +937,8 @@ function evalArrayFirst(args, evaluate2, ctx) {
887
937
  }
888
938
  function evalArrayLast(args, evaluate2, ctx) {
889
939
  const arr = evaluate2(args[0], ctx);
890
- return arr?.[arr.length - 1];
940
+ if (!Array.isArray(arr) || arr.length === 0) return void 0;
941
+ return arr[arr.length - 1];
891
942
  }
892
943
  function evalArrayNth(args, evaluate2, ctx) {
893
944
  const arr = evaluate2(args[0], ctx);
@@ -2592,6 +2643,18 @@ var SExpressionEvaluator = class {
2592
2643
  case "render-ui":
2593
2644
  evalRenderUI(args, evaluate2, ctx);
2594
2645
  return void 0;
2646
+ // Resource operators
2647
+ case "ref":
2648
+ return evalRef(args, evaluate2, ctx);
2649
+ case "deref":
2650
+ return evalDeref(args, evaluate2, ctx);
2651
+ case "swap!":
2652
+ return evalSwap(args, evaluate2, ctx);
2653
+ case "watch":
2654
+ evalWatch(args, evaluate2, ctx);
2655
+ return void 0;
2656
+ case "atomic":
2657
+ return evalAtomic(args, evaluate2, ctx);
2595
2658
  // ===============================
2596
2659
  // Standard Library: math/*
2597
2660
  // ===============================
@@ -3039,6 +3102,6 @@ function executeEffects(effects, ctx) {
3039
3102
  evaluator.executeEffects(effects, ctx);
3040
3103
  }
3041
3104
 
3042
- export { SExpressionEvaluator, createChildContext, createEffectContext, createMinimalContext, evalAbs, evalAdd, evalAnd, evalCallService, evalCeil, evalClamp, evalConcat, evalCount, evalDecrement, evalDespawn, evalDivide, evalDo, evalEmit, evalEmpty, evalEqual, evalFilter, evalFind, evalFirst, evalFloor, evalFn, evalGreaterThan, evalGreaterThanOrEqual, evalIf, evalIncludes, evalIncrement, evalLast, evalLessThan, evalLessThanOrEqual, evalLet, evalMap, evalMatches, evalMax, evalMin, evalModulo, evalMultiply, evalNavigate, evalNot, evalNotEqual, evalNotify, evalNth, evalOr, evalPersist, evalRenderUI, evalRound, evalSet, evalSetDynamic, evalSpawn, evalSubtract, evalSum, evalWhen, evaluate, evaluateGuard, evaluator, executeEffect, executeEffects, resolveBinding };
3105
+ export { SExpressionEvaluator, createChildContext, createEffectContext, createMinimalContext, evalAbs, evalAdd, evalAnd, evalAtomic, evalCallService, evalCeil, evalClamp, evalConcat, evalCount, evalDecrement, evalDeref, evalDespawn, evalDivide, evalDo, evalEmit, evalEmpty, evalEqual, evalFilter, evalFind, evalFirst, evalFloor, evalFn, evalGreaterThan, evalGreaterThanOrEqual, evalIf, evalIncludes, evalIncrement, evalLast, evalLessThan, evalLessThanOrEqual, evalLet, evalMap, evalMatches, evalMax, evalMin, evalModulo, evalMultiply, evalNavigate, evalNot, evalNotEqual, evalNotify, evalNth, evalOr, evalPersist, evalRef, evalRenderUI, evalRound, evalSet, evalSetDynamic, evalSpawn, evalSubtract, evalSum, evalSwap, evalWatch, evalWhen, evaluate, evaluateGuard, evaluator, executeEffect, executeEffects, resolveBinding };
3043
3106
  //# sourceMappingURL=index.js.map
3044
3107
  //# sourceMappingURL=index.js.map