• Stars
    star
    1
  • Language
    JavaScript
  • License
    Other
  • Created almost 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Clojure Atoms for Javascript. Shared, synchronous, independent state. A fork of https://github.com/cjohansen/js-atom.

Node.js CI npm

Atom.js

Shared, synchronous, independent state--Clojure Atoms for Javascript. A fork of js-atom.

Rational

Immutable values require some sort of external state management in order for your app to change state. One option is to use variables in scope to hold the current state of your app:

function startApp(root) {
    var data;

    function render() {
        React.renderComponent(AppUI(data), root);
    }

    pollForData(function (newData) {
        data = newData;
        render();
    });

    // ...
}

This sort of works, but quickly becomes unwieldy. Atoms offer a formal mechanism for maintaining a single mutable reference. An atom is used to hold the current state. It offers a small API for changing (or replacing) this state, subscribing to changes, and for validating state changes.

function startApp(root) {
    var state = atom({});

    function render() {
        React.renderComponent(AppUI(state.deref()), root);
    }

    // Render when the state changes
    state.addWatch("poll-update", render);

    pollForNewData(function (newData) {
        state.reset(newData);
    });

    // ...
}

Immutability

Atoms are most useful when containing immutable values, but there's nothing stopping you from sticking whatever you want in them. If you put a mutable value in the atom, you either have to make sure you don't actually mutate it, or lose some of the benefits (e.g. being able to trust past versions of the state).

API

The API is designed to mirror Clojure's atoms as closely as possible. Because atoms are references, and not values, I didn't see any problems with defining the API as methods on the atom object.

atom(val[, options])

Creates a new atom wrapping the provided value. options is optional, and currently only supports one option: validator:

var atom = require("atom");
var ref = atom([], { validator: Array.isArray });

atom.reset([1, 2, 3]); // OK
atom.reset({}); // Throws exception

atom.isAtom(val)

Returns true if val is an atom instance otherwise returns false.

atom#deref()

(aliased as atom.deref(value))

Returns the contained value.

atom#reset(val)

Replace the current state with a new value

atom#swap(fn[, ...])

Update the state by applying the function to the current value, and setting the return value as the new value of the atom. Any additional arguments are passed to the function as well, after the atom value, e.g.: atom.swap(fn, 1, 2, 3) will replace the current value with what is returned from fn(atomValue, 1, 2, 3).

atom#compareAndSet(oldValue, newValue)

Atomically sets the value of atom to newval if and only if the current value of the atom is identical to oldval. Returns true if set happened, else false.

atom#addWatch(key, function (key, ref, old, new) {})

Add a function that will be called whenever the atom value changes. The key is just a string identifying this watcher - it can be used to remove the watcher again. The callback is called with four arguments whenever the state changes (e.g. with reset or swap):

  • key - The key used to register the watcher
  • ref - The atom reference
  • old - The previous value
  • new - The new value

atom#removeWatch(key)

Removes the previously added watcher.

atom#toString

Prints a useful string representation of the contents of the atom.

License

Copyright © 2014, 2019 Christian Johansen, Delon Newman.

See LICENSE

Support

Buy Me A Coffee

More Repositories

1

invokable

Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).
Ruby
43
star
2

mini-levenshtein

Simple, fast Levenshtein distance and similarity ratio for Ruby
C
27
star
3

activerecord-setops

Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Ruby
21
star
4

magbot

A CLI application for fetching media from jw.org feeds
Perl
9
star
5

zera-5

A light-weight Clojure interpreter
JavaScript
8
star
6

activerecord-pull

A simple query interface for pulling deeply nested data from records.
Ruby
4
star
7

contracts-gen

Generate data from contracts
Ruby
2
star
8

dragnet

A work-in-progress system for programmable surveys, and forms.
Ruby
2
star
9

ALJAFP

A Little Java, A Few Patterns
Java
1
star
10

piglatin

Pig Latin translator
Perl
1
star
11

cantor

Relational Algebra and Reporting
Ruby
1
star
12

sinatra-rest-service-auth

Ruby
1
star
13

sleepr

Stop hacking and get some sleep
Perl
1
star
14

connect4

JavaScript
1
star
15

tether

Ruby
1
star
16

tetris.js

An (incomplete) tetris clone
JavaScript
1
star
17

wonderscript-alpha

A lisp for the web
TypeScript
1
star
18

periscope

A Perl Module for viewing sites through a periscope
Perl
1
star
19

pandora-periscope

A Periscope for Pandora
Perl
1
star
20

java-posix

POSIX for Java a clone (for posterity, I have no intention of maintaining this) imported from tarball found here: http://www.bmsi.com/java/posix/index.html
Java
1
star
21

Pudu

A light-weight Moose-like object system that makes it easy to create encapsulated, immutable objects
Perl
1
star
22

Makakilo-Elementary

Ruby
1
star
23

sleepr-preferences

Preferences dialog for sleepr
Python
1
star
24

yugo

An experimental tool for converting legacy ColdFusion applications into Ruby / Rack applications.
Ruby
1
star
25

talks

1
star
26

asdf-vm.el

asdf-vm integration for Emacs
Emacs Lisp
1
star
27

kigo

Ruby is already pretty lispy let's take it the rest of the way.
Ruby
1
star
28

rubygems

A fork to add offline installation features, all changes are in offline branch
Ruby
1
star