-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9e53f8
commit 761c6ab
Showing
31 changed files
with
3,640 additions
and
28,169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. | ||
* | ||
* Camunda licenses this file to you under the MIT; you may not use this file | ||
* except in compliance with the MIT License. | ||
*/ | ||
|
||
const { EventEmitter } = require('node:events'); | ||
|
||
const { | ||
toFileUrl | ||
} = require('./util'); | ||
|
||
const Indexer = require('./indexer.js'); | ||
const Processor = require('./processor.js'); | ||
const Watcher = require('./watcher.js'); | ||
const Workqueue = require('./workqueue.js'); | ||
|
||
/** | ||
* @typedef { import('./types').Processor } Processor | ||
* @typedef { { | ||
* watch: boolean; | ||
* processors: Processor[]; | ||
* } } FileContextOptions | ||
*/ | ||
|
||
const DEFAULT_PROCESSORS = require('./processors'); | ||
|
||
const DEFAULT_OPTIONS = { | ||
watch: true, | ||
processors: DEFAULT_PROCESSORS | ||
}; | ||
|
||
/** | ||
* File context that indexes and processes files. | ||
*/ | ||
module.exports = class FileContext extends EventEmitter { | ||
|
||
/** | ||
* @param {import('./types.js').Logger} logger | ||
* @param {FileContextOptions} [options] | ||
*/ | ||
constructor(logger, options = DEFAULT_OPTIONS) { | ||
super(); | ||
|
||
const { processors } = options; | ||
|
||
this._logger = logger; | ||
|
||
this._workqueue = new Workqueue(this); | ||
this._processor = new Processor(logger, processors); | ||
this._indexer = new Indexer(logger, this, this._processor, this._workqueue); | ||
|
||
this._init(options); | ||
} | ||
|
||
/** | ||
* Add root | ||
* | ||
* @param {string} uri | ||
*/ | ||
addRoot(uri) { | ||
return this._indexer.addRoot(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* Remove root | ||
* | ||
* @param {string} uri | ||
*/ | ||
removeRoot(uri) { | ||
return this._indexer.removeRoot(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* Add file | ||
* | ||
* @param {string} uri | ||
*/ | ||
addFile(uri) { | ||
return this._indexer.add(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* Notify file changed | ||
* | ||
* @param {string} uri | ||
*/ | ||
updateFile(uri) { | ||
return this.addFile(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* Remove file | ||
* | ||
* @param {string} uri | ||
*/ | ||
removeFile(uri) { | ||
return this._indexer.remove(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* Notify file opened | ||
* | ||
* @param { { uri: string, value: string } } fileProps | ||
*/ | ||
fileOpened(fileProps) { | ||
return this._indexer.fileOpened(fileProps); | ||
} | ||
|
||
/** | ||
* Notify file content changed | ||
* | ||
* @param { { uri: string, value: string } } fileProps | ||
*/ | ||
fileContentChanged(fileProps) { | ||
return this._indexer.fileContentChanged(fileProps); | ||
} | ||
|
||
/** | ||
* Notify file closed | ||
* | ||
* @param {string} uri | ||
*/ | ||
fileClosed(uri) { | ||
return this._indexer.fileClosed(toFileUrl(uri)); | ||
} | ||
|
||
/** | ||
* @return { Promise<Void> } | ||
*/ | ||
close() { | ||
if (this._watcher) { | ||
return this._watcher.close(); | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* @param {FileContextOptions} options | ||
*/ | ||
_init(options = {}) { | ||
const { | ||
processors, | ||
watch = true | ||
} = options; | ||
|
||
if (watch) { | ||
this._watcher = new Watcher(this._logger, this, processors); | ||
|
||
this.once('watcher:ready', () => { | ||
this.once('workqueue:empty', () => this.emit('ready')); | ||
}); | ||
} else { | ||
this.once('workqueue:empty', () => this.emit('ready')); | ||
} | ||
} | ||
|
||
}; |
Oops, something went wrong.