Answers for "eventemitter to promise"

0

eventemitter to promise

const { EventEmitter } = require('eventemitter3')

/**
 * @description Create owner custom costructor for inhertitance event emitter data
 */
function EventDriven() {
	this.resolves
	this.rejects
	EventEmitter.call(this)
}

/**
 * @description Inheritance all event core method from EventEmitter to EventDriven Function
 */
util.inherits(EventDriven, EventEmitter)

EventDriven.prototype.pub = function (eventName, data) {
	const compress = $zlib.brotliCompressSync(JSON.stringify({ data }), {
		chunkSize: 999999999,
		maxOutputLength: 999999999
	})

	const decompress = $zlib.brotliDecompressSync(compress, {
		chunkSize: 999999999,
		maxOutputLength: 999999999
	})

	const promise = new Promise((resolve, reject) => {
		this.resolves = resolve
		this.rejects = reject
	})

	promise.emit = this.emit
	promise.on = this.on

	process.nextTick(() => this.emit(eventName, decompress.toString('utf-8')))
	process.nextTick(() => this.off(eventName))
	return this
}

EventDriven.prototype.sub = function (eventName) {
	let self = this
	return new Promise(function (resolve, reject) {
		self.on(eventName, function (data) {
			resolve(JSON.parse(data).data)
		})
	})
}

exports.event = new EventDriven()
Posted by: Guest on August-01-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language