Socket
Socket
Sign inDemoInstall

@vechain/sdk-network

Package Overview
Dependencies
Maintainers
6
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vechain/sdk-network - npm Package Compare versions

Comparing version 1.0.0-beta.2 to 1.0.0-beta.3

6

package.json
{
"name": "@vechain/sdk-network",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"description": "This module allows to connect you seamlessly to the VechainThor blockchain",

@@ -37,6 +37,6 @@ "author": "vechain Foundation",

"@types/ws": "^8.5.10",
"@vechain/sdk-core": "1.0.0-beta.2",
"@vechain/sdk-errors": "1.0.0-beta.2",
"@vechain/sdk-core": "1.0.0-beta.3",
"@vechain/sdk-errors": "1.0.0-beta.3",
"axios": "^1.6.7"
}
}

@@ -8,3 +8,3 @@ import { AccountsModule } from './accounts';

import { GasModule } from './gas';
import { type HttpClient } from '../utils';
import { HttpClient } from '../utils';
import { DebugModule } from './debug';

@@ -79,2 +79,16 @@

/**
* Creates a new `ThorClient` instance from a given URL.
*
* @param networkUrl - The URL of the network to connect to.
* @param options - (Optional) Other optional parameters for polling and error handling.
* @returns A new `ThorClient` instance.
*/
public static fromUrl(
networkUrl: string,
options?: BlocksModuleOptions
): ThorClient {
return new ThorClient(new HttpClient(networkUrl), options);
}
/**
* Destroys the `ThorClient` instance by stopping the event polling

@@ -81,0 +95,0 @@ * and any other cleanup.

@@ -19,17 +19,20 @@ import { EventEmitter } from 'events';

/**
* The function to be called.
* The current iteration. It counts how many iterations have been done.
* This parameter is useful to know how many iterations have been done.
* For example, it can be used to stop the poll after a certain number of iterations.
*/
private readonly pollingFunction: () => Promise<TReturnType>;
private currentIteration: number = 0;
/**
* The interval of time (in milliseconds) between each request.
* Error thrown during the execution of the poll.
*/
private readonly requestIntervalInMilliseconds: number;
private error?: Error;
/**
* The current iteration. It counts how many iterations have been done.
* This parameter is useful to know how many iterations have been done.
* For example, it can be used to stop the poll after a certain number of iterations.
* Indicates whether to stop execution on error of the
* {@link _intervalLoop} function.
*
* @type {boolean}
*/
private currentIteration: number = 0;
private readonly hasToStopOnError: boolean;

