Fix QueryProcessManager type signatures

This commit is contained in:
Guangcong Luo 2021-01-30 11:35:11 -08:00
parent 0685e88e4f
commit f4abf52f61
8 changed files with 22 additions and 21 deletions

View File

@ -286,6 +286,8 @@
"@typescript-eslint/quotes": "off",
"semi": "off",
"@typescript-eslint/semi": ["error", "always"],
"func-call-spacing": "off",
"@typescript-eslint/func-call-spacing": "error",
// whitespace
"@typescript-eslint/type-annotation-spacing": "error",

View File

@ -102,10 +102,10 @@ interface ProcessWrapper {
}
/** Wraps the process object in the PARENT process. */
export class QueryProcessWrapper implements ProcessWrapper {
export class QueryProcessWrapper<T, U> implements ProcessWrapper {
process: ChildProcess;
taskId: number;
pendingTasks: Map<number, (resp: string) => void>;
pendingTasks: Map<number, (resp: U) => void>;
pendingRelease: Promise<void> | null;
resolveRelease: (() => void) | null;
debug?: string;
@ -149,7 +149,7 @@ export class QueryProcessWrapper implements ProcessWrapper {
return this.pendingTasks.size;
}
query(input: any): Promise<any> {
query(input: T): Promise<U> {
this.taskId++;
const taskId = this.taskId;
this.process.send(`${taskId}\n${JSON.stringify(input)}`);
@ -178,7 +178,7 @@ export class QueryProcessWrapper implements ProcessWrapper {
this.process.disconnect();
for (const resolver of this.pendingTasks.values()) {
// maybe we should track reject functions too...
resolver('');
resolver('' as any);
}
this.pendingTasks.clear();
if (this.resolveRelease) {
@ -500,7 +500,7 @@ export class QueryProcessManager<T = string, U = string> extends ProcessManager
processManagers.push(this);
}
async query(input: T) {
const process = this.acquire() as QueryProcessWrapper;
const process = this.acquire() as QueryProcessWrapper<T, U>;
if (!process) return this._query(input);
@ -518,7 +518,7 @@ export class QueryProcessManager<T = string, U = string> extends ProcessManager
return result;
}
createProcess() {
return new QueryProcessWrapper(this.filename);
return new QueryProcessWrapper<T, U>(this.filename);
}
listen() {
if (this.isParentProcess) return;

View File

@ -1474,7 +1474,7 @@ export const commands: ChatCommands = {
return TeamValidatorAsync.get(format.id).validateTeam(user.battleSettings.team).then(result => {
const matchMessage = (originalFormat === format ? "" : this.tr`The format '${originalFormat.name}' was not found.`);
if (result.charAt(0) === '1') {
if (result.startsWith('1')) {
connection.popup(`${(matchMessage ? matchMessage + "\n\n" : "")}${this.tr`Your team is valid for ${format.name}.`}`);
} else {
connection.popup(`${(matchMessage ? matchMessage + "\n\n" : "")}${this.tr`Your team was rejected for the following reasons:`}\n\n- ${result.slice(1).replace(/\n/g, '\n- ')}`);

View File

@ -2482,7 +2482,7 @@ function runSearch(query: {target: string, cmd: string, canAll: boolean, message
* Process manager
*********************************************************/
export const PM = new QueryProcessManager<AnyObject, AnyObject | null>(module, query => {
export const PM = new QueryProcessManager<AnyObject, AnyObject>(module, query => {
try {
if (Config.debugdexsearchprocesses && process.send) {
process.send('DEBUG\n' + JSON.stringify(query));
@ -2501,7 +2501,7 @@ export const PM = new QueryProcessManager<AnyObject, AnyObject | null>(module, q
case 'learn':
return runLearn(query.target, query.message, query.canAll, query.message);
default:
return null;
throw new Error(`Unrecognized Dexsearch command "${query.cmd}"`);
}
} catch (err) {
Monitor.crashlog(err, 'A search query', query);

View File

@ -85,7 +85,6 @@ function prettifyResults(
const lines = resultArray.length;
let curDate = '';
const resultString = resultArray.map(result => {
if (!result) return '';
const date = new Date(result.time || Date.now());
const entryRoom = result.visualRoomID || result.roomID || 'global';
let [dateString, timestamp] = Chat.toTimestamp(date, {human: true}).split(' ');

View File

@ -171,7 +171,7 @@ class Ladder extends LadderStore {
valResult = await validator.validateTeam(team, {removeNicknames});
}
if (valResult.charAt(0) !== '1') {
if (!valResult.startsWith('1')) {
connection.popup(
`Your team was rejected for the following reasons:\n\n` +
`- ` + valResult.slice(1).replace(/\n/g, `\n- `)
@ -179,7 +179,7 @@ class Ladder extends LadderStore {
return null;
}
const settings = {...user.battleSettings, team: valResult.slice(1) as string};
const settings = {...user.battleSettings, team: valResult.slice(1)};
user.battleSettings.inviteOnly = false;
user.battleSettings.hidden = false;
return new BattleReady(userid, this.formatid, settings, rating, challengeType);

View File

@ -467,23 +467,23 @@ export class Modlog {
}
}
// if I don't do this TypeScript thinks that (ModlogResult | undefined)[] is a function
// and complains about an "nexpected newline between function name and paren"
// even though it's a type not a function...
type ModlogResult = ModlogEntry | undefined;
export const mainModlog = new Modlog(MODLOG_PATH, MODLOG_DB_PATH);
// the ProcessManager only accepts text queries at this time
// SQL support is to be determined
export const PM = new QueryProcessManager<ModlogTextQuery, ModlogResult[]>(module, async data => {
export const PM = new QueryProcessManager<ModlogTextQuery, ModlogEntry[]>(module, async data => {
const {rooms, regexString, maxLines, onlyPunishments} = data;
try {
if (Config.debugmodlogprocesses && process.send) {
process.send('DEBUG\n' + JSON.stringify(data));
}
const results = await mainModlog.runTextSearch(rooms, regexString, maxLines, onlyPunishments);
return results.map((line: string, index: number) => parseModlog(line, results[index + 1]));
const lines = await mainModlog.runTextSearch(rooms, regexString, maxLines, onlyPunishments);
const results = [];
for (const [i, line] of lines.entries()) {
const result = parseModlog(line, lines[i + 1]);
if (result) results.push(result);
}
return results;
} catch (err) {
Monitor.crashlog(err, 'A modlog query', data);
return [];

View File

@ -1436,7 +1436,7 @@ const commands: ChatCommands = {
return;
}
const result = await TeamValidatorAsync.get(tournament.fullFormat).validateTeam(user.battleSettings.team);
if (result.charAt(0) === '1') {
if (result.startsWith('1')) {
connection.popup("Your team is valid for this tournament.");
} else {
const formatName = Dex.getFormat(tournament.baseFormat).name;