Last active
August 29, 2015 14:02
-
-
Save danscotton/6be0c64188ec1ac42111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file is responsible for actually creating the objects, and kicking things off. | |
// We create a simple object with a run method (which Poller expects) that when called, | |
// calls checker.check(). | |
define('MyApp', ['Poller, StatusChecker'], function(Poller, StatusChecker) { | |
var checker = new StatusChecker({ url: 'http://localhost:3000/foobar', limit: 5000 }), | |
poller = new Poller({ | |
run: function() { | |
return checker.check(function(status) { | |
if (status == 200) { | |
return true; | |
} else { | |
return false; | |
} | |
}); | |
} | |
}, { delay: 5000 }); | |
poller.poll(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Poller knows nothing about ajax requests. It only know how to maintain state | |
// over its internal data, which it encapsulates. It expects a 'Pollable' with | |
// a `run` method that returns true or false. If true, we reset delay, else we | |
// double it. | |
define('Poller', function() { | |
function Poller(pollable, config) { | |
this.pollable = pollable; | |
this.originalDelay = config.delay; | |
this.threshold = config.threshold; | |
this.delay = this.originalDelay; | |
} | |
Poller.prototype.get = function() { | |
return this.delay; | |
}; | |
Poller.prototype.increment = function() { | |
this.delay = (this.delay < this.threshold) ? this.delay * 2 : this.delay; | |
}; | |
Poller.prototype.reset = function() { | |
this.delay = this.originalDelay; | |
}; | |
Poller.prototype.poll = function() { | |
window.setTimeout($.proxy(function() { | |
if (this.pollable.run()) { | |
this.reset(); | |
} else { | |
this.increment(); | |
} | |
}, this), this.get()); | |
}; | |
Poller.defaults = { | |
delay: 5000, | |
threshold: 20000 | |
}; | |
return function(pollable, config) { | |
return new Poller(pollable, $.extend(Poller.defaults, config)); | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// StatusChecker is a very simple object that takes a url, and everytime | |
// you call .check(callback), makes a request to the url, yielding the status | |
define('StatusChecker', ['module/bootstrap'], function(news) { | |
var $ = news.$; | |
function StatusChecker(config) { | |
this.config = config; | |
} | |
StatusChecker.prototype.check = function(callback) { | |
$.ajax(this.config.url, { timeout: this.config.limit }) | |
.done(function(data, status, xhr) { callback(xhr.status); }); | |
.fail(function(xhr, status, error) { callback(xhr.status); }); | |
}; | |
StatusChecker.defaults = { | |
limit: 5000 | |
}; | |
return function(config) { | |
return new StatusChecker($.extend(StatusChecker.defaults, config)); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I thought that by initially thinking in an OO way, I could work out where the responsibilities were, so that I could better approach looking at it functionally. Turns out, it didn't really help me with the functional stuff at all!