Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
feat: payloads now have a fingerprint which can be used to de-dupe (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Apr 19, 2016
1 parent 8d8fbf5 commit d8fe372
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
9 changes: 6 additions & 3 deletions index.js
Expand Up @@ -3,9 +3,9 @@ var Mustache = require('mustache')

function AnnotationPoller (opts) {
this.pollInterval = opts.pollInterval || 3000
this.endpoint = '/api/v1/annotations.js'
this.endpoint = '/api/v1/annotations'
this.annotations = {}
this.template = '<li id="annotation-{{id}}" style="{{status}}"><span>{{description}}</span><a href="{{{external-link}}}">{{external-link-text}}</a></li>'
this.template = '<li id="annotation-{{id}}" style="{{status}}" data-fingerprint={{fingerprint}}><span>{{description}}</span><a href="{{{external-link}}}">{{external-link-text}}</a></li>'
this.addonSelector = '#npm-addon-box'
}

Expand Down Expand Up @@ -58,7 +58,10 @@ AnnotationPoller.prototype.renderAnnotations = function () {
annotationElement = $('#annotation-' + annotation.id)
newAnnotationElement = Mustache.render(_this.template, annotation)
if (annotationElement.length) {
annotationElement.replaceWith(newAnnotationElement)
// don't render the element unless its fingerprint has changed.
if (annotationElement.data('fingerprint') !== annotation.fingerprint) {
annotationElement.replaceWith(newAnnotationElement)
}
} else {
addonBox.append(newAnnotationElement)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "annotation-poller",
"version": "1.0.0",
"version": "1.1.0",
"description": "poll for annotations from external services, place them on packages",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 9 additions & 8 deletions test.js
Expand Up @@ -3,7 +3,7 @@
require('jsdom-global')()

var annotationPoller = require('./')
var endpoint = '/api/v1/annotations.js'
var endpoint = '/api/v1/annotations'
var $ = require('jquery')

require('jquery-mockjax')($)
Expand All @@ -12,9 +12,10 @@ $.mockjaxSettings.logging = false
require('chai').should()

describe('annotation-poller', function () {
beforeEach(function () {
beforeEach(function (cb) {
document.body.innerHTML = '<ul class="box" id="npm-addon-box"></ul>'
$.mockjax.clear()
return cb()
})

it('grabs an initial list of annotations when the page loads', function (done) {
Expand All @@ -27,7 +28,7 @@ describe('annotation-poller', function () {
description: 'foo security integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'start audit',
timeout: 20
fingerprint: 'foo'
}]
})

Expand All @@ -50,7 +51,7 @@ describe('annotation-poller', function () {
description: 'my awesome integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'start audit',
timeout: 20
fingerprint: 'bar'
}]
})

Expand All @@ -73,7 +74,7 @@ describe('annotation-poller', function () {
description: 'my awesome integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'start audit',
timeout: 20
fingerprint: 'foo'
}]
})

Expand All @@ -89,7 +90,7 @@ describe('annotation-poller', function () {
description: 'my awesome integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'view details',
timeout: 20
fingerprint: 'bar'
}]
})

Expand All @@ -112,7 +113,7 @@ describe('annotation-poller', function () {
description: 'my awesome integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'start audit',
timeout: 20
fingerprint: 'foo'
}]
})

Expand All @@ -128,7 +129,7 @@ describe('annotation-poller', function () {
description: 'my second integration',
'external-link': 'http://example.com/foo-package/audit',
'external-link-text': 'view details',
timeout: 20
fingerprint: 'foo'
}]
})

Expand Down

0 comments on commit d8fe372

Please sign in to comment.