@parity/api
Version:
The Parity Promise-based API library for interfacing with Ethereum over RPC
167 lines (166 loc) • 6.06 kB
JavaScript
;
// 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');
// https://github.com/electron/electron/issues/2288
var IS_ELECTRON = !!(typeof window !== 'undefined' && window && window.process && window.process.type);
var ipcRenderer;
if (IS_ELECTRON) {
ipcRenderer = window.require('electron').ipcRenderer;
}
var METHOD_REQUEST_TOKEN = 'shell_requestNewToken';
var Ipc = /** @class */ (function (_super) {
__extends(Ipc, _super);
function Ipc(appId) {
var _this = _super.call(this) || this;
_this._appId = appId;
_this.id = 0;
_this._messages = {};
_this._queued = [];
if (!IS_ELECTRON) {
throw new Error('IpcProvider must be used in Electron environment.');
}
ipcRenderer.on('PARITY_SHELL_IPC_CHANNEL', _this.receiveMessage.bind(_this));
return _this;
}
Ipc.prototype._constructMessage = function (id, data) {
return Object.assign({}, data, {
id: id,
to: 'shell',
from: this._appId,
token: this._token
});
};
Ipc.prototype.receiveMessage = function (_, _a) {
var id = _a.id, error = _a.error, from = _a.from, to = _a.to, token = _a.token, result = _a.result;
var isTokenValid = token ? token === this._token : true;
if (from !== 'shell' || to !== this._appId || !isTokenValid) {
return;
}
if (this._messages[id].subscription) {
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;
}
};
Ipc.prototype.requestNewToken = function () {
var _this = this;
return new Promise(function (resolve, reject) {
// Webview is ready when receivin the ping
ipcRenderer.once('ping', function () {
_this.send(METHOD_REQUEST_TOKEN, [], function (error, token) {
if (error) {
reject(error);
}
else {
_this.setToken(token);
resolve(token);
}
});
});
});
};
Ipc.prototype._send = function (message) {
if (!this._token && message.data.method !== METHOD_REQUEST_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);
ipcRenderer.sendToHost('parity', { data: postMessage });
};
Ipc.prototype.send = function (method, params, callback) {
this._send({
data: {
method: method,
params: params
},
options: {
callback: callback
}
});
};
Ipc.prototype._sendQueued = function () {
if (!this._token) {
return;
}
this._queued.forEach(this._send.bind(this));
this._queued = [];
};
Ipc.prototype.setToken = function (token) {
if (token) {
this._connected = true;
this._token = token;
this.emit('connected');
this._sendQueued();
}
};
Ipc.prototype.subscribe = function (api, callback, params) {
var _this = this;
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
Ipc.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);
}
}
});
});
};
Ipc.prototype.unsubscribeAll = function () {
return this.unsubscribe('*');
};
return Ipc;
}(EventEmitter));
module.exports = Ipc;