Skip to content

Instantly share code, notes, and snippets.

@kennyp
Created July 30, 2012 17:57
Show Gist options
  • Save kennyp/3208712 to your computer and use it in GitHub Desktop.
Save kennyp/3208712 to your computer and use it in GitHub Desktop.
Pattern match or no?
JobQueue.prototype.nextJob = function () {
var that = this;
match(this.queue.shift(), {
'string': function (url) {
$.ajax(url, function () {
that.nextJob();
});
},
'function': function (callback) {
callback(function () {
that.nextJob();
});
},
'string,object': function (url, data) {
$.ajax({
url: url,
data: data,
method: 'post',
success: function () {
that.nextJob();
}
});
},
'string,function': function (url, callback) {
$.ajax(url, function (res) {
callback(res, function () {
that.nextJob();
});
});
},
'string,object,function': function (url, data, callback) {
$.ajax({
url: url,
data: data,
method: 'post',
success: function (res) {
callback(res, data, function () {
that.nextJob();
});
}
});
},
'': function () {
that.empty = true;
}
});
};
JobQueue.prototype.nextJob = function () {
var that = this, next = this.queue.shift();
if (next) {
if (next.length === 1) {
if (typeof next[0] === 'function') {
next[0](function () {
that.nextJob();
});
} else {
$.ajax(next[0], function (res) {
that.nextJob();
});
}
} else if (next.length === 2) {
if (typeof next[1] === 'function') {
$.ajax(next[0], function (res) {
next[1](res, function () {
that.nextJob();
});
});
} else {
$.ajax({
url: next[0],
data: next[1],
method: 'post',
success: function (res) {
that.nextJob();
}
});
}
} else {
$.ajax({
url: next[0],
data: next[1],
method: 'post',
success: function (res) {
next[2](res, next[1], function () {
that.nextJob();
});
}
});
}
} else {
this.empty = true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment