Add documentation about onStateChange usage

This commit is contained in:
Arnaud 2026-06-01 15:30:49 +04:00
parent f66c6114f9
commit c9a976fe6e
No known key found for this signature in database
GPG Key ID: A6C7C781817146FA
2 changed files with 11 additions and 1 deletions

9
api.md
View File

@ -98,13 +98,20 @@ Returns a `MappingResult` containing the mapping `id` (needed for `destroyMappin
Returns an error if no NAT device is found, the mapping fails, or the timeout expires.
> **Warning:** `onStateChange` runs on libplum's internal C thread, not the
> chronos event loop. Do not call chronos APIs or touch non-thread-safe state
> from it; restrict it to thread-safe operations (e.g. `Atomic`, a channel).
### destroyMapping
```nim
proc destroyMapping*(id: cint)
```
Removes a mapping. Must be called exactly once after a successful `createMapping`.
Removes a mapping. Must be called exactly once after a successful `createMapping`,
otherwise the mapping's internal handle is leaked for the lifetime of the process.
Calling it again, or with an unknown `id`, is a safe no-op. `cleanup` also releases
any mappings still active.
### hasMapping

View File

@ -59,6 +59,9 @@ type
mapping*: PlumMapping
PlumStateCallback* = proc(state: PlumState, mapping: PlumMapping) {.callback.}
## Invoked on mapping state changes after the initial result. Runs on
## libplum's internal C thread, not the chronos loop: only touch
## thread-safe state from it (e.g. Atomic), never chronos APIs.
MappingHandle = ref object
signal: ThreadSignalPtr