diff --git a/src/engine/runtime.js b/src/engine/runtime.js index a8bd98c5..6e4c0c87 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -83,6 +83,12 @@ var Runtime = function () { */ this._scriptGlowsPreviousFrame = []; + /** + * Number of threads running during the previous frame + * @type {number} + */ + this._threadCount = 0; + /** * Currently known number of clones, used to enforce clone limit. * @type {number} @@ -179,6 +185,18 @@ Runtime.BLOCK_GLOW_ON = 'BLOCK_GLOW_ON'; */ Runtime.BLOCK_GLOW_OFF = 'BLOCK_GLOW_OFF'; +/** + * Event name for glowing the green flag + * @const {string} + */ +Runtime.PROJECT_RUN_START = 'PROJECT_RUN_START' + +/** + * Event name for unglowing the green flag + * @const {string} + */ +Runtime.PROJECT_RUN_STOP = 'PROJECT_RUN_STOP' + /** * Event name for visual value report. * @const {string} @@ -552,6 +570,7 @@ Runtime.prototype._step = function () { this.redrawRequested = false; var inactiveThreads = this.sequencer.stepThreads(); this._updateGlows(inactiveThreads); + this._setThreadCount(this.threads.length); if (this.renderer) { // @todo: Only render when this.redrawRequested or clones rendered. this.renderer.draw(); @@ -639,6 +658,20 @@ Runtime.prototype._updateGlows = function (optExtraThreads) { this._scriptGlowsPreviousFrame = finalScriptGlows; }; +/** + * Emit run start/stop after each tick. Emits when `this.threads.length` goes + * between non-zero and zero + */ +Runtime.prototype._setThreadCount = function (threadCount) { + if (this._threadCount === 0 && threadCount > 0) { + this.emit(Runtime.PROJECT_RUN_START); + } + if (this._threadCount > 0 && threadCount === 0) { + this.emit(Runtime.PROJECT_RUN_STOP); + } + this._threadCount = threadCount; +}; + /** * "Quiet" a script's glow: stop the VM from generating glow/unglow events * about that script. Use when a script has just been deleted, but we may diff --git a/src/index.js b/src/index.js index 8b390f35..4ed724f3 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,11 @@ var VirtualMachine = function () { instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (glowData) { instance.emit(Runtime.BLOCK_GLOW_OFF, glowData); }); + instance.runtime.on(Runtime.PROJECT_RUN_START, function () { + instance.emit(Runtime.PROJECT_RUN_START); + }); + instance.runtime.on(Runtime.PROJECT_RUN_STOP, function () { + instance.emit(Runtime.PROJECT_RUN_STOP); }); instance.runtime.on(Runtime.VISUAL_REPORT, function (visualReport) { instance.emit(Runtime.VISUAL_REPORT, visualReport);