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

businessman

v3.0.0-alpha.1

Published

Powerful, Secure, Multi-threaded Flux Patterns.

Downloads

35

Readme

Powerful, Secure, Multi-threaded Flux Patterns.

Motivation

If application state management is performed by an application thread, the responsiveness of the application may be impaired. There is also a risk that the application itself will manipulate the state.

The application keep responsiveness by delegating state management to the Worker. It also eliminates the risk of direct manipulation of the state.

API

Create Store

In Businessman, only the states manipulated by dispatching are sent to the main thread. Because the data size between threads affects performance.

In short, Businessman only has one small state in one store.

The store is defined as follows.

import { worker } from 'businessman'

worker.addStore( {
	type: 'counter',
	state: 0,
	actions: {
		increment( commit, num = 1 ) {
			commit( 'increment', num )
		}
	},
	mutations: {
		increment( state, num ) {
			return state += num
		}
	},
	getters: {
		absolute(state) {
			return Math.abs( state )
		}
	}
} )

Type

Type is a string that is the identity of the store. This needs to be unique.

type: 'counter'

State

Save the state of the store. Any type can be used for the state.

state: 0

Actions

Execute the mutation. Asynchronous processing can be placed on the action.

Pass the mutation name to the function of the first argument of the action. The payload is provided from the second argument.

increment( commit, num = 1 ) {
	commit( 'increment', num )
}

If the third argument of commit is false, it does not provide state.

In this case, the state passed to the subscriber is null.

increment( commit, num = 1 ) {
	commit( 'increment', num, false )
}

Mutations

Change the state. After that, the new state is automatically notified to the main thread.

It will receive the current state and payload and return the new value.

The state is changed and the main thread is notified.

increment( state, num ) {
	return state += num
}

ATTENTION

  • Do not place asynchronous processing on mutations.

Getters

Getters gets state by calculation.

absolute(state) {
	return Math.abs( state )
}

An option is provided for the second argument, and another Getter is provided for the third argument.

absolute( state, options, getters ) {
	// state: Current state
	// options: Some option
	// getters: All Getters in this store
}

Default getters

A default getter default that returns self-state is provided.

You do not need to add getters if you just want to return your self-state.

Create Manager

Mutation and action belong to one store.

If you want to dispatch to multiple stores at the same time, you can use the manager.

It can be registered using worker.addManager().

import { worker } from 'businessman'

worker.addManager( {
	type: 'countUpMessage',
	handler( stores, num = 1 ) {
		stores.counter.dispatch( 'increment', num )
		stores.message.dispatch( 'update', `${num} has been added to the counter` )
	}
} )

Call operate() with manager type and payload specified.

import { operate } from 'businessman'

operate( 'countUpMessage', 1 )

Start worker

Call worker.start() to start a worker.

worker.start()

When added to the source of the Create Store as shown earlier, it becomes as follows.

import { worker } from 'businessman'

worker.addStore( {
	type: 'counter',
	state: 0,
	actions: {
		increment( commit, num = 1 ) {
			commit( 'increment', num )
		}
	},
	mutations: {
		increment( state, num ) {
			return state += num
		}
	},
	getters: {
		absolute(state) {
			return Math.abs( state )
		}
	}
} )

worker.start()

Install worker

Install workers and start state management.

import { install } from 'businessman'

install( '/path/to/worker.js' )

ATTENTION

  • Workers need to be converted for browsers with arbitrary compiler.

On initialization

By subscribing 'INIT' you can receive notification of installation.

Receives an array of store and manager types as payload.

import { subscribe } from 'businessman'

subscribe( 'INIT', data => {
	console.log( data ) // { stores: [...], managers: [...] }
} )

Dispatch and Subscribe

import { dispatch, subscribe } from 'businessman'

dispatch( 'counter', 'increment', 1 )

subscribe( 'counter', state => {
	console.log( state ) // 1
} )

Unsubscribe

You can stop / delete subscribe.

import { unsubscribe } from 'businessman'

unsubscribe( 'counter' ) // Delete all listeners subscribing to the store
unsubscribe( 'counter', listener ) // Delete one listener

getState

In Businessman getState() is also executed asynchronously.

import { getState } from 'businessman'

// Get state with `default` getter
getState( 'counter' )
.then( state => {
	console.log( state )
} )

// You can also specify Getter.
getState( 'counter', 'absolute' )
.then( state => {
	console.log( state )
} )

getAllState

getState() can only get the state of one store.

To get the state of all stores, use getAllState().

import { getAllState } from 'businessman'

getAllState()
.then( state => {
	console.log( state )
} )

How to contribute

You can feel free to join anytime!

GitHub Issues

GitHub Contributing Guide