From ec498b3d598930f97e6e7117486d6f5f7cc07232 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Mon, 11 Apr 2022 09:25:10 -0500 Subject: [PATCH 1/2] Document how to load worker multiple times See https://github.com/avajs/ava/pull/3006 for context. --- docs/recipes/shared-workers.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/recipes/shared-workers.md b/docs/recipes/shared-workers.md index 976891459..a74298b96 100644 --- a/docs/recipes/shared-workers.md +++ b/docs/recipes/shared-workers.md @@ -34,6 +34,25 @@ const shared = registerSharedWorker({ Within a test process you can only register one worker for each `filename`. Filenames are compared as-is, without normalization. If you call `registerSharedWorker()` a second time, the same worker instance is returned. +If for some reason you want to load the same file as multiple different workers, you can append a unique hash to the end of the filename: + +```js +import crypto from 'crypto'; +import {registerSharedWorker} from 'ava/plugin'; + +const randomKey = crypto.randomBytes(20).toString('hex'); + +const shared = registerSharedWorker({ + filename: new URL( + `file:${path.resolve( + __dirname, + 'worker.js' + )}#${encodeURIComponent(randomKey)}` + ), + supportedProtocols: ['ava-4'] +}); +``` + You can supply a `teardown()` function which will be called after all tests have finished. If you call `registerSharedWorker()` multiple times then the `teardown()` function will be invoked for each registration, even though you only got one worker instance. The most recently registered `teardown()` function is called first, and so forth. `teardown()` functions execute sequentially. ```js From 81e8373b5b959ea2a116f60247ade472afdde04c Mon Sep 17 00:00:00 2001 From: Max Isom Date: Wed, 20 Apr 2022 17:52:40 -0500 Subject: [PATCH 2/2] Update shared-workers.md --- docs/recipes/shared-workers.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/recipes/shared-workers.md b/docs/recipes/shared-workers.md index a74298b96..365a8c59d 100644 --- a/docs/recipes/shared-workers.md +++ b/docs/recipes/shared-workers.md @@ -40,19 +40,22 @@ If for some reason you want to load the same file as multiple different workers, import crypto from 'crypto'; import {registerSharedWorker} from 'ava/plugin'; -const randomKey = crypto.randomBytes(20).toString('hex'); +const key = Math.random() > 0.5 ? 'worker-a' : 'worker-b'; const shared = registerSharedWorker({ filename: new URL( `file:${path.resolve( __dirname, 'worker.js' - )}#${encodeURIComponent(randomKey)}` + )}#${encodeURIComponent(key)}` ), + initialData: {workerKey: key}, supportedProtocols: ['ava-4'] }); ``` +This works because the `filename` parameter accepts [URL](https://nodejs.org/api/url.html) objects, meaning you could use a query component for the key instead if you wanted. + You can supply a `teardown()` function which will be called after all tests have finished. If you call `registerSharedWorker()` multiple times then the `teardown()` function will be invoked for each registration, even though you only got one worker instance. The most recently registered `teardown()` function is called first, and so forth. `teardown()` functions execute sequentially. ```js