lukebarnard1 / matrix-redux-wrap

A library that exposes matrix-js-sdk state via Redux
Apache License 2.0
3 stars 1 forks source link

Design architecture, write specification, make a plan #1

Closed lukebarnard1 closed 6 years ago

lukebarnard1 commented 6 years ago

Architecture

To wrap the js-sdk, each event emitted by it will be handled by the store as an action. Each action will update the store state accordingly with a set of reducers.

Many actions will reduce state in a similar fashion; updating the stored values for matrix state. All state will be copied from the js-sdk to prevent bugs from state changing without an action having been fired. This has many advantages but one major disadvantage is memory footprint. The plan is to replace this wrapper with a proper matrix-redux-store that has its own logic for handling the /sync response, effectively replacing stateful parts of the js-sdk but leaving HTTP API wrappers intact.

Apps wanting multi-account support for their app should simply create multiple stores; the accounts should be effectively sandboxed.

Glossary

Store structure

Store key Type Description
mrw.wrapped_api.login.credentials Object The credentials of the User.
mrw.wrapped_api.login.credentials.userId String The user ID of the User.
mrw.wrapped_api.login.credentials.accessToken String The access token of the User.
mrw.wrapped_state.sync Object The sync data for the current session.
mrw.wrapped_state.sync.state String The sync state of the matrix client.
mrw.wrapped_state.rooms Array Rooms that the User has interacted with before.
lukebarnard1 commented 6 years ago

I've realised that redux apps will want to store non-matrix state in the same store under a different state key. This is still compatible with all of the above, but we leave the store creation to the app.

So this library will only expose:

lukebarnard1 commented 6 years ago

To be specified:

lukebarnard1 commented 6 years ago

Action Creators

matrix-js-sdk events

The events will be as actions fairly simply:

{
    "type": "mrw.wrapped_event",
    "emittedType": emittedType,
    "emittedArgs": emittedArgs,
}

where:

The advantages of this are:

and the disadvantages are:

matrix-js-sdk Client-Server API

The API is exposed in a series of many Promise-returning functions that do some asynchronous work. These can easily be encapsulated in Asynchronous Action Creators, which are essentially actions that take the form:

    (dispatch) => { ... }

and when dispatched on the Redux store (which has the redux-thunk middleware applied), the function will be called with a reference to the dispatch function of the store, allowing it to be called asynchronously.

The three async actions that are:

These will be encapsulated in actions that resemble:

{
    "type": "mrw.wrapped_api.pending",
    "method": "login",
    "pendingState": pendingState,
    "id": "48ghdkh9"
}

where:

{
    "type": "mrw.wrapped_api.success",
    "method": "login",
    "result": { ... },
    "id": "48ghdkh9"
}

where:

{
    "type": "mrw.wrapped_api.failure",
    "method": "login",
    "error": new Error(':slight_frown:'),
    "id": "48ghdkh9"
}

where:

lukebarnard1 commented 6 years ago

Closing due to progress being made, I shall make a big checklist of things that need to be done next 🙂