From 6bd2307c992286fa1825d66b93bb69d1b8dd7596 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Tue, 4 Dec 2018 16:43:31 -0500 Subject: [PATCH] Clear the pen layer when runtime dispose happens. --- src/engine/runtime.js | 9 +++++++++ src/extensions/scratch3_pen/index.js | 1 + test/unit/engine_runtime.js | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 4031ea49d..0547e622d 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -626,6 +626,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} @@ -1547,6 +1555,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..26ffe8c87 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.start(); + t.equal(disposed, true); + t.end(); +});