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

fluxible-immutable-store

v1.2.0

Published

A helper for creating Fluxible/Dispatchr BaseStores that manage immutable data structures.

Downloads

35

Readme

Fluxible Immutable Store

npm version Build Status Coverage Status

A helper function to create stores that protect private data from being set from outside. That is the one and only feature of this module.

Use whatever data structures you want inside. You can choose to use immutable data structures from Immutable, Mori, or any other library, or you can choose to return deep clones of your internal data. All this function does is help you keep the internals of your store internal.

The library relies on Yahoo!'s Dispatchr createStore function. You can use these stores with just the dispatcher or with Yahoo!'s Fluxible library.

Installation

npm install --save fluxible-immutable-store

Example

Here is part of a reimplementation of the TodoStore from the Fluxible examples. It uses Facebook's Immutable data structures internally, but that is merely a design choice, not a requirement of this module.

var Immutable = require('immutable');
var createImmutableStore = require('fluxible-immutable-store');

var TodoStore = createImmutableStore(function () {
  // Hide any data you want in variables declared in the closure
  var todos;

  // This is the spec that is expected by createStore
  return {
    storeName: 'TodoStore',
    handlers: {
      'CREATE_TODO_START': '_createTodoStart'
    },
    initialize: function () {
      todos = Immutable.List();
    },
    _createTodoStart: function (todo) {
      todos = todos.push(Immutable.Map(todo));
      this.emitChange();
    },

    // Define the accessors to the internal state
    accessors: {
      'todos': function () {
        return todos.toJS();
      }
    }
  };
});

module.exports = TodoStore;

The variables that represent the store's state can be exposed as accessors. They will have read-only properties created for them. If you use an immutable data structure, then there will be no way to set the property from the outside or to mutate the structure's internal data.

In your component that needs access to the TodoStore, you would have code similar to this:

var TodoList = React.createClass({
  mixins: [ StoreMixin ],

  statics: {
    storeListeners: [ 'TodoStore' ]
  },

  // Called when TodoStore emits a change event
  onChange: function onChange() {
    var todoStore = this.getStore('TodoStore');
    this.setState({
      todos: todoStore.todos
    });
  }

  render: function render() {
    ...
  }
});

You won't be able to reassign a new object to the todoStore.todos field, thus getting you one step closer to an immutable store.

Alternate Accessor Definition

Alternatively, you can set the private data on the privates object that is passed into your spec factory function and define the accessors as a simple array of field names. If you do not need to change the data structure upon access (such as converting an Immutable.List to a plain Javascript array), then this method is more succinct.

var Immutable = require('immutable');
var createImmutableStore = require('fluxible-immutable-store');

// Set any private field data as fields on `privates`
var TodoStore = createImmutableStore(function (privates) {
  // This is the spec that is expected by createStore
  return {
    storeName: 'TodoStore',
    handlers: {
      'CREATE_TODO_START': '_createTodoStart'
    },
    initialize: function () {
      privates.todos = Immutable.List();
    },
    _createTodoStart: function (todo) {
      privates.todos = privates.todos.push(Immutable.Map(todo));
      this.emitChange();
    },

    // Define the accessors to the internal state by their names in `privates`
    accessors: [ 'todos' ]
  };
});

module.exports = TodoStore;