Skip to content

Commit

Permalink
Clean up cacher handlers in build.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Nov 17, 2023
1 parent a38d783 commit c01cb50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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))
Expand Down
File renamed without changes.
6 changes: 1 addition & 5 deletions src/compile/build-recipe.ts → src/compile/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -32,8 +32,6 @@ export const MAX_PRINT_LINE = '10000'
export async function build(rootFile: string, langId: string, buildLoop: () => Promise<void>, recipeName?: string) {
logger.log(`Build root file ${rootFile}`)

events.onToBuildHandlers.forEach(handler => handler())

await vscode.workspace.saveAll()

createOutputSubFolders(rootFile)
Expand All @@ -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())
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/core/cacherlib/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 10 additions & 1 deletion src/core/events.ts
Original file line number Diff line number Diff line change
@@ -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<T>(handlers: T[], handler: T): Disposable<T> {
Expand Down

0 comments on commit c01cb50

Please sign in to comment.