aldikhan13

Custom EventEmitter To Faster Write/Read Data

Aug 1st, 2021 (edited)
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // you can use default eventemitter or using third part library, checkout about eventemitter3 in npm
  2. const { EventEmitter } = require('eventemitter3')
  3. const reusify = require('reusify')
  4.  
  5. /**
  6.  * @description Create owner custom costructor for inhertitance event emitter data
  7.  */
  8. function EventDriven() {
  9.     /**
  10.      * @description Inheritance all event core method from EventEmitter to reusify Function
  11.      */
  12.     this.reusify = reusify(EventEmitter).get()
  13.     Object.defineProperties(this.reusify, {
  14.         nextTick: {
  15.             value: process.nextTick,
  16.             writable: true,
  17.             enumerable: true
  18.         },
  19.         parse: {
  20.             value: JSON.parse,
  21.             writable: true,
  22.             enumerable: true
  23.         },
  24.         stringify: {
  25.             value: JSON.stringify,
  26.             writable: true,
  27.             enumerable: true
  28.         },
  29.         promise: {
  30.             value: Promise,
  31.             writable: true,
  32.             enumerable: true
  33.         },
  34.         eventName: {
  35.             value: undefined,
  36.             writable: true,
  37.             enumerable: true
  38.         },
  39.         data: {
  40.             value: undefined,
  41.             writable: true,
  42.             enumerable: true
  43.         },
  44.         response: {
  45.             value: undefined,
  46.             writable: true,
  47.             enumerable: true
  48.         }
  49.     })
  50. }
  51.  
  52. EventDriven.prototype.pub = function (eventName, data) {
  53.     this.reusify.eventName = eventName
  54.     this.reusify.data = data
  55.  
  56.     if (this.reusify.data !== undefined) {
  57.         this.reusify.nextTick(() => this.reusify.emit(this.reusify.eventName, this.reusify.stringify({ data: this.reusify.data })))
  58.         return true
  59.     }
  60.  
  61.     this.reusify.nextTick(() => this)
  62. }
  63.  
  64. EventDriven.prototype.sub = function (eventName) {
  65.     let self = this
  66.     return new self.reusify.promise(function (resolve, reject) {
  67.         self.reusify.eventName = eventName
  68.  
  69.         self.reusify.on(self.reusify.eventName, function (data) {
  70.             self.reusify.data = data
  71.             self.reusify.response = self.reusify.parse(self.reusify.data)
  72.             resolve(self.reusify.response.data)
  73.         })
  74.  
  75.         if (self.reusify.response !== (undefined && null)) {
  76.             /**
  77.              * @description after success unsubscribe event name
  78.              */
  79.  
  80.             self.reusify.nextTick(() => self.reusify.off(self.reusify.eventName))
  81.  
  82.             /**
  83.              * @description after subscribe success reset property value
  84.              */
  85.  
  86.             self.reusify.nextTick(() => {
  87.                 self.reusify.eventName = undefined
  88.                 self.reusify.data = undefined
  89.                 self.reusify.response = undefined
  90.             })
  91.         }
  92.     })
  93. }
  94.  
  95. exports.event = new EventDriven()
Add Comment
Please, Sign In to add comment