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

barren

v0.0.7

Published

A lightweight state management for React

Downloads

10

Readme

barren

A lightweight state management for React

install

npm i barren

usage

On the main/entry React component

import StoreProvider from 'barren/lib/provider'

import createStore from 'barren/lib/createStore'

// should be imported elsewhere
const ACTION_STORE = {
    counter: function(param) {
        if(param === 'add') {
            this.STORE['counter'] += 1
        }
        if(param === 'reduce') {
            this.STORE['counter'] -= 1
        }
    }
}

// should be imported elsewhere
const STORE = {
    counter: 0
};

const barren = createStore(ACTION_STORE, STORE)

ReactDOM.render(
    <StoreProvider {...barren}>
      <App />
    </StoreProvider>,
document.getElementById('root'))

On children component

import store from 'barren'

function App(props) {
  const { store } = props 

  const count = store.fetch('counter')

  function add(){
    store.dispatch('counter', 'add')
  }

  function reduce(){
    store.dispatch('counter', 'reduce')
  }

  return (
    <div style={{display: 'flex'}}>
      <button onClick={reduce}> - </button> {count} <button onClick={add}> + </button>
    </div>
  );
}

export default store(App)

async

Usage sample using axios

import { request, setConfig } from 'barren/api/axios-api'

setConfig({ baseURL: 'https://api.github.com/search'})

const ACTION_STORE = {
  users: function(params = {}){
    const {
      users = 'tom',
      repos = 1,
      followers = 1
    } = params
    const query = `users?q=${users}+repos:>${repos}+followers:>${followers}`
    return request(query)
  }
}

const STORE = {
  // to process output from ACTION_STORE use function
  users: function(result = {}){
    const { 
      total_count = 0, 
      items = []
    } = result
    return {
      usersCount: total_count,
      userList: items
    }
  }
}

On children component

import store from 'barren'

function App(props) {
  const { store } = props 

  const userCount = store.fetch('users', 'usersCount')
  const userList = store.fetch('users', 'userList')

  const [input, setInput] = useState('')

  const onChange = e => {
    setInput(e.target.value)
  }

  function searchUsers(){
    store.dispatch('users', { users: input })
  }

  return (
    <div>
      <input onChange={onChange}/>
      <button onClick={searchUsers}>search github users</button>
      <p>users count: {userCount}</p>
      <pre>
        {userList && userList.length && JSON.stringify(userList, false, 2)}
      </pre>
    </div>
  )
}

export default store(App)

api

store.dispatch(action, params, options, caller)

dispatch an action from ACTION_STORE

  • action: (object) A valid string identifier, should be unique.
  • params: (any types) Parameter to pass to the dispatcher event reference.
  • options: (object, optional) Options parameter for the dispatcher.
  • caller: (function, optional) Callback function to run after the dispatcher event finish.

options parameter

  • forceRewrite (boolean) Force rewriting the store cache on request.
  • shallowUpdate (boolean) Once request is made do not cache to the store.
  • timeout (integer) Custom timeout for for dispatcher request.
  • event (boolean) Dispatch as event to listeners.
  • inflightRequestCancellation (boolean) Assign request cancellation handler to the dispatcher.
store.fetch(action, param)

fetch result from STORE

  • action: (string) A valid string identifier, should be unique.
  • param: (string, optional) Property name of the result if is a valid object.
store.clearCache(action)

flag an action to process next request

  • action: (string) A valid string identifier, should be unique.
store.clearAllCache()

flag all actions to process all incoming request

store.overrideStore (action, value)

override the store cache value

  • action: (string) A valid string identifier, should be unique.
  • value: (any types) New value for the reassignment.
forceEvent (event, params)

forcefully emit an event

  • event: (string) A valid string identifier, should be unique.
  • params: (any types) Value to pass to the event.