@@ -10,15 +10,13 @@ import std/[locks, sequtils, options, tables]
1010import chronicles
1111import ./ ffi_types, ./ cbor_serial
1212
13-
1413type EventEnvelope * [T] = object
1514 # # Standard wire shape for CBOR-encoded FFI events:
1615 # # { eventType: tstr, payload: <T> }
1716 # # Pair with `dispatchFFIEventCbor` (or call `cborEncode` directly).
1817 eventType* : string
1918 payload* : T
2019
21-
2220type
2321 FFIEventListener * = object
2422 id* : uint64
3331 nextId* : uint64 # # Monotonic id source. 0 is reserved as "invalid"; ids start at 1.
3432 byEvent* : Table [string , seq [FFIEventListener ]]
3533
36-
3734proc initEventRegistry * (reg: var FFIEventRegistry ) =
3835 # # Must be called exactly once on the owning thread before the registry
3936 # # is shared. The embedded `Lock` wraps a platform primitive that cannot
@@ -129,7 +126,6 @@ proc snapshotListeners*(
129126 listeners.add (l)
130127 listeners
131128
132-
133129const EventQueueCapacity * = 1024
134130 # # ~24 KiB per context. Sustained backlog at this depth means a
135131 # # listener is wedged — what the stuck flag exists to surface.
@@ -202,7 +198,6 @@ proc eventQueueLen*(q: var EventQueue): int {.raises: [], gcsafe.} =
202198 withLock q.lock:
203199 return q.count
204200
205-
206201const emptyListenerPayload* : cstring = " "
207202 # # Non-nil zero-length buffer handed to listeners when a payload is
208203 # # empty, so a consumer doing `std::string(data, len)` / `memcpy` never
@@ -216,17 +211,21 @@ proc notifyListeners*(
216211 # # consumer doing `std::string(data, len)` / `memcpy` never receives nil.
217212 let n = max (dataLen, 0 )
218213 let dataPtr =
219- if n > 0 and not data.isNil (): cast [ptr cchar ](data)
220- else : cast [ptr cchar ](emptyListenerPayload)
214+ if n > 0 and not data.isNil ():
215+ cast [ptr cchar ](data)
216+ else :
217+ cast [ptr cchar ](emptyListenerPayload)
221218 for listener in listeners:
222219 listener.callback (retCode, dataPtr, cast [csize_t ](n), listener.userData)
223220
224221proc notifyListenersErr * (listeners: seq [FFIEventListener ], msg: string ) =
225222 # # Error fan-out: adapts the message string to `notifyListeners`, which
226223 # # supplies the non-nil pointer for the empty-message case.
227224 let p =
228- if msg.len > 0 : cast [pointer ](unsafeAddr msg[0 ])
229- else : cast [pointer ](emptyListenerPayload)
225+ if msg.len > 0 :
226+ cast [pointer ](unsafeAddr msg[0 ])
227+ else :
228+ cast [pointer ](emptyListenerPayload)
230229 notifyListeners (listeners, RET_ERR , p, msg.len)
231230
232231var ffiCurrentEventRegistry* {.threadvar .}: ptr FFIEventRegistry
@@ -269,8 +268,10 @@ template dispatchFFIEvent*(eventName: string, body: untyped) =
269268 withFFIEventDispatch (eventName, listeners):
270269 let event = body
271270 let dataPtr: pointer =
272- if event.len > 0 : cast [pointer ](unsafeAddr event[0 ])
273- else : cast [pointer ](emptyListenerPayload)
271+ if event.len > 0 :
272+ cast [pointer ](unsafeAddr event[0 ])
273+ else :
274+ cast [pointer ](emptyListenerPayload)
274275 notifyListeners (listeners, RET_OK , dataPtr, event.len)
275276
276277template dispatchFFIEventCbor * (eventName: string , eventPayload: typed ) =
0 commit comments