Packages

Stores and subscriptions

Current section

Files

Jump to
store README.md
Raw

README.md

# store
[![Package Version](https://img.shields.io/hexpm/v/store)](https://hex.pm/packages/store)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/store/)
![Erlang-compatible](https://img.shields.io/badge/target-erlang-b83998)
![JavaScript-compatible](https://img.shields.io/badge/target-javascript-f1e05a)
An immutable store structure, with the ability to add subscribers. Subscribe with callbacks that will be called when a set or update is called on a store. Checkout the mutable version, [mut_cell](https://hexdocs.pm/mut_cell/) with a mutable store that changes the value in place.
## Install
```sh
gleam add store@1
```
```gleam
import store
import gleam/int
import gleam/io
pub fn main() {
let cell = store.make(10)
let #(cell, unsub) =
cell
|> store.subscribe(fn(value) {
{ "value: " <> value |> int.to_string } |> io.println
})
let cell = cell |> store.set(20)
let cell = unsub(cell)
let cell = cell |> store.set(30)
cell
|> store.get
|> int.to_string
|> io.println
}
```
Further documentation can be found at <https://hexdocs.pm/store>.