diff --git a/src/engine/runtime.js b/src/engine/runtime.js index ca7447ffa..98926c21e 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -627,6 +627,14 @@ class Runtime extends EventEmitter { return 'RUNTIME_STARTED'; } + /** + * Event name when the runtime dispose has been called. + * @const {string} + */ + static get RUNTIME_DISPOSED () { + return 'RUNTIME_DISPOSED'; + } + /** * Event name for reporting that a block was updated and needs to be rerendered. * @const {string} @@ -1548,6 +1556,7 @@ class Runtime extends EventEmitter { this.stopAll(); this.targets.map(this.disposeTarget, this); this._monitorState = OrderedMap({}); + this.emit(Runtime.RUNTIME_DISPOSED); // @todo clear out extensions? turboMode? etc. // *********** Cloud ******************* diff --git a/src/extensions/scratch3_pen/index.js b/src/extensions/scratch3_pen/index.js index 4c46ce370..da36ba4f3 100644 --- a/src/extensions/scratch3_pen/index.js +++ b/src/extensions/scratch3_pen/index.js @@ -67,6 +67,7 @@ class Scratch3PenBlocks { this._onTargetMoved = this._onTargetMoved.bind(this); runtime.on('targetWasCreated', this._onTargetCreated); + runtime.on('RUNTIME_DISPOSED', this.clear.bind(this)); } /** diff --git a/test/unit/engine_runtime.js b/test/unit/engine_runtime.js index c9f92385e..741ab9c3f 100644 --- a/test/unit/engine_runtime.js +++ b/test/unit/engine_runtime.js @@ -239,3 +239,14 @@ test('setCompatibilityMode does not restart if it was not running', t => { t.equal(started, false); t.end(); }); + +test('Disposing the runtime emits an event', t => { + let disposed = false; + const rt = new Runtime(); + rt.addListener('RUNTIME_DISPOSED', () => { + disposed = true; + }); + rt.dispose(); + t.equal(disposed, true); + t.end(); +});