changeset 1352:d9a866bfb9d5

js syncedgraph reconnects to server Ignore-this: 26a78f094cf403c453fdde8a67db039a
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 05 Jun 2016 03:40:57 +0000
parents 6044e527d795
children 0de7fd3fed1f
files light9/web/graph.coffee
diffstat 1 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/light9/web/graph.coffee	Sun Jun 05 03:39:39 2016 +0000
+++ b/light9/web/graph.coffee	Sun Jun 05 03:40:57 2016 +0000
@@ -64,15 +64,23 @@
   # Note that applyPatch is the only method to write to the graph, so
   # it can fire subscriptions.
 
-  constructor: (@patchSenderUrl, prefixes) ->
-    @graph = N3.Store()
-    @_addPrefixes(prefixes)
+  constructor: (@patchSenderUrl, @prefixes) ->
+    @resetStore()
     @_watchers = new GraphWatchers()
     @patchesToSend = []
+
+    @_reconnectionTimeout = null
     @newConnection()
 
+  resetStore: ->
+    if @graph?
+      @_watchers.graphChanged({addQuads: [], delQuads: @graph.find()})
+    @graph = N3.Store()
+    @_addPrefixes(@prefixes)
+
   newConnection: ->
     fullUrl = 'ws://' + window.location.host + @patchSenderUrl
+    @ws.close() if @ws?
     @ws = new WebSocket(fullUrl)
 
     @ws.onopen = =>
@@ -82,9 +90,12 @@
 
     @ws.onerror = (e) =>
       log('ws error ' + e)
+      @ws.onclose()
 
     @ws.onclose = =>
       log('ws close')
+      clearTimeout(@_reconnectionTimeout) if @_reconnectionTimeout?
+      @_reconnectionTimeout = setTimeout(@newConnection.bind(@), 1000)
 
     @ws.onmessage = (evt) =>
       if evt.data == 'PONG'
@@ -100,7 +111,7 @@
 
   onMessage: (msg) ->
     log('from rdfdb: ', msg)
-    
+      
     patch = {delQuads: [], addQuads: []}
 
     parseAdds = (cb) =>