Skip to content

Commit

Permalink
Align shared worker protocol identifier with provider protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Nov 1, 2021
1 parent ad521af commit 60b7cf8
Show file tree
Hide file tree
Showing 22 changed files with 40 additions and 40 deletions.
22 changes: 11 additions & 11 deletions docs/recipes/shared-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ Plugins communicate with their shared worker using a *protocol*. Protocols are v

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.

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

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

const shared = registerSharedWorker({
filename: path.resolve(__dirname, 'worker.js'),
supportedProtocols: ['ava4']
supportedProtocols: ['ava-4']
});
```

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

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

```js
export default ({negotiateProtocol}) => {
const main = negotiateProtocol(['ava4']);
const main = negotiateProtocol(['ava-4']);
}
```

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

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

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

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

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

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

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

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

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

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

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

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

for await (const worker of testWorkers) {
counters.set(worker, 0);
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin-support/shared-worker-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ loadFactory(workerData.filename).then(factory => {

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

return {
initialData: workerData.initialData,
protocol: 'ava4',
protocol: 'ava-4',

ready() {
signalAvailable();
Expand Down
4 changes: 2 additions & 2 deletions lib/worker/plugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function createSharedWorker(filename, initialData, teardown) {

return {
available: channel.available,
protocol: 'ava4',
protocol: 'ava-4',

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

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

Expand Down
10 changes: 5 additions & 5 deletions plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {URL} from 'node:url';

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

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

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

export type Protocol<Data = unknown> = {
readonly initialData: Data;
readonly protocol: 'ava4';
readonly protocol: 'ava-4';
broadcast: (data: Data) => BroadcastMessage<Data>;
ready: () => Protocol<Data>;
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
Expand Down Expand Up @@ -55,7 +55,7 @@ export namespace SharedWorker {
export type Protocol<Data = unknown> = {
readonly available: Promise<void>;
readonly currentlyAvailable: boolean;
readonly protocol: 'ava4';
readonly protocol: 'ava-4';
publish: (data: Data) => PublishedMessage<Data>;
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
};
Expand All @@ -73,5 +73,5 @@ export namespace SharedWorker {
}
}

export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava4', Data>): SharedWorker.Plugin.Protocol<Data>;
export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava-4', Data>): SharedWorker.Plugin.Protocol<Data>;
// Add overloads for additional protocols.
4 changes: 2 additions & 2 deletions test-d/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {expectType} from 'tsd';

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

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

const factory: plugin.SharedWorker.Factory = ({negotiateProtocol}) => { // eslint-disable-line @typescript-eslint/no-unused-vars
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava4']));
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava-4']));
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default async ({negotiateProtocol}) => {
negotiateProtocol(['ava4']).ready();
negotiateProtocol(['ava-4']).ready();
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as plugin from 'ava/plugin';
test('cannot publish before ready', t => {
const worker = plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

t.throws(() => worker.publish(), {message: 'Shared worker is not yet available'});
Expand Down
2 changes: 1 addition & 1 deletion test/shared-workers/lifecycle/fixtures/_worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default ({negotiateProtocol}) => {
negotiateProtocol(['ava4']).ready();
negotiateProtocol(['ava-4']).ready();
};
2 changes: 1 addition & 1 deletion test/shared-workers/lifecycle/fixtures/available.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

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

const availableImmediately = worker.currentlyAvailable;
Expand Down
4 changes: 2 additions & 2 deletions test/shared-workers/lifecycle/fixtures/teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as plugin from 'ava/plugin';
let calledLast = false;
plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
teardown() {
assert(calledLast);
console.log('🤗TEARDOWN CALLED');
Expand All @@ -15,7 +15,7 @@ plugin.registerSharedWorker({

plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
teardown() {
calledLast = true;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default async ({negotiateProtocol}) => {
negotiateProtocol(['ava4']).ready();
negotiateProtocol(['ava-4']).ready();
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

test('registering', t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

test('shared worker should cause tests to fail', t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import * as plugin from 'ava/plugin';

export default plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default async ({negotiateProtocol}) => {
const protocol = negotiateProtocol(['ava4']);
const protocol = negotiateProtocol(['ava-4']);
protocol.ready();

crash(protocol.subscribe());
Expand Down
2 changes: 1 addition & 1 deletion test/shared-workers/worker-protocol/fixtures/_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import * as plugin from 'ava/plugin';

export default plugin.registerSharedWorker({
filename: new URL('_worker.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});
2 changes: 1 addition & 1 deletion test/shared-workers/worker-protocol/fixtures/_worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default async ({negotiateProtocol}) => {
const protocol = negotiateProtocol(['ava4']);
const protocol = negotiateProtocol(['ava-4']);

// When we're ready to receive workers or messages.
protocol.ready();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

plugin.registerSharedWorker({
filename: new URL('_factory-function.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

test('shared worker should cause tests to fail', t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

plugin.registerSharedWorker({
filename: new URL('_module.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

test('shared worker should cause tests to fail', t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as plugin from 'ava/plugin';

plugin.registerSharedWorker({
filename: new URL('_no-factory-function.js', import.meta.url),
supportedProtocols: ['ava4'],
supportedProtocols: ['ava-4'],
});

test('shared worker should cause tests to fail', t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as plugin from 'ava/plugin';

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

const messages = worker.subscribe();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'node:crypto';

export default async ({negotiateProtocol}) => {
const protocol = negotiateProtocol(['ava4']).ready();
const protocol = negotiateProtocol(['ava-4']).ready();

const random = crypto.randomBytes(16).toString('hex');
for await (const testWorker of protocol.testWorkers()) {
Expand Down

0 comments on commit 60b7cf8

Please sign in to comment.