0

I've several events which I need to listen to with additional event and pass object:

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);

ls.stderr.on('data', (data) => {
  myObj.data = true
  //here I need to raise the event with the property data
});

ls.on('close', (code) => {
  myObj.close = true
  //here I need to raise the event with the property close
});

For example inside of every event I want to emit my events and send object with property. For example raise myEvent with my object and every following event will update property in my object like data,close,open

Let's say this is my object

var myObj ={
  open:true,
  data:false,
  close:true
}

How can I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

You can create Events using the Event object/API.

Create a custom event (copy/paste from linked source)

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

You'll likely want to know about the next section of the docs too that let you add custom data to you events:

var event = new CustomEvent('build', { 'detail': elem.dataset.time });

function eventHandler(e) {
  console.log('The time is: ' + e.detail);
}

This should work in all modern browsers except IE11. If you look at the docs, there is a longer example showing the work-around for older browsers.

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • Thanks Matthew , but we still need to support IE11...any other clean way to do that? –  Jan 16 '16 at 09:39
  • @Mark I just edited the answer slightly. If you read the docs a little more, there is an example solution under the header "The old-fashioned way" that should work for all browsers. – Matthew Herbst Jan 16 '16 at 09:48
0

The obvious way is to code your own small event emitter/listener.

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);

var eventer = {
    events: {},
    on: function(event, callback){
       if(!(typeof callback === 'function'){
           return;
       }
       if(!this.events[event]){
           this.events[event] = [];
       }
       this.events[event].push(callback);
   },
   trigger: function(event, data){
       if(!this.events[event]){
           this.events[event] = [];
       }
       this.events[event].forEach(function(callback){
           callback(data);
       }
   }
}
var myObj = {
   open: true,
   data: false,
   close: true
}

ls.on('close', (code) => {
    myObj.close = true;
    eventer.trigger('data-changed', myObj);
});

ls.stderr.on('data', (data) => {
    myObj.data = true;
    eventer.trigger('data-changed', myObj);
});

eventer.on('data-changed', (data) => {
     //action on close
});

Edit

Since you're on Node, you can use the EventEmitter, which works in a similar way:

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
const EventEmitter = require('events');
const util = require('util');

function MyEmitter() {
    EventEmitter.call(this);
}
util.inherits(MyEmitter, EventEmitter);

const myEmitter = new MyEmitter();
ls.stderr.on('data', (data) => {
    myObj.data = true;
    myEmitter.emit('data-changed', myObj);
});
Camille Wintz
  • 951
  • 7
  • 8
  • Thanks Camille 1+! what should I require and can you give complite example with my context I want to use it ...(not sure what is ls and way to call to ls.on and the myObj.on ,Thank you! –  Jan 16 '16 at 09:43
  • ls is from your exemple above: const ls = spawn('ls', ['-lh', '/usr']); I don't know where you need to track the changes, if you can provide an exemple of what you're trying to do, I can add more exemples – Camille Wintz Jan 16 '16 at 09:47
  • Thanks ive 3 questions :) , when I try it I got error for the two close property it defined twice ,2. there is a nice way to sperate events from the objects which mean the object will have only the boolean properties ... 3. do I need to require the event emiter of node ? –  Jan 16 '16 at 10:03
  • I've separated the two in my exemple. I didn't see you were on Node, so my exemple was in pure JS, so you don't need to import anything. I've added an exemple with the node event emitter. – Camille Wintz Jan 16 '16 at 10:13
  • HI Camille sorry for my delay in the response, so I need to use just the second option ? –  Jan 17 '16 at 07:42
  • Can you please provide a complite example since im facing erorrs like "TypeError: MyEmitter.emit is not a function" ... –  Jan 17 '16 at 08:10