const { EventEmitter } = require('eventemitter3')
function EventDriven() {
this.resolves
this.rejects
EventEmitter.call(this)
}
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()