UNPKG

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