lukebarnard1 / matrix-redux-wrap

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

Could we add more explaination about why we wrap the Redux for Matrix API? #10

Open huan opened 5 years ago

huan commented 5 years ago

Hi, I'm playing with Matrix recently, and I'm also a fan of Redux.

When I saw your project I'm heavy interested, but after I read the README and examples I could not get the point of why we need this Redux Wrapper.

Could you add more explanation to the README so that we can understand quicker?

Loewe1000 commented 4 years ago

Hi luke, huan is right.

I'm very interested in your project, because I want to write a react native matrix client for our school chat. This would be very helpful during corona homeschooling.

But I've absolutely no idea, how to use your code with redux at all, because in none of your examples redux is used.

lukebarnard1 commented 4 years ago

Hi all,

I clearly haven't done a great job explaining this library, so I'll try and do that here and then update the documentation.

The two questions seem to be:

why?

Because I've used matrix-js-sdk a lot and have spent many hours debugging issues that were caused by it and I found it very difficult to read the code and reason with it. A lot of the library follows a pattern which looks a bit like OOP, with some event emitting, some async side-effect functions and getter functions. Ultimately in the process of debugging a lot of issues, I noticed that the code was difficult to comprehend.

Every time I had to debug the library, I first had to understand the specific feature that I was trying to fix. Bearing in mind, I was working on matrix-react-sdk at the time, it's possible that part of the issue lies there. matrix-react-sdk is very much entangled with matrix-js-sdk - the React SDK is more of an extension than a layer. I remember spending a lot of time logging things in both libraries in order to figure out an issue. Eventually I got to know the libraries a bit more and which parts were complicated, and that helped to an extent, but still it wasn't easy to know generally how things worked.

It's entirely practical to use matrix-js-sdk as it is today without matrix-redux-wrapper. If I could start from scratch, I would probably wrap matrix-js-sdk in a React hook to isolate the side-effects and abstract the events from the React app. I would probably make the hook accept client options and have the hook expose an immutable copy of the matrix client state, organised in a simple way (which could be based heavily on how state is organised in the JS SDK. The side-effects could be exposed as functions from the same hook, allowing for matrix event sending, auth flows, etc.

how?

I wouldn't actually recommend using this wrapper unless for some reason Redux is a hard constraint or you were just curious to see it working.

I would suggest starting with example.js and editing it so that doLoginAndSync is given put from redux-sagas instead of the suggested dispatch (which in example.js, acts as the redux store dispatch function). I think this essentially requires making doLoginAndSync a redux saga, which I will make an example for later.

I think an example that allows someone to paste in a matrix client into an existing redux app would be super useful - I'm not sure why I didn't do this before :)