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

Make go-app compatible with GopherJS #830

Open
wants to merge 2 commits into
base: master
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
4 changes: 2 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
const (
// IsClient reports whether the code is running as a client in the
// WebAssembly binary (app.wasm).
IsClient = runtime.GOARCH == "wasm" && runtime.GOOS == "js"
IsClient = runtime.GOOS == "js"

// IsServer reports whether the code is running on a server for
// pre-rendering purposes.
IsServer = runtime.GOARCH != "wasm" || runtime.GOOS != "js"
IsServer = runtime.GOOS != "js"

orientationChangeDelay = time.Millisecond * 500
engineUpdateRate = 120
Expand Down
101 changes: 0 additions & 101 deletions pkg/app/gen/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ var goappOnUpdate = function () {};
var goappOnAppInstallChange = function () {};

const goappEnv = {{.Env}};
const goappLoadingLabel = "{{.LoadingLabel}}";
const goappWasmContentLengthHeader = "{{.WasmContentLengthHeader}}";

let goappServiceWorkerRegistration;
let deferredPrompt = null;

goappInitServiceWorker();
goappWatchForUpdate();
goappWatchForInstallable();
goappInitWebAssembly();

// -----------------------------------------------------------------------------
// Service Worker
Expand Down Expand Up @@ -185,101 +182,3 @@ function goappKeepBodyClean() {

return () => mutationObserver.disconnect();
}

// -----------------------------------------------------------------------------
// Web Assembly
// -----------------------------------------------------------------------------
async function goappInitWebAssembly() {
if (!goappCanLoadWebAssembly()) {
document.getElementById("app-wasm-loader").style.display = "none";
return;
}

let instantiateStreaming = WebAssembly.instantiateStreaming;
if (!instantiateStreaming) {
instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

const loaderIcon = document.getElementById("app-wasm-loader-icon");
const loaderLabel = document.getElementById("app-wasm-loader-label");

try {
const showProgress = (progress) => {
loaderLabel.innerText = goappLoadingLabel.replace("{progress}", progress);
};
showProgress(0);

const go = new Go();
const wasm = await instantiateStreaming(
fetchWithProgress("{{.Wasm}}", showProgress),
go.importObject
);

go.run(wasm.instance);
} catch (err) {
loaderIcon.className = "goapp-logo";
loaderLabel.innerText = err;
console.error("loading wasm failed: ", err);
}
}

function goappCanLoadWebAssembly() {
return !/bot|googlebot|crawler|spider|robot|crawling/i.test(
navigator.userAgent
);
}

async function fetchWithProgress(url, progess) {
const response = await fetch(url);

let contentLength;
try {
contentLength = response.headers.get(goappWasmContentLengthHeader);
} catch {}
if (!goappWasmContentLengthHeader || !contentLength) {
contentLength = response.headers.get("Content-Length");
}

const total = parseInt(contentLength, 10);
let loaded = 0;

const progressHandler = function (loaded, total) {
progess(Math.round((loaded * 100) / total));
};

var res = new Response(
new ReadableStream(
{
async start(controller) {
var reader = response.body.getReader();
for (;;) {
var { done, value } = await reader.read();

if (done) {
progressHandler(total, total);
break;
}

loaded += value.byteLength;
progressHandler(loaded, total);
controller.enqueue(value);
}
controller.close();
},
},
{
status: response.status,
statusText: response.statusText,
}
)
);

for (var pair of response.headers.entries()) {
res.headers.set(pair[0], pair[1]);
}

return res;
}
102 changes: 102 additions & 0 deletions pkg/app/gen/driver-wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// -----------------------------------------------------------------------------
// Web Assembly
// -----------------------------------------------------------------------------
const goappLoadingLabel = "{{.LoadingLabel}}";
const goappWasmContentLengthHeader = "{{.WasmContentLengthHeader}}";

goappInitWebAssembly();

async function goappInitWebAssembly() {
if (!goappCanLoadWebAssembly()) {
document.getElementById("app-wasm-loader").style.display = "none";
return;
}

let instantiateStreaming = WebAssembly.instantiateStreaming;
if (!instantiateStreaming) {
instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

const loaderIcon = document.getElementById("app-wasm-loader-icon");
const loaderLabel = document.getElementById("app-wasm-loader-label");

try {
const showProgress = (progress) => {
loaderLabel.innerText = goappLoadingLabel.replace("{progress}", progress);
};
showProgress(0);

const go = new Go();
const wasm = await instantiateStreaming(
fetchWithProgress("{{.Wasm}}", showProgress),
go.importObject
);

go.run(wasm.instance);
} catch (err) {
loaderIcon.className = "goapp-logo";
loaderLabel.innerText = err;
console.error("loading wasm failed: ", err);
}
}

function goappCanLoadWebAssembly() {
return !/bot|googlebot|crawler|spider|robot|crawling/i.test(
navigator.userAgent
);
}

async function fetchWithProgress(url, progess) {
const response = await fetch(url);

let contentLength;
try {
contentLength = response.headers.get(goappWasmContentLengthHeader);
} catch {}
if (!goappWasmContentLengthHeader || !contentLength) {
contentLength = response.headers.get("Content-Length");
}

const total = parseInt(contentLength, 10);
let loaded = 0;

const progressHandler = function (loaded, total) {
progess(Math.round((loaded * 100) / total));
};

var res = new Response(
new ReadableStream(
{
async start(controller) {
var reader = response.body.getReader();
for (;;) {
var { done, value } = await reader.read();

if (done) {
progressHandler(total, total);
break;
}

loaded += value.byteLength;
progressHandler(loaded, total);
controller.enqueue(value);
}
controller.close();
},
},
{
status: response.status,
statusText: response.statusText,
}
)
);

for (var pair of response.headers.entries()) {
res.headers.set(pair[0], pair[1]);
}

return res;
}
6 changes: 5 additions & 1 deletion pkg/app/gen/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
}
defer f.Close()

fmt.Fprintln(f, "//go:build !wasm")
fmt.Fprintln(f, "//go:build !js")
fmt.Fprintln(f)
fmt.Fprintln(f, "package app")
fmt.Fprintln(f)
Expand All @@ -44,6 +44,10 @@ func main() {
"wasm_exec.js",
),
},
{
Var: "wasmDriverJS",
Filename: "gen/driver-wasm.js",
},
{
Var: "appJS",
Filename: "gen/app.js",
Expand Down
Loading