Skip to content

Commit 44fbbb9

Browse files
committed
fix(turtle): fixed turtle steps not resuming after timeouts
After all the steps in the queue were done, the Turtle's interval was cleared. But it was never resumed if an action was called at any time after that. This fixes the issue. fix #3
1 parent 02bb880 commit 44fbbb9

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/turtle.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class Turtle extends EventEmitter {
198198
/**
199199
* The delay in ms between each steps.
200200
*/
201-
speed?: number;
201+
delay?: number;
202202

203203
/**
204204
* The timer identifier for the step interval.
@@ -296,6 +296,23 @@ export class Turtle extends EventEmitter {
296296
return this;
297297
}
298298

299+
/**
300+
* Resets the interval with the current delay.
301+
*/
302+
private resetInterval() {
303+
if (this.interval) clearInterval(this.interval);
304+
this.interval = setInterval(this.nextStep.bind(this), this.delay);
305+
}
306+
307+
/**
308+
* Add a step to the queue.
309+
*/
310+
private addStep(step: Step): Turtle {
311+
this.steps.push(step);
312+
if (!this.interval) this.resetInterval();
313+
return this;
314+
}
315+
299316
/**
300317
* Wipes out the canvas.
301318
*
@@ -306,7 +323,7 @@ export class Turtle extends EventEmitter {
306323
this.emit('clear');
307324
clearContext(this.ctx);
308325
this.draw();
309-
} else this.steps.push({ type: StepType.Clear });
326+
} else this.addStep({ type: StepType.Clear });
310327

311328
return this;
312329
}
@@ -330,7 +347,7 @@ export class Turtle extends EventEmitter {
330347
this.hidden = true;
331348
this.restoreImageData();
332349
this.draw();
333-
} else this.steps.push({ type: StepType.Hide });
350+
} else this.addStep({ type: StepType.Hide });
334351
return this;
335352
}
336353

@@ -352,7 +369,7 @@ export class Turtle extends EventEmitter {
352369
this.emit('show');
353370
this.hidden = false;
354371
this.draw();
355-
} else this.steps.push({ type: StepType.Show });
372+
} else this.addStep({ type: StepType.Show });
356373
return this;
357374
}
358375

@@ -373,7 +390,7 @@ export class Turtle extends EventEmitter {
373390
this.setAngle(0);
374391
this.goto(0, 0);
375392
this.clear();
376-
} else this.steps.push({ type: StepType.Reset });
393+
} else this.addStep({ type: StepType.Reset });
377394
return this;
378395
}
379396

@@ -388,7 +405,7 @@ export class Turtle extends EventEmitter {
388405
this.emit('setShape', shape);
389406
this.shape = shape;
390407
this.draw();
391-
} else this.steps.push({ type: StepType.SetShape, args: [shape] });
408+
} else this.addStep({ type: StepType.SetShape, args: [shape] });
392409
return this;
393410
}
394411

@@ -409,12 +426,10 @@ export class Turtle extends EventEmitter {
409426
if (this.inStep) {
410427
this.emit('setDelay', ms);
411428
this.stepByStep = ms > 0;
412-
this.speed = ms;
413-
414-
if (this.interval) clearInterval(this.interval);
429+
this.delay = ms;
415430

416-
this.interval = setInterval(this.nextStep.bind(this), ms);
417-
} else this.steps.push({ type: StepType.SetDelay, args: [ms] });
431+
this.resetInterval();
432+
} else this.addStep({ type: StepType.SetDelay, args: [ms] });
418433
return this;
419434
}
420435

@@ -441,7 +456,7 @@ export class Turtle extends EventEmitter {
441456
if (this.inStep) {
442457
this.emit('putPenUp');
443458
this.penDown = false;
444-
} else this.steps.push({ type: StepType.PenUp });
459+
} else this.addStep({ type: StepType.PenUp });
445460
return this;
446461
}
447462

@@ -468,7 +483,7 @@ export class Turtle extends EventEmitter {
468483
if (this.inStep) {
469484
this.emit('putPenDown');
470485
this.penDown = true;
471-
} else this.steps.push({ type: StepType.PenDown });
486+
} else this.addStep({ type: StepType.PenDown });
472487
return this;
473488
}
474489

@@ -532,7 +547,7 @@ export class Turtle extends EventEmitter {
532547
this.color = convertToColor(color);
533548
this.restoreImageData();
534549
this.draw();
535-
} else this.steps.push({ type: StepType.SetColor, args: [color] });
550+
} else this.addStep({ type: StepType.SetColor, args: [color] });
536551
return this;
537552
}
538553

@@ -561,7 +576,7 @@ export class Turtle extends EventEmitter {
561576
this.width = size;
562577
this.restoreImageData();
563578
this.draw();
564-
} else this.steps.push({ type: StepType.SetWidth, args: [size] });
579+
} else this.addStep({ type: StepType.SetWidth, args: [size] });
565580
return this;
566581
}
567582

@@ -586,7 +601,7 @@ export class Turtle extends EventEmitter {
586601
if (this.inStep) {
587602
this.emit('setLineCap', cap);
588603
this.lineCap = cap;
589-
} else this.steps.push({ type: StepType.SetLineCap, args: [cap] });
604+
} else this.addStep({ type: StepType.SetLineCap, args: [cap] });
590605
return this;
591606
}
592607

@@ -609,7 +624,7 @@ export class Turtle extends EventEmitter {
609624
this.angle = ang;
610625
this.restoreImageData();
611626
this.draw();
612-
} else this.steps.push({ type: StepType.SetAngle, args: [ang] });
627+
} else this.addStep({ type: StepType.SetAngle, args: [ang] });
613628
return this;
614629
}
615630

@@ -638,7 +653,7 @@ export class Turtle extends EventEmitter {
638653
this.angle -= ang;
639654
this.restoreImageData();
640655
this.draw();
641-
} else this.steps.push({ type: StepType.Left, args: [ang] });
656+
} else this.addStep({ type: StepType.Left, args: [ang] });
642657
return this;
643658
}
644659

@@ -667,7 +682,7 @@ export class Turtle extends EventEmitter {
667682
this.angle += ang;
668683
this.restoreImageData();
669684
this.draw();
670-
} else this.steps.push({ type: StepType.Right, args: [ang] });
685+
} else this.addStep({ type: StepType.Right, args: [ang] });
671686
return this;
672687
}
673688

@@ -697,7 +712,7 @@ export class Turtle extends EventEmitter {
697712
this.position.y = y;
698713
this.restoreImageData();
699714
this.draw();
700-
} else this.steps.push({ type: StepType.Goto, args: [x, y] });
715+
} else this.addStep({ type: StepType.Goto, args: [x, y] });
701716
return this;
702717
}
703718

@@ -778,7 +793,7 @@ export class Turtle extends EventEmitter {
778793
*/
779794
forward(distance: number): Turtle {
780795
if (!this.inStep) {
781-
this.steps.push({ type: StepType.Forward, args: [distance] });
796+
this.addStep({ type: StepType.Forward, args: [distance] });
782797
return this;
783798
}
784799

0 commit comments

Comments
 (0)