modaq
Version:
Quiz Bowl Reader using TypeScript, React, and MobX
149 lines • 6.63 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppState = void 0;
const utilities_1 = require("@fluentui/utilities");
const mobx_1 = require("mobx");
require("../IStatus");
require("./CustomExport");
const CustomExport = __importStar(require("./CustomExport"));
const QBJ = __importStar(require("../qbj/QBJ"));
const GameState_1 = require("./GameState");
const UIState_1 = require("./UIState");
const StatusDisplayType_1 = require("./StatusDisplayType");
const minimumIntervalInMs = 5000;
class AppState {
constructor() {
Object.defineProperty(this, "game", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "uiState", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
mobx_1.makeAutoObservable(this);
this.game = new GameState_1.GameState();
this.uiState = new UIState_1.UIState();
}
// Only use in tests
static resetInstance() {
this.instance = new AppState();
}
// Could do a version with callbacks. There are 4 places this gets called from, and 3 use the same callback
// Could also just do a bool, and pass in a different value (e.g. enum) if we want more flexibility in the future
handleCustomExport(displayType, source) {
var _a;
// Custom export must be defined and we must have an existing game
if (this.uiState == undefined ||
this.uiState.customExportOptions == undefined ||
this.game.cycles.length === 0) {
return Promise.resolve();
}
const customExport = this.uiState.customExportOptions;
let exportPromise;
switch (customExport.type) {
case "Raw":
exportPromise = customExport.onExport(CustomExport.convertGameToExportFields(this.game), { source });
break;
case "QBJ":
exportPromise = customExport.onExport(QBJ.toQBJ(this.game, (_a = this.game.packet.name) !== null && _a !== void 0 ? _a : this.uiState.packetFilename), { source });
break;
default:
utilities_1.assertNever(customExport);
}
return exportPromise
.then((status) => {
if (status.isError) {
switch (displayType) {
case StatusDisplayType_1.StatusDisplayType.MessageDialog:
this.uiState.dialogState.showOKMessageDialog("Export Error", `Export failed: ${status.status}.`);
break;
case StatusDisplayType_1.StatusDisplayType.Label:
this.uiState.setCustomExportStatus(`Export failed: ${status.status}.`);
break;
default:
utilities_1.assertNever(displayType);
}
}
else {
this.game.markUpdateComplete();
switch (displayType) {
case StatusDisplayType_1.StatusDisplayType.MessageDialog:
this.uiState.dialogState.showOKMessageDialog("Export Succeeded", "Export succeeded.");
break;
case StatusDisplayType_1.StatusDisplayType.Label:
this.uiState.setCustomExportStatus("Export successful.");
break;
default:
utilities_1.assertNever(displayType);
}
}
})
.catch((e) => {
const message = e.message ? e.message : JSON.stringify(e);
switch (displayType) {
case StatusDisplayType_1.StatusDisplayType.MessageDialog:
this.uiState.dialogState.showOKMessageDialog("Export Error", `Error in exporting the game. Hit an exception. Exception message: ${message}`);
break;
case StatusDisplayType_1.StatusDisplayType.Label:
this.uiState.setCustomExportStatus(`Error in exporting the game. Hit an exception. Exception message: ${message}`);
break;
default:
utilities_1.assertNever(displayType);
}
});
}
// Need to think if we want this state in UIState or here. Odd to have the interval live elsewhere
setCustomExportInterval(interval) {
clearInterval(this.uiState.customExportIntervalId);
if (interval == undefined) {
return;
}
if (interval < minimumIntervalInMs) {
interval = minimumIntervalInMs;
}
const newIntervalId = setInterval(() => {
// Only export if the game has changes. We do this check here instead of handleCustomExport because the
// user should be able to explicitly export multiple times (to create new files, for example), but that doesn't
// make sense to do for something running from the timer.
if (this.game.hasUpdates) {
this.handleCustomExport(StatusDisplayType_1.StatusDisplayType.Label, "Timer");
}
}, interval);
this.uiState.setCustomExportIntervalId(newIntervalId);
}
setGame(game) {
this.game = game;
}
}
exports.AppState = AppState;
Object.defineProperty(AppState, "instance", {
enumerable: true,
configurable: true,
writable: true,
value: new AppState()
});
//# sourceMappingURL=AppState.js.map