// @flow
import C from './constants';
import type { Callback, Reg, Runner } from '../types';


const Task = (registry: Reg, runner: Runner, logger: Object) => {
  let topic: string;
  let subscribeCallback = C.NOOP;

  const subscribe = (payload: any) => {
    // publish message on topic without delay
    logger.info(payload);
    return subscribeCallback(payload);
  };

  const define = async (topicName: string, callBack: Callback) => {
    topic = topicName;
    subscribeCallback = callBack;
    const task = {
      topic,
      subscribe: subscribeCallback,
    };
    await registry.addNewTask(task, runner);
  };

  const publish = async (payload: Object) => {
    // check with registry for valid topic
    // publish message on topic
    await runner.send(topic, payload);
    logger.info(topic, payload);
  };


  return {
    define,
    publish,
    subscribe,
  };
};

export default Task;


@@ -736,24 +736,36 @@
 ge on topic%0A
+    try %7B%0A  
     await ru
@@ -791,24 +791,26 @@
 yload);%0A    
+  
 logger.info(
@@ -809,32 +809,170 @@
 er.info(
-topic, payload);
+'FROM PRODUCER', topic, payload);%0A      registry.successCallback(topic, payload);%0A    %7D catch (ex) %7B%0A      registry.failureCallback(topic, payload);%0A    %7D
 %0A  %7D;%0A%0A%0A
@@ -1007,16 +1007,16 @@
 ublish,%0A
-
     subs
@@ -1022,16 +1022,28 @@
 scribe,%0A
+    runner,%0A
   %7D;%0A%7D;%0A

add callbacks to track publish failures