Skip to content

IndexingWorker API

phoenixide edited this page Aug 27, 2022 · 4 revisions

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. This is similar to worker/ExtensionsWorker but with a full file index.

  • Extensions are advised to use worker/ExtensionsWorker if they do not use the file index and just want to offload minor tasks.
  • 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.

Import

// usage within extensions:
const IndexingWorker = brackets.getModule("worker/IndexingWorker");

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:

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.

WorkerComm-APIS

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.

Examples

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"

EVENT_CRAWL_STARTED

Raised when crawling started in the indexing worker.

Type: null

EVENT_CRAWL_PROGRESS

Raised when crawling in progressing within the worker. The handler will receive the following properties as parameter.

Type: object

Properties

  • 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.

Type: object

Properties