Johann Woelper 7 anni fa
parent
commit
eda37cd388
4 ha cambiato i file con 64 aggiunte e 74 eliminazioni
  1. 0 53
      app.js
  2. 10 9
      src/main.rs
  3. 41 5
      static/adapter.js
  4. 13 7
      static/index.html

+ 0 - 53
app.js

@@ -1,53 +0,0 @@
-var app = new Vue({
-    el: '#app',
-    data: {
-        config: {
-            released_episode: 80,
-            last_diorama: 50,
-            available_dioramas: 0,
-            first_diorama: 6,
-            github_levels: [],
-            github_episodes: [],
-            github_success: false,
-            github_access_token: "ac4311edd3cc5acf4ccc321525a96a",
-        },
-    },
-    mounted: function () {
-    },
-    watch: {
-    },
-    computed: {
-        test: function () {
-        },
-    }
-})
-
-
-function getData() {
-    var xhr = new XMLHttpRequest();
-    var url = "http://localhost:8000";
-    console.log("url", url);
-    xhr.onreadystatechange = function () {
-        if (this.readyState == 4 && this.status == 200) {
-            var response = JSON.parse(this.responseText);
-            app.config.github_episodes = response;
-            app.config.github_success = true;
-            console.log(xhr.responseText);
-        }
-    };
-    xhr.open("GET", url, true);
-    xhr.send();
-}
-
-
-function setData() {
-    var newName = 'John Smith',
-    xhr = new XMLHttpRequest();
-    xhr.open('POST', 'http://localhost:8000/set');
-    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-    xhr.onload = function() {
-        console.log(xhr.responseText);
-    };
-    xhr.send(encodeURI('name=' + newName));
-}
-

+ 10 - 9
src/main.rs

@@ -51,20 +51,21 @@ 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_result: Result<serde_json::Value, serde_json::Error> = 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());
 
-    match json_result {
+    match json_web_result {
         Ok(json_from_web) => {
             let mut locked_obj = OBJECT.lock().unwrap();
             // let current_object = json::parse(&locked_obj);
-            let mut parsed_object: Result<serde_json::Value, serde_json::Error> = from_str(&locked_obj);
+            let mut json_mem_result: Result<serde_json::Value, serde_json::Error> = from_str(&locked_obj);
             
-            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);
+            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);
 
                 },
                 Err(e) => {

+ 41 - 5
static/adapter.js

@@ -1,11 +1,46 @@
 var adapter = {
     server_url: "",
     responseJson: {},
-    onset: function() {},
-    onget: function() {},
+    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);
+    },
     setter: function () {
+
+        var diff = this.differ(this.responseJson, this.onset());
         xhr = new XMLHttpRequest();
-        xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(this.onset())));
+        xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(diff)));
         xhr.onload = function () {
             console.log(xhr.responseText);
         };
@@ -17,9 +52,10 @@ var adapter = {
         xhr.open('GET', this.server_url + '/get');
         xhr.onload = function () {
             try {
-                var response = JSON.parse(this.responseText);
-                self.responseJson = response;
+                console.log(JSON.parse(this.responseText));
+                self.responseJson = JSON.parse(this.responseText);
                 self.onget();
+                console.log('loaded');
             } catch (err) {
                 console.warn("json could not be parsed:", err, this.responseText);
             }

+ 13 - 7
static/index.html

@@ -22,19 +22,22 @@
 <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>{{config}}</pre>
+    <!-- <pre>{{adapter}}</pre> -->
+
 </div>
 
 
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
+<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
+
 <script src="adapter.js"></script>
 
 <script>
@@ -44,18 +47,21 @@
         data: {
             config: {
                 age: 42,
-                attributes: ["Fluff", "yo"],
+                person1: "Manfred",
+                person2: "Persona"
             },
         },
         mounted: function () {
+
             adapter.onget = function () {
-                app.config = adapter.responseJson
+                app.config = JSON.parse(JSON.stringify(adapter.responseJson));
             }
+
             adapter.onset = function () {
-                return app.config;
+                return app.config
             }
 
-            // adapter.getter();
+            adapter.getter();
         },
         watch: {},
         computed: {