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

immutable-app-state

v0.0.1

Published

v0.0.0

Downloads

5

Readme

immutable-app-state

v0.0.0

Build Status

A place to store the object that represents your app state. You can create subscriptions, set, and get paths.

Chrome extension

appstate-live-update

Immutability

Object retrieved from app-state are not immutable; however, the object backing app-state is immutable.

In practice this means that you cannot change the app-state by reference:

appState = require('immutable-app-state').init();
appState('user.name.first','gertrude');
appState('user').name.first = 'gordon';
appState('user.name.first'); // still gertrude

This has practical ramifications especially when dealing with streams and inspecting them after writes to the stream have happened. Basically appState.stream().toArray() would return all identical values without immutability.

Usage

Create a new app state object:

var state = require('app-state').init();

Init: require('app-state').init([ options ]) - returns state

Available options:

options.devTools - set this truthy if you want to use the dev tools Chrome Extension

Instance Methods

Set:

  • state(path, value) - returns state
  • state.set(path, value) - returns state

You can use the named method or the shortcut method that is the state instance itself.

state.set('user.profile', { library : library });

You can set paths that don't exist yet. Empty objects will be created. Only empty objects will be created, not arrays.

Subscription notifications are run after setting.

Get:

  • state.get(path) - returns value
  • state(path) - returns value

You can use the named method or the shortcut method that is the state instance itself.

state.get('user.library.book.4');

Will return undefined if the path doesn't have objects on it.

Can access items in an array using index numbers with the dot notation.

Subscribe: state.subscribe(path, callback) - returns state

Subscribe for change events with a callback. Callback is called with the state as context:

state.subscribe('user.profile.library', callback);

Subscriptions get called on any set that can potentially change them, whether it does or not. Can subscribe to properties that do not yet exist.

Te user.profile.library subscription gets notified for any of the following set paths:

  • user.profile.library
  • user
  • user.profile.library.public

The user.profile.library subscription does not get notified for any of the following set paths:

  • api
  • user.profile.notifications

Subscribers: state.subscribers(path) - returns length

Returns number of subscribers on an exact path. Doesn't count longer or shorter paths.

Transform: state.transform(key, transformFunction, varargs...) - returns the new value that was set

The transform function is called with state.get(key) followed by the varargs.

Calling transform is equivalent to:

state.set(key, transformFunction(state.get(key), ...));

This method allows a collection of transform calls to represent allowed ways to update the state. Since these calls can be implemented as simple input / output with no side effects, it allows easy testing as well.

Mixins

An array of mixins can be passed in on initialization. Each mixin will be called with the state instance before the state instance is returned.

Stream Mixin

This mixin allows you to subscribe to appState changes in the form of a stream. A stream is returned that is only written to when a subscription for the passed in key would run:

state.stream(key) - returns highland stream.

To use state.stream you must pass in the mixin. This functionality is separated out as a mixin so that if you do not use it then you do not pull in highland as a dependency.

var AppState = require('app-state'),
    streamMixin = require('app-state/mixin/stream');
    
appState = AppState.init({ mixins : [ streamMixin ] });
    
appState
    .stream('user')
    .filter(function(user) {
        return user.authorized;
    })
    .each(function(user) {
        console.log('user is authorized', user);
    });

Note that since appState functions via reference, if you do a toArray on the stream all items will look the same. If you template the results in an each, then each update will show the current state. In the case of toArray, each item in the array is also showing the current state at the time the results of toArray are inspected.

Note:

Highland is a convenient "Reactive Extensions like" library for Node users. Since it is built on top of the built on node stream functionality it is quite compact and its behavior depends on a stable core feature of Node.

Theory

The idea behind having an app state is that it is a unified event channel to communicate actions through the app. Views can subscribe to the paths that inform them. Models can trigger sets as they acquire new information. Models can also subscribe to app state changes to react to changes with business logic.

Having a central communication hub allows the modularization of apps and the adding and modification of features and business logic easily. The app state doesn't contain any business logic itself, just the Model layer should have that.

To keep things simply you cannot run a set while another one is running.

The concept is similar to that of a dispatcher in Flux.

Future features:

  • Ordered subscriptions (before / after other subscription).
  • Implement https://github.com/facebook/immutable-js as the data store.
  • will use get-setter npm as dependency

Release notes:

  • 1.0.0 - Backward incompatibility - removed stat.calculations, since it can be achieved with transforms or the stream mixin.