Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
*/
OstWSProvider.ManagedEventsMap = {
connect: { managedEvent: 'connect', callback: 'onopen', ostCallback: 'onConnectionOpen' },
end: { managedEvent: 'end', callback: 'onclose', ostCallback: 'onConnectionClose' },
error: { managedEvent: 'error', callback: 'onerror', ostCallback: 'onConnectionError' },
dead: { managedEvent: 'dead', callback: null, ostCallback: null }
};
//An array of Managed Event Names. (Useful to automate the code).
OstWSProvider.ManagedEvents = Object.keys(OstWSProvider.ManagedEventsMap);
// END: Static Stuff
// BEGIN : Prototype Stuff Begins
// Derive the prototype.
OstWSProvider.prototype = Object.create(WebsocketProvider.prototype);
// BEGIN : Declare new props if needed.
// These properties will be initialised by constructor.
OstWSProvider.prototype.endPointUrl = null;
OstWSProvider.prototype.eventEmitter = null;
OstWSProvider.prototype.options = null;
// END: New Props.
// BEGIN : Declare New Methods if needed.
// Helper Method to emit event.
OstWSProvider.prototype.emitEvent = function(eventName, args) {
const oThis = this;
// Check if we have listeners. This check is important for 'error' event.
const eventNames = oThis.eventEmitter.eventNames();
if (eventNames.indexOf(eventName) < 0) {
logger.error('Failed to reconnect WebSocket with endpoint ', oThis.endPointUrl);
//Emit Dead Event
oThis.emitEvent('dead', [oThis]);
if (oThis.options.killOnReconnectFailure) {
logger.warn('Shutting down proccess');
process.kill(process.pid, 'SIGTERM');
}
};
// END: New Methods.
// BEGIN : Override methods as needed.
//addDefaultEvents
const _base_addDefaultEvents = WebsocketProvider.prototype.addDefaultEvents;
OstWSProvider.prototype.addDefaultEvents = function() {
const oThis = this;
//Call the super method.
_base_addDefaultEvents.apply(oThis, arguments);
const connection = oThis.connection;
//Meta Info of connection callback to manage.
var eventMeta,
//Name of event to emit. E.g. "end" when connection ends. (Note: web3js decides this).
eventName,
// Same as eventName, but, is read from managedEvent.
// This is useful to change the emmited event name. Can also be set to null if you wish to prevent emiting event.
managedEventName,
//Function Name of callback. E.g: "onclose" for connection.onclose