forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer-integr-messaging.js
169 lines (148 loc) · 4.43 KB
/
viewer-integr-messaging.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This is a very simple messaging protocol between viewer and viewer client.
* @param {!Window} target
* @param {function(string, *, boolean):(!Promise<*>|undefined)}
* requestProcessor
* @constructor
*/
function ViewerMessaging(target, requestProcessor) {
this.sentinel_ = '__AMP__';
this.requestSentinel_ = this.sentinel_ + 'REQUEST';
this.responseSentinel_ = this.sentinel_ + 'RESPONSE';
this.requestIdCounter_ = 0;
this.waitingForResponse_ = {};
this.target_ = target;
this.requestProcessor_ = requestProcessor;
window.addEventListener('message', this.onMessage_.bind(this), false);
}
/**
* @param {string} eventType
* @param {*} payload
* @param {boolean} awaitResponse
* @return {!Promise<*>|undefined}
*/
ViewerMessaging.prototype.sendRequest = function(eventType, payload,
awaitResponse) {
var requestId = ++this.requestIdCounter_;
if (awaitResponse) {
var promise = new Promise(function(resolve, reject) {
this.waitingForResponse_[requestId] = {resolve: resolve, reject: reject};
}.bind(this));
this.sendMessage_(this.requestSentinel_, requestId, eventType, payload,
true);
return promise;
}
this.sendMessage_(this.requestSentinel_, requestId, eventType, payload,
false);
return undefined;
};
/**
* @param {!Event} event
* @private
*/
ViewerMessaging.prototype.onMessage_ = function(event) {
if (event.source != this.target_) {
return;
}
// TODO: must check for origin/target.
var message = event.data;
if (message.sentinel == this.requestSentinel_) {
this.onRequest_(message);
} else if (message.sentinel == this.responseSentinel_) {
this.onResponse_(message);
}
};
/**
* @param {*} message
* @private
*/
ViewerMessaging.prototype.onRequest_ = function(message) {
var requestId = message.requestId;
var promise = this.requestProcessor_(message.type, message.payload,
message.rsvp);
if (message.rsvp) {
if (!promise) {
this.sendResponseError_(requestId, 'no response');
throw new Error('expected response but none given: ' + message.type);
}
promise.then(function(payload) {
this.sendResponse_(requestId, payload);
}.bind(this), function(reason) {
this.sendResponseError_(requestId, reason);
}.bind(this));
}
};
/**
* @param {*} message
* @private
*/
ViewerMessaging.prototype.onResponse_ = function(message) {
var requestId = message.requestId;
var pending = this.waitingForResponse_[requestId];
if (pending) {
delete this.waitingForResponse_[requestId];
if (message.type == 'ERROR') {
pending.reject(message.payload);
} else {
pending.resolve(message.payload);
}
}
};
/**
* @param {string} sentinel
* @param {string} requestId
* @param {string} eventType
* @param {*} payload
* @param {boolean} awaitResponse
* @private
*/
ViewerMessaging.prototype.sendMessage_ = function(sentinel, requestId,
eventType, payload, awaitResponse) {
// TODO: must check for origin/target.
var message = {
sentinel: sentinel,
requestId: requestId,
type: eventType,
payload: payload,
rsvp: awaitResponse
};
this.target_./*TODO-REVIEW*/postMessage(message, '*');
};
/**
* @param {number} requestId
* @param {*} payload
* @private
*/
ViewerMessaging.prototype.sendResponse_ = function(requestId, payload) {
this.sendMessage_(this.responseSentinel_, requestId, null, payload, false);
};
/**
* @param {number} requestId
* @param {*} reason
* @private
*/
ViewerMessaging.prototype.sendResponseError_ = function(requestId, reason) {
this.sendMessage_(this.responseSentinel_, requestId, 'ERROR', reason, false);
};
/**
* Super crude way to share ViewerMessaging class without any kind of module
* system or packaging.
*/
if (window['__AMP_VIEWER_MESSAGING_CALLBACK']) {
window['__AMP_VIEWER_MESSAGING_CALLBACK'](ViewerMessaging);
}