qri-io / 2017-frontend

qri electron & web frontend
https://qri.io
MIT License
23 stars 7 forks source link

feat(middleware/cad): groundwork for content addressed data middleware #373

Closed ramfox closed 6 years ago

ramfox commented 6 years ago

We want to be able to get and display content from the distributed web!

Right now, our current middleware requires that all responses be in json. In order to get binary data we need to create new middleware and a new section in the state tree for this data to live.

The middleware, called caf (content addressed files), fetches a request from the hash given. This hash is assumed to be on ipfs or ipns. The response body is read as an ArrayBuffer. This ArrayBuffer is passed to the cafs (content addressed file system) reducers.

Actions:

Each action that uses the caf middleware created should have this signature:

export function loadTransform (hash) {
  return (dispatch) => {
    return dispatch({
      [GET_CAF]: {
        types: [CAFS_TRANSFORM_REQUEST, CAFS_TRANSFORM_SUCCESS, CAFS_TRANSFORM_FAILURE],
        hash
      }
    })
  }
}

Note: three types, just like the API middleware & you always need a hash

Reducer:

So even though we will have different action types for each action, we end up doing the same thing to all the content that comes through the reducer. If it's a request type, add the hash to the cafs object and set it to loading. If it's a success type, set loading to false, and the data to the content received. If it's failure type, set loading to false and set the error to the error message that was passed along.

We get around having distinct actions for each kind of request, by mapping each action to a more general CAFS_REQUEST, CAFS_SUCCESS, and CAFS_FAILURE.

Selectors:

I want to propose that the selectors be where we deal with how to display the ArrayBuffer. For example, we could have selectTransformString, which returns the ArrayBuffer as a string to display, or selectTransformFile, which returns as a file or blob. Right now, I will just create the former, since that's what we need, but I imagine that we can use the selector to return the values we need

b5 commented 6 years ago

oh yeah, also gonna need a rebase