Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Datasrouces can define Server Side Events to listen to.

Data is added to the collection of this datasource
Views are re-renderend because of collection change.

new version of W3C bootstrap, that defines the Server Side Event stream for the first datasource
  • Loading branch information...
commit 145e52e704c1eeed86ae941673f7d754a253f4d0 1 parent 9fa5f87
@steren steren authored
Showing with 39 additions and 3 deletions.
  1. +2 −1  app/bootstrap-w3c.js
  2. +37 −2 app/sleek.js
View
3  app/bootstrap-w3c.js
@@ -87,7 +87,8 @@
}
},
"outputType": "ImageObject",
- "runtime": "browser"
+ "runtime": "browser",
+ "sse" : {"url": "/stream"}
}, {
"name": "Flickr",
"db": "operator",
View
39 app/sleek.js
@@ -185,6 +185,11 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
collection: collection,
index: index
};
+
+ // if this datasource is registered to server side event,
+ if(datasource.config.sse) {
+ self.listenToSSE(sections[index], datasource.config.sse.url);
+ }
}, self));
// Create the views once all sections have been initialized
@@ -470,7 +475,7 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
},
/**
- * Refreshes a section list.
+ * Refreshes a section list by forcing a fetch from server
*
* @function
* @param {Object} the list section
@@ -488,7 +493,7 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
},
/**
- * Updates a section list.
+ * Updates a section list (uses cache if present)
*
* @function
* @param {Object} the list section
@@ -503,6 +508,36 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
},
/**
+ * Make this section listen to Server Side Event (SSE)
+ *
+ * @function
+ * @param section: the section that should listen to SSE
+ * @param streamURL: utl of the SSE stream
+ */
+ listenToSSE: function(section, streamURL) {
+ var source = new EventSource(streamURL);
+
+ var self = this;
+ source.addEventListener('message', function(e) {
+ console.log(e.data);
+ var data = JSON.parse(e.data);
+
+ section.collection.add(data);
+ }, false);
+
+ source.addEventListener('open', function(e) {
+ console.log("SSE opened");
+ }, false);
+
+ source.addEventListener('error', function(e) {
+ if (e.readyState == EventSource.CLOSED) {
+ console.log("SSE lost");
+ }
+ }, false);
+
+ },
+
+ /**
* Displays a section list
* (assuming the section is already active).
*
Please sign in to comment.
Something went wrong with that request. Please try again.