/* eslint no-console: 0 */
var DBWrkr = require('dbwrkr').DBWrkr;
var DBWrkrMongo = require('dbwrkr-mongodb');
var flw = require('flw');


// Setup DBWorker
//
var wrkr = new DBWrkr({
  storage: new DBWrkrMongo({
    dbName: 'dbwrkr-example'
  })
});
wrkr.on('error', function (error) {
  console.log('****** - error', error);
});
wrkr.on('dispatchWarning', function (warning) {
  console.log('****** - warning', warning);
});
wrkr.on('eventError', function (eventError) {
  console.log('****** - eventError', eventError);
});



// Start
//
return flw.series([
  connect,
  subscribeEvent,
  sendEvents,
  startPolling,
], function (err) {
  if (err) throw err;
});


function connect(c, cb) {
  console.log('connect');
  return wrkr.connect(cb);
}


// function disconnect(c, cb) {
//   console.log('disconnect');
//   return wrkr.disconnect(cb);
// }


function subscribeEvent(c, cb) {
  console.log('subscribeEvent');
  return wrkr.subscribe('example_event', 'example_queue', exampleEventHandler, cb);

  function exampleEventHandler(event, done) {
    console.log('got event', event.id, event.name, event.created);
    return done();
  }
}


function sendEvents(c, cb) {
  console.log('sendEvents');
  var intervalMs = 500;

  setTimeout(sendEvents, intervalMs);
  return cb();

  function sendEvents() {
    wrkr.publish({name: 'example_event'}, function (err, resultIds) {
      if (err) throw err;

      console.log('published id : ', resultIds[0]);
      return setTimeout(sendEvents, intervalMs);
    });
  }
}


function startPolling(c, cb) {
  console.log('startPolling');
  return wrkr.startPolling(cb);
}

@@ -154,45 +154,21 @@
 ker%0A
-//%0A
 var 
-wrkr = new DBWrkr(%7B%0A  
 storage
-:
+ =
  new
@@ -184,18 +184,16 @@
 ngo(%7B%0A  
- 
- 
 dbName: 
@@ -213,17 +213,60 @@
 le'%0A
-  
 %7D)
+;%0Avar wrkr = new DBWrkr(%7B%0A  storage: storage
 %0A%7D);%0A
+%0A
 wrkr
@@ -337,24 +337,25 @@
 error);%0A%7D);%0A
+%0A
 wrkr.on('dis
@@ -355,188 +355,226 @@
 on('
-dispatchWarning', function (warning) %7B%0A  console.log('****** - warning', warning);%0A%7D);%0Awrkr.on('eventError', function (eventError) %7B%0A  console.log('****** - eventError', eventError
+event', function (event, done) %7B%0A  var delayMs = new Date() - event.created;%0A  console.log(%0A    'received event', event.payload.counter, 'delay: ', delayMs,%0A    event.name, 'from queue', event.queue%0A  );%0A  return done(
 );%0A%7D
@@ -578,17 +578,16 @@
 ;%0A%7D);%0A%0A%0A
-%0A
 // Start
@@ -590,16 +590,41 @@
 tart%0A//%0A
+console.log('starting');%0A
 return f
@@ -829,112 +829,8 @@
 %7D%0A%0A%0A
-// function disconnect(c, cb) %7B%0A//   console.log('disconnect');%0A//   return wrkr.disconnect(cb);%0A// %7D%0A%0A%0A
 func
@@ -953,170 +953,12 @@
 e', 
-exampleEventHandler, cb);%0A%0A  function exampleEventHandler(event, done) %7B%0A    console.log('got event', event.id, event.name, event.created);%0A    return done();%0A  %7D
+cb);
 %0A%7D%0A%0A
@@ -1039,10 +1039,29 @@
 s = 
-50
+333;%0A  var counter = 
 0;%0A%0A
@@ -1074,33 +1074,32 @@
 imeout(sendEvent
-s
 , intervalMs);%0A 
@@ -1133,17 +1133,16 @@
 endEvent
-s
 () %7B%0A   
@@ -1156,16 +1156,23 @@
 ublish(%7B
+%0A      
 name: 'e
@@ -1184,16 +1184,56 @@
 e_event'
+,%0A      payload: %7Bcounter: counter%7D%0A    
 %7D, funct
@@ -1318,16 +1318,27 @@
  id : ',
+ counter++,
  resultI
@@ -1378,17 +1378,16 @@
 endEvent
-s
 , interv
@@ -1498,15 +1498,76 @@
 Polling(
+'example_queue', %7B%0A    idleTimer: 500,%0A    busyTimer: 0%0A  %7D, 
 cb);%0A%7D%0A

Update example