2017-05-31 12:08:04 +02:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-06-08 17:14:02 +02:00
|
|
|
export default class PostMessage {
|
|
|
|
id = 0;
|
2017-07-24 16:08:24 +02:00
|
|
|
_messages = {};
|
2017-06-08 17:14:02 +02:00
|
|
|
|
|
|
|
constructor (token, destination) {
|
|
|
|
this._token = token;
|
|
|
|
this._destination = destination;
|
|
|
|
|
|
|
|
window.addEventListener('message', this.receiveMessage, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
addMiddleware () {
|
|
|
|
}
|
2017-05-31 12:08:04 +02:00
|
|
|
|
2017-07-24 16:08:24 +02:00
|
|
|
_send (data) {
|
|
|
|
this._destination.postMessage(data, '*');
|
|
|
|
}
|
|
|
|
|
2017-05-31 12:08:04 +02:00
|
|
|
send = (method, params, callback) => {
|
2017-06-08 17:14:02 +02:00
|
|
|
const id = ++this.id;
|
|
|
|
|
2017-07-24 16:08:24 +02:00
|
|
|
this._messages[id] = { callback };
|
|
|
|
this._send({
|
2017-06-08 17:14:02 +02:00
|
|
|
id,
|
|
|
|
from: this._token,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
token: this._token
|
2017-07-24 16:08:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe = (api, callback, params) => {
|
2017-07-26 16:54:47 +02:00
|
|
|
// console.log('paritySubscribe', JSON.stringify(params), api, callback);
|
2017-07-24 16:08:24 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const id = ++this.id;
|
|
|
|
|
|
|
|
this._messages[id] = { callback, resolve, reject, subscription: true, initial: true };
|
|
|
|
this._send({
|
|
|
|
id,
|
|
|
|
from: this._token,
|
|
|
|
api,
|
|
|
|
params,
|
|
|
|
token: this._token
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe = (subId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const id = ++this.id;
|
|
|
|
|
|
|
|
this._messages[id] = { callback: (e, v) => e ? reject(e) : resolve(v) };
|
|
|
|
this._send({
|
|
|
|
id,
|
|
|
|
from: this._token,
|
|
|
|
subId,
|
|
|
|
token: this._token
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribeAll () {
|
|
|
|
return this.unsubscribe('*');
|
2017-06-08 17:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage = ({ data: { id, error, from, token, result }, origin, source }) => {
|
|
|
|
if (from !== 'shell' || token !== this._token) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-14 17:12:13 +02:00
|
|
|
if (error) {
|
|
|
|
console.error(from, error);
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:08:24 +02:00
|
|
|
if (this._messages[id].subscription) {
|
2017-07-26 16:54:47 +02:00
|
|
|
// console.log('subscription', result, 'initial?', this._messages[id].initial);
|
|
|
|
this._messages[id].initial
|
|
|
|
? this._messages[id].resolve(result)
|
|
|
|
: this._messages[id].callback(error && new Error(error), result);
|
2017-07-24 16:08:24 +02:00
|
|
|
this._messages[id].initial = false;
|
|
|
|
} else {
|
|
|
|
this._messages[id].callback(error && new Error(error), result);
|
|
|
|
this._messages[id] = null;
|
|
|
|
}
|
2017-05-31 12:08:04 +02:00
|
|
|
}
|
|
|
|
}
|