May 08, 2010

Oh boy - JavaScript and Flash really ARE on the same thread

Yeah, I know… this is something trivial that everyone knows, right? but I had to check it out.
The question I was presented with was: If I call a JavaScript function from Flash, will Flash UI keep on rendering or wait for a "response" from JS to continue?
The answer, as sad as it may be, is YES, it will stop rendering.

Here, check out the code below. It's a little test I did, where I have a button to launch an Alert from JS. I've also added a progress bar with it's indeterminate property set to "true" in order to see clearly if it stops, and there I have it. Once the Alert is up, the progress bar stops from rendering.
Hmmm… I don't like this. I don't like this at all.

Here is the Flex Application code:
<?xml version="1.0" encoding="utf-8"?>

private function onClick(event:MouseEvent):void {
if (ExternalInterface.available) {"showAlert");

label="Show JS Alert"

<mx:ProgressBar indeterminate="true"/>

And here is what I've added to the HTML template:
function showAlert() {
alert("Flash UI stops from rendering!");


Atarsh said...

how long has it been since you have stopped calling yourself a flashist?
how long since you had to debug something using alerts?
you just had to ask...

Flashmattic said...

Well, it’s a bit narrow to think that developing Flash has nothing to do with connecting to the java script and HTML that wraps it on the web. Comeon… :)

Atarsh said...

that's not what I meant.
When you rely on alerts for debugging flash apps you know that alerts from withing flash stop flash player - try using alerts to debug something that happens onEnterFrame...

Flashmattic said...

This has nothing to do with debugging, Atar. Any operation that relates to UI rendering and on JS will be done on the same thread of Flash. If I want flash to communicate with JS in order to invoke some UI operation on non-flash elements within the same HTML page… I'm pretty much screwed.

Atarsh said...

debugging using alerts is just the most obvious example, that's all.
can you give a "real life" example where this will actually matter? I mean, that the JS operation will take long enough to disturb the end user?

Flashmattic said...

Sure. Think about a scenraio where you flash file is a chart that resides on an HTML page along with other "widgets". Say that you have to render some kind of a tooltip from the HTML using JS for software look and feel consistency, but in the meantime you need to draw something on Flash. There are a lot of scenarios like these.

Atarsh said...

and rendering the tooltip takes so long it actually disturbs the ux? maybe that's the problem..
I'm not trying to pick on a specific scenario, but unless what you do is some huge calculation on the JS side that actually takes a notable time, it doesn't sound like a big problem.
of course I could be mistaking..

Flashmattic said...

All I'm saying is that JS and Flash use the same thread to render UI. This can create a serious trouble in responsiveness of the application you're building.

Atarsh said...

טוב נו...

guya said...

you're title is misleading.
No browser vendor will even think of putting them on the same thread.

What you describe is the most welcome feature of Javascript and Flash communication. Since Flash 8 and ExternalInterface, communication is synchronous.