Services.Once (fnord v0.9.38)

View Source

This module provides a mechanism to perform actions only once, using a unique key provided by the caller to determine whether the action has already been performed this session.

The agent is tree-scoped rather than VM-global: start_link/0 starts an unnamed agent and registers it for the current process tree via Services.Instance. Each instance root (a test, a Fnord.Instance checkout, or the escript's main process in production) gets its own notion of "once", so seen-keys cannot leak between trees sharing a BEAM.

Summary

Functions

Returns a specification to start this module under a supervisor.

Checks if a key has been seen before. If the key has not been seen, it returns {:error, :not_seen}. If the key has been seen, it returns {:ok, value} where value is the value associated with the key, or true if no value was specified.

Run the given zero-arity function only once per unique key. Subsequent calls with the same key will be ignored.

True when a Services.Once instance is running in the current process tree. Callers that may be exercised outside a live session (without the service roster running) use this to no-op instead of crashing.

Marks a key as seen. If the key has not been seen before, it returns true and updates the internal state. If the key has already been seen, it returns false without updating the state.

Starts the agent that keeps track of seen keys and registers it as the current process tree's instance.

Emits a warning (using UI.warn/1) if the message has not yet been emitted during this session.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get(key)

Checks if a key has been seen before. If the key has not been seen, it returns {:error, :not_seen}. If the key has been seen, it returns {:ok, value} where value is the value associated with the key, or true if no value was specified.

run(key, fun)

@spec run(term(), (-> any())) :: any() | :ignore

Run the given zero-arity function only once per unique key. Subsequent calls with the same key will be ignored.

running?()

@spec running?() :: boolean()

True when a Services.Once instance is running in the current process tree. Callers that may be exercised outside a live session (without the service roster running) use this to no-op instead of crashing.

set(key, value \\ true)

Marks a key as seen. If the key has not been seen before, it returns true and updates the internal state. If the key has already been seen, it returns false without updating the state.

start_link()

Starts the agent that keeps track of seen keys and registers it as the current process tree's instance.

warn(msg)

Emits a warning (using UI.warn/1) if the message has not yet been emitted during this session.