Browse Source

refcheck using id value, not object comparison due lack of identity map

Ivan Asmer 4 years ago
parent
commit
6563d4c2ef
2 changed files with 12 additions and 3 deletions
  1. 5 1
      asynchronize.js
  2. 7 2
      index.js

+ 5 - 1
asynchronize.js

@@ -64,7 +64,11 @@ function asynchronize({s, chunkEventName, endEventName, errEventName, countMetho
 
         if (countMethodName){
             let count = s[countMethodName](true)
-            const checker = count => count <= 0 && (end = true , closeAllEmptyPromises()/*, console.log(`COUNT ${count}`)*/  )
+            const checker = count => 
+                        count <= 0 && 
+                        (end = true , 
+                                closeAllEmptyPromises()
+                            /*, console.log(`COUNT ${count}`)*/  )
             if (count.then && typeof count.then === 'function')
                 count.then(checker)
             else 

+ 7 - 2
index.js

@@ -120,7 +120,7 @@ const mm = db => {
                         lastKey: backRefKey} = await getValueByField(backRef, foreignSavable)
 
                     if (backRefValue instanceof Array){
-                        if (!backRefValue.includes(this)){
+                        if (!Savable.existsInArray(backRefValue, this)){
                             backRefValue.push(this)
                         }
                     }
@@ -141,7 +141,9 @@ const mm = db => {
                     let {value, obj, lastKey: key} = await getValueByField(relation, this)
                     const valueAsArray = value instanceof Savable ? [value] : value
                     if (loadRelationAsArray){
-                        const removedRefs = valueAsArray ? loadRelationAsArray.filter(ref => !valueAsArray.includes(ref)) : loadRelationAsArray
+                        const removedRefs = valueAsArray ? 
+                                loadRelationAsArray.filter(ref => !Savable.existsInArray(valueAsArray, ref)) : 
+                                loadRelationAsArray
                         for (const ref of removedRefs){
                             try {
                                 await ref
@@ -241,6 +243,9 @@ const mm = db => {
 
 
 
+        static existsInArray(arr, obj){
+            return arr.filter(item => item._id.toString() === obj._id.toString()).length
+        }
 
         static isSavable(obj){
             return obj && obj._id && obj._class