From ba008e8e10e6fde60c38e0c72ccb025c820f30d6 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Sat, 2 Oct 2021 10:40:24 +0200 Subject: [PATCH] removing items from nested objects --- src/r3/r3-system/r3-system-linking.js | 96 +++++++++++++++++---------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/src/r3/r3-system/r3-system-linking.js b/src/r3/r3-system/r3-system-linking.js index c13f1f7..ca6b496 100644 --- a/src/r3/r3-system/r3-system-linking.js +++ b/src/r3/r3-system/r3-system-linking.js @@ -229,53 +229,58 @@ class SystemLinking extends System { //GENERATED_STATIC_SANITY_CHECKS_METHOD_END //CUSTOM_STATIC_SANITY_CHECKS_METHOD_START - /** - * First we check if this is a required property - */ - if (!r3Object.required.hasOwnProperty(property)) { - return; - } - /** - * We know this property is required - so continue.. - */ - if (r3Object.required[property] instanceof Array) { + for (let r = 0; r < r3Object.required.length; r++) { /** - * First we need to check that this value conforms to the requirement of being an Array + * First we check if this is a required property */ - if (!(value instanceof Array)) { - throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' was not properly defined - it should be an array'); - } - - if (value.length === 0) { + if (!r3Object.required[r].hasOwnProperty(property)) { return; } /** - * Check all items in the array are valid objects of type r3Object.required[property] + * We know this property is required - so continue.. */ - for (let i = 0; i < value.length; i++) { - r3Object.required[property].map( - function(constructor) { - if (!(value[i] instanceof constructor)) { - throw new TypeError('The property ' + property + ' of this object is not of the correct type'); + if (r3Object.required[r][property] instanceof Array) { + + /** + * First we need to check that this value conforms to the requirement of being an Array + */ + if (!(value instanceof Array)) { + throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' was not properly defined - it should be an array'); + } + + if (value.length === 0) { + return; + } + + /** + * Check all items in the array are valid objects of type r3Object.required[property] + */ + for (let i = 0; i < value.length; i++) { + r3Object.required[r][property].map( + function (constructor) { + if (!(value[i] instanceof constructor)) { + throw new TypeError('The property ' + property + ' of this object is not of the correct type'); + } } - } - ); - } - } else { + ); + } + } else { - if (value === null) { - return; - } + if (value === null) { + return; + } - if (typeof value === 'undefined') { - return; - } + if (typeof value === 'undefined') { + return; + } + + if (!(value instanceof r3Object.required[r][property])) { + throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' is not of the correct type') + } - if (!(value instanceof r3Object.required[property])) { - throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' is not of the correct type') } } @@ -325,6 +330,29 @@ class SystemLinking extends System { * We need to check if we removed an item from the existing array */ if (r3Object[property].length > 0) { + /** + * Find the missing value (if any) from the old array + */ + + let unlinked = r3Object[property].reduce( + function(result, object) { + if (value.indexOf(object) === -1) { + result.push(object); + } + return result; + }, + [] + ); + + unlinked.map( + function(unlinkObject) { + Utils.RemoveFromArray(unlinkObject.parents, r3Object); + Utils.RemoveFromArray(r3Object.children, unlinkObject); + if (r3Object instanceof Entity) { + Utils.RemoveFromArray(r3Object.components, unlinkObject); + } + } + ); }