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

stockage

v0.1.4

Published

Stockage is a small class that help you to create a data store.

Downloads

3

Readme

stockage

Stockage is a small class that help you to create a data store.

The API

action

The method action return a function.

let anActionNameMethod = YourStock.action('anActionName');

getState

The method getState return a copy of the stockage state.

let state = YourStock.getState();

subscribe

The method subscribe return a copy of the stockage state and give you the ability to run a function each time the state change.

YourStock.subscribe(anObject,'aMethodName');

unsubscribe

The method unsubscribe doesn't return anything.

YourStock.unsubscribe(anObject,'aMethodName');

Define a Stockage

This is an example of how do we define a stockage.

This Stockage will be use in a React App.

// AppStock.js
import Stockage from 'stockage';

class AppStock extends Stockage{

  constructor(){
    super({
      state : {
        text : ''
      },
      options : {
        storage : 'AppStock'
      }
    });
  }

  onTextInputChange(state,event){
    state.text = event.target.value;
    return state;
  }

}

let appStock = new AppStock();
export default appStock;

Defining Actions

All actions has a copy of the current state as a first argument.

Change the state

To apply the change you made to the state, you have to return it.

yourAction(state,[..some args you can pass when calling your action]){
  state.foo = "bar";
  return state;
}

Change the state asynchronously

The action's context has a method called async

The async method allow you to change the state after some asynchronous call.

The async method has a *fresh copy as a first argument, to apply the change you have to return the state.

yourAction(state,[..some args you can pass when calling your action]){
  state.foo = "bar";
  setTimeout(()=>{
    this.async((freshCopyOfTheState)=>{
      freshCopyOfTheState.bar = "foo";
      return freshCopyOfTheState;
    });
  },1000);
  return state;
}

Listen to Stockage change

This is how we listen the state's change of a Stockage.

import AppStock from './AppStock';

let anObject = {
  onChange : function(state){
    console.log('State change to :',state);
  }
}

AppStock.subcribe(anObject,'onChange');
//AppStock.unsubcribe(anObject,'onChange');

Link it to a react component

This is how we link the state of a React component with the state of a Stockage.

import React from 'react';
import AppStock from './AppStock';

class App extends React.Component{

  constructor(props){
    super(props);
    this.state = AppStock.subscribe(this,'setState');
  }

  componentWillUnmount(){
    AppStock.unsubscribe(this,'setState');
  }

  render(){
    return (<div>
      <input value={this.state.text}
             onChange={AppStock.action('onTextInputChange')} />
      {this.state.text}
    </div>)
  }

}

let appStock = new AppStock();
export default appStock;