rspectacles 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ /*global define: true EventSource: true */
2
+ define(['riffle'], function (riffle) {
3
+ 'use strict';
4
+
5
+ return function streams(uri) {
6
+ var stream = riffle.stream
7
+ , me
8
+ , events
9
+ , matching
10
+ , toJson
11
+ , stringEvents
12
+ , eventToString
13
+ ;
14
+
15
+ events = stream(function (o, i) {
16
+ new EventSource(uri).addEventListener('message', o);
17
+ }).invoke();
18
+
19
+ eventToString = stream(function (o, i) {
20
+ o(i.data);
21
+ });
22
+
23
+ matching = function (regex) {
24
+ return stream(function (o, i) {
25
+ if (i.match(regex)) { o(i); }
26
+ });
27
+ };
28
+
29
+ toJson = stream(function (o, i) {
30
+ o(JSON.parse(i));
31
+ });
32
+
33
+ stringEvents = eventToString.input(events);
34
+
35
+ return me = {
36
+ message: matching(/^message:/).input(stringEvents)
37
+ , start: matching(/^status:start/).input(stringEvents)
38
+ , stop: matching(/^status:stop/).input(stringEvents)
39
+ , example: toJson.input(matching(/^\{/).input(stringEvents))
40
+ };
41
+ };
42
+ });