Live Functions
About
Freestyle provides utilities to take any non-live function and make it live. This is done with invalidate
and useCloudQuery
.
Before reading this, make sure you understand the basics of Cloudstate.
How to use
-
Write your regular
@cloudstate
class. -
Any time you’d want to trigger anything reliant on a function to re-run, you can write (invalidate(thatFunction)), where thatFunction is the function you want any thing reliant on it to re-run.
-
In your frontend, wrap any
useCloud
methods you want to be live withuseCloudQuery
to make them live
Example
`
- Write your regular @cloudstate class.
- Any time you’d want to trigger anything reliant on a function to re-run, you can write (invalidate(thatFunction)), where thatFunction is the function you want any thing reliant on it to re-run.
- In your frontend, wrap any
useCloud
methods you want to be live withuseCloudQuery
to make them live
Gotchas
- Remember not to invalidate off
this
in a class, to invalidate functions used throughuseCloud
, you must invalidate off theuseCloud
method itself. - Remember invalidating a function that another function relies on will not re-run the function that relies on it. You must invalidate the function that will be subscribed to by the
useCloudQuery
method. useCloudQuery
is great for live data, but not for server side rendering, it is best paired with a single useCloud call at the start for first render, and then useCloudQuery for live data.