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

hc-redux-middleware

v1.0.3

Published

a few methods of redux middleware for holochain UI apps

Downloads

14

Readme

hc-redux-middleware

a few methods of redux middleware for holochain UI apps

To install: npm install --save hc-redux-middleware

To use:

import { hcMiddleware, requestSendingMiddleware } from 'hc-redux-middleware'
... 
const middleware = compact([
  hcMiddleware,
  requestSendingMiddleware,
  promiseMiddleware
])
let store = createStore(reducers, undefined, compose(applyMiddleware(...middleware)))

How it works

For a given backend function that you want to call, such as getFollow, you first define a constant within src/actions/index.js.

export const GET_FOLLOW = 'getFollow'

This is exported so that we can later, and easily, access it within our Redux reducers.

You then define a function which acts as a Redux "action creator", and give it special properties so that the custom Holochain Redux middleware will send the right HTTP request.

export function getFollow(userHash, type, then) {
  return {
    type: GET_FOLLOW,
    meta: {
      isHc: true,
      namespace: 'clutter',
      data: {
        from: userHash,
        type: type
      },
      then
    }
  }
}
  • Make the function accept any data that you need to send to the API.
  • Make the last parameter a variable called then which represents a callback function, which will be passed the final value of your API request as well. This uses the promise chains pattern.
  • Make the type equal to the constant you defined, like GET_FOLLOW.
  • Under meta, you must set isHc to true. This tells the middleware this is a HC request.
  • Under meta you must set a namespace, which must be the name of your running Holochain app, such as clutter. It will be used in the URL the request is sent to.
  • Under meta, set data to whatever your input parameters for the function were, such as userHash and type, in the format that the HC app defines. It can be a simple string, like data: "test", or an object like in the example. The middleware will stringify the data to send it to ther server if it's an object.
  • Under meta, set the key then equal to the value of the then parameter that was passed. It doesn't matter if then is undefined. This is like the callback that you can call when the async request completes.

For a given component, like App where you want to enable a server request, create a container component, which we use Redux to connect it.

import { connect } from 'react-redux'
import App from './App'
import {
  ...
  getFollow
  ...
} from './actions'
...
const mapDispatchToProps = (dispatch) => {
  return {
    ...
    getFollow: (userHash, type, then) => {
      dispatch(getFollow(userHash, type, then))
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App)

What this does is define a prop called getFollow for App, which will actually dispatch the getFollow request. Now within App.js I can just call

  this.props.getFollow(this.props.me, "following")

In this case, I don't provide a callback function.

Now the middleware kicks in. What it does under the hood:

  • It checks whether your action is an HC action: if (!(meta && meta.isHc))
  • Uses axios to send a network request with your data
const stringified = typeof data === "object" ? JSON.stringify(data) : data
return axios.post(`/fn/${namespace}/${fnName}`, stringified)
...
  • fnName is based on your action type constant that you defined so make sure that that matches the HC app function name

Now, since this performs an async network request, your action will not fire right away! But if you want something to HAPPEN right away, to offer the user feedback on a button click for example, there is another piece of middleware that helps with this. The requestSendingMiddleware. It will dispatch an action that looks something like getFollowSent. You can use this to make some change to state to show a pending status. It will have the same values in the meta of that action as the original action.

Our middleware is combined with redux-promises to complete the final step, dispatching the original action once the request completes, with the server response set under payload on the action.

You can see how this is set up in src/index.js

const middleware = compact([
  hcMiddleware,
  requestSendingMiddleware,
  promiseMiddleware
])
let store = createStore(clutterApp, undefined, compose(applyMiddleware(...middleware)))

Now we can access our API values in our reducers, and use them to modify state. e.g.

  // meta.data is the userHash, action.payload is the handle we retrieved for them
  case A.GET_HANDLE:
    return Object.assign(
      {},
      state,
      {
        handles: Object.assign({}, state.handles, { [meta.data]: action.payload })
      }
    )

Once we have set these values in state, we can access them using mapStateToProps in container components that we connect. e.g.

const mapStateToProps = state => {
  return {
    handles: state.handles
  }
}

Now we can access data from our HC backend in our component, in its props.

this.props.handles[post.author]

This is just a rough overview, just hopefully gives enough of an idea to get started. You will definitely need basic familiarity with redux to use this. They have excellent documentation, check it out. https://redux.js.org