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

nanostore

v1.2.1

Published

A tiny state manager for reactive apps.

Downloads

340

Readme

nanostore 👛

⚠️ Deprecated: this module is no longer beeing maintained. There are a few better options on the market as: MobX, Zustand...

A tiny state manager for reactive apps.
E.g. use with react, preact, yo-yo ...

  • Small in size
  • Works with ie 10 and up
  • Fast and simple

Install

npm i nanostore

Usage


const Nanostore = require('nanostore')

// create new nanostore with initial state
const myStore = new Nanostore({ foo: false })

// subscribe to store changes
myStore.subscribe(newState => {
  console.log('Wow the state changed...')
  console.log(newState)
})

// modify the state (works like Reacts setState() ...)
myStore.set({ foo: 'bar' })

// get state
myStore.get()

// create custom events
myStore.on('fetchSomeApi', (endpoint) => {
  fetch('https://some-cool.api/v2/' + endpoint)
  .then(res => res.json)
  .then(data => myStore.set({ apiData: data }))
})

// call custom events
myStore.emit('fetchSomeApi', 'products')

// actually thats it! 

API

new Store

Creates a new Store object.

Parameters

  • [initialData: Object]

Store.prototype.set

Sets the state (like in Reacts setState ...)
The set Function modifies current state like this: state = { ...state, ...newState}
(Existing props will be overwritten or kept)

Parameters

  • stateModifier: Object

Store.prototype.get

Return current state object

Store.prototype.subscribe

Creates a Subscription that will be rerun on every state change.

Parameters

  • fn: Function

Store.prototype.on

Creates custom events.
Comes in handy if you want to create state actions (like fetching something from an API etc...)

Parameters

  • eventName: String
  • fn: Function

Store.prototype.emit

Calls an custom event.
Normally you would pass that down to your UI Components, so they can call custom events...

Parameters

  • eventName: String
  • [data: Any]

How to use with a UI library (yo-yo in this case)

// main.js

const Nanostore = require('nanostore')
const yo = require('yo-yo')

const App = require('./components/App');
const store = new Nanostore({ count: 0 })

let app = App(store.get(), store.emit)

store.subscribe(state => {
  yo.update(app, App(state, store.emit)) // rerenders app on state change (yo-yos diffing algoryth will do the work)
})

store.on('increment', num => {
  const oldCount = store.get().count
  const newCount = oldCount + num
  store.set({ count: newCount })
})

document.body.appendChild(app)


// components/app.js

const yo = require('yo-yo')

module.exports = function App(state, emit) {
  const handleClick = evt => {
    emit('increment', 1)
  }

  return yo`
    <div>
      <h1>The current count is: ${ state.count }</h1>
      <button onclick=${handleClick}>increment</button>
    </div>
  `
}