@@ -42,18 +45,26 @@ /**

/**
* Error thrown during the execution of the poll.
* The function to be called.
*/
private error?: Error;
private readonly pollingFunction: () => Promise<TReturnType>;
/**
* Create a new eventPoll.
* The interval of time (in milliseconds) between each request.
*/
private readonly requestIntervalInMilliseconds: number;
/**
* Constructor for creating an instance of EventPoll.
*
* @param pollingFunction - The function to be called.
* @param requestIntervalInMilliseconds - The interval of time (in milliseconds) between each request.
* @param {Function} pollingFunction - The function to be executed repeatedly.
* @param {number} requestIntervalInMilliseconds - The interval in milliseconds between each execution of the polling function.
* @param {boolean} [hasToStopOnError=true] - Indicates whether to stop polling if an error occurs.
*/
constructor(
pollingFunction: () => Promise<TReturnType>,
requestIntervalInMilliseconds: number
requestIntervalInMilliseconds: number,
hasToStopOnError: boolean
) {
super();
this.pollingFunction = pollingFunction;
this.hasToStopOnError = hasToStopOnError;

@@ -80,25 +91,34 @@ // Positive number for request interval

/**
* Start listening to the event.
* Basic interval loop function.
* This function must be called into setInterval.
* It calls the promise and emit the event.
*/
startListen(): void {
// Start listening
this.emit('start', { eventPoll: this });
private async _intervalLoop(): Promise<void> {
try {
// Get data and emit the event
const data = await this.pollingFunction();
this.emit('data', { data, eventPoll: this });
} catch (error) {
// Set error
this.error = buildError(
'EventPoll - main interval loop function',
POLL_ERROR.POLL_EXECUTION_ERROR,
'Error during the execution of the poll',
{
message: (error as Error).message,
functionName: this.pollingFunction.name
}
);
// Execute `_intervalLoop` and then set an interval which calls `_intervalLoop` every `requestIntervalInMilliseconds`
void this._intervalLoop().then(() => {
// Create an interval
this.intervalId = setInterval(() => {
void (async () => {
await this._intervalLoop();
})();
}, this.requestIntervalInMilliseconds);
}); // No need for .catch(), errors are handled within _intervalLoop
}
// Emit the error
this.emit('error', { error: this.error });
/**
* Stop listening to the event.
*/
stopListen(): void {
clearInterval(this.intervalId);
this.emit('stop', { eventPoll: this });
// Stop listening?
if (this.hasToStopOnError) {
this.stopListen();
}
}
// Increment the iteration
this.currentIteration = this.currentIteration + 1;
}

@@ -140,21 +160,2 @@

/**
* Listen to the 'start' event.
* This happens when the poll is stopped.
*
* @param onStartCallback - The callback to be called when the event is emitted.
*/
public onStart(
onStartCallback: (eventPoll: EventPoll<TReturnType>) => void
): this {
this.on('start', (data) => {
onStartCallback(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
data.eventPoll as EventPoll<TReturnType>
);
});
return this;
}
/**
* Listen to the 'error' event.

@@ -184,2 +185,21 @@ * This method is the redefinition of the EventEmitter.on method.

/**
* Listen to the 'start' event.
* This happens when the poll is stopped.
*
* @param onStartCallback - The callback to be called when the event is emitted.
*/
public onStart(
onStartCallback: (eventPoll: EventPoll<TReturnType>) => void
): this {
this.on('start', (data) => {
onStartCallback(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
data.eventPoll as EventPoll<TReturnType>
);
});
return this;
}
/**
* Listen to the 'stop' event.

@@ -204,32 +224,25 @@ * This happens when the poll is stopped.

/**
* Basic interval loop function.
* This function must be called into setInterval.
* It calls the promise and emit the event.
* Start listening to the event.
*/
private async _intervalLoop(): Promise<void> {
try {
// Get data and emit the event
const data = await this.pollingFunction();
this.emit('data', { data, eventPoll: this });
} catch (error) {
// Set error
this.error = buildError(
'EventPoll - main interval loop function',
POLL_ERROR.POLL_EXECUTION_ERROR,
'Error during the execution of the poll',
{
message: (error as Error).message,
functionName: this.pollingFunction.name
}
);
startListen(): void {
// Start listening
this.emit('start', { eventPoll: this });
// Emit the error
this.emit('error', { error: this.error });
// Execute `_intervalLoop` and then set an interval which calls `_intervalLoop` every `requestIntervalInMilliseconds`
void this._intervalLoop().then(() => {
// Create an interval
this.intervalId = setInterval(() => {
void (async () => {
await this._intervalLoop();
})();
}, this.requestIntervalInMilliseconds);
}); // No need for .catch(), errors are handled within _intervalLoop
}
// Stop listening
this.stopListen();
}
// Increment the iteration
this.currentIteration = this.currentIteration + 1;
/**
* Stop listening to the event.
*/
stopListen(): void {
clearInterval(this.intervalId);
this.emit('stop', { eventPoll: this });
}

@@ -241,15 +254,22 @@

/**
* Create an event poll factory method.
* Creates an event poll that performs a callback function repeatedly at a specified interval.
* This method is useful to create an event poll in a more readable way.
*
* @param callBack - The function to be called.
* @param requestIntervalInMilliseconds - The interval of time (in milliseconds) between each request.
* @param {Function} callBack - The callback function to be executed on each interval. It should return a Promise.
* @param {number} requestIntervalInMilliseconds - The interval in milliseconds at which the callback function will be executed.
* @param {boolean} [hasToStopOnError=true] - Optional parameter to specify whether the poll should stop on error. Default is true.
* @returns {EventPoll} - The created event poll instance.
*/
function createEventPoll<TReturnType>(
callBack: () => Promise<TReturnType>,
requestIntervalInMilliseconds: number
requestIntervalInMilliseconds: number,
hasToStopOnError: boolean = true
): EventPoll<TReturnType> {
return new EventPoll<TReturnType>(callBack, requestIntervalInMilliseconds);
return new EventPoll<TReturnType>(
callBack,
requestIntervalInMilliseconds,
hasToStopOnError
);
}
export { EventPoll, createEventPoll };

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc