Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './events/events_block_create.js';
import './events/events_block_delete.js';

import {Blocks} from './blocks.js';
import {BlockDelete} from './events/events_block_delete.js';
import type {Comment} from './comment.js';
import * as common from './common.js';
import {Connection} from './connection.js';
Expand Down Expand Up @@ -330,10 +331,6 @@ export class Block implements IASTNodeLocation, IDeletable {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_DELETE))(this));
}

if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
}

eventUtils.disable();

try {
Expand Down Expand Up @@ -1027,7 +1024,13 @@ export class Block implements IASTNodeLocation, IDeletable {
this.workspace.removeChangeListener(this.onchangeWrapper_);
}
this.onchange = onchangeFn;
this.onchangeWrapper_ = onchangeFn.bind(this);
this.onchangeWrapper_ = (e) => {
onchangeFn.call(this, e);
if (e.type === eventUtils.BLOCK_DELETE &&
(e as BlockDelete).blockId === this.id) {
this.workspace.removeChangeListener(this.onchangeWrapper_!);
}
};
this.workspace.addChangeListener(this.onchangeWrapper_);
}

Expand Down
14 changes: 9 additions & 5 deletions tests/mocha/event_block_delete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown

suite('Block Delete Event', function() {
setup(function() {
sharedTestSetup.call(this);
const {clock} = sharedTestSetup.call(this, {fireEventsNow: false});
this.clock = clock;
defineRowBlock();
this.workspace = new Blockly.Workspace();
});
Expand All @@ -22,20 +23,23 @@ suite('Block Delete Event', function() {
});

suite('Receiving', function() {
test('blocks receive their own delete events', function() {
test('blocks receive their own delete events', function(done) {
Blockly.Blocks['test'] = {
onchange: function(e) {},
onchange: function(e) { },
};
// Need to stub the definition, because the property on the definition is
// what gets registered as an event listener.
const spy = sinon.spy(Blockly.Blocks['test'], 'onchange');
const testBlock = this.workspace.newBlock('test');

testBlock.dispose();
this.clock.tick(2); // Fire events. The built-in timeout is 0.

const deleteClass = eventUtils.get(eventUtils.BLOCK_DELETE);
chai.assert.isTrue(spy.calledOnce);
chai.assert.isTrue(spy.getCall(0).args[0] instanceof deleteClass);
chai.assert.isTrue(
spy.calledWith(sinon.match.instanceOf(deleteClass)),
'Expected the block to receive its own delete event.');
done();
});
});

Expand Down
5 changes: 5 additions & 0 deletions tests/mocha/test_helpers/setup_teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ function wrapDefineBlocksWithJsonArrayWithCleanup_(sharedCleanupObj) {
*
* @param {Object<string, boolean>} options Options to enable/disable setup
* of certain stubs.
* @return {{clock: *}} The fake clock (as part of an object to make refactoring
* easier).
*/
export function sharedTestSetup(options = {}) {
this.sharedSetupCalled_ = true;
Expand All @@ -122,6 +124,9 @@ export function sharedTestSetup(options = {}) {
this.blockTypesCleanup_ = this.sharedCleanup.blockTypesCleanup_;
this.messagesCleanup_ = this.sharedCleanup.messagesCleanup_;
wrapDefineBlocksWithJsonArrayWithCleanup_(this.sharedCleanup);
return {
clock: this.clock,
};
}

/**
Expand Down