event_hub/stateful

The stateful module provides a way to manage and notify subscribers about events based on a mutable state. It supports creating observers that can maintain and update state, which can be useful for handling stateful events in an application.

Examples

Stateful Observer

import gleam/io
import event_hub/stateful

pub fn main() {
  use hub <- stateful.new("initial state")
  let #(current_state, unsubscribe) =
    stateful.subscribe(hub, True, fn(value) {
      io.println("Received initial state: " <> value)
    })

  io.println("Current state: " <> current_state)

  stateful.notify(hub, "new state")

  unsubscribe()
  stateful.notify(hub, "final state")
}

Types

Represents a hub for managing event subscriptions and notifications with a mutable state.

pub type Hub(value_type)

Functions

pub fn new(with value: a, in context: fn(Hub(a)) -> b) -> b

Creates a new stateful observer hub with an initial state, executes the given context with the hub, and stops the hub afterward.

Parameters

  • value: The initial state value.
  • context: A function that takes the created Hub and returns a result.

Returns

The result of executing the context function.

Example

import event_hub/stateful

pub fn example() {
  stateful.new("initial state", fn(hub) {
    // Use the hub
    Nil
  })
}
pub fn notify(on hub: Hub(a), with value: a) -> Nil

Notifies subscribers of the hub about an event with a new state value. These notifications occur in parallel but notify waits for all of them to complete.

Parameters

  • hub: The Hub to notify.
  • value: The new state value.

Example

import event_hub/stateful

pub fn example(hub: stateful.Hub(String)) {
  stateful.notify(hub, "new state")
}
pub fn state(of hub: Hub(a)) -> a

Retrieves the current state of the hub.

Parameters

  • hub: The Hub to retrieve the state from.

Returns

The current state value.

Example

import event_hub/stateful

pub fn example(hub: stateful.Hub(String)) {
  let current_state = stateful.state(hub)
}
pub fn subscribe(
  on hub: Hub(a),
  should notify_current_state: Bool,
  with callback: fn(a) -> Nil,
) -> #(a, fn() -> Nil)

Subscribes to state changes and returns the current state and an unsubscribe function. The callback will be invoked with the current state value when notify is called. If notify_current_state is True, the callback will be immediately invoked with the current state.

Parameters

  • hub: The Hub to add the callback to.
  • notify_current_state: Whether to immediately invoke the callback with the current state.
  • callback: The callback function to invoke with the state value.

Returns

A tuple containing the current state value and an Unsubscribe function that can be called to remove the callback.

Example

import gleam/io
import event_hub/stateful

pub fn example(hub: stateful.Hub(String)) {
  let #(current_state, unsubscribe) =
    stateful.subscribe(hub, True, fn(value) {
      io.println("Received state: " <> value)
    })

  // To unsubscribe
  unsubscribe()
}
Search Document