@@ -4,6 +4,12 @@ import type { EventMap } from "../EventEmitter";
4
4
import type { EventEmitterWithHandshakeOptions } from "../handshake" ;
5
5
import { HandshakeParent } from "../handshake/Parent" ;
6
6
import { WindowTransport } from "../transport/WindowTransport" ;
7
+ import type { Transport } from "../transport/Transport" ;
8
+
9
+ type TransportClassConstructor < OutgoingEvents extends EventMap > = new (
10
+ otherWindow : Window ,
11
+ targetOrigin : string | string [ ]
12
+ ) => Transport < OutgoingEvents > ;
7
13
8
14
export class IFrameWindow < IncomingEvents extends EventMap , OutgoingEvents extends EventMap > extends HandshakeParent <
9
15
IncomingEvents ,
@@ -12,32 +18,35 @@ export class IFrameWindow<IncomingEvents extends EventMap, OutgoingEvents extend
12
18
private constructor (
13
19
public iframe : HTMLIFrameElement ,
14
20
targetOrigin : string ,
15
- options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents >
21
+ options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents > ,
22
+ TransportClass : TransportClassConstructor < OutgoingEvents > = WindowTransport
16
23
) {
17
24
const contentWindow = iframe . contentWindow ;
18
25
if ( ! contentWindow ) {
19
26
throw new Error ( "IFrame must have a contentWindow" ) ;
20
27
}
21
- const transport = new WindowTransport < OutgoingEvents > ( contentWindow , targetOrigin ) ;
28
+ const transport = new TransportClass ( contentWindow , targetOrigin ) ;
22
29
super ( transport , options ) ;
23
30
}
24
31
25
32
static async init < IncomingEvents extends EventMap , OutgoingEvents extends EventMap > (
26
33
urlOrExistingIframe : string | HTMLIFrameElement ,
27
- options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents >
34
+ options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents > ,
35
+ TransportClass : TransportClassConstructor < OutgoingEvents > = WindowTransport
28
36
) {
29
37
const iframe =
30
38
typeof urlOrExistingIframe === "string" ? await createIFrame ( urlOrExistingIframe ) : urlOrExistingIframe ;
31
39
const targetOrigin = options ?. targetOrigin || urlToOrigin ( iframe . src ) ;
32
- return new IFrameWindow < IncomingEvents , OutgoingEvents > ( iframe , targetOrigin , options ) ;
40
+ return new IFrameWindow < IncomingEvents , OutgoingEvents > ( iframe , targetOrigin , options , TransportClass ) ;
33
41
}
34
42
35
43
static initExistingIFrame < IncomingEvents extends EventMap , OutgoingEvents extends EventMap > (
36
44
iframe : HTMLIFrameElement ,
37
- options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents >
45
+ options ?: EventEmitterWithHandshakeOptions < IncomingEvents , OutgoingEvents > ,
46
+ TransportClass : TransportClassConstructor < OutgoingEvents > = WindowTransport
38
47
) {
39
48
const targetOrigin = options ?. targetOrigin || urlToOrigin ( iframe . src ) ;
40
- return new IFrameWindow < IncomingEvents , OutgoingEvents > ( iframe , targetOrigin , options ) ;
49
+ return new IFrameWindow < IncomingEvents , OutgoingEvents > ( iframe , targetOrigin , options , TransportClass ) ;
41
50
}
42
51
}
43
52
0 commit comments