npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@delonnewman/atom

v0.2.1

Published

Shared, synchronous, independent state - Clojure Atoms for Javascript

Downloads

1

Readme

Node.js CI npm dependencies Status devDependencies Status

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