Skip to content

Commit

Permalink
More JavaScript dev
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Feb 29, 2024
1 parent 573c839 commit 1c52188
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
11 changes: 6 additions & 5 deletions runtimepy/data/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ const blob = new Blob(
{ type: "text/javascript" },
);

// Creating a new document.worker property containing all our "text/js-worker" scripts.
document.worker = new Worker(window.URL.createObjectURL(blob));
// Creating a new document.worker property containing all our "text/js-worker"
// scripts.
let worker = new Worker(window.URL.createObjectURL(blob));

document.worker.onmessage = (event) => {
pageLog(`Received: ${event.data}`);
worker.onmessage = (event) => {
console.log(`Main thread received: ${event.data}.`);
};

// Start the worker.
window.onload = () => {
document.worker.postMessage("");
worker.postMessage("Message from main thread.");
};

console.log("Main thread!");
2 changes: 1 addition & 1 deletion runtimepy/data/worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
onmessage = (event) => {
console.log(event);
postMessage("Hello, world!");
postMessage("Worker received message!");
};

console.log("Worker thread!");
26 changes: 12 additions & 14 deletions runtimepy/net/server/app/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# third-party
from svgen.element import Element
from vcorelib.io import IndentedFileWriter
from vcorelib.paths import find_file

# internal
from runtimepy import PKG_NAME
Expand Down Expand Up @@ -48,7 +49,7 @@ def populate(self, body: Element) -> None:
# Worker code.
with StringIO() as stream:
writer = IndentedFileWriter(stream, per_indent=2)
self._write_worker(writer)
self._write_found_file(writer, f"package://{PKG_NAME}/worker.js")
children.append(
Element(
tag="script", type="text/js-worker", text=stream.getvalue()
Expand All @@ -58,19 +59,16 @@ def populate(self, body: Element) -> None:
# Main-thread code.
with StringIO() as stream:
writer = IndentedFileWriter(stream, per_indent=2)
self._write_javascript(writer)
self._write_found_file(writer, f"package://{PKG_NAME}/main.js")
children.append(Element(tag="script", text=stream.getvalue()))

def _write_worker(self, writer: IndentedFileWriter) -> None:
"""Write the application's JavaScript worker code."""
def _write_found_file(
self, writer: IndentedFileWriter, *args, **kwargs
) -> None:
"""Write a file's contents to the file-writer's stream."""

writer.write("console.log('Worker thread!');")
# write data/worker.js here, use a new method in FileWriter for
# embedding files (by path), delete this method

def _write_javascript(self, writer: IndentedFileWriter) -> None:
"""Write the application's JavaScript program."""

writer.write("console.log('Main thread!');")
# write data/main.js here, use a new method in FileWriter for
# embedding files (by path), delete this method
entry = find_file(*args, **kwargs)
assert entry is not None
with entry.open() as path_fd:
for line in path_fd:
writer.write(line)

0 comments on commit 1c52188

Please sign in to comment.