-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
100 lines (83 loc) · 2.88 KB
/
browser.js
File metadata and controls
100 lines (83 loc) · 2.88 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var Server = require ('./browser/Server');
var MultimediaStream = require ('./browser/MultimediaStream');
var EventEmitter = require ('events').EventEmitter;
var url = require ('url');
var eventQueue = {};
/** @module substation
@super EventEmitter
Realtime application gateway and authentication provider.
*/
var servers = {};
var core = new EventEmitter();
module.exports = core;
core.MultimediaStream = MultimediaStream;
core.getUserMedia = MultimediaStream.getUserMedia;
/** @property/Function inherit
There are two big issues with `util.inherits`: It won't work with IE <9 and you have to bundle
the entire `util` package to get it. This version works around both issues.
@argument/Function child
@argument/Function parent
*/
core.inherit = require ('./browser/inherit');
/** @property/Function emit
*/
core.emit = function (eventName) {
if (this.listeners (eventName).length)
return EventEmitter.prototype.emit.apply (this, arguments);
if (Object.hasOwnProperty.call (eventQueue, eventName))
eventQueue[eventName].push (arguments);
else
eventQueue[eventName] = [ arguments ];
};
core.on ('newListener', function (event, listener) {
if (!Object.hasOwnProperty.call (eventQueue, event))
return;
var queue = eventQueue[event];
delete eventQueue[event];
var i=0, j=queue.length;
process.nextTick (function queueStep(){
EventEmitter.emit.apply (core, queue[i]);
i++;
if (i >= j)
return;
process.nextTick (queueStep);
});
});
/** @property/Function getServer
@argument/String path
@returns/substation.Server server
*/
function getServer (path, options) {
var host = path ? url.parse (path) : window.location;
if (!host) throw new Error ('invalid url');
if (Object.hasOwnProperty.call (servers, host)) {
var server = servers[host];
if (options)
server.updateOptions (options);
return server;
}
return servers[host] = new Server (core, host, options);
}
core.getServer = getServer;
/** @property/Function sendEvents
Fire an Array of events from the module Object and this domain's Server instance, if it already
exists.
*/
function sendEvents (events) {
for (var i=0,j=events.length; i<j; i++)
core.emit.apply (core, events[i]);
}
core.sendEvents = sendEvents;
/** @property/events.EventEmitter otherTabs
@super events.EventEmitter
Emit events from the `otherTabs` Object in other tabs.
*/
window.otherTabs = new EventEmitter();
window.otherTabs.emit = function(){
var info = Array.prototype.slice.call (arguments);
window.localStorage.setItem ('__substation_event', JSON.stringify (info));
};
window.addEventListener ('storage', function (event) {
if (event.key != '__substation_event') return;
EventEmitter.prototype.emit.apply (otherTabs.emitter, JSON.parse (event.newValue));
});