Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mgartner/lazy_agent
Start agents lazily.
https://github.com/mgartner/lazy_agent
Last synced: 5 days ago
JSON representation
Start agents lazily.
- Host: GitHub
- URL: https://github.com/mgartner/lazy_agent
- Owner: mgartner
- License: mit
- Created: 2018-04-28T18:20:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-05-04T19:25:15.000Z (over 6 years ago)
- Last Synced: 2024-10-12T02:46:06.681Z (about 1 month ago)
- Language: Elixir
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# LazyAgent
![Hex.pm](https://img.shields.io/hexpm/v/lazy_agent.svg)
[![Build Docs](https://img.shields.io/badge/hexdocs-release-blue.svg)](https://hexdocs.pm/lazy_agent/LazyAgent.html)
[![Build Status](https://travis-ci.org/mgartner/lazy_agent.svg?branch=master)](https://travis-ci.org/mgartner/lazy_agent)LazyAgent wraps Elixir's Agent library to allow delayed execution of the
initial state generator function until the first time the Agent process is
accessed.It is intended to be used in test environments of applications with agents that
have initialization functions that take hundreds of milliseconds or more to
execute. When developing and running a subset of the test suite, these type of
agents can significantly increase the time it takes to run tests, which slows
down development. Using LazyAgent allows execution of only the initialization
functions necessary to run the test subset.## Installation
Install LazyAgent by adding `lazy_agent` to your list of dependencies in
`mix.exs`:```elixir
def deps do
[
{:lazy_agent, "~> 0.1.0"}
]
end
```## Configuration
To enable or disable LazyAgent, add the following to an environment
configuration file:```elixir
use Mix.Configconfig :lazy_agent, enabled?: true
```LazyAgent supports [Confex](https://github.com/Nebo15/confex)-style configs, so
you can rely on environment variables:```elixir
use Mix.Configconfig :lazy_agent,
enabled?: {:system, :boolean, "ENABLE_LAZY_AGENT", true}
```## Usage
Currently, LazyAgent supports the lazy equivalents of:
* `Agent.start/2`
* `Agent.start_link/2`
* `Agent.get/3`
* `Agent.update/3`
* `Agent.get_and_update/3`
* `Agent.stop/3````elixir
iex> {:ok, pid} = LazyAgent.start_link(fn -> 42 end)
iex> LazyAgent.get(pid, fn state -> state end)
42iex> LazyAgent.start_link(fn -> 42 end, name: :lazy)
iex> LazyAgent.update(:lazy, fn state -> state + 5 end)
:ok
iex> LazyAgent.get(:lazy, fn state -> state end)
47iex> {:ok, pid} = LazyAgent.start(fn -> 42 end)
iex> LazyAgent.get_and_update(pid, fn state -> {state, state + 5} end)
42
iex> LazyAgent.get(pid, fn state -> state end)
47
iex> LazyAgent.stop(pid)
:ok
```