-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (84 loc) · 2.59 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var EventEmitter = require('events').EventEmitter,
inherits = require('inherits');
var HERMES_READY = 'HERMES_READY';
function Hermes(frame, origin) {
this.targetFrame = frame;
this.targetOrigin = origin || '*';
this.ready = false;
this.callbacks = {};
this.callbackId = 0;
this._messageListener = this._receiveMessage.bind(this);
window.addEventListener('message', this._messageListener);
}
inherits(Hermes, EventEmitter);
Hermes.prototype.send = function send(data, cb) {
if (this.destroyed) {
throw new Error('Hermes instance already destroyed');
} else if (!this.targetFrame) {
throw new Error('Hermes not set up to send data, only receive & respond');
}
var obj = { data: data };
if (cb) {
obj._responseId = this._serializeCb(cb);
}
var text = this._serializeData(obj);
this.targetFrame.postMessage(text, this.targetOrigin);
};
Hermes.prototype.announceReady = function announceReady() {
this.send({
type: HERMES_READY
});
};
Hermes.prototype.destroy = function destroy() {
this.destroyed = true;
this.removeAllListeners();
window.removeEventListener('message', this._messageListener);
};
Hermes.prototype._receiveMessage = function _receiveMessage(event) {
if (this.targetOrigin !== '*' && event.origin !== this.targetOrigin) {
return;
}
var json, _this = this;
try {
json = JSON.parse(event.data);
} catch (err) {
console.error('Hermes: Error parsing response', event.data);
return;
}
// Ready event
if (json.type === HERMES_READY) {
this.ready = true;
this.emit('ready');
return;
}
// Response to a specific request, don't emit event, just call cb
if (json._callbackId !== undefined && this.callbacks[json._callbackId]) {
this.callbacks[json._callbackId](json.err, json.success);
delete this.callbacks[json._callbackId];
return;
}
// If expecting a response, give a way to respond
var cb;
if (json._responseId !== undefined && json._responseId !== null) {
cb = function(err, success) {
// Respond to the sender
event.source.postMessage(_this._serializeData({
_callbackId: json._responseId,
err: err,
success: success
}), event.origin);
};
}
// Emit a message and give ability to respond
this.emit('message', json.data, cb, event.source, event.origin);
};
Hermes.prototype._serializeData = function _serializeData(data) {
return JSON.stringify(data);
};
Hermes.prototype._serializeCb = function _serializeCb(cb) {
var id = this.callbackId;
this.callbacks[this.callbackId++] = cb;
return id;
};
module.exports = Hermes;