B4J Question ABMaterial - multiple calls to ws.Eval

JackKirk

Well-Known Member
Licensed User
Longtime User
This may prove useful to someone.

My ABMaterial webserver does on-the fly translation of text to whatever language the browser is using - its a bit more sophisticated than that in that it remembers any past translations for future use.

To achieve this I do something like:
B4X:
Sub ABMComp_Refresh(PassedPage As ABMPage, PassedInstanceName As String)
   
    ...
    'Create temporary script - telling user we are translating
    script_str = _
    $"
    document.getElementById('divtranslating').style.display = 'block';
    "$
    PassedPage.Pause
    PassedPage.ws.Eval(script_str, Null)
    PassedPage.Resume
    ...
    'Create raw script
    script_str = _
    $"
    document.getElementById('divtranslating').style.display = 'none';
    ...
    "$  
    ...
    'Translate raw script
    script_str = translatefunction(script_str)
    ....
    'Load translated script
    PassedPage.Pause
    PassedPage.ws.Eval(script_str, Null)
    PassedPage.Resume
    ...
   
End Sub
The problem was I initially just put the multiple PassedPage.ws.Eval(script_str, Null) calls without wrapping them in PassedPage.Pause/Resume.

Which led to very unreliable flakey results - sometimes it worked other times it didn't.

I eventually found that if they are wrapped as shown all works beautifully.
 

JackKirk

Well-Known Member
Licensed User
Longtime User
You do not really need to wrap them in a Pause/Resume, but you do need to call a PassedPage.ws.Flush after making the call.
I replaced:

PassedPage.Pause
PassedPage.ws.Eval(script_str, Null)
PassedPage.Resume

with:

PassedPage.ws.Eval(script_str, Null)
PassedPage.ws.Flush

throughout my app and am happy to report all seems to be stable - will run for a couple of days and report back if this is not the case.

COMMENT:

I saw ws.Flush when I was rummaging through both PassedPage and PassedPage.ws methods looking for a solution. I didn't proceed with ws.Flush because the inline documentation:

Flushes the output stream. Flush is called automatically when client events complete.
You need to explicitly call it at the end of server events.


Was as clear as mud - and I naturally assumed it meant that it deleted the output stream (my normal understanding of "flush").

On the other hand PassedPage.Pause/Resume which have zero inline documentation seemed more likely - and worked.
 
Upvote 0
Top