import { isObservableArray, isObservableMap, isObservableObject } from 'mobx'

/**
 * Helper function that supports merging maps
 * @param obj
 * @param other
 */
function mergeObservables(obj, other) {
    Object.keys(obj).forEach(key => {
        if (typeof obj[key] === 'object') {
            if (isObservableMap(obj[key])) return obj[key].merge(other[key])
            if (isObservableArray(obj[key])) return obj[key].replace(other[key])
            if (isObservableObject(obj[key])) return mergeObservables(obj[key], other[key])
            obj[key] = other[key]
        } else {
            obj[key] = other[key]
        }
    })
    // For debugging purposes
    window.__STATE = obj

    return obj
}

export default mergeObservables

@@ -40,28 +40,8 @@
 eMap
-, isObservableObject
  %7D f
@@ -112,19 +112,22 @@
  @param 
-obj
+target
 %0A * @par
@@ -129,21 +129,22 @@
  @param 
-other
+source
 %0A */%0Afun
@@ -170,18 +170,22 @@
 les(
-obj, other
+target, source
 ) %7B%0A
@@ -200,19 +200,22 @@
 ct.keys(
-obj
+target
 ).forEac
@@ -244,19 +244,22 @@
 (typeof 
-obj
+target
 %5Bkey%5D ==
@@ -304,19 +304,22 @@
 ableMap(
-obj
+target
 %5Bkey%5D)) 
@@ -321,27 +321,30 @@
 y%5D)) return 
-obj
+target
 %5Bkey%5D.merge(
@@ -343,21 +343,22 @@
 %5D.merge(
-other
+source
 %5Bkey%5D)%0A 
@@ -390,19 +390,22 @@
 leArray(
-obj
+target
 %5Bkey%5D)) 
@@ -411,19 +411,22 @@
  return 
-obj
+target
 %5Bkey%5D.re
@@ -431,21 +431,22 @@
 replace(
-other
+source
 %5Bkey%5D)%0A 
@@ -460,111 +460,22 @@
     
-if (isObservableObject(obj%5Bkey%5D)) return mergeObservables(obj%5Bkey%5D, other%5Bkey%5D)%0A            obj
+target
 %5Bkey%5D = 
 othe
@@ -462,37 +462,38 @@
   target%5Bkey%5D = 
-other
+source
 %5Bkey%5D%0A        %7D 
@@ -515,19 +515,22 @@
     
-obj
+target
 %5Bkey%5D = 
 othe
@@ -529,13 +529,14 @@
 %5D = 
-other
+source
 %5Bkey
@@ -558,78 +558,43 @@
  %7D)%0A
-    // For debugging purposes%0A    window.__STATE = obj%0A%0A    return obj
+%0A    return window.__STATE = target
 %0A%7D%0A%0A

Fix observable merge