source: trip-planner-front/node_modules/sockjs-client/lib/info-iframe.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1'use strict';
2
3var EventEmitter = require('events').EventEmitter
4 , inherits = require('inherits')
5 , JSON3 = require('json3')
6 , utils = require('./utils/event')
7 , IframeTransport = require('./transport/iframe')
8 , InfoReceiverIframe = require('./info-iframe-receiver')
9 ;
10
11var debug = function() {};
12if (process.env.NODE_ENV !== 'production') {
13 debug = require('debug')('sockjs-client:info-iframe');
14}
15
16function InfoIframe(baseUrl, url) {
17 var self = this;
18 EventEmitter.call(this);
19
20 var go = function() {
21 var ifr = self.ifr = new IframeTransport(InfoReceiverIframe.transportName, url, baseUrl);
22
23 ifr.once('message', function(msg) {
24 if (msg) {
25 var d;
26 try {
27 d = JSON3.parse(msg);
28 } catch (e) {
29 debug('bad json', msg);
30 self.emit('finish');
31 self.close();
32 return;
33 }
34
35 var info = d[0], rtt = d[1];
36 self.emit('finish', info, rtt);
37 }
38 self.close();
39 });
40
41 ifr.once('close', function() {
42 self.emit('finish');
43 self.close();
44 });
45 };
46
47 // TODO this seems the same as the 'needBody' from transports
48 if (!global.document.body) {
49 utils.attachEvent('load', go);
50 } else {
51 go();
52 }
53}
54
55inherits(InfoIframe, EventEmitter);
56
57InfoIframe.enabled = function() {
58 return IframeTransport.enabled();
59};
60
61InfoIframe.prototype.close = function() {
62 if (this.ifr) {
63 this.ifr.close();
64 }
65 this.removeAllListeners();
66 this.ifr = null;
67};
68
69module.exports = InfoIframe;
Note: See TracBrowser for help on using the repository browser.