Skip to content

Commit 59d95c4

Browse files
committed
Add TSDoc info to all public classes and interfaces
1 parent c1aaa33 commit 59d95c4

9 files changed

+316
-40
lines changed

src/common.ts

+12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ export const MARKER = '@post-me';
33
export type IdType = number;
44
export type KeyType = string;
55

6+
/**
7+
* The options that can be passed when calling a method on the other context with RemoteHandle.customCall()
8+
*
9+
* @public
10+
*
11+
*/
612
export type CallOptions = {
713
transfer?: Transferable[];
814
};
915

16+
/**
17+
* The options that can be passed when emitting an event to the other context with LocalHandle.emit()
18+
*
19+
* @public
20+
*
21+
*/
1022
export type EmitOptions = {
1123
transfer?: Transferable[];
1224
};

src/connection.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,41 @@ import {
77
RemoteHandle,
88
} from './handles';
99

10+
/**
11+
* An active connection between two contexts
12+
*
13+
* @typeParam M0 - The methods exposed by this context
14+
* @typeParam E1 - The events exposed by this context
15+
* @typeParam M1 - The methods exposed by the other context
16+
* @typeParam E1 - The events exposed by the other context
17+
*
18+
* @public
19+
*
20+
*/
1021
export interface Connection<
1122
M0 extends MethodsType = any,
1223
E0 extends EventsType = any,
1324
M1 extends MethodsType = any,
1425
E1 extends EventsType = any
1526
> {
16-
localHandle: () => LocalHandle<M0, E0>;
17-
remoteHandle: () => RemoteHandle<M1, E1>;
18-
close: () => void;
27+
/**
28+
* Get a handle to the local end of the connection
29+
*
30+
* @returns A {@link LocalHandle} to the local side of the Connection
31+
*/
32+
localHandle(): LocalHandle<M0, E0>;
33+
34+
/**
35+
* Get a handle to the other end of the connection
36+
*
37+
* @returns A {@link RemoteHandle} to the other side of the Connection
38+
*/
39+
remoteHandle(): RemoteHandle<M1, E1>;
40+
41+
/**
42+
* Stop listening to incoming message from the other side
43+
*/
44+
close(): void;
1945
}
2046

