1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Compress the Immer Patch History by Replaying Patches on a setInterval

    Michel WeststrateMichel Weststrate
    reactReact
    ^16.8.6
    javascriptJavaScript

    Many cool things can be done with immutable data and patches. In this lesson we will highlight one fun trick: Compressing patches by replaying and re-recording them so that we end up with the least amount of patches to end up in the correct final state.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:00 Another weak point of our current real-time synchronization approach is that the history we keep on server is ever and ever growing as running longer and longer. Even when we toggle the same item multiple times, this will generate multiple sets of patches which only to be sent to newly-connecting clients.

    0:20 One way to do this better is to compress the patches. There are a few ways in which we can do this. One is we could now and then take the current states of global affairs and then just record the patches from that point onwards.

    0:36 Another trick I want to share in this lesson is that we can compress the patches themselves. The idea of this approach is that at a certain form, we reapply all the patches we have so far to the original state we started with, and then we check what new patches this reapplication would result in. Because patches are computers' after effect, which results in a smaller set of patches.

    1:04 We use producer patches again. We apply the patches we've seen so far, and then we return the new set of patches that is inaudible . If we try that and we press reserve and unreserve a few times on the same item, and so then we see that this is also multiple patches which, once the compression kicks in, is compressed to only a single patch.