New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

atomo

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

atomo

Clojure's Atom implementation in JavaScript

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

atomo

Travis Badge

Clojure's Atom implementation in JavaScript.

From the Clojure docs:

Atoms provide a way to manage shared, synchronous, independent state.

On top of the above, atoms provide also validation and observation capabilities.

Installation

$ npm install atomo

Usage

Atoms are references to values that may change over time. The most basic operation on an atom is querying its current value:

var a = require("atomo");

var anAtom = a.atom(42);
anAtom.deref() === 42
// true

An atom's value can be set to another value:

anAtom.reset("foo");

anAtom.deref() === "foo"
// true

Alternatively, an atom's value can be transitioned to another value providing a function:

function increment(x) { return x + 1; }

var anAtom = a.atom(41);
anAtom.swap(increment);
anAtom.deref() === 42
// true

Validation

Atoms support validation through a validation function, and they will throw an exception whenever we try to set the atom's value to an illegal value.

function is42(x) { return x === 42 };

var anAtom = a.atom(42, {validator: is42});

anAtom.reset(43); // Error!
anAtom.swap(increment); // Error!

Observation

Atoms support adding and removing watches for listening to value changes. Watches are called with three arguments: the atom, the old value and the new value.

var anAtom = a.atom(42);

var watcher = function(theAtom, oldValue, newValue){
    console.log("Atom changed from", oldValue, "to", newValue);
};

anAtom.addWatch(watcher);

anAtom.swap(increment);
// Atom changed from 42 to 43

anAtom.reset(42);
// Atom changed from 43 to 42

anAtom.removeWatch(watcher);

anAtom.swap(increment);
anAtom.reset(42);

License

BSD 2-clause license, Copyright 2014 - 2015 Alejandro Gómez.

Keywords

atom

FAQs

Package last updated on 15 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts