import { GoBoard } from "./components/Board"; import { CoreRequest, CoreResponse } from "core-types"; import { CoreApi, initCore } from "./coreApi"; import { assertNever } from "./assertNever"; /* const processResponse = (response: CoreResponse) => { const root = document.getElementById("root"); if (!root) { return; } }; */ class UIState { private currentView: GoBoard | null; private rootElement: HTMLElement; coreApi: CoreApi; constructor(coreApi: CoreApi, root: HTMLElement) { this.currentView = null; this.rootElement = root; this.coreApi = coreApi; if (!root) { console.log("root element not found"); return; } } processResponse(response: CoreResponse) { switch (response.type) { case "PlayingFieldView": if (this.currentView) { this.currentView.setBoard(response.content.board); } else { this.currentView = new GoBoard({ board: response.content.board, onClick: async (request: CoreRequest) => { const response = await this.coreApi.dispatch(request); this.processResponse(response); }, }); this.rootElement?.appendChild(this.currentView.canvas); this.currentView.renderBoard(); } break; default: console.log("impossible branch: ", response); alert("impossible branch"); // assertNever(response); break; } } } const main = async () => { let coreApi = await initCore(); let response = await coreApi.dispatch({ type: "PlayingField" }); const root = document.getElementById("root"); if (!root) { console.log("root element not present"); return; } const uiState = new UIState(coreApi, root); uiState.processResponse(response); /* console.log("playing field response: ", response); if (!root) { alert("could not retrieve the app root container"); return; } const board = new GoBoard({ board: response.content.board, onClick: async (request: CoreRequest) => { const response = await coreApi.dispatch(request); }, }); root.appendChild(board.canvas); console.log("constructed board: ", board); board.renderBoard(); */ }; main();