worker_suspended.js 771 B

1234567891011121314151617181920212223242526272829303132
  1. var count = 0;
  2. function do_magic(data) {
  3. caches.open("test")
  4. .then(function(cache) {
  5. return cache.put("http://mochi.test:888/foo", new Response(data.type + "-" + count++));
  6. })
  7. .then(function() {
  8. if (count == 1) {
  9. postMessage("ready");
  10. }
  11. if (data.loop) {
  12. setTimeout(function() {do_magic(data); }, 500);
  13. }
  14. });
  15. }
  16. onmessage = function(e) {
  17. if (e.data.type == 'page1') {
  18. if (e.data.count > 0) {
  19. var a = new Worker("worker_suspended.js");
  20. a.postMessage({ type: "page1", count: e.data - 1 });
  21. a.onmessage = function() { postMessage("ready"); }
  22. } else {
  23. do_magic({ type: e.data.type, loop: true });
  24. }
  25. } else if (e.data.type == 'page2') {
  26. do_magic({ type: e.data.type, loop: false });
  27. }
  28. }