From d310b2cbe02bcbb36578573ed1162fe84401d56f Mon Sep 17 00:00:00 2001
From: Tristan Cavelier <tristan.cavelier@tiolive.com>
Date: Thu, 21 Feb 2013 09:32:04 +0100
Subject: [PATCH] 'Replace' job rule now add callbacks to the job. 'DontAccept'
 rule is not used anymore.

---
 src/jio/commands/command.js | 35 +++++++++++++++++++++++++++--------
 src/jio/jobs/job.js         | 11 ++---------
 src/jio/jobs/jobRules.js    | 26 +++++++++++++-------------
 test/jiotests.js            | 27 +++------------------------
 4 files changed, 45 insertions(+), 54 deletions(-)

diff --git a/src/jio/commands/command.js b/src/jio/commands/command.js
index bd63dfb..4f2be31 100644
--- a/src/jio/commands/command.js
+++ b/src/jio/commands/command.js
@@ -35,8 +35,8 @@ var command = function (spec, my) {
   priv.docid = spec.docid || priv.doc._id;
   priv.option = spec.options || {};
   priv.callbacks = spec.callbacks || {};
-  priv.success = priv.callbacks.success || function () {};
-  priv.error = priv.callbacks.error || function () {};
+  priv.success = [priv.callbacks.success || function () {}];
+  priv.error = [priv.callbacks.error || function () {}];
   priv.retry = function () {
     that.error({
       status: 13,
@@ -232,11 +232,14 @@ var command = function (spec, my) {
    */
   that.executeOn = function (storage) {};
   that.success = function (return_value) {
+    var i;
     priv.on_going = false;
-    priv.success(return_value);
+    for (i = 0; i < priv.success.length; i += 1) {
+      priv.success[i](return_value);
+    }
     priv.end(doneStatus());
-    priv.success = function () {};
-    priv.error = function () {};
+    priv.success = [];
+    priv.error = [];
   };
   that.retry = function (return_error) {
     priv.on_going = false;
@@ -247,15 +250,31 @@ var command = function (spec, my) {
     }
   };
   that.error = function (return_error) {
+    var i;
     priv.on_going = false;
-    priv.error(return_error);
+    for (i = 0; i < priv.error.length; i += 1) {
+      priv.error[i](return_error);
+    }
     priv.end(failStatus());
-    priv.success = function () {};
-    priv.error = function () {};
+    priv.success = [];
+    priv.error = [];
   };
   that.end = function () {
     priv.end(doneStatus());
   };
+  that.addCallbacks = function (success, error) {
+    if (arguments.length > 1) {
+      priv.success.push(success || function () {});
+      priv.error.push(error || function () {});
+    } else {
+      priv.success.push(function (response) {
+        (success || function () {})(undefined, response);
+      });
+      priv.error.push(function (err) {
+        (success || function () {})(err, undefined);
+      });
+    }
+  };
   that.onSuccessDo = function (fun) {
     if (fun) {
       priv.success = fun;
diff --git a/src/jio/jobs/job.js b/src/jio/jobs/job.js
index 6893dac..4fea85b 100644
--- a/src/jio/jobs/job.js
+++ b/src/jio/jobs/job.js
@@ -155,16 +155,9 @@ var job = function (spec) {
   * @param  {object} job The other job.
   */
   that.update = function (job) {
-    priv.command.error({
-      status: 12,
-      statusText: 'Replaced',
-      error: 'replaced',
-      message: 'Job has been replaced by another one.',
-      reason: 'job has been replaced by another one'
-    });
+    priv.command.addCallbacks(job.getCommand().onSuccessDo()[0],
+                              job.getCommand().onErrorDo()[0]);
     priv.date = new Date(job.getDate().getTime());
-    priv.command = job.getCommand();
-    priv.status = job.getStatus();
   };
 
   /**
diff --git a/src/jio/jobs/jobRules.js b/src/jio/jobs/jobRules.js
index 1aa3e26..5cd3a6c 100644
--- a/src/jio/jobs/jobRules.js
+++ b/src/jio/jobs/jobRules.js
@@ -210,7 +210,7 @@ var jobRules = (function () {
 
       For more information, see documentation
     */
-  that.addActionRule('post', true, 'post', that.dontAccept);
+  that.addActionRule('post', true, 'post', that.update);
   that.addActionRule('post', true, 'put', that.wait);
   that.addActionRule('post', true, 'get', that.wait);
   that.addActionRule('post', true, 'remove', that.wait);
@@ -221,12 +221,12 @@ var jobRules = (function () {
   that.addActionRule('post', false, 'remove', that.eliminate);
   that.addActionRule('post', false, 'putAttachment', that.wait);
 
-  that.addActionRule('put', true, 'post', that.dontAccept);
+  that.addActionRule('put', true, 'post', that.update);
   that.addActionRule('put', true, 'put', that.wait);
   that.addActionRule('put', true, 'get', that.wait);
   that.addActionRule('put', true, 'remove', that.wait);
   that.addActionRule('put', true, 'putAttachment', that.wait);
-  that.addActionRule('put', false, 'post', that.dontAccept);
+  that.addActionRule('put', false, 'post', that.update);
   that.addActionRule('put', false, 'put', that.update);
   that.addActionRule('put', false, 'get', that.wait);
   that.addActionRule('put', false, 'remove', that.eliminate);
@@ -234,7 +234,7 @@ var jobRules = (function () {
 
   that.addActionRule('get', true, 'post', that.wait);
   that.addActionRule('get', true, 'put', that.wait);
-  that.addActionRule('get', true, 'get', that.dontAccept);
+  that.addActionRule('get', true, 'get', that.update);
   that.addActionRule('get', true, 'remove', that.wait);
   that.addActionRule('get', true, 'putAttachment', that.wait);
   that.addActionRule('get', false, 'post', that.wait);
@@ -244,24 +244,24 @@ var jobRules = (function () {
   that.addActionRule('get', false, 'putAttachment', that.wait);
 
   that.addActionRule('remove', true, 'post', that.wait);
-  that.addActionRule('remove', true, 'get', that.dontAccept);
-  that.addActionRule('remove', true, 'remove', that.dontAccept);
-  that.addActionRule('remove', true, 'putAttachment', that.dontAccept);
+  that.addActionRule('remove', true, 'get', that.update);
+  that.addActionRule('remove', true, 'remove', that.update);
+  that.addActionRule('remove', true, 'putAttachment', that.update);
   that.addActionRule('remove', false, 'post', that.eliminate);
-  that.addActionRule('remove', false, 'put', that.dontAccept);
-  that.addActionRule('remove', false, 'get', that.dontAccept);
+  that.addActionRule('remove', false, 'put', that.update);
+  that.addActionRule('remove', false, 'get', that.update);
   that.addActionRule('remove', false, 'remove', that.update);
-  that.addActionRule('remove', false, 'putAttachment', that.dontAccept);
+  that.addActionRule('remove', false, 'putAttachment', that.update);
 
-  that.addActionRule('allDocs', true, 'allDocs', that.dontAccept);
+  that.addActionRule('allDocs', true, 'allDocs', that.update);
   that.addActionRule('allDocs', false, 'allDocs', that.update);
 
-  that.addActionRule('putAttachment', true, 'post', that.dontAccept);
+  that.addActionRule('putAttachment', true, 'post', that.update);
   that.addActionRule('putAttachment', true, 'put', that.wait);
   that.addActionRule('putAttachment', true, 'get', that.wait);
   that.addActionRule('putAttachment', true, 'remove', that.wait);
   that.addActionRule('putAttachment', true, 'putAttachment', that.wait);
-  that.addActionRule('putAttachment', false, 'post', that.dontAccept);
+  that.addActionRule('putAttachment', false, 'post', that.update);
   that.addActionRule('putAttachment', false, 'put', that.wait);
   that.addActionRule('putAttachment', false, 'get', that.wait);
   that.addActionRule('putAttachment', false, 'remove', that.eliminate);
diff --git a/test/jiotests.js b/test/jiotests.js
index 8bb8510..0beddea 100644
--- a/test/jiotests.js
+++ b/test/jiotests.js
@@ -634,13 +634,13 @@ test ("Several Jobs at the same time", function () {
 
 });
 
-test ("Similar Jobs at the same time (Replace)", function () {
+test ("Similar Jobs at the same time (Update)", function () {
 
     var o = generateTools(this);
 
     o.jio = JIO.newJio({"type":"dummyallok"});
-    o.spy(o, "status", 12, "job1 replaced", "f");
-    o.spy(o, "status", 12, "job2 replaced", "f2");
+    o.spy(o, "value", {"ok": true, "id": "file"}, "job1 ok", "f");
+    o.spy(o, "value", {"ok": true, "id": "file"}, "job2 ok", "f2");
     o.spy(o, "value", {"ok": true, "id": "file"}, "job3 ok", "f3");
     o.jio.put({"_id": "file", "content": "content"}, o.f);
     o.jio.put({"_id": "file", "content": "content"}, o.f2);
@@ -697,27 +697,6 @@ test ("One document aim jobs at the same time (Elimination)" , function () {
 
 });
 
-test ("One document aim jobs at the same time (Not Acceptable)" , function () {
-
-    var o = generateTools(this);
-
-    o.jio = JIO.newJio({"type":"dummyallok"});
-    o.spy(o, "value", {"_id": "file", "title": "get_title"}, "job1", "f");
-    o.spy(o, "status", 11, "job2 is not acceptable", "f2");
-
-    o.jio.get({"_id": "file"}, o.f);
-    o.testLastJobId(1, "job1 added to queue");
-    o.waitUntilLastJobIs("on going");
-
-    o.jio.get({"_id": "file"}, o.f2);
-    o.testLastJobId(1, "job2 not added");
-
-    o.tick(o, 1000, "f");
-    o.tick(o, "f2");
-    o.jio.stop();
-
-});
-
 test ("Server will be available soon (Wait for time)" , function () {
 
     var o = generateTools(this);
-- 
2.30.9