Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: allow blocks to receive their own delete events #6337

Merged
merged 18 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ const PROCEDURE_CALL_COMMON = {
Xml.domToWorkspace(xml, this.workspace);
Events.setGroup(false);
}
} else if (event.type === Events.BLOCK_DELETE) {
} else if (event.type === Events.BLOCK_DELETE && event.blockId != this.id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this is a breaking change and should probably be mentioned in the PR description, even if it's very unlikely to ever cause anyone an issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a breaking change. Before blocks did not receive their own events, so this case would have never been hit. Now I've added the blockId check so it continues to not be hit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onchange is not private, so it could be called by code outside Blockly, which could pass in an arbitrary event.

I agree this is unlikely in practice, which is why I said "strictly speaking". It's probably not useful to bump a major version number or to mention in the release notes, but it probably is worth making sure that I am not the only one who has considered this scenario, and hence the comment.

// Look for the case where a procedure definition has been deleted,
// leaving this block (a procedure call) orphaned. In this case, delete
// the orphan.
Expand Down
10 changes: 5 additions & 5 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,18 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
dispose(healStack: boolean) {
if (this.disposed) {
// Already deleted.
return;
}
// Terminate onchange event calls.
if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
}

this.unplug(healStack);
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_DELETE))!(this));
}

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

eventUtils.disable();

try {
Expand Down
39 changes: 39 additions & 0 deletions tests/mocha/block_delete_event_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

goog.declareModuleId('Blockly.test.blockDeleteEvent');

import * as eventUtils from '../../build/src/core/events/utils.js';
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';

suite('Block Delete Event', function() {
setup(function() {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
});

teardown(function() {
sharedTestTeardown.call(this);
});

suite('Receiving', function() {
test('blocks receive their own delete events', function() {
Blockly.Blocks['test'] = {
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();

const deleteClass = eventUtils.get(eventUtils.BLOCK_DELETE);
chai.assert.isTrue(spy.calledOnce);
chai.assert.isTrue(spy.getCall(0).args[0] instanceof deleteClass);
});
});
});
1 change: 1 addition & 0 deletions tests/mocha/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
// Test modules.
'Blockly.test.astNode',
'Blockly.test.blockChangeEvent',
'Blockly.test.blockDeleteEvent',
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
'Blockly.test.blockCreateEvent',
'Blockly.test.blockJson',
'Blockly.test.blocks',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

goog.declareModuleId('Blockly.test.lists');

import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {sharedTestSetup, sharedTestTeardown} from '../test_helpers/setup_teardown.js';
import {runSerializationTestSuite} from './test_helpers/serialization.js';
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
import {ConnectionType} from '../../build/src/core/connection_type.js';
import {defineStatementBlock} from '../test_helpers/block_definitions.js';
import {defineStatementBlock} from './test_helpers/block_definitions.js';


suite('Lists', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
goog.declareModuleId('Blockly.test.logicTernary');

import * as eventUtils from '../../build/src/core/events/utils.js';
import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {sharedTestSetup, sharedTestTeardown} from '../test_helpers/setup_teardown.js';
import {runSerializationTestSuite} from './test_helpers/serialization.js';
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';


suite('Logic ternary', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
goog.declareModuleId('Blockly.test.procedures');

import * as Blockly from '../../build/src/core/blockly.js';
import {assertCallBlockStructure, assertDefBlockStructure, createProcDefBlock, createProcCallBlock} from '../test_helpers/procedures.js';
import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} from '../test_helpers/setup_teardown.js';

import {assertCallBlockStructure, assertDefBlockStructure, createProcDefBlock, createProcCallBlock} from './test_helpers/procedures.js';
import {runSerializationTestSuite} from './test_helpers/serialization.js';
import {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} from './test_helpers/setup_teardown.js';

suite('Procedures', function() {
setup(function() {
Expand Down Expand Up @@ -1033,6 +1032,7 @@ suite('Procedures', function() {
});
});
});

BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
/**
* Test cases for serialization tests.
* @type {Array<SerializationTestCase>}
Expand Down Expand Up @@ -1210,3 +1210,43 @@ suite('Procedures', function() {
});
});
});

suite('Procedures, dont auto fire events', function() {
setup(function() {
sharedTestSetup.call(this, {fireEventsNow: false});
this.workspace = new Blockly.Workspace();
});
teardown(function() {
sharedTestTeardown.call(this);
});

const testSuites = [
{title: 'procedures_defreturn', hasReturn: true,
defType: 'procedures_defreturn', callType: 'procedures_callreturn'},
{title: 'procedures_defnoreturn', hasReturn: false,
defType: 'procedures_defnoreturn', callType: 'procedures_callnoreturn'},
];

testSuites.forEach((testSuite) => {
suite(testSuite.title, function() {
suite('Disposal', function() {
test('callers are disposed when definitions are disposed', function() {
this.defBlock = new Blockly.Block(this.workspace, testSuite.defType);
this.defBlock.setFieldValue('proc name', 'NAME');
this.callerBlock = new Blockly.Block(
this.workspace, testSuite.callType);
this.callerBlock.setFieldValue('proc name', 'NAME');

// Run the clock now so that the create events get fired. If we fire
// it after disposing, a new procedure def will get created when
// the caller create event is heard.
this.clock.runAll();
this.defBlock.dispose();
this.clock.runAll();

chai.assert.isTrue(this.callerBlock.disposed);
});
});
});
});
});
File renamed without changes.