Answers for "eventemitter nodejs"

6

node js event emitter

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {
  foo() {
    this.emit('test');
  }
}
const myEmitter = new MyEmitter();
myEmitter.on('test', () => console.log('Yay, it works!'));

myEmitter.foo();	// Yay it works!
Posted by: Guest on October-29-2020
2

event module in node js

const myEmitter = new MyEmitter();
let m = 0;
myEmitter.on('event', () => {
  console.log(++m);
});
myEmitter.emit('event');
// Prints: 1
myEmitter.emit('event');
// Prints: 2
Posted by: Guest on September-04-2020
0

eventemitter nodejs

const { EventEmitter } = require('eventemitter3')
const util = require('util')
const zlib = require('zlib')

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

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

DataDriven.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
	})

	this.emit(eventName, decompress.toString('utf-8'))
}

DataDriven.prototype.sub = function (eventName, callback) {
	this.on(eventName, function (data) {
		callback(JSON.parse(data).data)
	})
}

const dataDriven = new DataDriven()

exports.event = {
	pub(eventName, data) {
		dataDriven.pub(eventName, data)
	},
	sub(eventName) {
		return new Promise(function (resolve, reject) {
			dataDriven.sub(eventName, (res) => resolve(res))
		})
	}
}
Posted by: Guest on July-31-2021
1

eventemitter nodejs

myEmitter.on('status', (code, msg)=> console.log(`Got ${code} and ${msg}`));
Posted by: Guest on September-04-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language