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

flux-a-duck

v1.0.0

Published

A module that allows you to break your flux actions and store methods into smaller pieces that can be placed in the components that use them.

Downloads

4

Readme

#Flux-A-Duck

A simple wrapper for the flux pattern with an Immutable store and a sprinkle of AJAX.

Flux-A-Duck takes the traditional flux protocol, hides the complicated bits, and gives you only what you need in one convenient API wrapper. This way you can keep the methods used to access your data close to the Components that depend on it... Or you can put them on the moon. The point is that you can break your store functions and actions into bite sized components that go anywhere while retaining the single event loop style of Flux.

#installation

npm install flux-a-duck 

Assuming you're using React, this is how you would set up your basic controller-view:

import React from 'react';
import FAD from 'flux-a-duck';

class app extends React.Component {

    constructor(props){
        super(props);
    }

    componentDidMount() {
        FAD.addChangeListener(this._onChange.bind(this));
    }

    componentWillUnmount() {
        FAD.removeChangeListener(this._onChange.bind(this));
    }

    render() {
        return (
            <SomeKindOfReactComponent {...this.state}/>
        );
    }

    _onChange() {
        this.setState(FAD.getAllJS());
    }
}

app.displayName = 'App';
export default app;

This passes all your store data into the props of your top level react components.

#Top Level Methods

FAD.addChangeListener(callback) Register a callback to trigger when a store change event is emitted. Typically only used in your top most controller-view.

FAD.removeChangeListener(callback) Removes callback from event listening. Typically used when react component is unmounted.

FAD.getAllJS() Returns the entire data store in a plain js object. This is an alias for store.getAllJS() that is provided in the callback arguments.

FAD.action(callback, options) The heavy lifting is done in the FAD.action callback, it's what adds your data into the store. The options object is used for sending an AJAX call and if no call is required, the options object may be omitted.

#Action method without AJAX call

This simplest use case of flux-a-duck. the code below puts a data object into the store.

import FAD from 'flux-a-duck';
import Immutable from 'immutable';

FAD.action((store) => {
    let _store = store.getAll();
    let data = { foo : 'bar' };
    _store = _store.setIn(['objectTest'], Immutable.fromJS(data));
    store.replace(_store);
});

A Quick word about Immutable: The store in Flux-A-Duck is set up with Immutable data collections, so the store argument comes with all the immutable methods for that data type attached. Any top level Immutabe methods (example: Immutable.fromJS()) require that you import Immutable. Using Immutable means you can only replace the store by using store.replace() as no direct mutation of the store is allowed. The duck has spoken.

Argument methods provided in callback(store, data)

store The store object and it's public methods which are as follows:

store.getAll() Returns the entire data store in an immutable object.

store.getAllJS() Returns the entire data store in a plain js object.

store.replace(newStore) Use this to replace your old store with a new mutated store.

data (optional) Typically a javascript object or string that is returned from an AJAX call. Normally you would parse this data in the callback and add it to the store. If no ajax call is required then data is not returned as an argument.

#Action method with AJAX call

Flux-a-duck provides an optional built-in AJAX service to easily make server calls and get the data directly into your action method. Create an AJAX call by adding the options object as an argument to the action method like so:

FAD.action({
	   url        : '/testGet',
       httpMethod : 'POST',
       postData   : 'some kind of post data',
       headers    : {
           'Accept'       : 'text/plain',
           'Content-Type' : 'text/plain'
       }
    }, (store, data) => {
        let _store = store.getAll();
        let newData = Immutable.fromJS(JSON.parse(data));
	    store.replace(_store.setIn(['testData'], newData);
    });

It can be added in front or behind the callback argument. Flux-A-Duck don't care; Flux-A-Duck abides.

#AJAX options object API

url A string containing the URL to which the request is sent.

httpMethod Any valid http method to use with your request. If this is not included it defaults to the GET method.

headers An object of additional header key/value pairs to send along with requests.

postData An object or a string to send to the server in a POST request. The format will largely be dependent on what content-type you specify in the headers.