This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
584e899
commit 7fa5f15
Showing
6 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
### `Rx.DOM.from(url, [openObserver])` | ||
[Ⓢ](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/src/eventsource.js "View in source") | ||
|
||
This method wraps an EventSource as an observable sequence which is used to send server-side events. Note that your browser must support EventSource objects. | ||
|
||
#### Arguments | ||
1. `url` *(String)*: The URL of the Server-Side Events. | ||
3. `[openObserver]` *(`Rx.Observer`)*: An optional Observer to capture the open event. | ||
|
||
#### Returns | ||
*(`Observable`)*: An observable sequence which represents the data from a server-side event. | ||
|
||
#### Example | ||
```js | ||
// Using a function for the open | ||
var source = Rx.DOM.fromEventSource('foo.php'); | ||
|
||
source.subscribe(function (e) { | ||
console.log('Received data: ' + e.data); | ||
}); | ||
|
||
|
||
// Using an observer for the open | ||
var observer = Rx.Observer.create(function (e) { | ||
console.log('Opening'); | ||
}); | ||
|
||
// Using a function for the open | ||
var source = Rx.DOM.fromEventSource('foo.php', observer); | ||
|
||
socket.subscribe(function (e) { | ||
console.log('Received data: ' + e.data); | ||
}); | ||
|
||
socket.onNext('data'); | ||
``` | ||
|
||
### Location | ||
|
||
File: | ||
- [`/src/eventsource.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/src/eventsource.js) | ||
|
||
Dist: | ||
- [`rx.dom.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/dist/rx.dom.js) | - [`rx.dom.compat.js`](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/dist/rx.dom.compat.js) | ||
|
||
Prerequisites: | ||
- If using `rx.js` | ||
- [`rx.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.js) | [`rx.compat.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.compat.js) | ||
- [`rx.binding.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/dist/rx.binding.js) | ||
- [`rx.lite.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/rx.lite.js) | [`rx.lite.compat.js`](https://github.com/Reactive-Extensions/RxJS/blob/master/rx.lite.compat.js) | ||
|
||
NPM Packages: | ||
- [`rx-dom`](https://preview.npmjs.com/package/rx-dom) | ||
|
||
NuGet Packages: | ||
- [`RxJS-Bridges-HTML`](http://www.nuget.org/packages/RxJS-Bridges-HTML/) | ||
|
||
Unit Tests: | ||
- [`/tests/tests.eventsource.js](https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/tests/tests.eventsource.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
if (!!root.EventSource) { | ||
|
||
/** | ||
* This method wraps an EventSource as an observable sequence. | ||
* @param {String} url The url of the server-side script. | ||
* @param {Observer} [openObserver] An optional observer for the 'open' event for the server side event. | ||
* @returns {Observable} An observable sequence which represents the data from a server-side event. | ||
*/ | ||
dom.fromEventSource = function (url, openObserver) { | ||
return new AnonymousObservable(function (observer) { | ||
var source = new root.EventSource(url); | ||
|
||
function onOpen(e) { | ||
openObserver.onNext(e); | ||
openObserver.onCompleted(); | ||
source.removeEventListener('open', onOpen, false); | ||
} | ||
|
||
function onError(e) { | ||
if (e.readyState === EventSource.CLOSED) { | ||
observer.onCompleted(); | ||
} else { | ||
observer.onError(e); | ||
} | ||
} | ||
|
||
function onMessage(e) { | ||
observer.onNext(e); | ||
} | ||
|
||
openObserver && source.addEventListener('open', onOpen, false); | ||
source.addEventListener('error', onError, false); | ||
source.addEventListener('message', onMessage, false); | ||
|
||
return function () { | ||
source.removeEventListener('error', onError, false); | ||
source.removeEventListener('message', onMessage, false); | ||
source.close(); | ||
}; | ||
}); | ||
}; | ||
} |