💧 ELIXIR ALCHEMY DIFFICULTY: Adept Behavioral

Memento

Capture and externalize an actor's internal state to restore it from doom.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Memento
ARCANUM SCHOOL: Chronomancy // State-Preservation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CONCURRENT ACTOR-MODEL ALCHEMY //
defmodule Tekromancy.TimeWeaver do
  def save_state(state_map), do: :erlang.term_to_binary(state_map)

  def restore_state(binary_memento), do: :erlang.binary_to_term(binary_memento)
end

# Usage inside an actor before a risky computation
# memento = Tekromancy.TimeWeaver.save_state(current_state)
# # If crash occurs, a supervisor can reboot and we inject the memento to restore

Death is merely a transition in Elixir’s actor model. A Supervisor resurrects a fallen process, but what of its memories? The Memento pattern captures a GenServer’s state, serializing it into an immutable crystal (binary). Upon rebirth, the TimeWeaver shatters the crystal, restoring the actor exactly as it was.

Runic Tags: #elixir #behavioral #memento #serialization #state