setInterval/setTimeout override?

1,505 views
Skip to first unread message

Miro

unread,
Feb 23, 2014, 10:11:03 AM2/23/14
to meteo...@googlegroups.com

Meteor.setTimeout() & Meteor.setInterval() "...functions work just like their native JavaScript equivalents. If you call the native function, you'll get an error stating that Meteor code must always run within a Fiber...".

Meteor is doing an override on the native functions to throw an error.

In some applications I've used (and had problems) with third party libraries (client side) that are using the native functions, totally out of one's control.

I'm wondering if it would be wise to do the opposite - ie. not throwing an error but instead invoke the Meteor.setTimeout()/Meteor.setInterval() by overriding the "native" JavaScript functions, at least on the client side.

What are implications of doing so and why Meteor didn't do it that way?

One problem would be, if done from the package, the loading order of packages - the packages loaded before the overriding package would still access the "native" functions (though overridden by Meteor)...

Miro

Slava Kim

unread,
Feb 23, 2014, 5:45:28 PM2/23/14
to meteo...@googlegroups.com
Hi,

Meteor doesn't override native setTimeout and setInterval. It is Meteor-specific code that requires additional environment and Fibers. So in reality, it is Meteor.setInterval and Meteor.setTimeout those wrap native versions and set the required environment before the callback execution.

To resolve your problems with 3rd party libraries, you can wrap your callbacks into Meteor.bindEnvironment or use Meteor._wrapAsync to make functions look like synchronous on the server side.


Also see these videos on EventedMind if you are interested why it happens or want more insight on what is happening behind the scenes: 

Slava

Miro

unread,
Feb 24, 2014, 4:56:43 AM2/24/14
to meteo...@googlegroups.com
Hi,

Thank you for your answer.

I'm well aware of the Fibers/bidEnvironment/_wrapAsinc but I was referring to the client side of things - 3rd party javascript libraries using native functions and messing up the UX.

Miro

Slava Kim

unread,
Feb 24, 2014, 3:03:00 PM2/24/14
to meteo...@googlegroups.com
Hi,

I don't really understand how 3rd party javascript libraries can mess up the UX if they use native timeouts. Meteor doesn't change native timeout.

Maybe direct links to code will give you more information on how to deal with issues you have in your app:

David Glasser

unread,
Feb 24, 2014, 5:16:08 PM2/24/14
to meteo...@googlegroups.com
As an analogy, pretend we had this code:

var safe = false;

var functionThatMustBeCalledSafely = function () {
if (!safe)
throw Error("It's not safe!");
...
};

Meteor.setTimeout = function (f, time) {
setTimeout(function () {
safe = true;
try {
f();
} finally {
safe = false;
}
}, time);
});

If you call setTimeout(functionThatMustBeCalledSafely, 100), then
functionThatMustBeCalledSafely will throw an error... because we
*DON'T* override setTimeout. Using Meteor.setTimeout, things work
properly.

In the actual case, "being called safely" means "being called in the
context of a Fiber", and functionThatMustBeCalledSafely is any
function that needs to block (yield) on IO, such as reading from a
database.
> --
> You received this message because you are subscribed to the Google Groups
> "meteor-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to meteor-talk...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Miro

unread,
Feb 27, 2014, 5:43:01 PM2/27/14
to meteo...@googlegroups.com
Hi,

I might have been a bit too general in my question and maybe sent discussion in a wrong direction, so let me explain:

I have a Bootstrap 3 modal that closes immediately after opening at page load. I have tried many advices, but besides them being "hacky", I'm still losing modal in about 3 out of 10 cases.

Now, I'm not saying that I've done everything possible to make it happen - I'm just wondering why do I have to hack around to open a darn modal (and keep it open) at all?

While trying to crush that bug, I came to a conclusion(?) that it must be Bootstrap's native setInterval/setTimeout functions firing at will - out of sync with Meteor.

So, my question was regarding the possibility of circumventing or avoiding those problems by "overriding" the native functions with Meteor.setInterval/Meteor.setTimeout.

Does that make any sense?

Miro
Reply all
Reply to author
Forward
0 new messages