From 8815707137af660a47620e71445f57f8ec6884f2 Mon Sep 17 00:00:00 2001 From: puopg Date: Fri, 13 Nov 2015 11:03:24 -0800 Subject: [PATCH] Supporting email completion before generating output --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4d584f1..9dcbdf3 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,16 @@ var thru = require('through2'), mail = require('mailgun-send'); exports.config = function (opts) { mail.config(opts); }; -exports.send = function (obj) { +exports.send = function (obj) { var stream = thru.obj(transform, flush); if (obj) stream.write(obj); return stream; }; +exports.sendWait = function (obj) { + var stream = thru.obj(transformWait, flush); + if (obj) stream.write(obj); + return stream; +}; function transform (obj, encoding, callback) { if (typeof obj != 'object') return callback("Expecting object, but got " + typeof obj); @@ -15,6 +20,18 @@ function transform (obj, encoding, callback) { callback(); } +// Method will wait to pipe output until email is done being sent. +function transformWait (obj, encoding, callback) { + var me = this; + if (typeof obj != 'object') return callback("Expecting object, but got " + typeof obj); + + // Pipe the output after email has been sent + mail.send(obj, function(err) { + me.push(obj); + callback(); + }); +} + function flush (callback) { callback(); }