Skip to content

Commit

Permalink
+ anticore.sse (Server-Sent Events)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Jan 25, 2019
1 parent 37fd3aa commit 67b3087
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
16 changes: 16 additions & 0 deletions anticore.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ Can be prevented by `event.preventDefault()`
anticore.fetchFromEvent(event)
```

## anticore.sse

## Usage

To use the internal `toDom()` reviver

```js
source = anticore.sse(url, config)
```

To use your own reviver

```js
source = anticore.sse(url, config, reviver)
```

## anticore.onError

## Usage
Expand Down
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import { getTarget } from './dom/emitter/getTarget'
import { onClick } from './dom/emitter/on/onClick'
import { onError } from './dom/emitter/on/onError'
import { onMessage } from './dom/emitter/on/onMessage'
import { onSubmit } from './dom/emitter/on/onSubmit'
import { prevent } from './dom/emitter/prevent'
import { nodeName } from './dom/info/nodeName'
Expand All @@ -30,6 +32,7 @@ const console = window.console
const encodeURIComponent = window.encodeURIComponent
const URL = window.URL
const FormData = window.FormData
const EventSource = window.EventSource
const registry = create()
const types = ['html', 'svg', 'xml']
const selector = 'input[type=submit]:focus,' +
Expand Down Expand Up @@ -186,6 +189,28 @@ anticore.fetchFromEvent = function (event) {
return false
}

/**
* Listens an event source
* turns the contents to DOM, except if a reviver is provided
* @param {string} url
* @param {object} config
* @param {function} reviver
* @returns {EventSource} source
*/
anticore.sse = function (url, config, reviver) {
const source = new EventSource(url, config)

onMessage(source, function (event) {
populate((reviver || toDOM)(event.data), true, url)
})

onError(source, function (event) {
anticore.onError(event)
})

return source
}

anticore.onError = console.error.bind(console)

/**
Expand Down Expand Up @@ -258,10 +283,10 @@ function fetchRequest () {
}

function onResponse (response) {
const item = queue[0]
const type = ((response.headers.get('content-type') ||
'application/octet-stream').match(/json|html|svg|xml|text(?=\/plain)/) ||
['blob'])[0]
const item = queue[0]

item.type = type
item.request.response = response
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anticore",
"version": "2.23.2",
"version": "2.23.3",
"description": "A generic living DOM library to simplify your client code, like with its easiest middleware manager for AJAX requests.",
"main": "index.js",
"module": "index.js",
Expand Down

0 comments on commit 67b3087

Please sign in to comment.