WebWorker Example
While isomorphic-git
tries not to block the main thread, it still does on occasion.
This can cause your webapp to stutter or even freeze up briefly!
To achieve buttery smooth performance, you'll eventually want to move all your isomorphic-git
usage off of the main thread.
Actually, you should move all your logic that's not directly responsible for updating the DOM off the main thread.
That's still a real challenge in 2020, but more and more libraries are appearing to help solve this.
Introduction
WebWorkers live in a separate operating system thread from the main JS thread and communicate using the worker.postMessage() method. Code in the worker thread does not have access to heap objects in the main thread. So all objects sent via postMessage need to be serialized before being sent. Technically, this is done using the structured clone algorithm.
What kinds of Objects can be sent using postMessage
? Functions cannot. Therefore objects with methods cannot.
JSON objects can. Date
objects and RegExp
objects can. Also Uint8Array
objects and Map
objects and Set
objects.
So basically the types of objects you can send to a worker are a superset of JSON but a subset of full JavaScript objects.
If you don't already have a WebWorker RPC solution, then I recommend using MagicPortal (because I wrote it) or Comlink which is a similar library. The example below will use MagicPortal.
Example
Here is a complete example that runs git in a WebWorker.
The worker wraps some git functions and exposes them to the main thread, while the main thread exposes some functions to the worker for use in callbacks like onProgress
, onMessage
, and onAuth
.