-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsc-sse.html
84 lines (77 loc) · 2.3 KB
/
sc-sse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<link rel="import" href="../polymer/polymer.html">
<!--
sc-sse is webcomponent to Server Side Events of HTML5.
##### Example
<sc-sse stream="/stream/" channel="message" alias="updates"></sc-sse>
@element sc-sse
@blurb Element providing an interface to consume updates by Server Side Events
@status beta
@homepage http://github.com/siscomando/sc-sse
-->
<polymer-element name="sc-sse" hidden attributes="notitle author stream channel alias">
<script>
Polymer('sc-sse', {
/**
* Fired when a reply is returned by Server Side Event.
*
*
* @event sc-sse-response
*/
/**
* The `author` attribute sets an initial author
*
* @attribute author
* @type String
* @default 'Horacio Ibrahim'
*/
author: 'Horacio Ibrahim',
/**
* The `alias` is name for recognize one of multiple sc-sse element fired.
*
* @attribute alias
* @type String
* @default default
*/
/**
* The `stream` attribute sets an url to get updates
*
* @attribute stream
* @type String
* @default ''
*/
publish: {
stream: '',
channel: 'message',
alias: 'default'
},
ready: function() {
var stream = this.stream;
var callback = this.fire; //this.fire;
var channel = this.channel;
var el = this;
try {
var source = new EventSource(stream);
this.makeRead(source, channel, callback, el);
} catch (x) {
console.warn('sc-sse caught an exception trying create new EventSource.');
console.warn(x);
}
},
/**
* The `makeRead` make connection with Server Side Event stream.
*
*
* @method makeRead
* @param source an instance of EventSource
* @param channel a type of event
* @param callback a callback for to work with polymer objects
* @param onNode this current polymer element
*/
makeRead: function(source, channel, callback, onNode) {
source.addEventListener(channel, function(e) {
callback('sc-sse-response', {response: e.data, alias: onNode.alias}, onNode);
});
},
});
</script>
</polymer-element>