Code to launch WebWorkers

This commit is contained in:
Kovid Goyal 2021-05-16 11:02:28 +05:30
parent d7efe7d8f9
commit 486413f267
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 30 additions and 1 deletions

View File

@ -1,6 +1,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
# globals: ρσ_get_module # globals: ρσ_get_module, self
from __python__ import hash_literals from __python__ import hash_literals
import initialize # noqa: unused-import import initialize # noqa: unused-import
@ -13,6 +13,15 @@ autoreload_enabled = False
AUTO_UPDATE_THRESHOLD = 1000 # millisecs AUTO_UPDATE_THRESHOLD = 1000 # millisecs
def worker_main(entry_point):
m = ρσ_get_module(window.iframe_entry_point)
main = m?.main
if main:
main()
else:
console.log('worker entry_point ' + entry_point + ' not found')
def iframe_main(script): def iframe_main(script):
script.parentNode.removeChild(script) # free up some memory script.parentNode.removeChild(script) # free up some memory
script = undefined script = undefined
@ -58,3 +67,8 @@ if document?:
iframe_main(iframe_script) iframe_main(iframe_script)
else: else:
toplevel_main() toplevel_main()
url = window.URL.createObjectURL(Blob([main_js()]), {'type': 'text/javascript'})
worker = new window.Worker(url + '#moo')
console.log(worker)
elif self?:
entry_point = self.location.hash[1:]

15
src/pyj/worker.pyj Normal file
View File

@ -0,0 +1,15 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import bound_methods, hash_literals
from book_list.globals import main_js
def worker_js_url(entry_point):
if not worker_js_url.ans:
worker_js_url.ans = window.URL.createObjectURL(Blob([main_js()]), {'type': 'text/javascript'})
return worker_js_url.ans + '#' + entry_point
def start_worker(entry_point):
return new window.Worker(worker_js_url(entry_point))