Skip to content

Commit

Permalink
refactor: use typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Jun 2, 2024
1 parent e9c12f6 commit e36db43
Show file tree
Hide file tree
Showing 72 changed files with 3,528 additions and 3,154 deletions.
File renamed without changes.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
"type": "module",
"private": true,
"scripts": {
"build": "tsc -b --verbose",
"clean": "rm -rf dist",
"lint": "eslint .",
"start": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"@opentelemetry/instrumentation/hook.mjs\", pathToFileURL(\"./\"));' --import ./instrument.mjs server.js",
"start": "node --openssl-legacy-provider --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"@opentelemetry/instrumentation/hook.mjs\", pathToFileURL(\"./\"));' --import ./instrument.js server.js",
"test": "vitest run"
},
"keywords": [],
"author": "",
"license": "LGPL-3.0-or-later",
"devDependencies": {
"@eslint/js": "^9.3.0",
"@types/express": "^4.17.21",
"@types/node": "^20.12.12",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^9.3.0",
"globals": "^15.3.0",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"packageManager": "[email protected]+sha256.30a1801ac4e723779efed13a21f4c39f9eb6c9fbb4ced101bce06b422593d7c9",
Expand All @@ -27,6 +31,7 @@
"@sentry/node": "^8.7.0",
"@sentry/profiling-node": "^8.7.0",
"express": "^4.19.2",
"obsidian-main": "link:packages/main"
"obsidian-main": "link:packages/main",
"tsx": "^4.11.0"
}
}
12 changes: 0 additions & 12 deletions packages/main/jsconfig.json

This file was deleted.

40 changes: 40 additions & 0 deletions packages/main/src/MainLoop.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference types="node" resolution-mode="require"/>
import { KeypressEvent, TTask } from "./types.js";
export declare class MainLoop {
/** @type {NodeJS.Timeout | undefined} */
_timer: NodeJS.Timeout | undefined;
/** @type {Array<import("obsidian-main").Task>} */
_startTasks: Array<TTask>;
/** @type {Array<import("obsidian-main").Task>} */
_stopTasks: Array<TTask>;
/** @type {Array<import("obsidian-main").Task>} */
_loopTasks: Array<TTask>;
/**
*
* @param {import("obsidian-main").KeypressEvent} key
*/
handleKeypressEvent(key: KeypressEvent): void;
/**
*
* @param {"start" | "loop" | "stop"} type
* @param {import("obsidian-main").Task} task
*/
addTask(type: "start" | "loop" | "stop", task: TTask): void;
/**
* @param {Array<import("obsidian-main").Task>} tasks
*/
_callTasks(tasks: Array<TTask>): Promise<void>;
/**
* Starts the main loop.
*
*/
start(): Promise<void>;
/**
* Stops the main loop.
*/
stop(): Promise<void>;
/**
* Body of the main loop.
*/
loop(): Promise<void>;
}
167 changes: 78 additions & 89 deletions packages/main/src/MainLoop.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/main/src/MainLoop.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions packages/main/src/MainLoop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// obsidian-spoon is a game server
// Copyright (C) 2024 Molly Crendraven

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { emitKeypressEvents } from "node:readline";
import { _atExit } from "obsidian-main";
import { KeypressEvent, TTask } from "./types.js";

export class MainLoop {
/** @type {NodeJS.Timeout | undefined} */
_timer: NodeJS.Timeout | undefined = undefined;

/** @type {Array<import("obsidian-main").Task>} */
_startTasks: Array<TTask> = [];

/** @type {Array<import("obsidian-main").Task>} */
_stopTasks: Array<TTask> = [];

/** @type {Array<import("obsidian-main").Task>} */
_loopTasks: Array<TTask> = [];

/**
*
* @param {import("obsidian-main").KeypressEvent} key
*/
handleKeypressEvent(key: KeypressEvent) {
const keyString = key.sequence;

if (keyString === "x") {
this.stop();
}
}
/**
*
* @param {"start" | "loop" | "stop"} type
* @param {import("obsidian-main").Task} task
*/
addTask(type: "start" | "loop" | "stop", task: TTask) {
if (type === "start") {
this._startTasks.push(task);
} else if (type === "stop") {
this._stopTasks.push(task);
} else if (type === "loop") {
this._loopTasks.push(task);
}
}

/**
* @param {Array<import("obsidian-main").Task>} tasks
*/
async _callTasks(tasks: Array<TTask>) {
tasks.forEach(async (task) => {
await task();
});
}

/**
* Starts the main loop.
*
*/
async start() {
this._timer = setTimeout(this.loop.bind(this), 1000);
if (process.stdin.isTTY !== true) {
return;
}
emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.resume();
console.log("Press X to exit");
process.stdin.on("keypress", (str, key) => {
if (key !== undefined) {
this.handleKeypressEvent(key);
}
});
await this._callTasks(this._startTasks);
}

/**
* Stops the main loop.
*/
async stop() {
if (this._timer !== undefined) {
clearInterval(this._timer);
process.stdin.setRawMode(false);
console.log("Exiting...");
await this._callTasks(this._stopTasks);
_atExit();
}
}

/**
* Body of the main loop.
*/
async loop() {
await this._callTasks(this._loopTasks);
this._timer = setTimeout(this.loop.bind(this), 1000);
}
}
Loading

0 comments on commit e36db43

Please sign in to comment.