Javascript, by itself, does not have a wait function. However, you can create a pseudo wait function by doing the following:

while( !input_available() )
    mch_delay(50);
.
.
.
function mch_delay( time )
{
    if( time !== undefined ){ settimeout( "mch_delay()", time ); }
        else return true;

    return false;
}

If the above is called with a time it will set the timeout and then return false. This might seem like it will cause the program to do nothing else but the timeout retains its own return information about where it came from. Basically, javascript splits (or does a fork) on the timeout. So the next thing it will do is to hit the "return false". The return false will be the flag to say "stop this branch of the process". When the timeout kicks back in though, it will be calling the function without a time on the call line. This will make the ELSE branch fire off which will return true and your program will continue.

The thing to note is - you have to take the false return into consideration. The problem with Javascript is two fold: 1)No sleep function, and 2)No END/EXIT function either. In looking for a way to exit a program I found this:

|function  exit(  status)  {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brettz9.blogspot.com)
    // +      input by: Paul
    // +   bugfixed by: Hyam Singer (http://www.impact-computing.com/)
    // +   improved by: Philip Peterson
    // +   bugfixed by: Brett Zamir (http://brettz9.blogspot.com)
    // %        note 1: Should be considered experimental. Please comment on 
this function.
    // *     example 1: exit();
    // *     returns 1: null

    var  i;

    if  (typeof  status===  'string')  {
        alert(status);
    }

    window.addEventListener('error',  function  (e)  
{e.preventDefault();e.stopPropagation();},  false);

    var  handlers=  [
        'copy',  'cut',  'paste',
        'beforeunload',  'blur',  'change',  'click',  'contextmenu',  
'dblclick',  'focus',  'keydown',  'keypress',  'keyup',  'mousedown',  
'mousemove',  'mouseout',  'mouseover',  'mouseup',  'resize',  'scroll',
        'DOMNodeInserted',  'DOMNodeRemoved',  'DOMNodeRemovedFromDocument',  
'DOMNodeInsertedIntoDocument',  'DOMAttrModified',  'DOMCharacterDataModified', 
 'DOMElementNameChanged',  'DOMAttributeNameChanged',  'DOMActivate',  
'DOMFocusIn',  'DOMFocusOut',  'online',  'offline',  'textInput',
        'abort',  'close',  'dragdrop',  'load',  'paint',  'reset',  'select', 
 'submit',  'unload'
    ];

    function  stopPropagation(e)  {
        e.stopPropagation();
        // e.preventDefault(); // Stop for the form controls, etc., too?
    }
    for  (i=0;  i<  handlers.length;  i++)  {
        window.addEventListener(handlers[i],  function  (e)  
{stopPropagation(e);},  true);
    }

    if  (window.stop)  {
        window.stop();
    }

    throw  '';
}

So the "return false" could be changed to using the above - if it works.  
Otherwise, just check for the false value
being returned and act accordingly.

I hope this helps.  :-)
|



On 11/29/2013 11:29 PM, ?? wrote:
Hi all,

    I'm now trying to port vim into javascript with emscripten.

    The main barrier I found is that the async model of JavaScript. JavaScript 
is event-driven, and there is no mechanism to sleep and wait for events to be 
processed, so the following won't work:


```
void wait_for_input()
{
     // wait until there's some input
     while(!input_available())
         mch_delay(50); // there's no sleep() in JavaScript, and we cannot do a 
busy-wait here
}


// in gui_start()
// do something
wait_for_input()
// do something else
```

     Instead we have to split the function to make the function call async:

```
static callback_t wait_for_input_cb;
void wait_for_input()
{
     if(!input_available())
         mch_delay_async(50, wait_for_input);
     else
         wait_for_input_cb();
}


// in gui_start()
// do something
wait_for_input_cb = gui_start_cb;
wait_for_input();
} // end gui_start() here
void gui_start_cb()
{
   // do something else
```

    Generally it's hard to maintain both versions, I'm trying to define some 
macros to ease the process, but the code is still not readable due to function 
splitting. The easiest way of doing this is to simulate the async model in the 
sync model, which would however introduce (un-necessary) overhead for sync 
situations.

    The worst case is that the async (JavaScript) version has to be maintained 
separately, but I wonder if there are better solutions?

    Please let me know what you guys think about it.

    Thanks!


regards,
    - Lu Wang
    2013.11.30


--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Raspunde prin e-mail lui