Cancel service call in Dojo - web-services

I want to know if it is possible to cancel a call to a webservice using Dojo. My problem is, e.g., that the user has many elementes to select in a map, and every time he clicks on one of them a call to a webservice is excuted to bring an important volume of data. This operation takes a few seconds of latency. Imagine that user clicks on five elements almost simultaneously: there will be a selected element (the last clicked), but the info panel will be refreshing periodically with the info of the previous elements clicked until the last element info is loaded.
What I want is to cancel all the previous calls (if it possible) and only allow to complete to the last one. I'm working with Dojo. I've seen the documentation but I haven't seen anything useful. Something like the "ajax.abort()" method is what I would expect to find.
I have also thought to create a class to manage the calls and add to a queue every call to a same url in order to only let the last call to render the data, but by this way I still would to wait to complete every service call and then discard the results. I think that the proper way to proceed is to cancel the old calls.
Any suggestion? Thanks.

After search in some forums and pages, I've found this one: www.mike-griffith.com - Cancel dojo XHR in progress. This is exactly what I was looking for (although in the web example is used to "GET" services, the "cancel()" method also works for "POST" calls).
So I will combine the service calls manager with this code to cancel calls with the same url. I also have to manage how to avoid (if I am able to) the raised error when a call is canceled, but this is other task and much less priority.

Related

How do I conditionally elicit an Alexa multi-turn dialog

I have a customer requirement that necessitates a conditionally prompt from Alexa. Basically, the user will ask {intent} {utterance}, the back-end will check their account to see if they have more than one item in a list; if so, it will ask "which one item 1 or item 2". They will need to respond with 1 or 2.
If there is only one item in their list, it will default to that item and not require input from the user.
My understanding of how multi-turn dialogs works is that I must create a dialog model with at least one required slot. As you can see in my example, there isn't always a required slot.
Is this possible? If so, can you outline (at a high-level of course) what steps I should take?
Note: Unfortunately, one of the requirements is that the endpoints be handled in Azure; therefore, I must utilize Alexa.NET instead of the typical SDKs. Not sure if that changes anything.
Dialog handling
Yes it's possible.
After your:
ask {intent} {utterance}
it will hit your backend with dialogState STARTED
now you can check if you have multiple items
yes(multiple items): delegate dialoge handling back to alexa and alexa will ask for the slot number. You now check if dialogState = COMPLETED and so on
no (only one item): just respond back to the user without dialog delegation
SDK
It does not make a difference with Alexa.NET if dialog handling is implemented there. I also struggle sometimes to find examples for the Java SDK ;-).
Here is a video which helped me.

how continuously run function on page request

I want to use third party's REST API providing real-time foreign exchange rates in my django app which should continuously show changing exchange rates.
my code works but only on every page reload. but I want my code to be running continuously and showing exchange rate on page even if the page is not reloaded.
def example(request)
RATE__API_URL = 'REST API url'
while True
rate = requests.get(RATE__API_URL).json()
b = rate['rates']['EURUSD']['rate']
context = {'b': b}
return render(request, 'example.html', context)
on my example.html
<h1>
{{b}}
</h1>
code is running and does not show any errors
There are a few ways you can solve your requirement and none of them are the "right way", also much of it depends of what you have in your code, so I'll try to lay them out for you, and given the extent of what needs to be done while providing some links so you can work on it, but I will not give code because you'll require a fair amount of tailored code (sorry for that) and the references are good enough for you to develop your own solution.
The first thing you have to keep in mind is than you'll need to solve two really big and really different requirements:
The first part of your solution is retrieving the data from the source in a timely manner. The second part is to have a way to update the data in the template without the need for the user to reload the page.
To retrieve the data you said you already have an API where you'll get the data, but your code is not an efficient way to approach this, and it also may generate a risk because it is prone to hit too many times the API server; the best way I can think of would be if the API has webhooks or push notifications to which you can subscribe (which I doubt), the second best choice is to implement a Celery task, that way you will be calling the data regularly, and you'll not eat the API service resources.
With the first part out of the way, what you have left to do is to implement a way to call regularly from the UI for the newest data. Perhaps the simplest way to solve it is to implement an asynchronous call with Javascript/JQuery embedded in a script inside your template, but remember:
For this to work you'll need a model to store the data (If you don't
have an use for historic data, then just keep the most recent
one).
You'll need a view that exposes the data to your UI call (one that sends a JSON)
Another solution is to implement websockets, and the best way to achieve this for Django is using django-channels. You'll have to implement two main things:
In the backed you need to define the consumers flow so you can send
the data to the UI.
In the template you need to implement the websocket connection and a way to handle the updating part of the data for the user.
If you choose this way, and given than you don't need historic data, you can obviate the model and go straight from the Celery task to the UI through the consumer.

How to detect a period of inactivity in EmberJS?

I am trying to detect a period of user inactivity in my EmberJS application so that, after a certain duration I can call a session.inValidate method and revoke their authentication.
The only resource I have been able to find is an article here which I was advised in an original question is out of date and doesn't use current Ember concepts.
The out-of-date article suggested adding a property to the App object and updating the property on certain events like mousemove but I was unable to find a location to set this property correctly in either app.js or the config environment.
ember-concurrency was recommended but after looking at some of the documentation I still don't understand where such code fits in an EmberJS application structure.
Unfortunately I'm stuck at a point before I can give a code sample. Any advice is appreciated.
Edit: As per #hernanvicente excellent question, I am defining inactivity as 'no user input'. So no mousemove or key press events within X period
The hard question here is what activity actually means. Generally I would advise against your idea. There is no way to know if the user is actually still looking on your page.
However if you really want to do something after the user hasn't done something for some amount of time the interesting question is where to place your code.
You have multiple options here, but probably not the worst option would be to use the application controller. You could also use a service, but then you need to use that service somewhere to initialize it. You could also just use a component.
If you want to couple this to authentication you could consider to use the existing session service.
However everything else is not really ember specific. You can just place some code in the application controllers init hook and access document.addEventListener(). Then you could just save a timestamp away and act accordingly.
To have a bit a nicer API for waiting you can utilize ember-concurrency with a restartable task:
didSomething: task(function * () {
yield timeout(1000); // number of miliseconds to wait
logout();
}).restartable(),
here logout() will be called, after the task hasn't been called for 1 second. A nice visual demonstration about task concurrency and restarable can be found here.
Here is an ember-twiddle showing a complete example.

Generic handling of race condition in Django from multiple "clicks" in web interface

I've followed this post (Race conditions in django) regarding race conditions in Django. The select_for_update solution solves the problem of multiple updates overriding each other.
I have a problem in the same area. I have a quest-like app running on top of Django. A quest has multiple levels; when the user provides a correct answer by filling an answer form and clicking the "Check answer" button he gets promoted to the next level. However, if a user were to provide the correct answer and then click the "Check answer" simultaneously on two browser screens, the server side would get two requests.
On the server side, if the answer is correct, one would also get a level increase. The code is something like:
self.current_level += 1
self.save()
Even if I used a select_for_update approach, the two requests might be accepted on the client side then reach the server. On the server side they would be processed sequentially and result in a two level increase (instead of just one). This would happen even if using a select_for_update approach.
The solution I would use, particular to the implementation, is verifying the current level before any update and making sure the current level is the one for the current question (not the next one). Though only rarely would this check fail.
Is there a more generic way to handle these kind of problems? Or would one need to check the current value of the level/object/data and update it only if it matches the expected value? In conjunction with the select_for_update approach, of course.
The key here is to acquire the update lock before you check if the conditions are satisfied. Once you have the lock, other threads can't change the data, and you're free to check the conditions and update. If there are concurrent requests, the second thread will always wait until the data is updated to check the conditions.

Issues with On Demand Process Response

I am using Oracle ApEx v4.1 together with Dynamic Actions, which basically calls a javascript function, which in turn calls an On Demand Process to save data, to the database.
Just a bit of background, I am using jQuery to scan each of the elements with their values, when the user either presses the "Save" button or the "Next" button, which in turn then passes these elements with values into the above on demand process.
My question/issue is, it looks like sometimes the process is missing the data passed in and I am not sure why and I thought that perhaps in my dynamic action I am performing both a ape.submit('SUBMIT') as well as a JavaScript function call to an on demand process.
Do I need to delay one of these calls, as at the moment, I am unsure why it works sometimes and other times, it doesn't.
Any ideas on how to lay out the code, i.e.
apex.submit('SUBMIT');
saveTheData(); <-- calls my ondemand process to save data to database
First of all, let's hark back a bit to your previous question. And what exactly you are exactly doing here. It seems wildly unnecessary!
What reason is there to collect item values in jQuery and submit them to session state when you are submitting the page anyway? When you use a next/prev/appy button and the page submits then the items' values are in session state, and you can use them in processes.
You are submitting the page with apex.submit. This submits the page, and sets all item's values in session state. You perform your JavaScript function which would call an on-demand process, providing values to the process. These values are page item values, and thus you're actually just setting session states in your on-demand process. It honestly seems like you have a real wacky design going on!
As to why it sometimes works, and sometimes it doesn't: apex.submit will submit the page. Like, right away. If you need code executed before the page is processed then do it before the submit. Note that if you were to switch the lines around it might still not work, depending on how you call the ondemand process (async or synchronous), and whether you want a success function to do something or not. When the call is async, then it might be your success function is not handled before the submit is done.