Skip to content

Commit 60b7cf8

Browse files
committed
Align shared worker protocol identifier with provider protocols
1 parent ad521af commit 60b7cf8

File tree

22 files changed

+40
-40
lines changed

22 files changed

+40
-40
lines changed

docs/recipes/shared-workers.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Plugins communicate with their shared worker using a *protocol*. Protocols are v
3737

3838
Plugins can be compatible with multiple protocols. AVA will select the best protocol it supports. If AVA does not support any of the specified protocols it'll throw an error. The selected protocol is available on the returned worker object.
3939

40-
**For AVA 3, substitute `'ava4'` with `'experimental'`.**
40+
**For AVA 3, substitute `'ava-4'` with `'experimental'`.**
4141

4242
```js
4343
import {registerSharedWorker} from 'ava/plugin';
4444

4545
const shared = registerSharedWorker({
4646
filename: path.resolve(__dirname, 'worker.js'),
47-
supportedProtocols: ['ava4']
47+
supportedProtocols: ['ava-4']
4848
});
4949
```
5050

@@ -55,7 +55,7 @@ You can supply a `teardown()` function which will be called after all tests have
5555
```js
5656
const worker = registerSharedWorker({
5757
filename: path.resolve(__dirname, 'worker.js'),
58-
supportedProtocols: ['ava4'],
58+
supportedProtocols: ['ava-4'],
5959
teardown () {
6060
// Perform any clean-up within the test process itself.
6161
}
@@ -68,7 +68,7 @@ You can also provide some data passed to the shared worker when it is loaded. Of
6868
const shared = registerSharedWorker({
6969
filename: path.resolve(__dirname, 'worker.js'),
7070
initialData: {hello: 'world'},
71-
supportedProtocols: ['ava4']
71+
supportedProtocols: ['ava-4']
7272
});
7373
```
7474

@@ -84,7 +84,7 @@ The default export must be a factory method. Like when calling `registerSharedWo
8484

8585
```js
8686
export default ({negotiateProtocol}) => {
87-
const main = negotiateProtocol(['ava4']);
87+
const main = negotiateProtocol(['ava-4']);
8888
}
8989
```
9090

@@ -104,7 +104,7 @@ In the shared worker you can subscribe to messages from test workers:
104104

105105
```js
106106
export default async ({negotiateProtocol}) => {
107-
const main = negotiateProtocol(['ava4']).ready();
107+
const main = negotiateProtocol(['ava-4']).ready();
108108

109109
for await (const message of main.subscribe()) {
110110
//
@@ -122,7 +122,7 @@ To illustrate this here's a "game" of Marco Polo:
122122

123123
```js
124124
export default ({negotiateProtocol}) => {
125-
const main = negotiateProtocol(['ava4']).ready();
125+
const main = negotiateProtocol(['ava-4']).ready();
126126

127127
play(main.subscribe());
128128
};
@@ -143,7 +143,7 @@ You can also broadcast messages to all connected test workers:
143143

144144
```js
145145
export default async ({negotiateProtocol}) => {
146-
const main = negotiateProtocol(['ava4']).ready();
146+
const main = negotiateProtocol(['ava-4']).ready();
147147

148148
for await (const message of main.subscribe()) {
149149
if (message.data === 'Bingo!') {
@@ -163,7 +163,7 @@ Of course you don't need to wait for a message *from* a test worker to access th
163163

164164
```js
165165
export default async ({negotiateProtocol}) => {
166-
const main = negotiateProtocol(['ava4']).ready();
166+
const main = negotiateProtocol(['ava-4']).ready();
167167

168168
for await (const testWorker of main.testWorkers()) {
169169
main.broadcast(`New test file: ${testWorker.file}`);
@@ -205,7 +205,7 @@ You can register teardown functions to be run when the test worker exits:
205205

206206
```js
207207
export default async ({negotiateProtocol}) => {
208-
const main = negotiateProtocol(['ava4']).ready();
208+
const main = negotiateProtocol(['ava-4']).ready();
209209

210210
for await (const testWorker of main.testWorkers()) {
211211
testWorker.teardown(() => {
@@ -221,7 +221,7 @@ More interestingly, a wrapped teardown function is returned so that you can call
221221

222222
```js
223223
export default ({negotiateProtocol}) => {
224-
const main = negotiateProtocol(['ava4']).ready();
224+
const main = negotiateProtocol(['ava-4']).ready();
225225

226226
for await (const worker of testWorkers) {
227227
counters.set(worker, 0);

lib/plugin-support/shared-worker-loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ loadFactory(workerData.filename).then(factory => {
173173

174174
factory({
175175
negotiateProtocol(supported) {
176-
if (!supported.includes('ava4')) {
176+
if (!supported.includes('ava-4')) {
177177
fatal = new Error(`This version of AVA (${pkg.version}) is not compatible with shared worker plugin at ${workerData.filename}`);
178178
throw fatal;
179179
}
@@ -213,7 +213,7 @@ loadFactory(workerData.filename).then(factory => {
213213

214214
return {
215215
initialData: workerData.initialData,
216-
protocol: 'ava4',
216+
protocol: 'ava-4',
217217

218218
ready() {
219219
signalAvailable();

lib/worker/plugin.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function createSharedWorker(filename, initialData, teardown) {
6767

6868
return {
6969
available: channel.available,
70-
protocol: 'ava4',
70+
protocol: 'ava-4',
7171

7272
get currentlyAvailable() {
7373
return channel.currentlyAvailable;
@@ -95,7 +95,7 @@ function registerSharedWorker({
9595
throw new Error('Shared workers can be used only when worker threads are enabled');
9696
}
9797

98-
if (!supportedProtocols.includes('ava4')) {
98+
if (!supportedProtocols.includes('ava-4')) {
9999
throw new Error(`This version of AVA (${pkg.version}) does not support any of the desired shared worker protocols: ${supportedProtocols.join(',')}`);
100100
}
101101

plugin.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import {URL} from 'node:url';
22

33
export namespace SharedWorker {
4-
export type ProtocolIdentifier = 'ava4';
4+
export type ProtocolIdentifier = 'ava-4';
55

66
export type FactoryOptions = {
7-
negotiateProtocol <Data = unknown>(supported: readonly ['ava4']): Protocol<Data>;
7+
negotiateProtocol <Data = unknown>(supported: readonly ['ava-4']): Protocol<Data>;
88
// Add overloads for additional protocols.
99
};
1010

1111
export type Factory = (options: FactoryOptions) => void;
1212

1313
export type Protocol<Data = unknown> = {
1414
readonly initialData: Data;
15-
readonly protocol: 'ava4';
15+
readonly protocol: 'ava-4';
1616
broadcast: (data: Data) => BroadcastMessage<Data>;
1717
ready: () => Protocol<Data>;
1818
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
@@ -55,7 +55,7 @@ export namespace SharedWorker {
5555
export type Protocol<Data = unknown> = {
5656
readonly available: Promise<void>;
5757
readonly currentlyAvailable: boolean;
58-
readonly protocol: 'ava4';
58+
readonly protocol: 'ava-4';
5959
publish: (data: Data) => PublishedMessage<Data>;
6060
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
6161
};
@@ -73,5 +73,5 @@ export namespace SharedWorker {
7373
}
7474
}
7575

76-
export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava4', Data>): SharedWorker.Plugin.Protocol<Data>;
76+
export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava-4', Data>): SharedWorker.Plugin.Protocol<Data>;
7777
// Add overloads for additional protocols.

test-d/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {expectType} from 'tsd';
22

33
import * as plugin from '../plugin'; // eslint-disable-line import/extensions
44

5-
expectType<plugin.SharedWorker.Plugin.Protocol>(plugin.registerSharedWorker({filename: '', supportedProtocols: ['ava4']}));
5+
expectType<plugin.SharedWorker.Plugin.Protocol>(plugin.registerSharedWorker({filename: '', supportedProtocols: ['ava-4']}));
66

77
const factory: plugin.SharedWorker.Factory = ({negotiateProtocol}) => { // eslint-disable-line @typescript-eslint/no-unused-vars
8-
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava4']));
8+
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava-4']));
99
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default async ({negotiateProtocol}) => {
2-
negotiateProtocol(['ava4']).ready();
2+
negotiateProtocol(['ava-4']).ready();
33
};

test/shared-workers/cannot-publish-before-available/fixtures/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as plugin from 'ava/plugin';
44
test('cannot publish before ready', t => {
55
const worker = plugin.registerSharedWorker({
66
filename: new URL('_worker.js', import.meta.url),
7-
supportedProtocols: ['ava4'],
7+
supportedProtocols: ['ava-4'],
88
});
99

1010
t.throws(() => worker.publish(), {message: 'Shared worker is not yet available'});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default ({negotiateProtocol}) => {
2-
negotiateProtocol(['ava4']).ready();
2+
negotiateProtocol(['ava-4']).ready();
33
};

test/shared-workers/lifecycle/fixtures/available.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';
33

44
const worker = plugin.registerSharedWorker({
55
filename: new URL('_worker.js', import.meta.url),
6-
supportedProtocols: ['ava4'],
6+
supportedProtocols: ['ava-4'],
77
});
88

99
const availableImmediately = worker.currentlyAvailable;

test/shared-workers/lifecycle/fixtures/teardown.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as plugin from 'ava/plugin';
66
let calledLast = false;
77
plugin.registerSharedWorker({
88
filename: new URL('_worker.js', import.meta.url),
9-
supportedProtocols: ['ava4'],
9+
supportedProtocols: ['ava-4'],
1010
teardown() {
1111
assert(calledLast);
1212
console.log('🤗TEARDOWN CALLED');
@@ -15,7 +15,7 @@ plugin.registerSharedWorker({
1515

1616
plugin.registerSharedWorker({
1717
filename: new URL('_worker.js', import.meta.url),
18-
supportedProtocols: ['ava4'],
18+
supportedProtocols: ['ava-4'],
1919
teardown() {
2020
calledLast = true;
2121
},

0 commit comments

Comments
 (0)