Builder Mode
#
MotivationThe main point of v3 is the ability to inferring the type of RjObject
by your
configuration and plugins.
When using the standard rj constructor rj(...plugins, config)
some stuff can't be
infered Es.. (the type of state in selectors) to avoid bad types in some situation
we give up and we fallback to any
.
We expected that in future version of Typescript we can improve the types experience.
If your are interessed there is an open issue.
Here at InMagik Labs we follow this mantra:
Mater artium necessitas
So to have the maxium from Typescript we introduce the Builder Mode!
#
Use builder modeWhen you invoke rj()
or rjPlugin()
without parameters you enter the builder mode.
Instead of providing big object of options you chain the same option as builder
and when your are done call .effect({ ... })
on rj()
to build an RjObject
or
.build()
on rjPlugin()
to build a plugin.
#
rj builder modeWhen use the builder mode with rj
you should a logic order to permit the
library to infer good types.
First, if you need them, you can to specify .plugins()
.
Then, if you need them , you can specify .reducer()
,
.composeReducer()
, .combineReducers()
and .actions()
.
At this point you can define .mutations()
.
Why now? Simple now RocketJump can infer the REAL state in your updater
function and suggest the string names infered from your previous .actions()
configure and even from your plugins! I encourge you to try the following example
in vscode:
Ok, now you can provide .selectors()
and have the state infered by RocketJump.
The last possible configuration before closing the builder is .computed()
.
...And yes what you think is exact we can infer both selector names and
the final state!
Where at the end, to close builder inovke the .effect()
method and the
RjObject is returned!
You call .effect()
with a function it's used as short cat as effect function:
Otherwise you can provide all effect related property:
#
rj plugin builder modeAll rules described for the rj builder also applied to the rj plugin builder.
Logic, for plugin builder plugins rules are applied. So .computed()
and .mutations()
methods are not available.
To close rjPlugin
builder and return an RjObject you should call the .close()
method, (this is different from rj builder cause in plugin there are no required options).
An example:
#
Final considerationsThe builder mode is not only useful to writing configrations the accurated types
infered in the configuration are also applied to the final RjObject
.
The good news it's that, where is possible, this advantage works good even in plain JavaScrit environment.
So using the example from previous sections: