Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webworker #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eyg/public/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bundle.js
*.js
12 changes: 12 additions & 0 deletions eyg/src/atelier/worker.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gleam/io
import eygir/decode
import eyg/runtime/standard

pub fn infer(data) {
io.debug(Ok("foo111111111111"))
// io.debug()
case decode.decoder(data) {
Ok(source) -> standard.infer(source)
Error(_) -> todo("need to not send")
}
}
15 changes: 15 additions & 0 deletions eyg/src/browser_ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ export function listenKeypress(dispatch) {
dispatch(event.key);
});
}

export function startWorker(path) {
return new Worker(path);
}

export function postMessage(worker, message) {
worker.postMessage(message);
}

export function onMessage(worker, callback) {
worker.addEventListener("message", function (msg) {
console.log("message from worker received in main:", msg);
callback(msg.data);
});
}
2 changes: 2 additions & 0 deletions eyg/src/plinth/browser.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub external fn log(any) -> Nil =
"" "console.log"
13 changes: 13 additions & 0 deletions eyg/src/plinth/browser/worker.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import gleam/dynamic.{Dynamic}
import gleam/json.{Json}

external type Worker

external fn start_worker(String) -> Worker =
"../../browser_ffi.js" "startWorker"

external fn post_message(Worker, Json) -> Nil =
"../../browser_ffi.js" "postMessage"

external fn on_message(Worker, fn(Dynamic) -> Nil) -> Nil =
"../../browser_ffi.js" "onMessage"
6 changes: 6 additions & 0 deletions eyg/src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { infer } from "./atelier/worker.mjs";
// build as es
self.onmessage = function (msg) {
console.log("received msg: ", msg);
self.postMessage(infer(msg.data));
};
6 changes: 5 additions & 1 deletion language.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ WORKDIR /opt/app/eyg
# RUN npx rollup -f iife -i ./build/dev/javascript/eyg/bundle.js -o public/bundle.js
# CMD gleam run web

# gleam format && gleam build && npx rollup -f iife -i ./build/dev/javascript/eyg/bundle.js -o public/bundle.js && gleam run web
# gleam format && \
# gleam build && \
# npx rollup -f iife -i ./build/dev/javascript/eyg/bundle.js -o public/bundle.js && \
# npx rollup -f iife -i ./build/dev/javascript/eyg/worker.js -o public/worker.js && \
# gleam run web