on master help sign in
Fix for websockets message during xhr.send
a51df99Hamish Mackenzie
jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+5−1
@@ -133,9 +133,11 @@ jsaddleJs = "\
133 \ var syncKey = \"\";\n\
133 \ var syncKey = \"\";\n\
134 \\n\
134 \\n\
135 \ ws.onopen = function(e) {\n\
135 \ ws.onopen = function(e) {\n\
136− \ " <> initState <> "\
136+ \ " <> initState <> "\n\
137+ \ var inXhrSend = false;\n\
137 \\n\
138 \\n\
138 \ ws.onmessage = function(e) {\n\
139 \ ws.onmessage = function(e) {\n\
140+ \ if(inXhrSend) return;\n\
139 \ var batch = JSON.parse(e.data);\n\
141 \ var batch = JSON.parse(e.data);\n\
140 \ if(typeof batch === \"string\") {\n\
142 \ if(typeof batch === \"string\") {\n\
141 \ syncKey = batch;\n\
143 \ syncKey = batch;\n\
@@ -144,10 +146,12 @@ jsaddleJs = "\
144 \\n\
146 \\n\
145 \ " <> runBatch (\a -> "ws.send(JSON.stringify(" <> a <> "));")
147 \ " <> runBatch (\a -> "ws.send(JSON.stringify(" <> a <> "));")
146 (Just (\a -> "(function(){\n\
148 (Just (\a -> "(function(){\n\
149+ \ inXhrSend = true;\n\
147 \ var xhr = new XMLHttpRequest();\n\
150 \ var xhr = new XMLHttpRequest();\n\
148 \ xhr.open('POST', '/sync/'+syncKey, false);\n\
151 \ xhr.open('POST', '/sync/'+syncKey, false);\n\
149 \ xhr.setRequestHeader(\"Content-type\", \"application/json\");\n\
152 \ xhr.setRequestHeader(\"Content-type\", \"application/json\");\n\
150 \ xhr.send(JSON.stringify(" <> a <> "));\n\
153 \ xhr.send(JSON.stringify(" <> a <> "));\n\
154+ \ inXhrSend = false;\n\
151 \ return JSON.parse(xhr.response);})()")) <> "\
155 \ return JSON.parse(xhr.response);})()")) <> "\
152 \ };\n\
156 \ };\n\
153 \ };\n\
157 \ };\n\