Skip to content

Commit

Permalink
Updated delete method to recursively call itself when dealing with an…
Browse files Browse the repository at this point in the history
… object of children (created by a for loop).
  • Loading branch information
michielvandergeest committed Apr 17, 2024
1 parent e1ad4b3 commit c884fab
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/lib/setup/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,19 @@ export default (component, name) => {
const deleteChildren = function (children) {
for (let i = 0; i < children.length; i++) {
if (!children[i]) return
if (Array.isArray(children[i])) {
deleteChildren(children[i])
} else if (Object.getPrototypeOf(children[i]) === Object.prototype) {
Object.keys(children[i]).forEach((k) => {
if (children[i][k].destroy) {
children[i][k].destroy()
}
children[i][k] = null
})
} else if (children[i].destroy) {
// call destroy when method is available on child
if (children[i].destroy && typeof children[i].destroy === 'function') {
children[i].destroy()
}
// recursively call deleteChildren when it's an object of items (happens when using a forloop construct)
else if (Object.getPrototypeOf(children[i]) === Object.prototype) {
deleteChildren(Object.values(children[i]))
}
// todo: this case may not be needed anymore
else if (Array.isArray(children[i])) {
deleteChildren(children[i])
}

children[i] = null
}

Expand Down

0 comments on commit c884fab

Please sign in to comment.