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

monkberry-flux

v0.0.1

Published

A Reactive library for Monkberry applications or any Javascript application, inspired by Flux

Downloads

3

Readme

monkberry-flux

npm package

A Reactive library for Monkberry applications or any Javascript application, inspired by Flux.

Why monkberry-flux?

monkberry-flux is a reactive library that provides for your application multiple stores, wich each store manage your state.

Install

  • Npm: npm install monkberry-flux

Reasons for use

  • Tiny size: ~1kb
  • Best Performance
  • Reactive Stores
  • Simple and minimalistic API
  • Unidirectional data flow

Data Flow

In monkberry-flux data flow is unidirectional, as it should be in Flux:

  • The store dispatch her actions
  • Actions change the state.
  • When state changes you can trigger a handler

Principles:

  • Application state is held in the store, as a single object.
  • The only way to mutate the state is by dispatching store actions.
  • Actions must be synchronous, and the only side effects they produce should be mutating the state.

Stores:

A Store is basically a container that holds your application state. There are two things that makes a monkberry-flux store different:

  • A Store is reactive. Every time the state changes, you can trigger a handler.

  • You cannot directly change the store's state. The only way to change a store's state is by explicitly dispatching store actions.

Creating a Store is pretty straightforward - just provide an name, state and actions:

import monkberryFlux from "monkberry-flux";

const TodoStore = monkberryFlux.createStore({
  name: 'Todo',
  state:  {
    todos: []
  },
  actions: {
    ADD_TASK ( state, task ) {
      state.todos.push(task);
    },
    REMOVE_TASK ( state, index ) {
      state.todos.splice(index, 1);
    }
  }
});

// every time the state changes, this function will be triggered 
TodoStore.observe( templates => {
  // update each template that are subscribed on the store with
  // the new store state
  templates.forEach( template => template.update(TodoStore.getState());
})

module.exports = TodoStore;

Template:

On your template, you can register methods and subscribe your template for when the store state change, your template can update with the new state.

// templates/main/main.js
import Template from './main.monk';
import TodoStore from 'stores/todo';

export default class extends Template {
  constructor(){
    super();
    super.update(TodoStore.getState());
    TodoStore.subscribe(this);
    this.on('click', '#change', this.changeName)
  }

  changeName(){
    TodoStore.dispatch('name', 'CHANGE_NAME', 'Luis Vincius');
  }
}

State

Application state is held in the store, as a single object. monkberry-flux uses a single state tree - that is, this single object contains all your Store level state and serves as the "single source of truth".

Store Actions

Actions are just functions that call the store actions. All actions receive a state as first argument.

Creating an action inside your Store:

actions: {
  increment ( state, n ){
    state.count += n
}

Calling an action on your component

import { TodoStore } from './store/todo';

TodoStore.dispatch('todos', 'ADD_TASK', 10);

An action receives the state property that you want to change as first argument, the *action event name as the second argument, anything after these are passed as arguments to the action callback.

Get

To get the store state value, use store.getState() or store.getState( name ), in your Components or Stores.

Application Structure

Just suggesting.

├──index.html
├──templates
|   ├──component.monk
|   ├──component.js
├──stores
|   ├──todo.js
|   ├──api.js

API Reference

  • Create a Store:

    • monkberryFlux.createStore({ name, state, actions }): Create a single store with the name of Store, State and Actions.
  • Store Actions:

    • store.dispatch(stateName, action [,...arguments]): Call a store action.
  • Add you store handler to be called when the state changes:

    • store.observe(listeners, stateName, stateValue): Register a handler that will be triggered when any state change in you store.
  • Observing the store state changes in your Component or other Store:

    • yourStore.subscribe( listener ): Add the listener for watch the Store state changes.
    • yourStore.unsubscribe( listener ): Remove the listener for unwatch the Store state changes.
    • yourStore.getState(state?): Gets a value of the store state that you passed as argument or all state if argument are present.

License

MIT License.