Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make close() a prototype method. Closes #67. #77

Merged
merged 1 commit into from May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/eventsource.js
Expand Up @@ -214,7 +214,7 @@ function EventSource (url, eventSourceInitDict) {
}
}

this.close = function () {
this._close = function () {
if (readyState === EventSource.CLOSED) return
readyState = EventSource.CLOSED
if (req.abort) req.abort()
Expand Down Expand Up @@ -309,6 +309,16 @@ EventSource.prototype.CONNECTING = 0
EventSource.prototype.OPEN = 1
EventSource.prototype.CLOSED = 2

/**
* Closes the connection, if one is made, and sets the readyState attribute to 2 (closed)
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource/close
* @api public
*/
EventSource.prototype.close = function () {
this._close()
}

/**
* Emulates the W3C Browser based WebSocket interface using addEventListener.
*
Expand Down
18 changes: 18 additions & 0 deletions test/eventsource_test.js
Expand Up @@ -872,6 +872,24 @@ describe('readyState', function () {
})
})

describe('Methods', function () {
it('close method exists and can be called to close an eventsource', function (done) {
createServer(function (err, server) {
if (err) return done(err)
var es = new EventSource(server.url)
server.on('request', writeEvents([]))
es.onopen = function () {
assert.equal(es.close(), undefined)
server.close(done)
}
})
})

it('close method is a prototype method', function () {
assert.equal(typeof EventSource.prototype.close, 'function')
})
})

describe('Properties', function () {
it('url exposes original request url', function () {
var url = 'http://localhost:' + _port
Expand Down