monorepo/kifu/pwa/src/main.ts

63 lines
1.6 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";
2023-05-16 14:21:57 +00:00
// 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;
}
}
}
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
};
main();