Let’s say I have a socket, which will have data written to it. It might output messages like this.
writing names
doing x y and z
I love cats
:
What I am wondering is, what is the appropriate way to handle this with Futures? Here’s kind of what I have.
let socket = net.connect(connection);
let listenForMessage = function (socket, message, callback) {
let future = new Future();
let listenFunc = Meteor.bindEnvironment(function (future, message, data) {
let resolver = future.resolver();
// do something with data
callback(data.toString('ascii'));
if (message.test(data.toString('ascii'))) {
done();
}
}.bind(this, future, message));
future.resolve(() => {
socket.removeListener('data', listener);
});
socket.addListener('data', listener);
future.wait();
};
I’m not sure if this is the right way to do it. The alternative is some kind of observer pattern.
let socket = net.connect(connection);
let messages = new Mongo.Collection('_messages');
socket.addListener('data', function (data) {
messages.insert({
addedAt: new Date,
message: data.toString('ascii')
});
});
messages.find().observe({
added: function () { /**do something with data*/ }
});
What’s the most “meteoric” way to handle this?