-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (60 loc) · 2.21 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
var EventEmitter = require( 'eventemitter2' ).EventEmitter2;
var extend = require( 'extend' );
module.exports = BrowserEventBus;
function BrowserEventBus( options ) {
var self = this;
EventEmitter.call( self );
self._options = extend( {
namespace: '',
target: '*',
origin: '*'
}, options );
window.addEventListener( 'message', self._onMessage.bind( self ), false );
}
BrowserEventBus.supported = ( 'postMessage' in window ) && ( 'bind' in function(){} ) && ( 'JSON' in window );
BrowserEventBus.prototype = Object.create( EventEmitter.prototype, {} );
BrowserEventBus.prototype._emit = BrowserEventBus.prototype.emit;
BrowserEventBus.prototype.emit = function() {
var self = this;
var args = Array.prototype.slice.call( arguments, 0 );
var event = ( self._options.namespace ? self._options.namespace + ':' : '' ) + JSON.stringify( args );
// get all our contained frames
var targets = Array.prototype.slice.call( window.frames, 0 );
// walk up any iframe tree
var win = ( window === window.parent ) ? null : window.parent;
while( win ) {
targets.push( win );
win = ( win === win.parent ) ? null : win.parent;
}
targets.forEach( function( target ) {
if ( target !== window ) {
target.postMessage( event, self._options.target );
}
} );
};
BrowserEventBus.prototype._onMessage = function( event ) {
var self = this;
if ( self._options.namespace && event.data.indexOf( self._options.namespace ) !== 0 ) {
return;
}
if ( self._options.origin !== '*' && event.origin !== self._options.origin ) {
return;
}
var json = event.data.slice( self._options.namespace ? self._options.namespace.length + 1 : 0 );
var msg = null;
try {
msg = JSON.parse( json );
}
catch( ex ) {
msg = null;
self._emit( 'error', 'browser-event-bus: ' + ex );
return;
}
if ( !Array.isArray( msg ) ) {
self._emit( 'error', new Error( 'browser-event-bus: Did not get an array from event: ' + event.data ) );
return;
}
msg.push( event );
self._emit.apply( self, msg );
};