1// ===== main.go =====
2//go:build js && wasm
3
4// Package main wasm/term/main.go — the desk and the terminal storefront
5// in one binary.
6//
7// Both pages were about fifty megabytes and all but the same fifty: the
8// desk links pkg/storepane to put a store in a window, and the storefront
9// page is pkg/storepane and nothing else. So one of them contained the
10// other, twice compiled and twice downloaded — and a reader who opened
11// both fetched the Go runtime, xterm-go, websh, tcell and the whole
12// catalog client a second time to get about a megabyte they did not
13// already have.
14//
15// This is both, chosen at startup by what the page offers. It is not a
16// third implementation of either: wasm/desk and wasm/tui still exist and
17// still build, and all three call the same two functions.
18//
19// Joining the OTHER drop-ins to this would cost more than it saves. cart
20// and deco are a megabyte between them and ride on every catalog page;
21// folding them into fifty would put fifty on the page where a shopper
22// looks at diodes. They stay separate, and so does stl2, which is its own
23// module with its own vendor directory by design.
24package main
25
26import (
27 "syscall/js"
28
29 "github.com/0magnet/m2/pkg/deskapp"
30 "github.com/0magnet/m2/pkg/storepane"
31)
32
33// wasmName is stamped by the server's wasm build (-X main.wasmName=…) so
34// log lines identify which binary they came from.
35var wasmName string
36
37func main() {
38 document := js.Global().Get("document")
39 // The element each page is built around, which is how every drop-in
40 // here already decides whether it has anything to do. Waiting for the
41 // DOM first, because before it is parsed neither element exists and
42 // the answer would always be neither.
43 storepane.WaitForDOM(document)
44
45 switch {
46 case document.Call("getElementById", "desktop").Truthy():
47 deskapp.Run(wasmName)
48 default:
49 // RunPage looks for #terminal itself and says so if it is
50 // missing, which is the same thing wasm/tui does on a page that
51 // is neither.
52 storepane.RunPage(wasmName)
53 }
54}
55
56