🚀Getting Started
Installation
If you don't have RxJS already, you'll need to install it first npm install rxjs
, and then
If you don't have access to a module bundler, here's the UMD bundle link. (not needed for frameworks like Angular, React, Vue etc.)
Prerequisite
ActiveJS assumes a prior understanding of Observables. Some understanding of RxJS can also be helpful, but the basic knowledge of Observables should be enough. This official guide or this primer might help you get up to speed.
Prelude
Imagine a variable that can only be assigned a value of a particular data type, let's say only number
, and when we change the value of this variable, it gets broadcasted to observers, while we also keep past values in the cache to be able to undo/redo. That's basically what an ActiveJS Unit allows us to do, and the one that only allows numbers is called NumUnit.
At the heart of ActiveJS are these reactive data structures called Units, that emulate JavaScript's native data structures. Units behave almost exactly like a native data structure would, while the actual value is kept encapsulated inside the Unit. Units are observable and every operation and mutation is broadcasted.
Quick Example:
Let's take a simple and comparable example of implementing a counter.
We'd use a NumUnit for the counter since we expect the value to always be a number
.
Try it on StackBlitz. Or, let's also try some built-in methods.
ActiveJS Units are cache-enabled, and by default, every Unit caches two values. When you navigate through the cache, the cache stays intact, while the value changes. This makes it very easy to travel back in time and then go back to the future.
There are even more built-in things that a Unit can do, but this is the crux of it.
Fundamentals
Other constructs within ActiveJS build on the core concept of independent storage Units and work together to create a complete workflow for advanced use cases.
ActiveJS is made up of four fundamental constructs, visualized with their core components in the diagram below. Each one of them plays a different yet crucial role in the complete state-management lifecycle.
Units are independent, reactive data structures. Units are responsible for holding the state of your App.
Units are cache-enabled, and based on JavaScript's native data structures like
boolean
,string
orarray
.For example, ListUnit is an elaborate
array
like construct which is also an Observable and implements all theArray.prototype
methods in a way that, any mutation caused by these methods, emits a newarray
with the mutations applied to it.All available Units are BoolUnit, NumUnit, StringUnit, ListUnit, DictUnit, and GenericUnit.
Systems encompass a certain number of Units in a specialized way and implement inter-relationships among these Units to pertain to a specific task. For now, there's only one of its kind.
AsyncSystem helps with async APIs like
XHR
orfetch
, it makes sharing their state very easy.
Actions are meant to represent unique events as
Observables
. Action is an RxJSSubject
like construct, with extra features like replay on demand. Actions are useful, but not essential to start using ActiveJS.
To jump right into the action without writing any code, you can try out this visual playground, it might give you some more perspective, and help understand the most important ActiveJS constructs Units, and Systems.
Do I need ActiveJS?
As much as we'd like to say YES, we don't want you to get into something that you later realize is not for you.
That being said, If you're coming from a Flux background you'll see that it's different yet somehow familiar and easier. If you feel like Flux based state management solutions are overkill, you'll love it. If you're happy with RxJS, you'll be able to do more by doing less. If your single-page app is not using Observables, it can give you wings (maybe).
So, do you need it? The answer is we are not sure, it can definitely help you in one way or another, you can start small without much commitment, give it a go and see how it pans out.
Last updated