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

interbit-middleware

v0.4.14

Published

Redux middleware that boots an Interbit hypervisor and syncs the blockchain in your Redux state.

Downloads

6

Readme

Interbit Middleware

The interbit-middleware package contains a Redux middleware and reducer. The middleware and reducer work together to boot the Interbit blockchains specified in the DOM, sync their states with the Redux store, and allow you to dispatch to them.

The Interbit middleware reads the index.html file generated when the blockchain node boots to connect to the chains. To learn more about this file [click here](TODO: LINK or DESCRIBE).

Whenever the blockchains update their state, an action is dispatched to the redux store and its state is updated to match.

The blockchain's dispatch functions are wrapped in the redux dispatch wrapped in an action provided with the reducer.

Getting started

You will need to include the interbit reducer as well as the interbit middleware to sync your blockchains and your state. You will also need to have launched an interbit node and included the updated index.html file with connection information for interbit.

Let's assume starting interbit output an index.html from configuration to work with.

Setting Up

Once the node has generated with chain IDs you can simply include the middleware and reducer as follows to use Interbit.

const { createStore, applyMiddleware } = require('redux')
const {
  reducer as interbitReducer,
  middleware as interbitMiddleware
} = require('interbit-middleware')

// including the reducer
const reducers = combineReducers({
  interbit: interbitReducer,
  custom: yourReducer
})

// including the middleware
const store = createStore(reducers, applyMiddleware(interbitMiddleware))

Dispatching to the Blockchain

To dispatch an action to the blockchain you will need to wrap your blockchain action in a 'CHAIN_DISPATCH' action. This redux dispatch function will return the resulting promise from the blockchain.

const { chainDispatch } = require('interbit-middleware')
const { myBlockchainActionCreator } = require('./actionCreators')

// Take your chain alias (from interbit.config.js) and the blockchain action ...
const chainAlias = 'myChain'
const blockchainAction = myBlockchainActionCreator(...args)

// ... and wrap them in interbit-middleware's chainDispatch ...
const action = chainDispatch(chainAlias, blockchainAction)

// ... then dispatch to the redux store!
store.dispatch(action)

Getting the blockchain's state

Assuming we have used the setup above and the reducer was mounted at interbit the redux store would look somthing like this:

const state = store.getState()

console.log(state)
// LOGGED:
// {
//   interbit: {
//     chains: {
//       myChain: {
//         ...myBlockchainState
//       },
//       myOtherChain: {
//         ...myOtherBlockchainState
//       }
//     }
//   },
//   custom: {
//     ...myCustomReduxState
//   }
// }

How It Works

Now that we know how to use it, here is an overview of what it does.

The Middleware

The middleware reads the interbit script tag from index.html and constructs a chainData object that is used to connect your chain to the redux store. It then boots a hypervisor and attaches a cli to each defined chain and returns the chains to the middleware so that dispatches destined for the blockchains can be intercepted. When a chain forms a block and its state is updated, the middleware dispatches a 'interbit-middleware/CHAIN_UPDATED' action to the store to sync the state.

The middleware also intercepts any 'interbit-middleware/CHAIN_DISPATCH' actions, unwrapping them and sending them to the blockchain that was specified by chainAlias. Instead of the usual dispatch return from redux, the blockchain's dispatch promise is returned so you can know when your blockchain action was blocked and stored in the chain. If the blockchain has not loaded yet when you dispatch an action to it, dispatch returns undefined and the action is buffered to be dispatched when the chain is loaded.

The reducer

There is a corresponding reducer that must be used with the middleware that accepts the 'interbit-middleware/CHAIN_UPDATED' actions and updates the redux store with the dispatched state.

The reducer also comes with an action creator for 'interbit-middleware/CHAIN_DISPATCH' actions that wraps a blockchain action and takes the alias of the chain that action is meant for.

index.html

The index file created by interbit start has a script tag with id="interbit" and data that has been appended from the chain configuration file that was used to boot your blockchains. The script tag looks something like this:

<script id="interbit" src="http://todo.cdn.interbit.io/interbit.bundle.js" data-chain-id-my-chain="j08534j9rwj9rwej09532bgv98uijnhtrfg89i04t5rw3i" data-chain-id-my-other-chain="890uij4e3wd7yfdsjklhr3982nri4e0tj4scnmxzq9asd3" />