gleeth/watcher
Block watcher using a BEAM actor (OTP gen_server).
Spawns a supervised actor process that polls for new blocks and sends events to a caller-owned Subject. Uses the OTP actor pattern for proper lifecycle management and graceful shutdown.
Examples
let assert Ok(w) = watcher.start(provider)
// Block until a new block arrives (or timeout after 30s)
case watcher.receive(w, 30_000) {
Ok(watcher.NewBlock(number, hash)) -> io.println("Block: " <> number)
Error(Nil) -> io.println("Timeout")
}
watcher.stop(w)
Types
A block event emitted by the watcher.
pub type BlockEvent {
NewBlock(number: String, hash: String)
}
Constructors
-
NewBlock(number: String, hash: String)A new block was detected.
Configuration for the watcher.
pub type WatcherConfig {
WatcherConfig(poll_interval_ms: Int)
}
Constructors
-
WatcherConfig(poll_interval_ms: Int)Arguments
- poll_interval_ms
-
How often to poll in milliseconds. Default: 2000.
Values
pub fn receive(
watcher: Watcher,
timeout_ms: Int,
) -> Result(BlockEvent, Nil)
Receive the next block event, blocking up to timeout_ms milliseconds.
Returns Error(Nil) on timeout.
Examples
case watcher.receive(w, 10_000) {
Ok(watcher.NewBlock(number, hash)) -> // new block
Error(Nil) -> // timeout
}
pub fn start(
provider: provider.Provider,
) -> Result(Watcher, String)
Start watching for new blocks with default config (poll every 2s).
Examples
let assert Ok(w) = watcher.start(provider)
pub fn start_with_config(
provider: provider.Provider,
config: WatcherConfig,
) -> Result(Watcher, String)
Start watching with custom configuration.
Examples
let config = watcher.WatcherConfig(poll_interval_ms: 500)
let assert Ok(w) = watcher.start_with_config(provider, config)
pub fn start_with_subject(
provider: provider.Provider,
config: WatcherConfig,
events: process.Subject(BlockEvent),
) -> Result(Watcher, String)
Start watching, sending events to a caller-provided Subject. Use this when another process needs to own the receiving Subject (e.g. the event watcher actor).