Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

businessman

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

businessman

Multi-thread State Management by the Worker API

  • 1.4.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Multi-thread State Management by Worker API.

Businessman

API

Create Store

Overview

import { worker } from 'businessman'

worker.registerStore( {
    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
}

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.registerManager().

import { worker } from 'businessman'

worker.registerManager( {
    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.registerStore( {
    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.

Dispatch and Subscribe

import { dispatch, subscribe } from 'businessman'

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

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

Dispatch / Subscribe is also available in Store style.

counter.dispatch( 'increment', 1 )

counter.subscribe( ( state ) => {
    console.log( state )
} )

The store style is available after the store in the worker has been created for the client. It can be obtained by subscribing CREATE_CLIENT_STORE.

let counter

subscribe( 'CREATE_CLIENT_STORE', ( stores ) => {
    console.log( stores ) // { counter: { dispatch: function () {...}, subscribe: function () {...}, unsubscribe: function () {...}, getState: function () {...} } }
    counter = stores.counter
} )

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

For store style ...

counter.unsubscribe()
counter.unsubscribe( listener )

getState

In Businessman getState() is also executed asynchronously.

counter.getState()
.then( ( state ) => {
    console.log( state )
} )

You can also specify Getter.

counter.getState( 'absolute' )
.then( ( state ) => {
    console.log( state )
} )

How to contribute

See Board on GitLab.

You can feel free to join anytime!

GitLab Project

FAQs

Package last updated on 09 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc