capacitor-native-agent 0.9.3 → 0.9.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.
@@ -0,0 +1,70 @@
1
+ package com.t6x.plugins.nativeagent
2
+
3
+ import java.util.concurrent.CopyOnWriteArrayList
4
+ import uniffi.native_agent_ffi.NativeAgentHandle
5
+
6
+ /**
7
+ * Listener interface for in-process Kotlin code that wants to observe
8
+ * raw FFI events alongside the WebView listener. Mirrors the shape of
9
+ * uniffi.native_agent_ffi.NativeEventCallback but is registered through
10
+ * NativeAgentBridge instead of the FFI itself, so multiple consumers
11
+ * can subscribe at once.
12
+ */
13
+ interface NativeEventListener {
14
+ fun onEvent(eventType: String, payloadJson: String)
15
+ }
16
+
17
+ /**
18
+ * Process-wide accessor that exposes the live NativeAgentHandle and a
19
+ * multi-listener event fan-out to other modules in the same Android
20
+ * process — most notably the STOMP transport service in theshell.
21
+ *
22
+ * The single FFI-side NativeEventCallback registered by NativeAgentPlugin
23
+ * forwards every event into [dispatch] in addition to its existing
24
+ * notifyListeners("nativeAgentEvent", ...) call, so the WebView pipeline
25
+ * is byte-equivalent to before this bridge existed.
26
+ *
27
+ * Listeners are strong-referenced; callers MUST call
28
+ * [removeNativeEventListener] when their lifecycle ends.
29
+ */
30
+ object NativeAgentBridge {
31
+
32
+ @Volatile
33
+ private var handleRef: NativeAgentHandle? = null
34
+
35
+ private val listeners = CopyOnWriteArrayList<NativeEventListener>()
36
+
37
+ /**
38
+ * The live FFI handle, or null if NativeAgent has not been
39
+ * initialized yet (or has been destroyed).
40
+ */
41
+ @JvmStatic
42
+ fun handle(): NativeAgentHandle? = handleRef
43
+
44
+ @JvmStatic
45
+ fun addNativeEventListener(listener: NativeEventListener) {
46
+ listeners.addIfAbsent(listener)
47
+ }
48
+
49
+ @JvmStatic
50
+ fun removeNativeEventListener(listener: NativeEventListener) {
51
+ listeners.remove(listener)
52
+ }
53
+
54
+ internal fun setHandle(h: NativeAgentHandle?) {
55
+ handleRef = h
56
+ }
57
+
58
+ internal fun dispatch(eventType: String, payloadJson: String) {
59
+ for (l in listeners) {
60
+ try {
61
+ l.onEvent(eventType, payloadJson)
62
+ } catch (t: Throwable) {
63
+ android.util.Log.w(
64
+ "NativeAgentBridge",
65
+ "listener threw on $eventType: ${t.message}",
66
+ )
67
+ }
68
+ }
69
+ }
70
+ }
@@ -98,6 +98,7 @@ class NativeAgentPlugin : Plugin() {
98
98
  data.put("eventType", eventType)
99
99
  data.put("payloadJson", payloadJson)
100
100
  notifyListeners("nativeAgentEvent", data)
101
+ NativeAgentBridge.dispatch(eventType, payloadJson)
101
102
  }
102
103
  })
103
104
  h.setNotifier(NativeNotifierImpl(context.applicationContext))
@@ -109,6 +110,7 @@ class NativeAgentPlugin : Plugin() {
109
110
  }
110
111
  h.persistConfig()
111
112
  handle = h
113
+ NativeAgentBridge.setHandle(h)
112
114
  context
113
115
  .getSharedPreferences(STORAGE_FILE, android.content.Context.MODE_PRIVATE)
114
116
  .edit()
@@ -530,6 +532,7 @@ class NativeAgentPlugin : Plugin() {
530
532
 
531
533
  override fun handleOnDestroy() {
532
534
  scope.cancel()
535
+ NativeAgentBridge.setHandle(null)
533
536
  handle = null
534
537
  }
535
538