@@ -207,14 +207,8 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
207207 ..height = '1000' ;
208208 domDocument.body! .appendChild (iframe);
209209
210- // Use this to communicate securely with the iframe.
211- final DomMessageChannel channel = createDomMessageChannel ();
212210 final StreamChannelController <dynamic > controller = StreamChannelController <dynamic >(sync : true );
213211
214- // Use this to avoid sending a message to the iframe before it's sent a
215- // message to us. This ensures that no messages get dropped on the floor.
216- final Completer <dynamic > readyCompleter = Completer <dynamic >();
217-
218212 final List <DomSubscription > domSubscriptions = < DomSubscription > [];
219213 final List <StreamSubscription <dynamic >> streamSubscriptions = < StreamSubscription <dynamic >> [];
220214 _domSubscriptions[id] = domSubscriptions;
@@ -229,37 +223,42 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
229223 if (message.origin != domWindow.location.origin) {
230224 return ;
231225 }
232-
233- if (message.data['href' ] != iframe.src) {
226+ if (message.source.location? .href != iframe.src) {
234227 return ;
235228 }
236229
237230 message.stopPropagation ();
238231
239- if (message.data['ready' ] == true ) {
232+ if (message.data == 'port' ) {
233+ final DomMessagePort port = message.ports.first;
234+ domSubscriptions.add (
235+ DomSubscription (port, 'message' ,(DomEvent event) {
236+ controller.local.sink.add ((event as DomMessageEvent ).data);
237+ }));
238+ port.start ();
239+ streamSubscriptions.add (controller.local.stream.listen (port.postMessage));
240+ } else if (message.data['ready' ] == true ) {
240241 // This message indicates that the iframe is actively listening for
241242 // events, so the message channel's second port can now be transferred.
243+ final DomMessageChannel channel = createDomMessageChannel ();
244+ channel.port1.start ();
242245 channel.port2.start ();
243- iframe.contentWindow.postMessage ('port' , domWindow.location.origin,
244- < DomMessagePort > [channel.port2]);
245- readyCompleter.complete ();
246+ iframe.contentWindow.postMessage (
247+ 'port' , domWindow.location.origin, < DomMessagePort > [channel.port2]);
248+ domSubscriptions
249+ .add (DomSubscription (channel.port1, 'message' , (DomEvent message) {
250+ controller.local.sink.add ((message as DomMessageEvent ).data['data' ]);
251+ }));
252+
253+ streamSubscriptions
254+ .add (controller.local.stream.listen (channel.port1.postMessage));
246255 } else if (message.data['exception' ] == true ) {
247256 // This message from `dart.js` indicates that an exception occurred
248257 // loading the test.
249258 controller.local.sink.add (message.data['data' ]);
250259 }
251260 }));
252261
253- channel.port1.start ();
254- domSubscriptions.add (DomSubscription (channel.port1, 'message' ,
255- (DomEvent message) {
256- controller.local.sink.add ((message as DomMessageEvent ).data['data' ]);
257- }));
258-
259- streamSubscriptions.add (controller.local.stream.listen ((dynamic message) async {
260- await readyCompleter.future;
261- channel.port1.postMessage (message);
262- }));
263262
264263 return controller.foreign;
265264}
0 commit comments