@parity/api
Version:
The Parity Promise-based API library for interfacing with Ethereum over RPC
176 lines (175 loc) • 6.43 kB
JavaScript
"use strict";
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
// 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/>.
var EventEmitter = require('eventemitter3');
var PostMessage = /** @class */ (function (_super) {
__extends(PostMessage, _super);
function PostMessage(appId, destination, source) {
var _this = _super.call(this) || this;
_this._appId = appId;
_this._destination = destination || window.parent;
_this.id = 0;
_this._connected = false;
_this._messages = {};
_this._queued = [];
_this._receiveMessage = _this._receiveMessage.bind(_this);
_this._send = _this._send.bind(_this);
_this.send = _this.send.bind(_this);
_this.subscribe = _this.subscribe.bind(_this);
_this.unsubscribe = _this.unsubscribe.bind(_this);
(source || window).addEventListener('message', _this._receiveMessage, false);
return _this;
}
Object.defineProperty(PostMessage.prototype, "isConnected", {
get: function () {
return this._connected;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PostMessage.prototype, "isParity", {
get: function () {
return true;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PostMessage.prototype, "queuedCount", {
get: function () {
return this._queued.length;
},
enumerable: true,
configurable: true
});
PostMessage.prototype.setToken = function (token) {
if (token) {
this._connected = true;
this._token = token;
this.emit('connected');
this._sendQueued();
}
};
PostMessage.prototype.addMiddleware = function () {
};
PostMessage.prototype._constructMessage = function (id, data) {
return Object.assign({}, data, {
id: id,
to: 'shell',
from: this._appId,
token: this._token
});
};
PostMessage.prototype._send = function (message) {
if (!this._token) {
this._queued.push(message);
return;
}
var id = ++this.id;
var postMessage = this._constructMessage(id, message.data);
this._messages[id] = Object.assign({}, postMessage, message.options);
this._destination.postMessage(postMessage, '*');
};
PostMessage.prototype.send = function (method, params, callback) {
this._send({
data: {
method: method,
params: params
},
options: {
callback: callback
}
});
};
PostMessage.prototype._sendQueued = function () {
if (!this._token) {
return;
}
this._queued.forEach(this._send);
this._queued = [];
};
PostMessage.prototype.subscribe = function (api, callback, params) {
var _this = this;
// console.log('paritySubscribe', JSON.stringify(params), api, callback);
return new Promise(function (resolve, reject) {
_this._send({
data: {
api: api,
params: params
},
options: {
callback: callback,
resolve: resolve,
reject: reject,
subscription: true,
initial: true
}
});
});
};
// FIXME: Should return callback, not promise
PostMessage.prototype.unsubscribe = function (subId) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._send({
data: {
subId: subId
},
options: {
callback: function (error, result) {
error
? reject(error)
: resolve(result);
}
}
});
});
};
PostMessage.prototype.unsubscribeAll = function () {
return this.unsubscribe('*');
};
PostMessage.prototype._receiveMessage = function (_a) {
var _b = _a.data, id = _b.id, error = _b.error, from = _b.from, to = _b.to, token = _b.token, result = _b.result, origin = _a.origin, source = _a.source;
var isTokenValid = token
? token === this._token
: true;
if (from !== 'shell' || to !== this._appId || !isTokenValid) {
return;
}
if (this._messages[id].subscription) {
// 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);
this._messages[id].initial = false;
}
else {
this._messages[id].callback(error && new Error(error), result);
this._messages[id] = null;
}
};
return PostMessage;
}(EventEmitter));
module.exports = PostMessage;