s3db.js 13.3.0 → 13.4.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.
package/dist/s3db.es.js CHANGED
@@ -26159,7 +26159,7 @@ class Database extends EventEmitter {
26159
26159
  })();
26160
26160
  this.version = "1";
26161
26161
  this.s3dbVersion = (() => {
26162
- const [ok, err, version] = tryFn(() => true ? "13.3.0" : "latest");
26162
+ const [ok, err, version] = tryFn(() => true ? "13.4.0" : "latest");
26163
26163
  return ok ? version : "latest";
26164
26164
  })();
26165
26165
  this._resourcesMap = {};
@@ -31488,14 +31488,36 @@ class StateMachinePlugin extends Plugin {
31488
31488
  }
31489
31489
  /**
31490
31490
  * Setup an event-based trigger
31491
+ * Supports both old API (trigger.event) and new API (trigger.eventName + eventSource)
31491
31492
  * @private
31492
31493
  */
31493
31494
  async _setupEventTrigger(machineId, stateName, trigger, triggerName) {
31494
- const eventName = trigger.event;
31495
+ const baseEventName = trigger.eventName || trigger.event;
31496
+ const eventSource = trigger.eventSource;
31497
+ if (!baseEventName) {
31498
+ throw new StateMachineError(`Event trigger '${triggerName}' must have either 'event' or 'eventName' property`, {
31499
+ operation: "_setupEventTrigger",
31500
+ machineId,
31501
+ stateName,
31502
+ triggerName
31503
+ });
31504
+ }
31495
31505
  const eventHandler = async (eventData) => {
31496
31506
  const entities = await this._getEntitiesInState(machineId, stateName);
31497
31507
  for (const entity of entities) {
31498
31508
  try {
31509
+ let resolvedEventName;
31510
+ if (typeof baseEventName === "function") {
31511
+ resolvedEventName = baseEventName(entity.context);
31512
+ } else {
31513
+ resolvedEventName = baseEventName;
31514
+ }
31515
+ if (eventSource && typeof baseEventName === "function") {
31516
+ const eventIdMatch = eventData?.id || eventData?.entityId;
31517
+ if (eventIdMatch && entity.entityId !== eventIdMatch) {
31518
+ continue;
31519
+ }
31520
+ }
31499
31521
  if (trigger.condition) {
31500
31522
  const shouldTrigger = await trigger.condition(entity.context, entity.entityId, eventData);
31501
31523
  if (!shouldTrigger) continue;
@@ -31509,28 +31531,76 @@ class StateMachinePlugin extends Plugin {
31509
31531
  continue;
31510
31532
  }
31511
31533
  }
31512
- const result = await this._executeAction(
31513
- trigger.action,
31514
- { ...entity.context, eventData },
31515
- "TRIGGER",
31516
- machineId,
31517
- entity.entityId
31518
- );
31519
- await this._incrementTriggerCount(machineId, entity.entityId, triggerName);
31520
- if (trigger.sendEvent) {
31521
- await this.send(machineId, entity.entityId, trigger.sendEvent, {
31522
- ...entity.context,
31523
- triggerResult: result,
31524
- eventData
31534
+ if (trigger.targetState) {
31535
+ await this._transition(
31536
+ machineId,
31537
+ entity.entityId,
31538
+ stateName,
31539
+ trigger.targetState,
31540
+ "TRIGGER",
31541
+ { ...entity.context, eventData, triggerName }
31542
+ );
31543
+ const machine = this.machines.get(machineId);
31544
+ const resourceConfig = machine.config;
31545
+ if (resourceConfig.resource && resourceConfig.stateField) {
31546
+ let resource;
31547
+ if (typeof resourceConfig.resource === "string") {
31548
+ resource = await this.database.getResource(resourceConfig.resource);
31549
+ } else {
31550
+ resource = resourceConfig.resource;
31551
+ }
31552
+ if (resource) {
31553
+ const [ok] = await tryFn(
31554
+ () => resource.patch(entity.entityId, { [resourceConfig.stateField]: trigger.targetState })
31555
+ );
31556
+ if (!ok && this.config.verbose) {
31557
+ console.warn(`[StateMachinePlugin] Failed to update resource stateField for entity ${entity.entityId}`);
31558
+ }
31559
+ }
31560
+ }
31561
+ const targetStateConfig = machine.config.states[trigger.targetState];
31562
+ if (targetStateConfig?.entry) {
31563
+ await this._executeAction(
31564
+ targetStateConfig.entry,
31565
+ { ...entity.context, eventData },
31566
+ "TRIGGER",
31567
+ machineId,
31568
+ entity.entityId
31569
+ );
31570
+ }
31571
+ this.emit("plg:state-machine:transition", {
31572
+ machineId,
31573
+ entityId: entity.entityId,
31574
+ from: stateName,
31575
+ to: trigger.targetState,
31576
+ event: "TRIGGER",
31577
+ context: { ...entity.context, eventData, triggerName }
31525
31578
  });
31579
+ } else if (trigger.action) {
31580
+ const result = await this._executeAction(
31581
+ trigger.action,
31582
+ { ...entity.context, eventData },
31583
+ "TRIGGER",
31584
+ machineId,
31585
+ entity.entityId
31586
+ );
31587
+ if (trigger.sendEvent) {
31588
+ await this.send(machineId, entity.entityId, trigger.sendEvent, {
31589
+ ...entity.context,
31590
+ triggerResult: result,
31591
+ eventData
31592
+ });
31593
+ }
31526
31594
  }
31595
+ await this._incrementTriggerCount(machineId, entity.entityId, triggerName);
31527
31596
  this.emit("plg:state-machine:trigger-executed", {
31528
31597
  machineId,
31529
31598
  entityId: entity.entityId,
31530
31599
  state: stateName,
31531
31600
  trigger: triggerName,
31532
31601
  type: "event",
31533
- eventName
31602
+ eventName: resolvedEventName,
31603
+ targetState: trigger.targetState
31534
31604
  });
31535
31605
  } catch (error) {
31536
31606
  if (this.config.verbose) {
@@ -31539,16 +31609,25 @@ class StateMachinePlugin extends Plugin {
31539
31609
  }
31540
31610
  }
31541
31611
  };
31542
- if (eventName.startsWith("db:")) {
31543
- const dbEventName = eventName.substring(3);
31544
- this.database.on(dbEventName, eventHandler);
31612
+ if (eventSource) {
31613
+ const baseEvent = typeof baseEventName === "function" ? "updated" : baseEventName;
31614
+ eventSource.on(baseEvent, eventHandler);
31545
31615
  if (this.config.verbose) {
31546
- console.log(`[StateMachinePlugin] Listening to database event '${dbEventName}' for trigger '${triggerName}'`);
31616
+ console.log(`[StateMachinePlugin] Listening to resource event '${baseEvent}' from '${eventSource.name}' for trigger '${triggerName}'`);
31547
31617
  }
31548
31618
  } else {
31549
- this.on(eventName, eventHandler);
31550
- if (this.config.verbose) {
31551
- console.log(`[StateMachinePlugin] Listening to plugin event '${eventName}' for trigger '${triggerName}'`);
31619
+ const staticEventName = typeof baseEventName === "function" ? "updated" : baseEventName;
31620
+ if (staticEventName.startsWith("db:")) {
31621
+ const dbEventName = staticEventName.substring(3);
31622
+ this.database.on(dbEventName, eventHandler);
31623
+ if (this.config.verbose) {
31624
+ console.log(`[StateMachinePlugin] Listening to database event '${dbEventName}' for trigger '${triggerName}'`);
31625
+ }
31626
+ } else {
31627
+ this.on(staticEventName, eventHandler);
31628
+ if (this.config.verbose) {
31629
+ console.log(`[StateMachinePlugin] Listening to plugin event '${staticEventName}' for trigger '${triggerName}'`);
31630
+ }
31552
31631
  }
31553
31632
  }
31554
31633
  }