anear-js-api 1.4.2 → 1.4.3

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.
@@ -79,6 +79,19 @@ class AnearEvent extends JsonApiResource {
79
79
  this.send({ type: "CLOSE" })
80
80
  }
81
81
 
82
+ /**
83
+ * Close the event AND persist the AppM context to ANAPI (app_event_context) as part of shutdown.
84
+ *
85
+ * This is intended for "normal close" flows where we want the final AppM context saved
86
+ * (e.g. for cloning/resume/debugging).
87
+ *
88
+ * @param {Object} context - AppM context to persist
89
+ * @param {Object} resumeEvent - event to send to AppM if this context is later rehydrated and resumed
90
+ */
91
+ closeEventWithContext(context, resumeEvent = { type: 'RESUME' }) {
92
+ this.send({ type: 'CLOSE', appmContext: { context, resumeEvent } })
93
+ }
94
+
82
95
  restartEvent() {
83
96
  this.send({ type: "RESTART" })
84
97
  }
@@ -902,13 +902,38 @@ const ActiveEventStatesConfig = {
902
902
  closeEvent: {
903
903
  id: 'closeEvent',
904
904
  entry: ({ context }) => logger.info(`[AEM] Event ${context.anearEvent.id} live → closed`),
905
- initial: 'notifyingParticipants',
905
+ initial: 'savingContextMaybe',
906
906
  on: {
907
907
  APPM_FINAL: {
908
908
  actions: () => logger.debug('[AEM] Ignoring APPM_FINAL during closeEvent.')
909
909
  }
910
910
  },
911
911
  states: {
912
+ savingContextMaybe: {
913
+ always: [
914
+ {
915
+ guard: ({ event }) => {
916
+ const c = event?.appmContext?.context
917
+ return !!(c && typeof c === 'object')
918
+ },
919
+ target: 'savingAppEventContext'
920
+ },
921
+ { target: 'notifyingParticipants' }
922
+ ]
923
+ },
924
+ savingAppEventContext: {
925
+ invoke: {
926
+ src: 'saveAppEventContext',
927
+ input: ({ context, event }) => ({ context, event }),
928
+ onDone: {
929
+ target: 'notifyingParticipants'
930
+ },
931
+ onError: {
932
+ // If save fails, continue closing anyway (best-effort persistence).
933
+ target: 'notifyingParticipants'
934
+ }
935
+ }
936
+ },
912
937
  notifyingParticipants: {
913
938
  entry: 'sendParticipantExitEvents',
914
939
  always: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {