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

almy

v2.0.0

Published

Store for a simple state management

Downloads

39

Readme

🗄️ almy

Version Build Status Coverage Status dependencies Status

The simpliest store for managing the state in your application.
Works in all environments and all browsers.

Why do I need a centralized state management?

Managing the information rendered is difficult, mostly when our apps grow large and the state is scattered across many components and the interactions between them without control.

To solve this, the current state of the art solution is to use a globalized state where we can centralize and have more control over the information we have to render. Almy is a simple library that uses a pub/sub façade along with a centralized state management which makes the the side effects of changing information easy to control and eliminates the risk of getting race conditions in our applications.

Installation

npm install --save almy

Methods

  • dispatch(key: string, value: any)
    Dispatches some event that happened in a key value fashion
  • subscribe(key: string, callback: Function)
    Subscribes to dispatched events. If someone has dispatched an event before, it calls the callback right away
  • state([key:string]):Object
    Returns the state of your application

Usage

Including it as a script tag

<script src="./node_modules/almy/dist/almy.umd.js"></script>
<script>
  almy.almy.dispatch('window_width', 524)
</script>
<script>
  almy.almy.subscribe('window_width', function(newWidth) {
    //Do something with the new width
  })
</script>

Including it as a module

<div id="content"></div>
<script type='module'>
    import {almy} from './node_modules/almy/dist/almy.esm.js'

    almy.subscribe('user->name', (username) => {
        document.getElementById('content').textContent = username;
    });

    almy.dispatch('user', {id: 1, name: 'nick'})
</script>

Using in a node environment

const { almy } = require('almy')
almy.subscribe('cpu_usage', function(newCpuUsage) {
    //Do something with the new cpu usage
})

//In some other place in your code
almy.dispatch('cpu_usage', 9000)

You can also dispatch objects:

const { almy } = require('almy')
almy.subscribe('cpu', function(cpu) {
    console.log(cpu.temperature)
})

almy.dispatch('cpu->temperature', 65)

Or subscribe to objects properties and receive every change:

almy.subscribe('cpu->ips', function(ips) {
    console.log('Intructions per seconds are '+ips)
});
...

almy.dispatch('cpu', {ips: 1})

...

almy.dispatch('cpu', {ips: 5})

// This would ouput:
// "Intructions per seconds are 1"
// "Intructions per seconds are 5"

Limitations

Only one object deepness subscriptions are supported. Example:

almy.dispatch('user', {favorites: {televisions: {'4k': true}}})

// This doesn't work
almy.subscribe('user->favorites->televisions->4k', value => {
    
})

// This does work
almy.subscribe('user->favorites', favorites => {
    if (favorites.televisions['4k']) {
        
    }
})

A flatten state is easier to reason and understand.

Other state management libraries

  • Vuex: https://github.com/vuejs/vuex
  • Redux: https://github.com/reduxjs/redux
  • Flux: https://github.com/facebook/flux

References

Worlds: Controlling the Scope of Side Effects http://www.vpri.org/pdf/tr2011001_final_worlds.pdf