Skip to content
Snippets Groups Projects
Verified Commit e0c9ad3f authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll:
Browse files

Improve WorkerBee events typization

parent b2c9f993
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,31 @@ export const DEFAULT_WORKERBEE_OPTIONS = { ...@@ -40,6 +40,31 @@ export const DEFAULT_WORKERBEE_OPTIONS = {
export const DEFAULT_BLOCK_INTERVAL_TIMEOUT = 1500; export const DEFAULT_BLOCK_INTERVAL_TIMEOUT = 1500;
interface IWorkerBeeEvents {
"stop": () => void | Promise<void>;
"start": () => void | Promise<void>;
"block": (blockData: IBlockData) => void | Promise<void>;
"transaction": (transactionData: ITransactionData) => void | Promise<void>;
"error": (error: WorkerBeeError) => void | Promise<void>;
}
/* eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging */
export declare interface WorkerBee {
on<U extends keyof IWorkerBeeEvents>(
event: U, listener: IWorkerBeeEvents[U]
): this;
once<U extends keyof IWorkerBeeEvents>(
event: U, listener: IWorkerBeeEvents[U]
): this;
off<U extends keyof IWorkerBeeEvents>(
event: U, listener: IWorkerBeeEvents[U]
): this;
}
/* eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging */
export class WorkerBee extends EventEmitter implements IWorkerBee { export class WorkerBee extends EventEmitter implements IWorkerBee {
public running: boolean = false; public running: boolean = false;
...@@ -150,7 +175,7 @@ export class WorkerBee extends EventEmitter implements IWorkerBee { ...@@ -150,7 +175,7 @@ export class WorkerBee extends EventEmitter implements IWorkerBee {
} // Else -> no new block } // Else -> no new block
} catch (error) { } catch (error) {
// Ensure we are emitting the Error instance // Ensure we are emitting the Error instance
super.emit("error", error instanceof Error ? error : new Error(`Unknown error occurred during automation: ${String(error)}`)); super.emit("error", new WorkerBeeError(`Error occurred during automation: ${String(error)}`, error));
// Wait before any next operation is performed to reduce spamming the API // Wait before any next operation is performed to reduce spamming the API
await new Promise(res => { setTimeout(res, DEFAULT_BLOCK_INTERVAL_TIMEOUT); }); await new Promise(res => { setTimeout(res, DEFAULT_BLOCK_INTERVAL_TIMEOUT); });
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @internal * @internal
*/ */
export class WorkerBeeError extends Error { export class WorkerBeeError extends Error {
public constructor(message: string) { public constructor(message: string, public readonly originator?: Error | any) {
super(message); super(message);
this.name = "WorkerBeeError"; this.name = "WorkerBeeError";
} }
......
...@@ -2,7 +2,7 @@ import { createHiveChain, IHiveChainInterface, IWaxOptionsChain, TWaxExtended } ...@@ -2,7 +2,7 @@ import { createHiveChain, IHiveChainInterface, IWaxOptionsChain, TWaxExtended }
export const WaxExtendTypes = {}; export const WaxExtendTypes = {};
export const getWax = async (explicitHiveChain?: IHiveChainInterface, options?: Partial<IWaxOptionsChain>): Promise<TWaxExtended<typeof WaxExtendTypes>> => { export const getWax = async(explicitHiveChain?: IHiveChainInterface, options?: Partial<IWaxOptionsChain>): Promise<TWaxExtended<typeof WaxExtendTypes>> => {
if(explicitHiveChain === undefined) if(explicitHiveChain === undefined)
explicitHiveChain = await createHiveChain(options); explicitHiveChain = await createHiveChain(options);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment