|
| 1 | +import {URL} from 'node:url'; |
| 2 | + |
| 3 | +export namespace SharedWorker { |
| 4 | + export type ProtocolIdentifier = 'ava-4'; |
| 5 | + |
| 6 | + export type FactoryOptions = { |
| 7 | + negotiateProtocol <Data = unknown>(supported: readonly ['ava-4']): Protocol<Data>; |
| 8 | + // Add overloads for additional protocols. |
| 9 | + }; |
| 10 | + |
| 11 | + export type Factory = (options: FactoryOptions) => void; |
| 12 | + |
| 13 | + export type Protocol<Data = unknown> = { |
| 14 | + readonly initialData: Data; |
| 15 | + readonly protocol: 'ava-4'; |
| 16 | + broadcast: (data: Data) => BroadcastMessage<Data>; |
| 17 | + ready: () => Protocol<Data>; |
| 18 | + subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 19 | + testWorkers: () => AsyncIterableIterator<TestWorker<Data>>; |
| 20 | + }; |
| 21 | + |
| 22 | + export type BroadcastMessage<Data = unknown> = { |
| 23 | + readonly id: string; |
| 24 | + replies: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 25 | + }; |
| 26 | + |
| 27 | + export type PublishedMessage<Data = unknown> = { |
| 28 | + readonly id: string; |
| 29 | + replies: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 30 | + }; |
| 31 | + |
| 32 | + export type ReceivedMessage<Data = unknown> = { |
| 33 | + readonly data: Data; |
| 34 | + readonly id: string; |
| 35 | + readonly testWorker: TestWorker; |
| 36 | + reply: (data: Data) => PublishedMessage<Data>; |
| 37 | + }; |
| 38 | + |
| 39 | + export type TestWorker<Data = unknown> = { |
| 40 | + readonly id: string; |
| 41 | + readonly file: string; |
| 42 | + publish: (data: Data) => PublishedMessage<Data>; |
| 43 | + subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 44 | + teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>; |
| 45 | + }; |
| 46 | + |
| 47 | + export namespace Plugin { |
| 48 | + export type RegistrationOptions<Identifier extends ProtocolIdentifier, Data = unknown> = { |
| 49 | + readonly filename: string | URL; |
| 50 | + readonly initialData?: Data; |
| 51 | + readonly supportedProtocols: readonly Identifier[]; |
| 52 | + readonly teardown?: () => void; |
| 53 | + }; |
| 54 | + |
| 55 | + export type Protocol<Data = unknown> = { |
| 56 | + readonly available: Promise<void>; |
| 57 | + readonly currentlyAvailable: boolean; |
| 58 | + readonly protocol: 'ava-4'; |
| 59 | + publish: (data: Data) => PublishedMessage<Data>; |
| 60 | + subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 61 | + }; |
| 62 | + |
| 63 | + export type PublishedMessage<Data = unknown> = { |
| 64 | + readonly id: string; |
| 65 | + replies: () => AsyncIterableIterator<ReceivedMessage<Data>>; |
| 66 | + }; |
| 67 | + |
| 68 | + export type ReceivedMessage<Data = unknown> = { |
| 69 | + readonly data: Data; |
| 70 | + readonly id: string; |
| 71 | + reply: (data: Data) => PublishedMessage<Data>; |
| 72 | + }; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function registerSharedWorker<Data = unknown>(options: SharedWorker.Plugin.RegistrationOptions<'ava-4', Data>): SharedWorker.Plugin.Protocol<Data>; |
| 77 | +// Add overloads for additional protocols. |
0 commit comments