From c01cb5014b6c479855d2fc88978d8755d8027e1f Mon Sep 17 00:00:00 2001 From: James Yu Date: Fri, 17 Nov 2023 14:36:18 +0000 Subject: [PATCH] Clean up cacher handlers in build.ts --- src/compile/build.ts | 8 ++++---- src/compile/{build-external.ts => external.ts} | 0 src/compile/{build-recipe.ts => recipe.ts} | 6 +----- src/core/cacherlib/watcher.ts | 13 +++++++++++++ src/core/events.ts | 11 ++++++++++- 5 files changed, 28 insertions(+), 10 deletions(-) rename src/compile/{build-external.ts => external.ts} (100%) rename src/compile/{build-recipe.ts => recipe.ts} (98%) diff --git a/src/compile/build.ts b/src/compile/build.ts index cdc68eb82d..4b37109e4c 100644 --- a/src/compile/build.ts +++ b/src/compile/build.ts @@ -7,8 +7,8 @@ import { AutoBuildInitiated, AutoCleaned, BuildDone } from '../core/event-bus' import { rootFile as pickRootFile } from '../utils/quick-pick' import { getLogger } from '../utils/logging/logger' import { parser } from '../parse/parser' -import { BIB_MAGIC_PROGRAM_NAME, MAGIC_PROGRAM_ARGS_SUFFIX, MAX_PRINT_LINE, TEX_MAGIC_PROGRAM_NAME, build as buildRecipe } from './build-recipe' -import { build as buildExternal } from './build-external' +import { BIB_MAGIC_PROGRAM_NAME, MAGIC_PROGRAM_ARGS_SUFFIX, MAX_PRINT_LINE, TEX_MAGIC_PROGRAM_NAME, build as buildRecipe } from './recipe' +import { build as buildExternal } from './external' import { queue } from './queue' const logger = getLogger('Build') @@ -18,8 +18,8 @@ events.onToBuild(() => { state.compile.compiling = true }) events.onDidBuild(() => { state.compile.compiling = false }) -lw.cacher.src.onChange(filePath => autoBuild(filePath, 'onFileChange')) -lw.cacher.bib.onChange(filePath => autoBuild(filePath, 'onFileChange', true)) +events.onDidTeXChange(filePath => autoBuild(filePath, 'onFileChange')) +events.onDidBibChange(filePath => autoBuild(filePath, 'onFileChange', true)) export function autoBuild(file: string, type: 'onFileChange' | 'onSave', bibChanged: boolean = false) { const configuration = vscode.workspace.getConfiguration('latex-workshop', vscode.Uri.file(file)) diff --git a/src/compile/build-external.ts b/src/compile/external.ts similarity index 100% rename from src/compile/build-external.ts rename to src/compile/external.ts diff --git a/src/compile/build-recipe.ts b/src/compile/recipe.ts similarity index 98% rename from src/compile/build-recipe.ts rename to src/compile/recipe.ts index 350a17959b..5751bba93e 100644 --- a/src/compile/build-recipe.ts +++ b/src/compile/recipe.ts @@ -3,7 +3,7 @@ import path from 'path' import fs from 'fs' import * as cp from 'child_process' import * as lw from '../lw' -import { events, state } from '../core' +import { state } from '../core' import { getLogger } from '../utils/logging/logger' import { replaceArgumentPlaceholders } from '../utils/utils' import { queue } from './queue' @@ -32,8 +32,6 @@ export const MAX_PRINT_LINE = '10000' export async function build(rootFile: string, langId: string, buildLoop: () => Promise, recipeName?: string) { logger.log(`Build root file ${rootFile}`) - events.onToBuildHandlers.forEach(handler => handler()) - await vscode.workspace.saveAll() createOutputSubFolders(rootFile) @@ -52,8 +50,6 @@ export async function build(rootFile: string, langId: string, buildLoop: () => P state.compile.compiledPDFPath = lw.manager.tex2pdf(rootFile) await buildLoop() - - events.onDidBuildHandlers.forEach(handler => handler()) } /** diff --git a/src/core/cacherlib/watcher.ts b/src/core/cacherlib/watcher.ts index 005b59598f..c5adc5404b 100644 --- a/src/core/cacherlib/watcher.ts +++ b/src/core/cacherlib/watcher.ts @@ -5,6 +5,7 @@ import * as lw from '../../lw' import * as eventbus from '../event-bus' import { getLogger } from '../../utils/logging/logger' import { isBinary } from '../root-file' +import { events } from '..' const logger = getLogger('Cacher', 'Watcher') @@ -29,6 +30,17 @@ export class Watcher { this.onDeleteHandlers.add(handler) } + private getHandlersFromEvents() { + if (this.fileExt === '.*') { + return events.onDidTeXChangeHandlers + } else if (this.fileExt === '.bib') { + return events.onDidBibChangeHandlers + } else if (this.fileExt === '.pdf') { + return events.onDidPDFChangeHandlers + } + return [] + } + private createWatcher(globPattern: vscode.GlobPattern): vscode.FileSystemWatcher { const watcher = vscode.workspace.createFileSystemWatcher(globPattern) watcher.onDidCreate((uri: vscode.Uri) => this.onDidChange('create', uri)) @@ -59,6 +71,7 @@ export class Watcher { if (!isBinary(path.extname(uri.fsPath))) { logger.log(`"${event}" emitted on ${uri.fsPath} .`) this.onChangeHandlers.forEach(handler => handler(uri.fsPath)) + this.getHandlersFromEvents().forEach(handler => handler(uri.fsPath)) lw.eventBus.fire(eventbus.FileChanged, uri.fsPath) return } diff --git a/src/core/events.ts b/src/core/events.ts index a738b0a8e3..001a888a77 100644 --- a/src/core/events.ts +++ b/src/core/events.ts @@ -1,11 +1,20 @@ type onToBuildHandler = () => void type onDidBuildHandler = () => void +type onDidTeXChangeHandler = (filePath: string) => void +type onDidBibChangeHandler = (filePath: string) => void +type onDidPDFChangeHandler = (filePath: string) => void export const events = { onToBuildHandlers: [] as onToBuildHandler[], onDidBuildHandlers: [] as onDidBuildHandler[], + onDidTeXChangeHandlers: [] as onDidTeXChangeHandler[], + onDidBibChangeHandlers: [] as onDidBibChangeHandler[], + onDidPDFChangeHandlers: [] as onDidPDFChangeHandler[], onToBuild: (handler: onToBuildHandler) => addHandler(events.onToBuildHandlers, handler), - onDidBuild: (handler: onDidBuildHandler) => addHandler(events.onDidBuildHandlers, handler) + onDidBuild: (handler: onDidBuildHandler) => addHandler(events.onDidBuildHandlers, handler), + onDidTeXChange: (handler: onDidTeXChangeHandler) => addHandler(events.onDidTeXChangeHandlers, handler), + onDidBibChange: (handler: onDidBibChangeHandler) => addHandler(events.onDidBibChangeHandlers, handler), + onDidPDFChange: (handler: onDidPDFChangeHandler) => addHandler(events.onDidPDFChangeHandlers, handler), } function addHandler(handlers: T[], handler: T): Disposable {