Skip to content
This repository

Remove code relating to parallel execution from PeriodicalExecuter. #23

Open
wants to merge 2 commits into from

3 participants

Austin Bales Cecil Phillip Andrew Dupont
Austin Bales

In PeriodicalExcuter, removed unnecessary currentlyExecuting ivar and other code relating to parallel execution.

Since #onTimerEvent contained no logic after this change, made setInterval call (a bound) #execute and renamed #registerCallback to #start so that the timer can be restarted at will. This behavior already existed in the previous (undocumented) function.

Thanks!

Austin Bales arbales commented on the diff
test/unit/periodical_executer_test.js
@@ -12,24 +12,4 @@ new Test.Unit.Runner({
12 12 this.assertEqual(3, peEventCount);
13 13 });
14 14 },
15   -
16   - testOnTimerEventMethod: function() {
1
Austin Bales
arbales added a note

Didn't seem like this was performing any function that the previous test wasn't already doing, given that currentlyExecuting was removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Austin Bales

(ping)

Austin Bales

Mind merging or closing?

Cecil Phillip

I'm guessing they've given up over here

Austin Bales
Andrew Dupont
Collaborator

Sorry — we dropped the ball on pull requests for a while.

Can I ask why you think the code relating to parallel execution is unnecessary? I'm confused. It looks quite necessary to me if the point is to prevent an accumulation of callbacks in the case where your callback routinely takes longer than the interval. If your point is that JavaScript is single-threaded and thus nothing actually executes in "parallel," then I'll concede that it's a poor choice of words.

Just want to make sure I'm not missing something here.

Andrew Dupont
Collaborator

HAHAHA DISREGARD THAT (now I see your point)

Just shows how little this code has been touched over the years. Yeah, it doesn't prevent callback accumulation, and it's quite silly of us to have pretended that it did.

So I have four options, as I see it:

  1. Change the documentation to match existing behavior, but otherwise do nothing.
  2. Change the documentation to match existing behavior. Also, admit defeat on the issue and apply this patch, thereby conceding that PeriodicalExecuter is nothing more than a thin wrapper around setInterval.
  3. Change it to call setTimeout at the end of the callback execution, thus guaranteeing at least frequency seconds between calls (a somewhat-significant change in behavior).
  4. Change the behavior to match existing documentation. In other words, adopt an actual strategy for preventing callback accumulation. For instance, onTimerEvent could keep track of how long it's been since the last time it was called. Like, if we've got a frequency of one second, and onTimerEvent sees that it's been more than two seconds since the last time it was called, it could just return early, knowing that setInterval has scheduled another call to onTimerEvent that ought to run in no time flat.

I am leaning towards option #4 because we can't get rid of PeriodicalExecuter (backward compatibility), and as long as we're stuck with it I think it should serve some purpose beyond setInterval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jul 16, 2011
Austin Bales arbales In PeriodicalExcuter, removed unnecessary `currentlyExecuting` ivar a…
…nd other code relating to parallel execution.
00dfd0d
Austin Bales arbales Check to ensure that `this.timer` doesn't exist before assigning it i…
…n `#start`
2b67f9e
This page is out of date. Refresh to see the latest.
31 src/prototype/lang/periodical_executer.js
@@ -31,13 +31,18 @@ var PeriodicalExecuter = Class.create({
31 31 initialize: function(callback, frequency) {
32 32 this.callback = callback;
33 33 this.frequency = frequency;
34   - this.currentlyExecuting = false;
35 34
36   - this.registerCallback();
  35 + this.start();
37 36 },
38 37
39   - registerCallback: function() {
40   - this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
  38 + /**
  39 + * PeriodicalExecuter#start() -> undefined
  40 + *
  41 + * Starts the [[PeriodicalExecuter]] or restarts it if `#stop`
  42 + * was previously called.
  43 + **/
  44 + start: function() {
  45 + if (!this.timer) this.timer = setInterval(this.execute.bind(this), this.frequency * 1000);
41 46 },
42 47
43 48 execute: function() {
@@ -71,22 +76,4 @@ var PeriodicalExecuter = Class.create({
71 76 clearInterval(this.timer);
72 77 this.timer = null;
73 78 },
74   -
75   - onTimerEvent: function() {
76   - if (!this.currentlyExecuting) {
77   - // IE doesn't support `finally` statements unless all errors are caught.
78   - // We mimic the behaviour of `finally` statements by duplicating code
79   - // that would belong in it. First at the bottom of the `try` statement
80   - // (for errorless cases). Secondly, inside a `catch` statement which
81   - // rethrows any caught errors.
82   - try {
83   - this.currentlyExecuting = true;
84   - this.execute();
85   - this.currentlyExecuting = false;
86   - } catch(e) {
87   - this.currentlyExecuting = false;
88   - throw e;
89   - }
90   - }
91   - }
92 79 });
20 test/unit/periodical_executer_test.js
@@ -12,24 +12,4 @@ new Test.Unit.Runner({
12 12 this.assertEqual(3, peEventCount);
13 13 });
14 14 },
15   -
16   - testOnTimerEventMethod: function() {
17   - var testcase = this,
18   - pe = {
19   - onTimerEvent: PeriodicalExecuter.prototype.onTimerEvent,
20   - execute: function() {
21   - testcase.assert(pe.currentlyExecuting);
22   - }
23   - };
24   -
25   - pe.onTimerEvent();
26   - this.assert(!pe.currentlyExecuting);
27   -
28   - pe.execute = function() {
29   - testcase.assert(pe.currentlyExecuting);
30   - throw new Error()
31   - }
32   - this.assertRaise('Error', pe.onTimerEvent.bind(pe));
33   - this.assert(!pe.currentlyExecuting);
34   - }
35 15 });

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.