Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'web3-providers-ws' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
*/
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
constructor(host, options, store, eventHub) {
this.wsProvider = new Web3WSProvider(host, options);
this.oWSProvider = new Web3WSProvider(host, options);
this.lastMessage = new Date().getTime();
const keepAlive = () => {
if (
this.oWSProvider.connection.readyState ===
this.oWSProvider.connection.OPEN
)
this.wsProvider.connection.send(
'{"jsonrpc":"2.0","method":"net_version","params":[],"id":0}'
);
if (
this.wsProvider.connection.readyState ===
this.wsProvider.connection.OPEN
)
this.oWSProvider.connection.send(
'{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}'
);
constructor(host, options, store, eventHub) {
this.wsProvider = new Web3WSProvider(host, options);
this.oWSProvider = new Web3WSProvider(host, options);
this.lastMessage = new Date().getTime();
const keepAlive = () => {
if (
this.oWSProvider.connection.readyState ===
this.oWSProvider.connection.OPEN
)
this.wsProvider.connection.send(
'{"jsonrpc":"2.0","method":"net_version","params":[],"id":0}'
);
if (
this.wsProvider.connection.readyState ===
this.wsProvider.connection.OPEN
)
this.oWSProvider.connection.send(
'{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}'
const OstWSProvider = (module.exports = function(url, i_d_k, options) {
const oThis = this;
//Note Down url
oThis.endPointUrl = url;
//Take Care of Options
oThis.options = Object.assign({}, OstWSProvider.DefaultOptions, options || {});
//Create Event Emiter.
oThis.eventEmitter = new Events.EventEmitter();
oThis.eventEmitter.setMaxListeners(oThis.options.emitterMaxListeners);
//Call the baseclass constructor with arguments.
return WebsocketProvider.apply(oThis, arguments) || oThis;
});