When I started to follow the cryptocurrency market I was wondering: Can Google Trends Predict Cryptocurrencies Movements? The answer was yes. I will tell you how I got to this insight. I come from…
Your Javascript app runs any number of async tasks, reacting to their outcome. You diligently manage the lifecycle of your tasks, but ask yourself, is that just more boilerplate? Have you considered all the edge cases, or did you miss something this time around?
In this article, we’ll look at a JavaScript Task Wapper for arbitrary work, that can be reacted to, and is cancellable, time bounded, and restartable.
We’ll wrap it in a Vue mixin and put it to use in a single page Vue app that fetches images from Flickr, reacting to all of it’s lifecycle states.
A task wrapper isn’t rocket science. Here’s what a typical wrapper lifecycle looks like:
Employing a DFA in the Task class gets us a bulk of our requirements — transitioning between states (or staying in a current state) based on user inputs and notifications from the encapsulated work.
We will need to augment the DFA with code, e.g. to initialize data in the idle state and start a timer when running, and provide it with memory to manage the request to, and the result from, the encapsulated work.
Since we’re allowing the Task to be restartable (after reset), we will need to guard against stale notifications of completion or errors, from earlier previously cancelled but unterminated work. We’ll create a unique session identifier when entering the idle state and require work notifications to include the session id.
Figure 2 show shows our DFA state diagram augmented with the code and decision blocks.
Here’s what we want from our state machine (beyond a simple DFA):
To program our state machine, we need to provide gen-statem
with an ordered list of event handlers:
We’ll incorporate our Task class into Vue
as a mixin for reuse.
VueTaskMixin
provides the following methods:
The app cycles through the following scenarios:
In this article, we looked at the design of a state machine with necessary features to handle a typical data fetching scenario that is cancellable and restartable, with an optional timeout; a Vue mixin that provides this as reusable functionality and a demo with a simple data fetching app.
We hope that this functionality or a variation, frees up boilerplate in your next application.
For latest date report which includes top 500 players,market activities (acquisitions, partnerships etc.), business insights (market overview, opportunities etc.) and much more visit our AI powered…
Suffice it to say that I’m in a boatload of homesteading, farming, small business farming, and hobby farming groups on social media. Normally, I just scroll around until something catches my eye…