monorepo/kifu/pwa/src/main.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

import { GoBoard } from "./components/Board";
import { CoreRequest, CoreResponse } from "core-types";
2023-05-11 13:39:31 +00:00
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;
}
}
}
2023-05-10 13:42:02 +00:00
const main = async () => {
2023-05-11 13:39:31 +00:00
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);
/*
2023-05-10 13:42:02 +00:00
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);
},
});
2023-05-14 02:16:24 +00:00
root.appendChild(board.canvas);
console.log("constructed board: ", board);
2023-05-14 02:16:24 +00:00
board.renderBoard();
*/
2023-05-10 13:42:02 +00:00
};
main();