-
Notifications
You must be signed in to change notification settings - Fork 93
/
setup.js
47 lines (36 loc) · 1.68 KB
/
setup.js
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
36
37
38
39
40
41
42
43
44
45
46
47
/* @flow */
import type { CrossDomainWindowType, DomainMatcher } from '@krakenjs/cross-domain-utils/src';
import { initHello } from './lib';
import { listenForMessages, stopListenForMessages, receiveMessage, setupGlobalReceiveMessage, cancelResponseListeners } from './drivers';
import { getGlobal, deleteGlobal } from './global';
import { on, send } from './public';
import { setupBridge } from './bridge';
import { serializeMessage as internalSerializeMessage, deserializeMessage as internalDeserializeMessage, ProxyWindow } from './serialize';
export function serializeMessage<T>(destination : CrossDomainWindowType | ProxyWindow, domain : DomainMatcher, obj : T) : string {
return internalSerializeMessage(destination, domain, obj, { on, send });
}
export function deserializeMessage<T : mixed>(source : CrossDomainWindowType | ProxyWindow, origin : string, message : string) : T {
return internalDeserializeMessage(source, origin, message, { on, send });
}
export function createProxyWindow(win? : CrossDomainWindowType) : ProxyWindow {
return new ProxyWindow({ send, win });
}
export function toProxyWindow(win : CrossDomainWindowType | ProxyWindow) : ProxyWindow {
return ProxyWindow.toProxyWindow(win, { send });
}
export function setup() {
if (!getGlobal().initialized) {
getGlobal().initialized = true;
setupGlobalReceiveMessage({ on, send });
listenForMessages({ on, send });
if (__POST_ROBOT__.__IE_POPUP_SUPPORT__) {
setupBridge({ on, send, receiveMessage });
}
initHello({ on, send });
}
}
export function destroy() {
cancelResponseListeners();
stopListenForMessages();
deleteGlobal();
}