Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

62
LINES

< > BotCompany Repo | #1013795 // Auto-Restarting Web Worker Test

JavaX module [tags: ai1-lol use-pretranspiled] - homepage

Download Jar. Libraryless. Click here for Pure Java version (1856L/12K/43K).

!7

html {
  if (eq(uri, "/worker.js"))
    ret [[
      addEventListener('message', function(e) {
        var startValue = e.data, i = startValue;
        postMessage("WORKING!");
        var startTime = new Date();
        while (true) {
          i = i+1;
          if ((i % 1000000) == 0) {
            var ms = new Date()-startTime;
            var mips = ((i-startValue)/ms/1000).toFixed(2);
            postMessage("Counted to: " + i + " in " + ms + " ms ("
              + mips + " million steps/s)");
            if (ms >= 4000) {
              postMessage(["continue", i]);
              break;
            }
          }
        }
      });
    ]];
    
  ret htitle("Web Worker Test")
    + hbody(
      p("Count numbers: " + hfulltag("output", "", id := "result"))
    + p(hbutton("Restart Worker", onClick := "restart()"))
    + p(hbutton("Dummy Button"))
  + hjavascript([[
    var worker;
    
    function restart() {
      worker.terminate();
      go();
    }
    
    function go() {
      cont(0);
    }
    
    function cont(i) {
      if (typeof(Worker) === "undefined") {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
        return;
      }
      document.getElementById("result").innerHTML = "Making web worker";
      worker = new Worker($LINK);
      worker.onmessage = function(event) {
        var e = event.data;
        if (typeof(e) == "object" && e[0] == 'continue') {
          worker.terminate();
          //setTimeout(function() { cont(e[1]) }, 1000);
          cont(e[1]);
        } else
          document.getElementById("result").innerHTML = event.data;
      };
      worker.postMessage(i);
    }
  ]]).replace("$LINK", jsQuote(relativeRawBotLink(programID(), "worker.js"))), onLoad := "go()");
}

Author comment

Began life as a copy of #1013783

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1013795
Snippet name: Auto-Restarting Web Worker Test
Eternal ID of this version: #1013795/8
Text MD5: 2bf7e8160cebb4566dd5604c82877feb
Transpilation MD5: d277ea8875588978b3176ccf0a9931cb
Author: stefan
Category: javax / web workers
Type: JavaX module
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-02-28 13:35:07
Source code size: 1862 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 512 / 1061
Version history: 7 change(s)
Referenced in: