1
1
/* @flow */
2
2
/* eslint max-lines: 0 */
3
3
4
- import { isSameDomain , matchDomain , getDomain , getOpener ,
5
- getNthParentFromTop , getAncestor , getAllFramesInWindow ,
6
- type CrossDomainWindowType , onCloseWindow , assertSameDomain } from 'cross-domain-utils/src' ;
7
- import { markWindowKnown , deserializeMessage , type CrossDomainFunctionType } from 'post-robot/src' ;
4
+ import { isSameDomain , matchDomain , getAllFramesInWindow , type CrossDomainWindowType ,
5
+ onCloseWindow , assertSameDomain } from 'cross-domain-utils/src' ;
6
+ import { markWindowKnown , type CrossDomainFunctionType } from 'post-robot/src' ;
8
7
import { ZalgoPromise } from 'zalgo-promise/src' ;
9
- import { extend , onResize , elementReady , assertExists , noop } from 'belter/src' ;
8
+ import { extend , onResize , elementReady , noop } from 'belter/src' ;
10
9
11
- import { getGlobal , tryGlobal } from '../lib' ;
12
- import { CONTEXT , INITIAL_PROPS , WINDOW_REFERENCES } from '../constants' ;
10
+ import { getGlobal , tryGlobal , getInitialParentPayload } from '../lib' ;
11
+ import { CONTEXT } from '../constants' ;
13
12
import type { NormalizedComponentOptionsType , getSiblingsPropType } from '../component' ;
14
13
import type { PropsType , ChildPropsType } from '../component/props' ;
15
- import type { WindowRef , PropRef , ParentExportsType } from '../parent' ;
16
14
import type { StringMatcherType } from '../types' ;
17
15
18
16
import { normalizeChildProps } from './props' ;
19
- import { getChildPayload } from './window' ;
20
17
21
18
export type ChildExportsType < P > = { |
22
19
updateProps : CrossDomainFunctionType < [ PropsType < P > ] , void> ,
@@ -39,37 +36,6 @@ export type ChildHelpers<P, X> = {|
39
36
getSiblings : getSiblingsPropType
40
37
| } ;
41
38
42
- function getParentComponentWindow ( ref : WindowRef ) : CrossDomainWindowType {
43
- const { type } = ref ;
44
-
45
- if ( type === WINDOW_REFERENCES . OPENER ) {
46
- return assertExists ( 'opener' , getOpener ( window ) ) ;
47
-
48
- } else if ( type === WINDOW_REFERENCES . PARENT && typeof ref . distance === 'number' ) {
49
- return assertExists ( 'parent' , getNthParentFromTop ( window , ref . distance ) ) ;
50
-
51
- } else if ( type === WINDOW_REFERENCES . GLOBAL && ref . uid && typeof ref . uid === 'string' ) {
52
- const { uid } = ref ;
53
- const ancestor = getAncestor ( window ) ;
54
-
55
- if ( ! ancestor ) {
56
- throw new Error ( `Can not find ancestor window` ) ;
57
- }
58
-
59
- for ( const frame of getAllFramesInWindow ( ancestor ) ) {
60
- if ( isSameDomain ( frame ) ) {
61
- const win = tryGlobal ( frame , global => global . windows && global . windows [ uid ] ) ;
62
-
63
- if ( win ) {
64
- return win ;
65
- }
66
- }
67
- }
68
- }
69
-
70
- throw new Error ( `Unable to find ${ type } parent component window` ) ;
71
- }
72
-
73
39
function checkParentDomain ( allowedParentDomains : StringMatcherType , domain : string ) {
74
40
if ( ! matchDomain ( allowedParentDomains , domain ) ) {
75
41
throw new Error ( `Can not be rendered by domain: ${ domain } ` ) ;
@@ -88,27 +54,6 @@ function destroy() : ZalgoPromise<void> {
88
54
} ) ;
89
55
}
90
56
91
- function getPropsByRef < P > ( parentComponentWindow : CrossDomainWindowType , domain : string , propRef : PropRef ) : ( PropsType < P > ) {
92
- let props ;
93
-
94
- if ( propRef . type === INITIAL_PROPS . RAW ) {
95
- props = propRef . value ;
96
- } else if ( propRef . type === INITIAL_PROPS . UID ) {
97
- if ( ! isSameDomain ( parentComponentWindow ) ) {
98
- throw new Error ( `Parent component window is on a different domain - expected ${ getDomain ( ) } - can not retrieve props` ) ;
99
- }
100
-
101
- const global = getGlobal ( parentComponentWindow ) ;
102
- props = assertExists ( 'props' , global && global . props [ propRef . uid ] ) ;
103
- }
104
-
105
- if ( ! props ) {
106
- throw new Error ( `Could not find props` ) ;
107
- }
108
-
109
- return deserializeMessage ( parentComponentWindow , domain , props ) ;
110
- }
111
-
112
57
export type ChildComponent< P , X > = { |
113
58
getProps : ( ) = > ChildPropsType < P , X > ,
114
59
init : ( ) => ZalgoPromise < void >
@@ -118,23 +63,20 @@ export function childComponent<P, X, C>(options : NormalizedComponentOptionsType
118
63
const { tag , propsDef , autoResize , allowedParentDomains } = options;
119
64
120
65
const onPropHandlers = [];
121
- const childPayload = getChildPayload ( ) ;
66
+
67
+ const { parent , payload } = getInitialParentPayload();
68
+ const { win : parentComponentWindow , domain : parentDomain } = parent;
69
+
122
70
let props : ChildPropsType< P , X > ;
123
71
const exportsPromise = new ZalgoPromise();
124
72
125
- if ( ! childPayload ) {
126
- throw new Error ( `No child payload found` ) ;
127
- }
73
+ const { version , uid , exports : parentExports , context , props : initialProps } = payload;
128
74
129
- if ( childPayload . version !== __ZOID__ . __VERSION__ ) {
130
- throw new Error ( `Parent window has zoid version ${ childPayload . version } , child window has version ${ __ZOID__ . __VERSION__ } ` ) ;
75
+ if (version !== __ZOID__.__VERSION__) {
76
+ throw new Error ( `Parent window has zoid version ${ version } , child window has version ${ __ZOID__ . __VERSION__ } ` ) ;
131
77
}
132
78
133
- const { uid , parent : parentRef , parentDomain , exports : parentExports , context , props : propsRef } = childPayload ;
134
- const parentComponentWindow = getParentComponentWindow ( parentRef ) ;
135
- const parent : ParentExportsType < P , X > = deserializeMessage ( parentComponentWindow , parentDomain , parentExports ) ;
136
-
137
- const { show, hide, close } = parent ;
79
+ const { show , hide , close , onError , checkClose , export : parentExport , resize : parentResize , init : parentInit } = parentExports;
138
80
139
81
const getParent = () => parentComponentWindow ;
140
82
const getParentDomain = ( ) => parentDomain ;
@@ -143,23 +85,13 @@ export function childComponent<P, X, C>(options : NormalizedComponentOptionsType
143
85
onPropHandlers . push ( handler ) ;
144
86
} ;
145
87
146
- const onError = ( err : mixed ) : ZalgoPromise < void > => {
147
- return ZalgoPromise . try ( ( ) => {
148
- if ( parent && parent . onError ) {
149
- return parent . onError ( err ) ;
150
- } else {
151
- throw err ;
152
- }
153
- } ) ;
154
- } ;
155
-
156
88
const resize = ({ width , height } : { | width : ?number , height : ?number | } ) : ZalgoPromise< void > => {
157
- return parent . resize . fireAndForget ( { width, height } ) ;
89
+ return parentResize . fireAndForget ( { width, height } ) ;
158
90
} ;
159
91
160
92
const xport = (xports : X) : ZalgoPromise< void > => {
161
93
exportsPromise . resolve ( xports ) ;
162
- return parent . export ( xports ) ;
94
+ return parentExport ( xports ) ;
163
95
} ;
164
96
165
97
const getSiblings = ({ anyParent } = { } ) => {
@@ -213,11 +145,11 @@ export function childComponent<P, X, C>(options : NormalizedComponentOptionsType
213
145
214
146
const watchForClose = () => {
215
147
window . addEventListener ( 'beforeunload' , ( ) => {
216
- parent . checkClose . fireAndForget ( ) ;
148
+ checkClose . fireAndForget ( ) ;
217
149
} ) ;
218
150
219
151
window . addEventListener ( 'unload' , ( ) => {
220
- parent . checkClose . fireAndForget ( ) ;
152
+ checkClose . fireAndForget ( ) ;
221
153
} ) ;
222
154
223
155
onCloseWindow ( parentComponentWindow , ( ) => {
@@ -276,7 +208,7 @@ export function childComponent<P, X, C>(options : NormalizedComponentOptionsType
276
208
markWindowKnown ( parentComponentWindow ) ;
277
209
watchForClose ( ) ;
278
210
279
- return parent . init ( { updateProps, close : destroy } ) ;
211
+ return parentInit ( { updateProps, close : destroy } ) ;
280
212
281
213
} ) . then ( ( ) => {
282
214
return watchForResize ( ) ;
@@ -289,10 +221,10 @@ export function childComponent<P, X, C>(options : NormalizedComponentOptionsType
289
221
const getProps = () => {
290
222
if ( props ) {
291
223
return props ;
224
+ } else {
225
+ setProps ( initialProps , parentDomain ) ;
226
+ return props ;
292
227
}
293
-
294
- setProps ( getPropsByRef ( parentComponentWindow , parentDomain , propsRef ) , parentDomain ) ;
295
- return props;
296
228
} ;
297
229
298
230
return {
0 commit comments