From d8fe372e87b8ebe65919b9e16206fd95048c107a Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 18 Apr 2016 22:31:16 -0700 Subject: [PATCH] feat: payloads now have a fingerprint which can be used to de-dupe (#3) --- index.js | 9 ++++++--- package.json | 2 +- test.js | 17 +++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index a2baff9..bbb0b4b 100644 --- a/index.js +++ b/index.js @@ -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 = '
  • {{description}}{{external-link-text}}
  • ' + this.template = '
  • {{description}}{{external-link-text}}
  • ' this.addonSelector = '#npm-addon-box' } @@ -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) } diff --git a/package.json b/package.json index 9b71a9d..0939429 100644 --- a/package.json +++ b/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": { diff --git a/test.js b/test.js index e6acbbe..1c3d500 100644 --- a/test.js +++ b/test.js @@ -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')($) @@ -12,9 +12,10 @@ $.mockjaxSettings.logging = false require('chai').should() describe('annotation-poller', function () { - beforeEach(function () { + beforeEach(function (cb) { document.body.innerHTML = '' $.mockjax.clear() + return cb() }) it('grabs an initial list of annotations when the page loads', function (done) { @@ -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' }] }) @@ -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' }] }) @@ -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' }] }) @@ -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' }] }) @@ -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' }] }) @@ -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' }] })