|
26 | 26 | stream: {}, |
27 | 27 | lastEventId: 0, |
28 | 28 | isHostApi: false, |
| 29 | + retry: 500, |
29 | 30 | history: {}, |
30 | 31 | options: {} |
31 | 32 | }, |
|
71 | 72 | var label = options.label; |
72 | 73 |
|
73 | 74 | stream.cache[ label ].stream.addEventListener("open", function(event) { |
74 | | - if ( stream.cache[label] ) { |
| 75 | + if ( stream.cache[ label ] ) { |
75 | 76 |
|
76 | 77 | this.label = label; |
77 | 78 |
|
78 | | - stream.cache[label].options.open.call(this, event); |
| 79 | + stream.cache[ label ].options.open.call(this, event); |
79 | 80 | } |
80 | 81 | }, false); |
81 | 82 |
|
82 | 83 | stream.cache[label].stream.addEventListener("message", function(event) { |
83 | 84 |
|
84 | 85 | var streamData = []; |
85 | 86 |
|
86 | | - if ( stream.cache[label] ) { |
| 87 | + if ( stream.cache[ label ] ) { |
87 | 88 |
|
88 | 89 | streamData[ streamData.length ] = jQuery.parseJSON( event.data ); |
89 | 90 |
|
90 | 91 | this.label = label; |
91 | 92 |
|
92 | | - stream.cache[label].lastEventId = +event.lastEventId; |
93 | | - stream.cache[label].history[stream.cache[label].lastEventId] = streamData; |
94 | | - stream.cache[label].options.message.call(this, streamData[0] ? streamData[0] : null, { |
| 93 | + stream.cache[ label ].lastEventId = +event.lastEventId; |
| 94 | + stream.cache[ label ].history[stream.cache[ label ].lastEventId] = streamData; |
| 95 | + stream.cache[ label ].options.message.call(this, streamData[0] ? streamData[0] : null, { |
95 | 96 | data: streamData, |
96 | | - lastEventId: stream.cache[label].lastEventId |
| 97 | + lastEventId: stream.cache[ label ].lastEventId |
97 | 98 | }, event); |
98 | 99 |
|
99 | 100 | // TODO: Add custom event triggering |
100 | 101 | } |
101 | 102 | }, false); |
102 | 103 |
|
103 | | - return stream.cache[label].stream; |
| 104 | + return stream.cache[ label ].stream; |
104 | 105 | }, |
105 | 106 | // open fallback event source |
106 | 107 | openPollingSource: function( options ) { |
107 | | - var label = options.label, |
| 108 | + var label = options.label, |
108 | 109 | source; |
109 | 110 |
|
110 | | - if ( stream.cache[label] ) { |
| 111 | + if ( stream.cache[ label ] ) { |
111 | 112 |
|
112 | 113 | source = jQuery.ajax({ |
113 | 114 | type: "GET", |
114 | 115 | url: options.url, |
115 | 116 | data: options.data, |
116 | 117 | beforeSend: function() { |
117 | | - if ( stream.cache[label] ) { |
| 118 | + if ( stream.cache[ label ] ) { |
118 | 119 | this.label = label; |
119 | | - stream.cache[label].options.open.call( this ); |
| 120 | + stream.cache[ label ].options.open.call( this ); |
120 | 121 | } |
121 | 122 | }, |
122 | 123 | success: function( data ) { |
123 | 124 |
|
124 | 125 | var tempdata, |
125 | 126 | label = options.label, |
126 | 127 | parsedData = [], |
127 | | - streamData = jQuery.map( data.split("\n"), function(sdata, i) { |
| 128 | + streamData = jQuery.map( data.split("\n\n"), function(sdata, i) { |
128 | 129 | return !!sdata && sdata; |
129 | | - }), |
130 | | - idx = 0, length = streamData.length; |
| 130 | + }), |
| 131 | + idx = 0, length = streamData.length, |
| 132 | + rretryprefix = /retry/, |
| 133 | + retries; |
131 | 134 |
|
132 | | - if ( jQuery.isArray(streamData) ) { |
| 135 | + if ( jQuery.isArray( streamData ) ) { |
133 | 136 |
|
134 | 137 | for ( ; idx < length; idx++ ) { |
135 | 138 |
|
136 | | - if ( streamData[idx] ) { |
137 | | - tempdata = streamData[idx].split("data: ")[ 1 ]; |
| 139 | + if ( streamData[ idx ] ) { |
138 | 140 |
|
139 | | - // Convert `dataType` here |
140 | | - if ( options.dataType === "json" ) { |
141 | | - tempdata = jQuery.parseJSON( tempdata ); |
142 | | - } |
| 141 | + if ( rretryprefix.test( streamData[ idx ] ) && |
| 142 | + (retries = streamData[ idx ].split("retry: ")).length ) { |
| 143 | + |
| 144 | + if ( retries.length === 2 && !retries[ 0 ] ) { |
| 145 | + |
| 146 | + stream.cache[ label ].retry = stream.cache[ label ].options.retry = +retries[ 1 ]; |
| 147 | + } |
| 148 | + |
| 149 | + } else { |
| 150 | + tempdata = streamData[ idx ].split("data: ")[ 1 ]; |
143 | 151 |
|
144 | | - parsedData[ parsedData.length ] = tempdata; |
| 152 | + // Convert `dataType` here |
| 153 | + if ( options.dataType === "json" ) { |
| 154 | + tempdata = jQuery.parseJSON( tempdata ); |
| 155 | + } |
| 156 | + |
| 157 | + parsedData[ parsedData.length ] = tempdata; |
| 158 | + } |
145 | 159 | } |
146 | 160 | } |
147 | 161 | } |
148 | 162 |
|
149 | | - if ( stream.cache[label] ) { |
| 163 | + if ( stream.cache[ label ] ) { |
150 | 164 |
|
151 | 165 | this.label = label; |
152 | 166 |
|
153 | | - stream.cache[label].lastEventId++; |
154 | | - stream.cache[label].history[stream.cache[label].lastEventId] = parsedData; |
155 | | - stream.cache[label].options.message.call(this, parsedData[0] ? parsedData[0] : null, { |
| 167 | + stream.cache[ label ].lastEventId++; |
| 168 | + stream.cache[ label ].history[ stream.cache[ label ].lastEventId ] = parsedData; |
| 169 | + stream.cache[ label ].options.message.call(this, parsedData[0] ? parsedData[0] : null, { |
156 | 170 | data: parsedData, |
157 | | - lastEventId: stream.cache[label].lastEventId |
| 171 | + lastEventId: stream.cache[ label ].lastEventId |
158 | 172 | }); |
159 | 173 |
|
160 | | - |
161 | 174 | setTimeout( |
162 | 175 | function() { |
163 | 176 | pluginFns._private.openPollingSource.call( this, options ); |
164 | 177 | }, |
165 | | - // matches speed of host api EventSource |
166 | | - 500 |
| 178 | + // Use server sent retry time if exists or default retry time if not |
| 179 | + ( stream.cache[ label ] && stream.cache[ label ].retry ) || 500 |
167 | 180 | ); |
168 | 181 | } |
169 | 182 | }, |
|
0 commit comments