Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Specify context for onComplete.

It seems to be a nice feature that the oncomplete method be allowed to
have a specific context within which to execute (GH-47).

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 41bd4f3a2e1f78457d08f8323ed843c2ea9cdf52 1 parent f58efd6
@ncb000gt authored
Showing with 42 additions and 2 deletions.
  1. +4 −2 lib/cron.js
  2. +38 −0 tests/test-cron.js
View
6 lib/cron.js
@@ -231,11 +231,12 @@ CronTime.prototype = {
-function CronJob(cronTime, onTick, onComplete, start, timeZone) {
+function CronJob(cronTime, onTick, onComplete, start, timeZone, context) {
if (typeof cronTime != "string" && arguments.length == 1) {
//crontime is an object...
onTick = cronTime.onTick;
onComplete = cronTime.onComplete;
+ context = cronTime.context;
start = cronTime.start;
timeZone = cronTime.timeZone;
cronTime = cronTime.cronTime;
@@ -243,6 +244,7 @@ function CronJob(cronTime, onTick, onComplete, start, timeZone) {
if (timeZone && !(CronDate.prototype.setTimezone)) console.log('You specified a Timezone but have not included the `time` module. Timezone functionality is disabled. Please install the `time` module to use Timezones in your application.');
+ this.context = (context || this);
this._callbacks = [];
this.onComplete = onComplete;
this.cronTime = new CronTime(cronTime, timeZone);
@@ -270,7 +272,7 @@ CronJob.prototype = {
for (var i = (this._callbacks.length - 1); i >= 0; i--) {
//send this so the callback can call this.stop();
- this._callbacks[i].call(this, this.onComplete);
+ this._callbacks[i].call(this.context, this.onComplete);
}
},
View
38 tests/test-cron.js
@@ -361,5 +361,43 @@ module.exports = testCase({
});
assert.done();
}, 1250);
+ },
+ 'test cronjob scoping': function(assert) {
+ assert.expect(2);
+ var c = new cron.CronJob('* * * * * *', function() {
+ assert.ok(true);
+ assert.ok(c instanceof cron.CronJob);
+ }, null, true);
+ setTimeout(function() {
+ c.stop();
+ assert.done();
+ }, 1250);
+ },
+ 'test non-cronjob scoping': function(assert) {
+ assert.expect(2);
+ var c = new cron.CronJob('* * * * * *', function() {
+ assert.ok(true);
+ assert.equal(this.hello, 'world');
+ }, null, true, null, {'hello':'world'});
+ setTimeout(function() {
+ c.stop();
+ assert.done();
+ }, 1250);
+ },
+ 'test non-cronjob scoping inside object': function(assert) {
+ assert.expect(2);
+ var c = new cron.CronJob({
+ cronTime: '* * * * * *',
+ onTick: function() {
+ assert.ok(true);
+ assert.equal(this.hello, 'world');
+ },
+ start: true,
+ context: {hello: 'world'}
+ });
+ setTimeout(function() {
+ c.stop();
+ assert.done();
+ }, 1250);
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.