Asynchronous delay in Lua? (like JavaScript setTimeout) - asynchronous

I need to asynchronously delay execution of a function in Lua by X milliseconds. Can this be done?
Given a simple JavaScript example:
setTimeout(function() {
alert('Hello world!');
}, 5000);
What's the Lua equivalent? Coroutines look like they may help, but I'm not sure.

Lua itself does not include asynchronous event support. If you're embedding Lua in something larger or using it with libraries, they may be able to provide callback support.

Try my lalarm library. It depends on alarm, which works in seconds, but can be easily changed to use ualarm if you have it.

Related

vue-router-multiguard respects asynchronous guards

Does anyone have experience with vue-router-multiguard?
The documentation says "Guards are executed serially in the order they are supplied, respecting asynchronous ones. "
But looking at the code I don't see how it could do so.
Anyone able to help me see what I'm missing?
The author of the library responded to me on github
async behavior is achieved via the next callback parameter. The guard chain will not proceed until the current guard calls it even if it is called in an async manner. Here's the README.md example which uses setTimeout to demonstrate async behavior in guards

React Native computational heavy task

Very simple and common use case, but I can't find satisfying answer.
I have react native app which needs to store some data locally. I am using redux and redux-observables and Realm as a storage.
What I need is: some action occur, let's say ADD_ITEM. I need to update UI with some payload (standard redux). Then I want to create/transform that payload to another piece of data and save that data into Realm (asynchronously). Whatever I try, it always lags UI (a lot).
I tried to wrap that realm call into promise and use switchMap, but it's still slow.
I haven't took a look at workers yet but they only accept strings, which is let's say much less usable for me.
I can offload completely that computation to native background thread but that will be very uncomfortable and a lot of writing.
Could async await help me? Of redux-saga? I feel like it's the same thing, not real async processing.
Or am I using completely wrong libraries for this?
const insertOrderItem = (action$) =>
action$.ofType(Action.ADD_ORDER_ITEM)
.switchMap(({ payload }) => Rx.Observable.fromPromise(
new Promise((resolve, reject) => {
storage.insert(createObject(payload)
resolve({
type: "OPERATION_ADDED"
})
})
))
In general, storing small piece of data to realm shouldn't be THAT much computational heavy but I feel like it's necessary to do this kind of jobs on background thread.
Am I missing something?
Thanks
I had same problem with my app using React Native + Realm DB. I tried all ways without having to write native code and make the bridge from react-native to native, and finally I found a solution:
https://corbt.com/posts/2015/12/22/breaking-up-heavy-processing-in-react-native.html
Install npm install next-frame --save.
Add next-frame to your code import nextFrame from 'next-frame';
Add this line within a for that took long, or a for which insert in DB await nextFrame();
A complete example would look like this:
import nextFrame from 'next-frame';
let response = await fetch("https://emberall.com/user/1/recordings");
let responseJSON = await response.json();
for (let recording of responseJSON.recordings) {
await nextFrame(); // The javascript will yield here and not resume execution until the next frame.
mergeRecordingToLocalDatabase(recording);
}
Basically what this does is that in every iteration or db-insert it waits for JS to render and process next UI frame, so your app will not get stuck or freeze.
P.D.: I know months have passed since question but I think a lot o people could benefit from this.
It's laggy because JS run in one thread. So if you wrap your code in Promise, it's still in one thread.
I think best solution for your is transfer data over React Native bridge to native and do this transform in native code. For guide how to communicate with native code look here
I'm not familiar with Realm, but I can't seem to find the storage.insert API you're using. I only see realm.write() transactions. I was trying to see if they had a truly non-blocking, asynchronous API but it appears Realm does not offer one for their JavaScript bindings.
They do appear to offer non-blocking APIs in their Java bindings with executeTransactionAsync() but that is not likely an option for you.
Seems like your options would be: do it in native on another thread, or some how reduce the write overhead (smaller object, write at a time where it's not noticeable, etc)

Subscribe to a simple event sent from server in Meteor

Sometimes I want to send simple events from the server to all clients in Meteor without having to deal with Collections - I feel like there must be some easy way to do this but I haven't managed to find it.
I want something like:
Server
connection.send("messageForAllUsers", {text: "Hello"});
Client
connection.subscribe("messageForAllUsers", function(result){
alert(result.text);
})
there is a meteor stream package which might be what you are needing.
http://arunoda.github.io/meteor-streams/communication-patterns.html#streaming_private_page
Take a look at this: http://meteorcapture.com/publishing-anything/
I think at the end of the day, collections is the easiest and most straightforward way to go when it comes to Meteor.

How to use Prerenderio with Meteor?

I would like to use prerenderio with Meteor instead of phantomjs on the server with modulus.
However given the examples they provide, I'm not sure how to integrate it. They only provide a node express middleware which doesn't translate 100%.
For SEO purposes? I mean, what else could it be? ;)
Firstly, remove the spiderable package if you haven't already.
Second, drop this at your server-side code (for example server/prerenderio.js):
// Use Prerender with your token
var prerenderio = Npm.require('prerender-node').set('prerenderToken', 'YOUR_TOKEN');
// Feed it to middleware! (app.use)
WebApp.connectHandlers.use(prerenderio);
If you're wondering about Npm.require (or Meteor.require), See this answer (by me, sorry for shameless plug) for the gist: https://stackoverflow.com/a/16481897/951773
Source: I've used prerenderio successfully for a couple of our clients.
![Good luck!][1]
EDIT:
Since there has been major differences now between express request and response objects to meteor's connect objects, it went to really complicated now. But this has been addressed now and hopefully the PR I put in works:
https://github.com/dfischer/meteor-prerenderio/issues/1
TL;DR Thanks to this question now we have a prerender.io meteor module.

Calling a function after data event in Meteor

I wonder what is the best way to do something like this:
When one of the data gets remove, all client will play an animation (like fading out the data, etc), or call a javascript function on client.
The cursor.observeChanges callbacks are exactly what you need in your client code to achieve this - specifically the removed callback. Have a look at the example provided in the docs and it should be quite straightforward.

Resources