IndexingWorker
Import :
const IndexingWorker = brackets.getModule("worker/IndexingWorker")
worker/IndexingWorker
Phoenix houses a file indexing worker which caches all cacheable files of a project in memory. This module can be used to communicate with the Index and extend it by attaching new js worker scripts to the indexing worker as discussed below. Any extension that works on a large number of files should use the indexing worker cache to free up the main thread of heavy file access.
- Extensions performing large compute tasks should create their own worker and may use easy util methods in worker/WorkerComm to communicate with the web worker.
Extending the indexing worker
You can add your own custom scripts to the indexing worker by following the below example. Suppose you have an extension folder with the following structure:
myExtensionFolder
│ my_worker.js // the script that you need to attach to the web worker
│ main.js
In main.js
extension module, we can import my_worker.js
script into IndexingWorker
by:
Example
let ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
let workerPath = ExtensionUtils.getModulePath(module, "my_worker.js")
IndexingWorker.loadScriptInWorker(workerPath);
Once the worker script is loaded with the above step:
- Phoenix can communicate with worker using the
IndexingWorker
reference in Phoenix. - Worker can communicate with Phoenix with the global
WorkerComm
reference within the Indexing worker. All utility methods in module worker/WorkerComm can be used for worker communication.
A global constant Phoenix.baseURL
is available in the worker context to get the base url from which phoenix was
launched.
NB: You can use all util methods available in worker/WorkerComm
as IndexingWorker
internally uses WorkerComm
to communicate with the underlying worker thread.
worker/IndexingWorker.WorkerComm
To communicate between the IndexingWorker and Phoenix, the following methods are available:
loadScriptInWorker
, execPeer
, setExecHandler
, triggerPeer
and other APIs described
in module worker/WorkerComm
.
The above methods can be used with either IndexingWorker
reference within Phoenix
or the global WorkerComm
reference within the Indexing worker. (See example below.)
See worker/WorkerComm for detailed API docs.
// To Execute a named function `extensionName.sayHello` in the worker from phoenix
// in my_worker.js. It is a good practice to prefix your `[extensionName]`
// to exec handler to prevent name collisions with other extensions.
WorkerComm.setExecHandler("extensionName.sayHello", (arg)=>{
console.log("hello from worker ", arg); // prints "hello from worker phoenix"
return "Hello Phoenix";
});
// In Phoenix/extension
let workerMessage = await IndexingWorker.execPeer("extensionName.sayHello", "phoenix");
console.log(workerMessage); // prints "Hello Phoenix"
Kind: inner property of worker/IndexingWorker
"EVENT_CRAWL_STARTED"
Raised when crawling started in the indexing worker.
Kind: event emitted by worker/IndexingWorker
"EVENT_CRAWL_PROGRESS"
Raised when crawling in progressing within the worker. The handler will receive the following properties as parameter.
Kind: event emitted by worker/IndexingWorker
Properties
Name | Type | Description |
---|---|---|
processed | number | The number of files cached till now. |
total | number | Number of files to cache. |
"EVENT_CRAWL_COMPLETE"
Raised when crawling is complete within the worker. The handler will receive the following properties as parameter.
Kind: event emitted by worker/IndexingWorker
Properties
Name | Type | Description |
---|---|---|
numFilesCached | number | |
cacheSizeBytes | number | |
crawlTimeMs | number | in milliseconds. |