State Management with a Single Line of Code

Ankit Singh
4 min readNov 2, 2020

If you’re like me and feel that there has to be an easier way of state-management, then you’d like what ActiveJS can do for you.

I feel like I’m selling snake-oil, but I spent the last 10 months trying to make state-management as intuitive and easy as possible because I couldn’t stand the state-management in the state it is right now.

For efficient state-management, we need a few things

  • data structures that are type-safe
  • data structures that can emit events on mutation
  • data structures that can guarantee immutability
  • data structures that can be persisted through sessions

The title promised all this in one line of code, so here it is:

(Okay 4 lines, but I formatted it so you don’t have to scroll :)

JavaScript doesn’t have anything like that, that’s why ActiveJS came into existence, and with it came reactive data structures called Units, one of them is DictUnit, that stores and ensures a dictionary object value at all times.

You might have already got a feeling from the configuration options we passed to the DictUnit and guessed what it’s all about, but to elaborate DictUnit is:

Let’s see what that means in the language we all understand, the code:

Observable

DictUnit extends RxJS Observable class, so you can subscribe to it and apply all the RxJS operators on it just as you would on an Observable.

Reactive

When you update the value of a DictUnit it emits it to all the observers so that they get access to the latest value.

Type-Safe

A DictUnit ensures that at all times the value is always a dictionary object, it’ll ignore any invalid value dispatch.

There are 5 other Units just like DictUnit in ActiveJS, ListUnit to store array, NumUnit to store number, StringUnit to store string, BoolUnit to store boolean, and GenericUnit to store anything.

Immutable

The immutable flag makes sure that the DictUnit doesn’t let the value get mutated in any way. Let’s try to mutate it anyway.

Persistent

The persistent flag makes the DictUnit persistent, such that whenever its value is updated, it saves that value to LocalStorage, so if we reinitialize a DictUnit with the same id and persistent: true flag, the DictUnit will restore its value from LocalStorage.

Cache-Enabled

What if I told you that we can go back to all the previous values we just updated in previous examples, and then come back to the current value, yup Time-Travel is possible. All you need to provide is how many steps you want to be able to go back using the cacheSize option, by default it keeps 2 values and supports up to Infinity.

That’s it, folks, all done.

There are still a few things that we haven’t covered that DictUnit can do, and we also haven’t covered things like managing asynchronous API calls. But maybe that’s a topic for the next article.

In the meantime, stay safe, try to have fun, and head over to ActiveJS website or documentation to learn more about how it can help you manage state with minimum effort.

Here’s the StackBlitz playground link if you want to try it out yourself.

Also, I forgot to tell you that this is my first ever article on any platform, please let me know if I did an okay job, or if there’s something that I can improve.

Cheers

Originally published at https://dev.to on November 2, 2020.

--

--