Skip to content

Extract partitionBlocksByType into dedicated util function#53408

Merged
ebeastlake merged 7 commits into
stagingfrom
chore/refactor-block-partitioning-util
Aug 23, 2023
Merged

Extract partitionBlocksByType into dedicated util function#53408
ebeastlake merged 7 commits into
stagingfrom
chore/refactor-block-partitioning-util

Conversation

@ebeastlake

@ebeastlake ebeastlake commented Aug 22, 2023

Copy link
Copy Markdown
Contributor

Before this PR, we had two versions of the partitionBlocksByType function, one in cdoBlockSerializer.js that operated on JSON blocks using block.type and one in cdoXml.js that operated on block elements using block.getAttribute('type'). This PR creates a single version in cdoUtils.js that can operate on either block elements or JSON blocks. Use of block.type or block.getAttribute('type') is controlled by the boolean argument isBlockElements. I also added some additional tests.

Links

https://codedotorg.atlassian.net/browse/SL-1088

Testing story

Added tests and included the test cases from the original implementation to confirm that they still pass.

Follow-up work

Rebase #53154.

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

Comment thread apps/src/blockly/cdoBlocklyWrapper.js Outdated
// Google Blockly only. Registers custom blocks for modal function editor.
},
partitionBlocksByType() {
// TODO: Add comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@molly-moen What do you think is the most useful way to phrase this comment?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd probably do something like the comment on registerCustomProcedureBlocks that this function only applies to google blockly.

@molly-moen molly-moen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

@molly-moen molly-moen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

couple nits on naming, but I think this looks good!

Comment thread apps/src/blockly/addons/cdoUtils.js Outdated
* @returns {Element[]|Object[]} A new array of block elements or JSON blocks partitioned based on their types.
*/
export function partitionBlocksByType(blockElements, prioritizedBlockTypes) {
export function partitionBlocksByType(blocks = [], options = {}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: is there a reason you went with an options object rather than having separate parameters for each option? I think separate parameters is more readable.

@ebeastlake ebeastlake Aug 23, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can switch back to separate parameters. In general, I think it makes the calls more readable to indicate what the flag means as part of the call to the function. That being said, happy to switch back/no strong feelings here!

Comment thread apps/src/blockly/addons/cdoUtils.js Outdated
*/
export function partitionBlocksByType(blockElements, prioritizedBlockTypes) {
export function partitionBlocksByType(blocks = [], options = {}) {
const {prioritizedBlockTypes = [], isJson = false} = options;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I wonder if there's a better name than isJson. In my mind the opposite of json is xml, which is not the false case here. Would it be better to use something like isBlockElements?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

isBlockElements works for me!

@ebeastlake ebeastlake merged commit e9a81fc into staging Aug 23, 2023
@ebeastlake ebeastlake deleted the chore/refactor-block-partitioning-util branch August 23, 2023 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants