Skip to content

Commit

Permalink
Isolate watcher.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Nov 20, 2023
1 parent 7a07f29 commit 6db13d9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class Builder {
private readonly MAX_PRINT_LINE = '10000'

constructor() {
lw.cacher.src.onChange(filePath => this.buildOnFileChanged(filePath))
lw.cacher.bib.onChange(filePath => this.buildOnFileChanged(filePath, true))
lw.watcher.src.onChange(filePath => this.buildOnFileChanged(filePath))
lw.watcher.bib.onChange(filePath => this.buildOnFileChanged(filePath, true))
// Check if pdflatex is available, and is MikTeX distro
try {
const pdflatexVersion = cp.execSync('pdflatex --version')
Expand Down
6 changes: 3 additions & 3 deletions src/completion/completer/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export class Citation implements IProvider {
private readonly bibEntries = new Map<string, CiteSuggestion[]>()

constructor() {
lw.cacher.bib.onCreate(filePath => this.parseBibFile(filePath))
lw.cacher.bib.onChange(filePath => this.parseBibFile(filePath))
lw.cacher.bib.onDelete(filePath => this.removeEntriesInFile(filePath))
lw.watcher.bib.onCreate(filePath => this.parseBibFile(filePath))
lw.watcher.bib.onChange(filePath => this.parseBibFile(filePath))
lw.watcher.bib.onDelete(filePath => this.removeEntriesInFile(filePath))
}

provideFrom(_result: RegExpMatchArray, args: IProviderArgs) {
Expand Down
34 changes: 15 additions & 19 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { ICompletionItem } from '../completion/latex'
import { InputFileRegExp } from '../utils/inputfilepath'
import * as CacherUtils from './cacherlib/cacherutils'
import * as PathUtils from './cacherlib/pathutils'
import { Watcher } from './cacherlib/watcher'
import { getLogger } from '../utils/logging/logger'
import { parser } from '../parse/parser'
import { performance } from 'perf_hooks'
Expand Down Expand Up @@ -59,19 +58,16 @@ export interface Cache {

export class Cacher {
private readonly caches: {[filePath: string]: Cache} = {}
readonly src: Watcher = new Watcher()
readonly pdf: Watcher = new Watcher('.pdf')
readonly bib: Watcher = new Watcher('.bib')
private caching = 0
private promises: {[filePath: string]: Promise<void>} = {}

constructor() {
this.src.onChange((filePath: string) => {
lw.watcher.src.onChange((filePath: string) => {
if (CacherUtils.canCache(filePath)) {
void this.refreshCache(filePath)
}
})
this.src.onDelete((filePath: string) => {
lw.watcher.src.onDelete((filePath: string) => {
if (filePath in this.caches) {
delete this.caches[filePath]
logger.log(`Removed ${filePath} .`)
Expand All @@ -84,9 +80,9 @@ export class Cacher {
logger.log(`Ignored ${filePath} .`)
return
}
if (!this.src.has(filePath)) {
if (!lw.watcher.src.has(filePath)) {
logger.log(`Adding ${filePath} .`)
this.src.add(filePath)
lw.watcher.src.add(filePath)
}
}

Expand Down Expand Up @@ -127,9 +123,9 @@ export class Cacher {
}

reset() {
this.src.reset()
this.bib.reset()
this.pdf.reset()
lw.watcher.src.reset()
lw.watcher.bib.reset()
lw.watcher.pdf.reset()
Object.keys(this.caches).forEach(filePath => delete this.caches[filePath])
}

Expand Down Expand Up @@ -204,7 +200,7 @@ export class Cacher {
})
logger.log(`Input ${result.path} from ${cache.filePath} .`)

if (this.src.has(result.path)) {
if (lw.watcher.src.has(result.path)) {
continue
}
this.add(result.path)
Expand Down Expand Up @@ -232,7 +228,7 @@ export class Cacher {
logger.log(`External document ${externalPath} from ${cache.filePath} .` +
(result[1] ? ` Prefix is ${result[1]}`: ''))

if (this.src.has(externalPath)) {
if (lw.watcher.src.has(externalPath)) {
continue
}
this.add(externalPath)
Expand Down Expand Up @@ -270,8 +266,8 @@ export class Cacher {
for (const bibPath of bibPaths) {
cache.bibfiles.add(bibPath)
logger.log(`Bib ${bibPath} from ${cache.filePath} .`)
if (!this.bib.has(bibPath)) {
this.bib.add(bibPath)
if (!lw.watcher.bib.has(bibPath)) {
lw.watcher.bib.add(bibPath)
}
}
}
Expand Down Expand Up @@ -306,7 +302,7 @@ export class Cacher {
!fs.existsSync(inputFile)) {
continue
}
if (inputFile === filePath || this.src.has(inputFile)) {
if (inputFile === filePath || lw.watcher.src.has(inputFile)) {
// Drop the current rootFile often listed as INPUT
// Drop any file that is already watched as it is handled by
// onWatchedFileChange.
Expand All @@ -330,7 +326,7 @@ export class Cacher {
} else {
logger.log(`Cache not finished on ${filePath} when parsing fls.`)
}
} else if (!this.src.has(inputFile)) {
} else if (!lw.watcher.src.has(inputFile)) {
// Watch non-tex files.
this.add(inputFile)
}
Expand Down Expand Up @@ -364,8 +360,8 @@ export class Cacher {
this.get(lw.manager.rootFile)?.bibfiles.add(bibPath)
logger.log(`Found .bib ${bibPath} from .aux ${filePath} .`)
}
if (!this.bib.has(bibPath)) {
this.bib.add(bibPath)
if (!lw.watcher.bib.has(bibPath)) {
lw.watcher.bib.add(bibPath)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/root-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Manager {

constructor() {
this.registerSetEnvVar()
lw.cacher.src.onDelete(filePath => {
lw.watcher.src.onDelete(filePath => {
if (filePath === this.rootFile) {
this.rootFile = undefined
void this.findRoot()
Expand Down Expand Up @@ -304,7 +304,7 @@ export class Manager {
// We also clean the completions from the old project
lw.completer.input.reset()
lw.dupLabelDetector.reset()
lw.cacher.src.reset()
lw.watcher.src.reset()
lw.cacher.add(rootFile)
void lw.cacher.refreshCache(rootFile).then(async () => {
// We need to parse the fls to discover file dependencies when defined by TeX macro
Expand Down
18 changes: 12 additions & 6 deletions src/core/cacherlib/watcher.ts → src/core/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as vscode from 'vscode'
import * as fs from 'fs'
import * as path from 'path'
import { lw } from '../../lw'
import * as eventbus from '../event-bus'
import { getLogger } from '../../utils/logging/logger'
import { isBinary } from '../root-file'
import { lw } from '../lw'
import * as eventbus from './event-bus'
import { getLogger } from '../utils/logging/logger'
import { isBinary } from './root-file'

const logger = getLogger('Cacher', 'Watcher')

export class Watcher {
class Watcher {
private readonly watchers: {[folder: string]: {watcher: vscode.FileSystemWatcher, files: Set<string>}} = {}
private readonly onCreateHandlers: Set<(filePath: string) => void> = new Set()
private readonly onChangeHandlers: Set<(filePath: string) => void> = new Set()
private readonly onDeleteHandlers: Set<(filePath: string) => void> = new Set()
private readonly polling: {[filePath: string]: {time: number, size: number}} = {}

constructor(private readonly fileExt: string = '.*') {}
constructor(private readonly fileExt: '.*' | '.bib' | '.pdf' = '.*') {}

onCreate(handler: (filePath: string) => void) {
this.onCreateHandlers.add(handler)
Expand Down Expand Up @@ -130,3 +130,9 @@ export class Watcher {
logger.log('Reset.')
}
}

export const watcher = {
src: new Watcher(),
pdf: new Watcher('.pdf'),
bib: new Watcher('.bib')
}
3 changes: 3 additions & 0 deletions src/lw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as vscode from 'vscode'
import type { watcher } from './core/watcher'

import type { Builder } from './compile/build'
import type { Cacher } from './core/cache'
import type { Cleaner } from './extras/cleaner'
Expand Down Expand Up @@ -29,6 +31,7 @@ import type * as commands from './core/commands'
export const lw = {
extensionContext: Object.create(null) as vscode.ExtensionContext,
extensionRoot: '',
watcher: {} as typeof watcher,
eventBus: Object.create(null) as EventBus,
configuration: Object.create(null) as Configuration,
lwfs: Object.create(null) as LwFileSystem,
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as vscode from 'vscode'
import * as path from 'path'
import { lw, registerDisposable } from './lw'

import { watcher } from './core/watcher'
lw.watcher = watcher

import { pdfViewerHookProvider, pdfViewerPanelSerializer } from './preview/viewer'
import { MathPreviewPanelSerializer } from './extras/math-preview-panel'
import { BibtexCompleter } from './completion/bibtex'
Expand Down
4 changes: 2 additions & 2 deletions src/preview/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export { pdfViewerPanelSerializer } from './viewerlib/pdfviewerpanel'

export class Viewer {
constructor() {
lw.cacher.pdf.onChange(pdfPath => {
lw.watcher.pdf.onChange(pdfPath => {
if (lw.builder.isOutputPDF(pdfPath)) {
this.refreshExistingViewer(pdfPath)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export class Viewer {
}
const pdfFileUri = vscode.Uri.file(pdfFile)
viewerManager.createClientSet(pdfFileUri)
lw.cacher.pdf.add(pdfFileUri.fsPath)
lw.watcher.pdf.add(pdfFileUri.fsPath)
try {
logger.log(`Serving PDF file at ${url}`)
await vscode.env.openExternal(vscode.Uri.parse(url, true))
Expand Down
2 changes: 1 addition & 1 deletion src/preview/viewerlib/pdfviewermanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PdfViewerManager {

initiatePdfViewerPanel(pdfPanel: PdfViewerPanel): PdfViewerPanel | undefined {
const pdfFileUri = pdfPanel.pdfFileUri
lw.cacher.pdf.add(pdfFileUri.fsPath)
lw.watcher.pdf.add(pdfFileUri.fsPath)
this.createClientSet(pdfFileUri)
const panelSet = this.getPanelSet(pdfFileUri)
if (!panelSet) {
Expand Down

0 comments on commit 6db13d9

Please sign in to comment.