monorepo/kifu/pwa/src/main.ts

63 lines
1.6 KiB
TypeScript

import { GoBoard } from "./components/Board";
import { CoreRequest, CoreResponse } from "core-types";
import { CoreApi, initCore } from "./coreApi";
// import { assertNever } from "./assertNever";
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);
};
main();