2147
export class ConcreteConnection<M0 extends MethodsType> implements Connection {

src/dispatcher.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IdType, KeyType, createUniqueIdFn } from './common';
22
import { Messenger } from './messenger';
3-
import { Emitter } from './emitter';
3+
import { ConcreteEmitter } from './emitter';
44
import {
55
isMessage,
66
isCallMessage,
@@ -42,7 +42,7 @@ export type DispatcherEvents = {
4242
[MessageType.Event]: EventMessage<any>;
4343
};
4444

45-
export class Dispatcher extends Emitter<DispatcherEvents> {
45+
export class Dispatcher extends ConcreteEmitter<DispatcherEvents> {
4646
private messenger: Messenger;
4747
private sessionId: IdType;
4848
private removeMessengerListener: () => void;
@@ -138,7 +138,7 @@ export type ParentHandshakeDispatcherEvents = {
138138
[x: number]: HandshakeResponseMessage;
139139
};
140140

141-
export class ParentHandshakeDispatcher extends Emitter<ParentHandshakeDispatcherEvents> {
141+
export class ParentHandshakeDispatcher extends ConcreteEmitter<ParentHandshakeDispatcherEvents> {
142142
private messenger: Messenger;
143143
private sessionId: IdType;
144144
private removeMessengerListener: () => void;
@@ -186,7 +186,7 @@ export type ChildHandshakeDispatcherEvents = {
186186
[MessageType.HandshakeRequest]: HandshakeRequestMessage;
187187
};
188188

189-
export class ChildHandshakeDispatcher extends Emitter<ChildHandshakeDispatcherEvents> {
189+
export class ChildHandshakeDispatcher extends ConcreteEmitter<ChildHandshakeDispatcherEvents> {
190190
private messenger: Messenger;
191191
private removeMessengerListener: () => void;
192192

src/emitter.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,61 @@
11
import { EventsType } from './common';
22

3-
export interface IEmitter<E extends EventsType> {
4-
addEventListener: <K extends keyof E>(
3+
/**
4+
* A simple event emitter interface used to implement the observer pattern throughout the codebase.
5+
*
6+
* @public
7+
*
8+
*/
9+
export interface Emitter<E extends EventsType> {
10+
/**
11+
* Add a listener to a specific event.
12+
*
13+
* @param eventName - The name of the event
14+
* @param listener - A listener function that takes as parameter the payload of the event
15+
*/
16+
addEventListener<K extends keyof E>(
517
eventName: K,
618
listener: (data: E[K]) => void
7-
) => void;
8-
removeEventListener: <K extends keyof E>(
19+
): void;
20+
21+
/**
22+
* Remove a listener from a specific event.
23+
*
24+
* @param eventName - The name of the event
25+
* @param listener - A listener function that had been added previously
26+
*/
27+
removeEventListener<K extends keyof E>(
928
eventName: K,
1029
listener: (data: E[K]) => void
11-
) => void;
12-
once: <K extends keyof E>(eventName: K) => Promise<E[K]>;
30+
): void;
31+
32+
/**
33+
* Add a listener to a specific event, that will only be invoked once
34+
*
35+
* @remarks
36+
*
37+
* After the first occurrence of the specified event, the listener will be invoked and
38+
* immediately removed.
39+
*
40+
* @param eventName - The name of the event
41+
* @param listener - A listener function that had been added previously
42+
*/
43+
once<K extends keyof E>(eventName: K): Promise<E[K]>;
1344
}
1445

15-
export class Emitter<E extends EventsType> implements IEmitter<E> {
46+
/**
47+
* A concrete implementation of the {@link Emitter} interface
48+
*
49+
* @public
50+
*/
51+
export class ConcreteEmitter<E extends EventsType> implements Emitter<E> {
1652
private _listeners: Partial<Record<keyof E, Set<(data: any) => void>>>;
1753

1854
constructor() {
1955
this._listeners = {};
2056
}
2157

58+
/** {@inheritDoc Emitter.addEventListener} */
2259
addEventListener<K extends keyof E>(
2360
eventName: K,
2461
listener: (data: E[K]) => void
@@ -33,6 +70,7 @@ export class Emitter<E extends EventsType> implements IEmitter<E> {
3370
listeners.add(listener);
3471
}
3572

73+
/** {@inheritDoc Emitter.removeEventListener} */
3674
removeEventListener<K extends keyof E>(
3775
eventName: K,
3876
listener: (data: E[K]) => void
@@ -46,6 +84,7 @@ export class Emitter<E extends EventsType> implements IEmitter<E> {
4684
listeners.delete(listener);
4785
}
4886

87+
/** {@inheritDoc Emitter.once} */
4988
once<K extends keyof E>(eventName: K): Promise<E[K]> {
5089
return new Promise((resolve) => {
5190
const listener = (data: E[K]) => {
@@ -57,6 +96,7 @@ export class Emitter<E extends EventsType> implements IEmitter<E> {
5796
});
5897
}
5998

99+
/** @internal */
60100
protected emit<K extends keyof E>(eventName: K, data: E[K]) {
61101
let listeners = this._listeners[eventName];
62102

@@ -69,6 +109,7 @@ export class Emitter<E extends EventsType> implements IEmitter<E> {
69109
});
70110
}
71111

112+
/** @internal */
72113
protected removeAllListeners() {
73114
Object.values(this._listeners).forEach((listeners) => {
74115
if (listeners) {

src/handles.ts

+115-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
EmitOptions,
66
CallOptions,
77
} from './common';
8-
import { Emitter, IEmitter } from './emitter';
8+
import { Emitter, ConcreteEmitter } from './emitter';
99
import { Dispatcher } from './dispatcher';
1010
import {
1111
MessageType,
@@ -16,49 +16,149 @@ import {
1616
} from './messages';
1717
import { createCallbackProxy, isCallbackProxy } from './proxy';
1818

19+
/**
20+
* A handle to the other end of the connection
21+
*
22+
* @remarks
23+
*
24+
* Use this handle to:
25+
*
26+
* - Call methods exposed by the other end
27+
*
28+
* - Add listeners to custom events emitted by the other end
29+
*
30+
* @typeParam M - The methods exposed by the other context
31+
* @typeParam E - The events exposed by the other context
32+
*
33+
* @public
34+
*
35+
*/
1936
export interface RemoteHandle<
2037
M extends MethodsType = any,
2138
E extends EventsType = any
22-
> extends IEmitter<E> {
23-
call: <K extends keyof M>(
39+
> extends Emitter<E> {
40+
/**
41+
* Call a method exposed by the other end.
42+
*
43+
* @param methodName - The name of the method
44+
* @param args - The list of arguments passed to the method
45+
* @returns A Promise of the value returned by the method
46+
*
47+
*/
48+
call<K extends keyof M>(
2449
methodName: K,
2550
...args: Parameters<M[K]>
26-
) => Promise<InnerType<ReturnType<M[K]>>>;
27-
customCall: <K extends keyof M>(
51+
): Promise<InnerType<ReturnType<M[K]>>>;
52+
53+
/**
54+
* Call a method exposed by the other end.
55+
*
56+
* @param methodName - The name of the method
57+
* @param args - The list of arguments passed to the method
58+
* @param options - The {@link CallOptions} to customize this method call
59+
* @returns A Promise of the value returned by the method
60+
*
61+
*/
62+
customCall<K extends keyof M>(
2863
methodName: K,
2964
args: Parameters<M[K]>,
3065
options?: CallOptions
31-
) => Promise<InnerType<ReturnType<M[K]>>>;
32-
setCallTransfer: <K extends keyof M>(
66+
): Promise<InnerType<ReturnType<M[K]>>>;
67+
68+
/**
69+
* Specify which parts of the arguments of a given method call should be transferred
70+
* into the other context instead of cloned.
71+
*
72+
* @remarks
73+
*
74+
* You only need to call setCallTransfer once per method. After the transfer function is set,
75+
* it will automatically be used by all subsequent calls to the specified method.
76+
*
77+
* @param methodName - The name of the method
78+
* @param transfer - A function that takes as parameters the arguments of a method call, and returns a list of transferable objects.
79+
*
80+
*/
81+
setCallTransfer<K extends keyof M>(
3382
methodName: K,
3483
transfer: (...args: Parameters<M[K]>) => Transferable[]
35-
) => void;
84+
): void;
3685
}
3786

87+
/**
88+
* A handle to the local end of the connection
89+
*
90+
* @remarks
91+
*
92+
* Use this handle to:
93+
*
94+
* - Emit custom events to the other end
95+
*
96+
* @typeParam M - The methods exposed by this context
97+
* @typeParam E - The events exposed by this context
98+
*
99+
* @public
100+
*
101+
*/
38102
export interface LocalHandle<
39103
M extends MethodsType = any,
40104
E extends EventsType = any
41105
> {
42-
emit: <K extends keyof E>(
106+
/**
107+
* Emit a custom event with a payload. The event can be captured by the other context.
108+
*
109+
* @param eventName - The name of the event
110+
* @param data - The payload associated with the event
111+
* @param options - The {@link EmitOptions} to customize this emit call
112+
*
113+
*/
114+
emit<K extends keyof E>(
43115
eventName: K,
44116
data: E[K],
45117
options?: EmitOptions
46-
) => void;
47-
setReturnTransfer: <K extends keyof M>(
118+
): void;
119+
120+
/**
121+
* Specify which parts of the return value of a given method call should be transferred
122+
* into the other context instead of cloned.
123+
*
124+
* @remarks
125+
*
126+
* You only need to call setReturnTransfer once per method. After the transfer function is set,
127+
* it will automatically be used every time a value is returned by the specified method.
128+
*
129+
* @param methodName - The name of the method
130+
* @param transfer - A function that takes as parameter the return value of a method call, and returns a list of transferable objects.
131+
*
132+
*/
133+
setReturnTransfer<K extends keyof M>(
48134
methodName: K,
49135
transfer: (result: InnerType<ReturnType<M[K]>>) => Transferable[]
50-
) => void;
51-
setEmitTransfer: <K extends keyof E>(
136+
): void;
137+
138+
/**
139+
* Specify which parts of the payload of a given event should be transferred
140+
* into the other context instead of cloned.
141+
*
142+
* @remarks
143+
*
144+
* You only need to call setEmitTransfer once per event type. After the transfer function is set,
145+
* it will automatically be used every time a payload is attached to the specific event.
146+
*
147+
* @param eventName - The name of the method
148+
* @param transfer - A function that takes as parameter the payload of an event, and returns a list of transferable objects.
149+
*
150+
*/
151+
setEmitTransfer<K extends keyof E>(
52152
eventName: K,
53153
transfer: (payload: E[K]) => Transferable[]
54-
) => void;
154+
): void;
55155
}
56156

57157
export class ConcreteRemoteHandle<
58158
M extends MethodsType = any,
59159
E extends EventsType = any
60160
>
61-
extends Emitter<E>
161+
extends ConcreteEmitter<E>
62162
implements RemoteHandle<M, E> {
63163
private _dispatcher: Dispatcher;
64164
private _callTransfer: { [x: string]: (...args: any) => Transferable[] };

0 commit comments

Comments
 (0)