Skip to content

Commit

Permalink
fix event dispatch and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pemrouz committed May 2, 2016
1 parent 04b4d59 commit 7926520
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
9 changes: 4 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ function fetched(ripple) {
};
}

function trickle(ripple) {
return function (res) {
var args = [arguments[0].body, arguments[1]];
return (0, _header2.default)('content-type', 'application/hypermedia')(res) && ripple.resources[res.name].body.emit('change', _to2.default.arr(args), (0, _not2.default)(_is2.default.in(['bubble'])));
var trickle = function trickle(ripple) {
return function (name, change) {
return (0, _header2.default)('content-type', 'application/hypermedia')(ripple.resources[name]) && ripple.resources[name].body.emit('change', [change || null], (0, _not2.default)(_is2.default.in(['bubble'])));
};
}
};
13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ function fetched(ripple){
}
}

function trickle(ripple){
return function(res){
var args = [arguments[0].body, arguments[1]]
return header('content-type', 'application/hypermedia')(res)
&& ripple.resources[res.name].body.emit('change', to.arr(args), not(is.in(['bubble'])))
}
}
const trickle = ripple => (name, change) =>
header('content-type', 'application/hypermedia')(ripple.resources[name]) &&
ripple
.resources[name]
.body
.emit('change', [change || null], not(is.in(['bubble'])))
49 changes: 34 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('Hypermedia API', function() {
it('should load root resource', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github').once('change', function(github){
ripple('github').once('change', function(change){
var github = change.value
expect(keys(github).length).to.be.gt(10)
done()
})
Expand All @@ -22,7 +23,8 @@ describe('Hypermedia API', function() {
it('should follow links', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github.current_user_url').on('change', function(user){
ripple('github.current_user_url').on('change', function(change){
var user = change.value
if (!user.login) return
expect('github' in ripple.resources).to.be.ok
expect('github.current_user_url' in ripple.resources).to.be.ok
Expand All @@ -35,7 +37,8 @@ describe('Hypermedia API', function() {
it('should load and cache intermediate links', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github.current_user_url.id').on('change', function(user){
ripple('github.current_user_url.id').on('change', function(change){
var user = change.value
if (!user.value) return
expect('github' in ripple.resources).to.be.ok
expect('github.current_user_url' in ripple.resources).to.be.ok
Expand All @@ -48,7 +51,8 @@ describe('Hypermedia API', function() {
it('should work with arrays', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github.current_user_url.repos_url.0').on('change', function(repo){
ripple('github.current_user_url.repos_url.0').on('change', function(change){
var repo = change.value
if (!repo.name) return
expect(repo.name).to.eql('builder')
done()
Expand All @@ -58,7 +62,8 @@ describe('Hypermedia API', function() {
it('should traverse deep simple keys too', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github.current_user_url.repos_url.0.owner.login').on('change', function(login){
ripple('github.current_user_url.repos_url.0.owner.login').on('change', function(change){
var login = change.value
if (!login.value) return
expect(login.value).to.eql('OGLES')
done()
Expand All @@ -71,7 +76,8 @@ describe('Hypermedia API', function() {

ripple('github', 'https://api.github.com', { http: http })

ripple('github.current_user_url').on('change.first', function(user){
ripple('github.current_user_url').on('change.first', function(change){
var user = change.value
if (!user.login) return
ripple('github.current_user_url').on('change.second', Function('throw Error'))
time(1000, done)
Expand All @@ -81,7 +87,8 @@ describe('Hypermedia API', function() {
it('should fail if cannot fetch resource - general', function(done){
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com')
ripple('github.current_user_url').on('change', function(body){
ripple('github.current_user_url').on('change', function(change){
var body = change.value
expect(body).to.be.eql({})
})

Expand All @@ -108,11 +115,14 @@ describe('Hypermedia API', function() {
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('repo', { owner: 'pemrouz', repo: 'ripple' }, { link: 'github.repository_url' })
.on('change', function(repo){
.on('change', function(change){
var repo = change.value
if (!ripple('repo').value) return
if (!repo.id) return
expect('repo' in ripple.resources).to.be.ok
expect('github' in ripple.resources).to.be.ok
expect('github.repository_url' in ripple.resources).to.be.ok
expect(ripple('repo').id).to.be.equal(21631189)
expect(ripple('repo').value.id).to.be.equal(21631189)
expect(ripple.resources['repo'].headers.timestamp).to.be.ok
done()
})
Expand All @@ -122,7 +132,8 @@ describe('Hypermedia API', function() {
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('github.repository_url', { owner: 'pemrouz', repo: 'ripple' })
.on('change', function(repo){
.on('change', function(change){
var repo = change.value
if (!repo.id) return
expect('github' in ripple.resources).to.be.ok
expect('github.repository_url' in ripple.resources).to.be.ok
Expand All @@ -139,8 +150,10 @@ describe('Hypermedia API', function() {
var ripple = hypermedia(data(core()))
ripple('repo', 'https://api.github.com/repos/pemrouz/ripple', { http: http })
ripple('repo.issues_url', { number: 1 })
.on('change', function(issue){
.on('change', function(change){
var issue = change.value
if (!issue.id) return
if (!ripple('repo.issues_url')) return
expect('repo' in ripple.resources).to.be.ok
expect('repo.issues_url' in ripple.resources).to.be.ok
expect(ripple('repo.issues_url').id).to.be.equal(39576741)
Expand All @@ -153,12 +166,15 @@ describe('Hypermedia API', function() {
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('issue', { owner: 'pemrouz', repo: 'ripple', number: 1 }, { link: 'github.repository_url.issues_url' })
.on('change', function(issue){
.on('change', function(change){
var issue = change.value
if (!ripple('issue').value) return
if (!issue.id) return
expect('issue' in ripple.resources).to.be.ok
expect('github' in ripple.resources).to.be.ok
expect('github.repository_url' in ripple.resources).to.be.ok
expect('github.repository_url.issues_url' in ripple.resources).to.be.ok
expect(ripple('issue').id).to.be.equal(39576741)
expect(ripple('issue').value.id).to.be.equal(39576741)
expect(ripple.resources['issue'].headers.timestamp).to.be.ok
expect(issue.id).to.be.equal(39576741)
done()
Expand All @@ -172,12 +188,15 @@ describe('Hypermedia API', function() {
var ripple = hypermedia(data(core()))
ripple('github', 'https://api.github.com', { http: http })
ripple('issues', { owner: 'pemrouz', repo: 'ripple' }, { link: 'github.repository_url.issues_url' })
.on('change', function(issues){
.on('change', function(change){
var issues = change.value
if (!ripple('issues').value) return
if (!issues.length) return
expect('issues' in ripple.resources).to.be.ok
expect('github' in ripple.resources).to.be.ok
expect('github.repository_url' in ripple.resources).to.be.ok
expect('github.repository_url.issues_url' in ripple.resources).to.be.ok
expect(ripple('issues').length).to.be.above(10)
expect(ripple('issues').value.length).to.be.above(10)
expect(ripple.resources['issues'].headers.timestamp).to.be.ok
expect(issues.length).to.be.above(10)
done()
Expand Down

0 comments on commit 7926520

Please sign in to comment.