Johann Woelper 7 years ago
parent
commit
a400212044
3 changed files with 21 additions and 63 deletions
  1. 9 10
      src/main.rs
  2. 5 41
      static/adapter.js
  3. 7 12
      static/index.html

+ 9 - 10
src/main.rs

@@ -51,21 +51,20 @@ fn set(obj: String) -> String {
 
     // let json_result = json::parse(obj.to_owned().as_str());
     // let json_result: serde_json::Value = from_str(obj.to_owned().as_str());
-    let json_web_result: Result<serde_json::Value, serde_json::Error> = from_str(obj.to_owned().as_str());
+    let json_result: Result<serde_json::Value, serde_json::Error> = from_str(obj.to_owned().as_str());
 
-    match json_web_result {
+    match json_result {
         Ok(json_from_web) => {
             let mut locked_obj = OBJECT.lock().unwrap();
             // let current_object = json::parse(&locked_obj);
-            let mut json_mem_result: Result<serde_json::Value, serde_json::Error> = from_str(&locked_obj);
+            let mut parsed_object: Result<serde_json::Value, serde_json::Error> = from_str(&locked_obj);
             
-            match json_mem_result {
-                Ok(mut json_from_mem) => {
-                    println!("{:?}", json_from_mem);
-                    merge(&mut json_from_mem, &json_from_web);
-                    // Store the merged result back to memory
-                    *locked_obj = to_string(&json_from_mem).unwrap();
-                    println!("JSON MERGE {:?}", json_from_mem);
+            match parsed_object {
+                Ok(mut json_object) => {
+                    println!("{:?}", json_object);
+                    merge(&mut json_object, &json_from_web);
+                    *locked_obj = to_string(&json_object).unwrap();
+                    println!("JSON MERGE {:?}", json_object);
 
                 },
                 Err(e) => {

+ 5 - 41
static/adapter.js

@@ -1,46 +1,11 @@
 var adapter = {
     server_url: "",
     responseJson: {},
-    onset: function () {},
-    onget: function () {},
-    differ: function (base, object) {
-        base = JSON.parse(JSON.stringify(base));
-        object = JSON.parse(JSON.stringify(object));
-
-        console.log('INPUT', base, object);
-
-        function difference(o1, o2) {
-            var k, kDiff,
-                diff = {};
-            for (k in o1) {
-                if (!o1.hasOwnProperty(k)) {
-                } else if (typeof o1[k] != 'object' || typeof o2[k] != 'object') {
-                    if (!(k in o2) || o1[k] !== o2[k]) {
-                        diff[k] = o2[k];
-                    }
-                } else if (kDiff = difference(o1[k], o2[k])) {
-                    diff[k] = kDiff;
-                }
-            }
-            for (k in o2) {
-                if (o2.hasOwnProperty(k) && !(k in o1)) {
-                    diff[k] = o2[k];
-                }
-            }
-            for (k in diff) {
-                if (diff.hasOwnProperty(k)) {
-                    return diff;
-                }
-            }
-            return {};
-        }
-        return difference(base, object);
-    },
+    onset: function() {},
+    onget: function() {},
     setter: function () {
-
-        var diff = this.differ(this.responseJson, this.onset());
         xhr = new XMLHttpRequest();
-        xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(diff)));
+        xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(this.onset())));
         xhr.onload = function () {
             console.log(xhr.responseText);
         };
@@ -52,10 +17,9 @@ var adapter = {
         xhr.open('GET', this.server_url + '/get');
         xhr.onload = function () {
             try {
-                console.log(JSON.parse(this.responseText));
-                self.responseJson = JSON.parse(this.responseText);
+                var response = JSON.parse(this.responseText);
+                self.responseJson = response;
                 self.onget();
-                console.log('loaded');
             } catch (err) {
                 console.warn("json could not be parsed:", err, this.responseText);
             }

+ 7 - 12
static/index.html

@@ -22,21 +22,19 @@
 <div id="app">
     <button onclick="adapter.setter()">save</button>
     <button onclick="adapter.getter()">load</button>
-    <button onclick="console.log(adapter)">adapter</button>
     <template v-for="(value, key) in config">
         <br>
 
        <div class="title">{{key}}</div><input v-model="config[key]">
 
     </template>
-    <pre>{{config}}</pre>
-    <!-- <pre>{{adapter}}</pre> -->
-
+    <pre>
+        {{config}}
+    </pre>
 </div>
 
 
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
-
 <script src="adapter.js"></script>
 
 <script>
@@ -46,21 +44,18 @@
         data: {
             config: {
                 age: 42,
-                person1: "Manfred",
-                person2: "Persona"
+                attributes: ["Fluff", "yo"],
             },
         },
         mounted: function () {
-
             adapter.onget = function () {
-                app.config = JSON.parse(JSON.stringify(adapter.responseJson));
+                app.config = adapter.responseJson
             }
-
             adapter.onset = function () {
-                return app.config
+                return app.config;
             }
 
-            adapter.getter();
+            // adapter.getter();
         },
         watch: {},
         computed: {