1

I am working with a library (ScalaJS and react specifically) where I have an interesting situation that I assume is pretty routine for an experienced reactive-programmer. I have a Component with State and a callback shouldComponentUpdate(State). The basic idea here is that certainly if the callback is triggered but the State has not changed from the last render, returnfalse. Otherwise, perhaps return true if the State change matters.

I am using a library monix but it seems identical to other reactive libraries so I would imagine this is a fairly context-independent question.

I would like to do something like: have some state that reflects the deltas of State since the last render. On each render, clear the buffer. Or, have a renderedState subject that reflects all rendered states as a sequence, a receivedState subject that reflects all received State updates, and a needsUpdate subject that reflects whether the latest receivedState matches the latest renderedState. I am having trouble actually executing these ideas, though. Here is where I am stuck at:

Here is what I've done for other callbacks:

lazy val channel_componentWillUpdate = channel_create[ComponentWillUpdate[Props, State, ResponsiveLayoutContainerBackend, TopNode]]

def componentWillUpdate(cwupd: ComponentWillUpdate[Props, State, ResponsiveLayoutContainerBackend, TopNode]) =
  Callback {
    channel_componentWillUpdate.onNext(cwupd)
  }

So when then componentWillUpdate callback is triggered, the handler fires onNext on the channel (subject).

The shouldComponentUpdate is different though. It returns a value, so it needs to be structured differently. I am having trouble thinking of the right adjustment.

To summarize a bit:

  • react has callbacks at different stages of the view lifecycle, like componentDidMount, componentDidUpdate, etc.
  • I am handling all but one stage the same way - the shape of the callback is State -> Callback<Void> so alls I have to do is use a Subject for each type of lifecycle event and submit its onNext when the callback is triggered.
  • But one type of event has shape either State -> Boolean or State -> Callback<Boolean>.
  • I feel like I should be able to model this with a subject representing the delta between the last state rendered/received.
  • However, I don't know how this fits into the reactive style.
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

0 Answers0