diff --git a/src/lib/core/EventWidget.ts b/src/lib/core/EventWidget.ts index 3260eaf45..cf32e693d 100644 --- a/src/lib/core/EventWidget.ts +++ b/src/lib/core/EventWidget.ts @@ -1,5 +1,6 @@ import { EventEmitter } from 'events'; import { NativeElement, Component, NativeRawPointer } from './Component'; +import { wrapWithActivateUvLoop } from '../utils/helpers'; function addDefaultErrorHandler(native: NativeElement, emitter: EventEmitter): void { native.subscribeToQtEvent('error'); @@ -38,7 +39,8 @@ export abstract class EventWidget extends Component { super(); if (native.initNodeEventEmitter) { this.emitter = new EventEmitter(); - native.initNodeEventEmitter(this.emitter.emit.bind(this.emitter)); + this.emitter.emit = wrapWithActivateUvLoop(this.emitter.emit.bind(this.emitter)); + native.initNodeEventEmitter(this.emitter.emit); } else { throw new Error('initNodeEventEmitter not implemented on native side'); } diff --git a/src/lib/core/bootstrap.ts b/src/lib/core/bootstrap.ts index 576b49fd7..bbff3799f 100644 --- a/src/lib/core/bootstrap.ts +++ b/src/lib/core/bootstrap.ts @@ -9,17 +9,7 @@ This is required inorder to make the timers work nicely due to merger of event loops */ -function noop(): void { - return; -} - -const wrapWithActivateUvLoop = (func: Function) => { - return (...args: any[]): any => { - const activateUvLoop = (process as any).activateUvLoop || noop; - activateUvLoop(); - return func(...args); - }; -}; +import { wrapWithActivateUvLoop } from '../utils/helpers'; function main(): void { process.nextTick = wrapWithActivateUvLoop(process.nextTick); diff --git a/src/lib/utils/helpers.ts b/src/lib/utils/helpers.ts index 0f70cbb79..d17c61c10 100644 --- a/src/lib/utils/helpers.ts +++ b/src/lib/utils/helpers.ts @@ -9,3 +9,15 @@ export function checkIfNativeElement(arg: any): boolean { export function checkIfNapiExternal(arg: any): boolean { return addon.NUtils.isNapiExternal(arg); } + +function noop(): void { + return; +} + +export const wrapWithActivateUvLoop = (func: Function) => { + return (...args: any[]): any => { + const activateUvLoop = (process as any).activateUvLoop || noop; + activateUvLoop(); + return func(...args); + }; +};