New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fluxury

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fluxury

High level store builder for Flux architecture

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-80.36%
Maintainers
1
Weekly downloads
 
Created
Source

fluxury

Circle CI

Quick start:

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

The Gist

This library forks Facbook's Flux implementation.

This library adds functions:

  • dispatch(type, data) or dispatch(action) returns Promise
  • createStore(reducer, selectors) or createStore(configObjct, selectors)
  • composeStore(...spec)

For react bindings please see react-fluxury.

API Reference

dispatch( type, data ) or dispatch( action )

Dispatch an action to all stores.

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

// dispatch an action with a string
dispatch('requestSettings')  // => { type: 'loadSettings', data: undefined }
// or with data
dispatch('loadSettings', { a: 1, b: 2 }) // => { type: 'loadSettings', data: { a: 1, b: 2 } }
// or with a custom object
dispatch({ type: 'move', mode: 'over rails' })
```

createStore(reducerOrConfig, selectors)

Define stores which respond to actions and manage state.

```js
const inc = 'inc'
import {createStore} from 'fluxury';

// a simple counting store
var countStore = createStore((state=0, action, waitFor) => {
  switch (action.type)
  case inc:
    return state + 1;
  default:
    return state;
})
```

If you do not prefer the `switch case` then may use a config object.

The config object uses the life cycle method `getInitialState` to configure
the initial value stored. This should look familiar to React programmers.

```js
const inc = 'inc'
import {createStore} from 'fluxury';

export default createStore({
  getInitialState: () => 0
  increment: (state) => state + 1,
  incrementN: (state, data, waitFor) => state + data,
  decrement: (state) => state - 1
})
```

`waitFor` is used to control the order which store process actions.

composeStore(...spec)

Compose one or more stores into composite store.

The spec may either be an array of stores or an Object with stores.

composeStores(MessageStore, CountStore)
composeStores({ count: CountStore, message: MessageStore })

Store Properties and Methods

namecomment
nameThe name supplied when creating the store
dispatchAnother method to access the dispatch function
dispatchTokenA number used to identity each store
subscribeRegister listener and return a function to remove listener
getStateA function that returns the current state
setStateReplace the current store state
reduceRun the reduce directly

Put it all together

var React = require('react');
var {createStore} = require('fluxury');

var messageStore = createStore({
  getInitialState: () => [],
  addMessage: (state) => state + 1,
});

var messageCountStore = createStore({
  getInitialState: () => 0,
  addMessage: (state, data, waitFor) => {
    waitFor([messageStore.dispatchToken])
    return state + 1
  }
});

var MyComponent = React.createClass({

  componentDidMount: function() {
    this.unsubscribe = messageCountStore.subscribe( this.handleStoreChange );
  },

  componentWillUnmount: function() {
    this.unsubscribe();
  },

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

  handleAdd: function() {
    /* Call dispatch to submit the action to the stores */
    messageStore.addMessage(this.refs.message_text.value)
  },


  render: function() {
    return (
      <div>
        <p>{this.state.count}</p>
        <input ref="message_text">
        <button onClick={this.handleAdd}>Add</button>
      </div>
    );
  }

});

module.exports = MyComponent;

Keywords

FAQs

Package last updated on 10 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc