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

sweetflux

v0.7.0

Published

Sweeten your flux

Downloads

10

Readme

sweetflux

Circle CI

Quick start:

npm install --save sweetflux
import {dispatch, createStore} from 'sweetflux'

The Gist

This library adds 2 functions to Facebook's flux implementation to guide you into the (state, action) -> state pattern.

In [email protected], Facebook added 3 new abstract ES 2015 classes (FluxMapStore -> FluxReduceStore -> FluxStore). These stores guide you into the reducer pattern but, unfortunately, they also lead you into classes. This library reimplements the FluxReduceStore in Douglas Crockford's class-free object oriented programming style. The FluxMapStore can be implemented using defensive copies or with immutable data structures. Examples for both techniques are included below.

This library is similar to Reflux and Redux except that this library doesn't try to replace the dispatcher with a new implementation. The library encourages you into simple patterns but doesn't try to change the core concepts. The flux/Dispatcher and fbemitter/EventEmitter modules are the key to Flux and this project depends directly on Facebook's implementations.

This new "Flux framework" adds a surface area of 2 new functions:

  • dispatch
  • createStore

Enjoy!

API Reference

  1. dispatch( type, data ) or dispatch( action )
Submit an action into the stores. You must specify the type and, optionally, some data.

```js
import {dispatch} from 'sweetflux';

// dispatch an action with a string
dispatch('REQUEST_SETTINGS')  // => { type: 'LOAD_SETTINGS', data: undefined }
// or with data
dispatch('LOAD_SETTINGS', { a: 1, b: 2 }) // => { type: 'LOAD_SETTINGS', data: { a: 1, b: 2 } }
// or with a custom object
dispatch({ actionType: 'move', mode: 'off the rails' })
```
  1. createStore(name, initialState, reducer, methods)
Create a new store with a name, initialState, reducer function and an object with methods that maybe used to operate state.

```js
const INC = 'INC'
import {createStore} from 'sweetflux';

export default createStore('CountStore', 0, (state, action) => {
  switch (action.type)
  case INC:
    return state + 1;
  default:
    return state;
}, {
  getCount: (state) => state // state is the count itself!
})
```

In addition to the state and action the reducer function sends the waitFor as the third argument. This allows stores to express dependencies on data in other stores and ensure that their reducers are executed prior to continuing execution. This ensures that the correct order of operation is performed.

```js
import {loadMessage} from './MyActions'
import {createStore} from 'sweetflux';

const MessageStore = createStore('MessageStore', [], function(state, action) {
    switch(action.type) {
      case loadMessage:
        return state.concat(action.message)
      default:
        return state
    }
})

const MessageCountStore = createStore( 'MessageCountStore', 0,
  function(state, action, waitFor) {
    // ensure that MessageStore reducer is executed before continuing
    waitFor([MessageStore.dispatchToken])
    switch(action.type) {
      case loadMessage:
        return state+1
      default:
        return state
    }
  }
)
```

Store Properties and Methods

| name | comment | |---------|------| | name | The name supplied when creating the store | | dispatchToken | A number used with waitFor | | addListener | A function to add a callback for events | | getState | A function that returns the current state |

Put it all together

const {INC, DEC} = ['INC', 'DEC'];
var React = require('react');
var {dispatch, createStore} = require('sweetflux');
var PureRenderMixin = require('react-addons-pure-render-mixin');


var countStore = createStore('CountStore', 0, function(state, action) {
  switch (action.type) {
    case INC:
      return state+1;
    case DEC:
      return state-1;
    default:
      return state;
  }
});

var MyComponent = React.createClass({

  mixins: [PureRenderMixin],

  componentDidMount: function() {
    this.token = countStore.addListener( this.handleStoreChange );
  },

  componentWillUnmount: function() {
    this.token.remove();
  },

  handleStoreChange: function() {
    this.setState({ count: countStore.getState() })
  },

  handleUpClick: function() {
    /* Call dispatch to submit the action to the stores */
    dispatch(INC)
  },

  handleDownClick: function() {
    /* Call dispatch to submit the action to the stores */
    dispatch(DEC)
  },

  render: function() {
    return (
      <div>
        <p>{this.state.count}</p>
        <button onClick={this.handleUpClick}>+1</button>
        <button onClick={this.handleDownClick}>-1</button>
      </div>
    );
  }

});

module.exports = MyComponent;

MapStore with defensive copies

A simple store that accumulates data on each SET action.

const SET = 'SET';
var {dispatch, createStore } = require('sweetflux');

var store = createStore('MapStore', {}, function(state, action) {
  switch (action.type) {
    case SET:
      // combine both objects into a single new object
      return Object.assign({}, state, action.data)
    default:
      return state;
  }
}, {
  getStates: (state) => state.states,
  getPrograms: (state) => state.programs,
  getSelectedState: (state) => state.selectedState
});

dispatch(SET, { states: ['CA', 'OR', 'WA'] })
// store.getStates() => { states: ['CA', 'OR', 'WA']  }

dispatch(SET, { programs: [{ name: 'A', states: ['CA']}] })
// store.getPrograms() => { programs: [{ name: 'A', states: ['CA']}] }

dispatch(SET, { selectedState: 'CA' })
// store.getSelectedState() => 'CA'

// store.getState() => { states: ['CA', 'OR', 'WA'], { states: ['CA', 'OR', 'WA'], programs: [{ name: 'A', states: ['CA']}] }, selectedState: 'CA' }

MapStore with Immutable data structures

Here is a similar MapStore with Immutable.js.

const {SET, DELETE} = ['SET', 'DELETE'];
var {dispatch, createStore } = require('sweetflux');
var {Map} = require('Immutable');

var store = createStore('MapStore', Map(), function(state, action) {
  t.plan(8)
  switch (action.type) {
    case SET:
      // combine both objects into a single new object
      return state.merge(action.data);
    default:
      return state;
  }
}, {
  get: (state, param) => state.get(param),
  has: (state, param) => state.has(param),
  includes: (state, param) => state.includes(param),
  first: (state) => state.first(),
  last: (state) => state.last(),
  all: (state) => state.toJS(),
});