Code Monkey home page Code Monkey logo

global-lazy's Introduction

GlobalLazy

Build Status Hex.pm Version Documentation

NOTE: This library is deprecated in favour of whitfin/global-flags. You should migrate when possible as this library will not receive updates - it only exists to avoid breaking existing applications.

This library is designed to provide an easy way to lazily initial global state in Elixir, without having to be linked to a main application tree.

This is useful for libraries which always require certain state, regardless of the state of the parent application. It's aimed at use cases where it's slow to have a separate process, and it's wasteful to start an ETS table.

This library is tiny, so you can include it with minimal overhead.

Installation

To install it for your project, you can pull it directly from Hex. Rather than use the version shown below, you can use the the latest version from Hex (shown at the top of this README).

def deps do
  [{:global_lazy, "~> 1.0"}]
end

Documentation and examples can be found on Hexdocs as they're updated automatically alongside each release.

Usage

As mentioned above, the API is extremely small, so there's really only one thing to learn how to use. Below is an example of lazily initializing an Agent, used to keep track of a global counter.

defmodule MyLibrary do

  @doc """
  Retrieves the next integer in the global counter.
  """
  def next_int do
    # initializes the Agent only the first time called
    GlobalLazy.init("my_library:started", fn ->
      Agent.start(fn -> 1 end, [ name: :my_library_agent ])
    end)

    # guaranteed to now have a started Agent
    Agent.get_and_update(:my_library_agent, fn count ->
      {count, count + 1}
    end)
  end
end

You should handle all errors inside the provided function; feel free to just crash if you'd prefer to retry initialization on the next call - the flag is only switched if the function returns successfully. You should also be careful to namespace your flags, because they're global (duh).

global-lazy's People

Contributors

whitfin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.