-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.ts
More file actions
36 lines (28 loc) · 903 Bytes
/
window.ts
File metadata and controls
36 lines (28 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {disposer} from "@e280/stz"
import {Conduit} from "./conduit.js"
import {ChannelMessage} from "../types.js"
import {is_valid_json_rpc_message, onMessage} from "../parts/helpers.js"
export class WindowConduit extends Conduit {
targetOrigin: string
dispose = disposer()
constructor(options: {
localWindow: Window
targetWindow: WindowProxy
targetOrigin: string
allow: (e: ChannelMessage) => boolean
}) {
super()
const {localWindow, targetWindow, allow} = options
this.targetOrigin = options.targetOrigin
this.dispose.schedule(
this.sendRequest.sub((m, transfer) =>
targetWindow.postMessage(m, this.targetOrigin, transfer)),
this.sendResponse.sub((m, transfer) =>
targetWindow.postMessage(m, this.targetOrigin, transfer)),
onMessage(localWindow, e => {
if (allow(e) && is_valid_json_rpc_message(e.data))
this.recv(e.data, e)
}),
)
}
}