I am writing a little "fun" Scala/Scala.js project.
On my server I have Entities which are referenced by uuid
-s
(inside Ref-s).
For the sake of "fun", I don't want to use flux/redux architecture but still use React on the client (with ScalaJS-React).
What I am trying to do instead is to have a simple cache, for example:
- when a React
UserDisplayComponent
wants the display theEntity
User
withuuid=0003
- then the
render()
method calls to theCache
(which is passed in as aprop
) - let's assume that this is the first time that the
UserDisplayComponent
asks for this particularUser
(withuuid=0003
) and theCache
does not have it yet - then the
Cache
makes anAjaxCall
to fetch theUser
from the server - when the
AjaxCall
returns theCache
triggersre-render
- BUT ! now when the component is asking for the
User
from theCache
, it gets theUser
Entity
from theCache
immediately and does not trigger anAjaxCall
The way I would like to implement this is the following :
- I start a
render()
- "stuff" inside
render()
asks theCache
for all sorts ofEntities
Cache
returns eitherLoading
or theEntity
itself.- at the end of render the
Cache
sends all theAjaxRequest
-s to the server and waits for all of them to return - once all
AjaxRequests
have returned (let's assume that they do - for the sake of simplicity) theCache
triggers a "re-render()
" and now all entities that have been requested before are provided by theCache
right away. - of course it can happen that the newly arrived
Entity
-s will trigger therender()
to fetch moreEntity
-s if for example I load anEntity
that is for examplecase class UserList(ul: List[Ref[User]])
type. But let's not worry about this now.
QUESTIONS:
1) Am I doing something really wrong if I am doing the state handling this way ?
2) Is there an already existing solution for this ?
I looked around but everything was FLUX/REDUX etc... along these lines... - which I want to AVOID for the sake of :
- "fun"
- curiosity
- exploration
- playing around
- I think this simple cache will be simpler for my use-case because I want to take the "REF" based "domain model" over to the client in a simple way: as if the client was on the server and the network would be infinitely fast and zero latency (this is what the cache would simulate).