Help with Generators.worker

At I’ve made a helper function to create Web Workers more easily, following

However I fail to see how I can post a Message to the worker created by Generators.worker().

Any advice welcome.


It looks like the API returns an async generator, you should be able to get the actual worker like:

for (const worker of Generators.worker(f, preamble))
1 Like

no luck so far :frowning:

& I can’t find any live example so… I guess I’ll stick with the other recipe for a while

The pattern that I prefer does not involve Generators.worker. Here are two examples.

For Voronoi Stippling, the worker is created in the canvas cell. The image data, canvas dimensions, and number of points is sent to the worker by postMessage, and then the canvas cell listens to the points computed by the worker to redraw the canvas. The invalidation promise is used to terminate the worker if the canvas cell is invalidated.

Here, Generators.observe is used to listen to the worker, so that the latest value computed by the worker is available as a reactive value (the digits cell).

In both cases the worker script is defined as a Blob URL which is revoked on invalidation (when you edit the script).

1 Like

Here is an example of Generators.worker:

The problem with Generators.worker (and Generators.disposable more generally) is that it’s only useful if the worker is a cell, so that the Observable runtime takes care of terminating the worker for you on invalidation. If the worker is not defined as a cell, and is instead a local variable, then Generators.worker doesn’t really buy you anything on top of the more explicit invalidation promise.

Furthermore, since workers tend to be stateful (they represent running programs), you often don’t want them to be exposed as cells, and instead you want them as local variables (as in the two examples I linked in my previous reply).


The voronoi-stippling notebook does not work on Brave - without errors… It does on Chrome, Firefox, Opera - on my machine. But the [](http://computing pi notebook) does work. So it’s not web workers…

Solved by
Brave issue: Allow device recognition and it works.