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

kwik-e-mart

v0.5.0

Published

kwik-e-mart =============

Downloads

4

Readme

kwik-e-mart

Another flux implementation. Based on the excellent Alt

Usage

actions

Actions trigger sets in stores and are your main means of broadcasting an intended state change to one or more stores. They are akin to triggering a global event.

Creating actions

Use the createActions() api to create a set of actions. All action method bodies have access to this.dispatch() which will send off the current action to the dispatcher.

var kwikemart = require('kwik-e-mart')

var userActions = kwikemart.createActions({

  //the simplest action is just to dispatch out the passed in infomation
  updateUser(user){
    this.dispatch(user)
  }
})

The example above is extremely common and straightforward, all it does is pass the user object on to any stores listening to the updateUser action. You can streamline this process by using the helper api generateActions(arrayOfActions) which will create an object of simple 'pass through' actions like the one above.

var kwikemart = require('kwik-e-mart')


var userActions = kwikemart.createActions(
    kwikemart.generateActions(['updateUser'])) 

Since generateActions just returns an object, you can combine these actions creation methods with Object.assign or the "spread" operator if you are using babel, or some other modern javascript transpiler.

var kwikemart = require('kwik-e-mart')

// creates an `updateUser` and a `saveUser` action
var userActions = kwikemart.createActions({

  ...kwikemart.generateActions(['updateUser']),
  
  saveUser(user){
    saveToServer(user).then(this.dispatch)
  }
}) 

action context

You can also reference other actions inside an action to compose and combine actions by using this.actions.

var kwikemart = require('kwik-e-mart')

var userActions = kwikemart.createActions({

  ...kwikemart.generateActions(['updateUser']),

  createUser(name){
    var user = { id: guid() }
    
    this.dispatch(user)

    user.name = name

    this.actions.updateUser(user)
  }
})

action asynchrony

Another common pattern when persisting changes back to a server is to dispatch the current object immediately so stores can optimistically update and then fire success and failure variants of hte action when the api request finishes.

var kwikemart = require('kwik-e-mart')

var userActions = kwikemart.createActions({

  ...kwikemart.generateActions([ 
    'updateUserSuccess', 
    'updateUserFailure'
  ]),
  
  updateUser(updatedUser){
    this.dispatch(updatedUser)
    saveToServer(updatedUser)
      .then(savedUser => this.actions.updateUserSuccess(savedUser))
      .catch(error =>  this.actions.updateUserFailure(error))
  }
}) 

kwik-e-mart makes this simplier by automatically creating success and failure actions for each action you create, they are available inside an action with this.success and this.failure or from outside an action like userActions.updateUser.success (more on that in the store section)

var kwikemart = require('kwik-e-mart')

var userActions = kwikemart.createActions({
  
  updateUser(updatedUser){
    this.dispatch(updatedUser)
    saveToServer(updatedUser)
      .then(this.success)
      .catch(this.failure)
  }
}) 

Stores

Stores are the locale for application state in a flux app

Creating stores

You can create stores as a plain old object or as a 'class' using es6 class syntax or any other form of prototypal inheritance patterns. With kwik-e-mart you don't instansiate stores yourself but pass them to createStore and get back an instance.

You configure stores to listen or 'bind' to specific actions. Binding a store to an action allows it to respond when that action is fired by the dispatcher.

To bind an action just pass it to the bindAction api along with the string name of method that will handle it. From an action handler you can update the stores state in much the same way you would a React Component's state. When a store's state is updated with setState it will emit a change and any listening view components can refetch store info to render with the updated data.

var kwikemart = require('kwik-e-mart')
var userActions = require('./userActions')

class UserStore {
  constructor(){
    this.bindAction(userActions.createUser, 'onUpdateUser')

    this.state = {
      users: []
    }
  }
  // -- getters
  getUsers(){
    return this.state.users
  }

  getUser(id){
    return _.find(this.state.users, { id: id })
  }
  //----------------------

  onUpdateUser(newUser){
    this.setState({
      users: this.state.users.concat(newUser)
    })
  }
}

var userStore = kwikemart.createStore(UserStore)

userActions.createUser('john') // Now the store will create the user

userStore.getUsers() // returns all users
userStore.getUser(1) // returns a single user

You can also bind all actions to a store by using bindActions which is a convenient alternative to using multiple bindAction calls.

var kwikemart = require('kwik-e-mart')
var userActions = require('./userActions')

class UserStore {
  constructor(){
    // bind all userActions to a method of the same name
    this.bindActions(userActions)
  }

  //-- handlers
  updateUser(newUser){
    // update code...
  }

  updateUserSuccess(newUser){
    // update code...
  }
  updateUserFailure(newUser){
    // update code...
  }
  createUser(newUser){
    // create code...
  }
}

var userStore = kwikemart.createStore(UserStore)

userActions.createUser('john') // Now the store will create the user

userStore.getUsers() // returns all users
userStore.getUser(1) // returns a single user

For cases where you want to bind multiple different actions to a single store handler there is an inverted api for doing that: bindListeners()

  class UserStore {
    constructor(){
      this.bindListeners({
        onSuccess: [ 
          userActions.createUser.success, 
          userActions.updateUser.success
        ],
        onFailure: [ 
          userActions.createUser.failure, 
          userActions.updateUser.failure
        ]   
      })
    }
    onSuccess(newUser){
      // success code...
    }
    onFailure(newUser){
      // failure code...
    }
  }

All stores are given as dispatchToken which they can pass to store.waitFor() in order to handle inter store dependencies.