-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the annotation count service client out of TabState
Separate out the logic for querying the server for annotation counts and processing the response from the logic for updating the tab state. * Move duplicated code for setting up an XHR and returning a Promise for the result into a separate module. At some point in future we'll be able to just replace it with a call to window.fetch() * Move the client code for the Hypothesis /badge endpoint out of TabState into its own module and divide up the tests accordingly. * Ensure the annotation count for a tab is reset to zero if querying the /badge endpoint fails.
- Loading branch information
1 parent
b4dd3d1
commit e3d5fec
Showing
9 changed files
with
181 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Performs a network fetch for JSON data and returns a Promise | ||
* for the result. | ||
*/ | ||
function fetchJSON(url) { | ||
return new Promise(function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onload = function () { | ||
try { | ||
resolve(JSON.parse(this.response)); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
}); | ||
} | ||
|
||
module.exports = fetchJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var fetchJSON = require('./fetch'); | ||
var settings = require('./settings'); | ||
|
||
/** encodeUriQuery encodes a string for use in a query parameter */ | ||
function encodeUriQuery(val) { | ||
return encodeURIComponent(val).replace(/%20/g, '+'); | ||
} | ||
|
||
/** | ||
* Queries the Hypothesis service that provides | ||
* statistics about the annotations for a given URL. | ||
*/ | ||
function query(uri) { | ||
return settings.then(function (settings) { | ||
return fetchJSON(settings.apiUrl + '/badge?uri=' + encodeUriQuery(uri)); | ||
}).then(function (data) { | ||
if (typeof data.total !== 'number') { | ||
throw new Error('Annotation count is not a number'); | ||
} | ||
return data; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
query: query | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"apiUrl": "http://127.0.0.1:5000/api/", | ||
"apiUrl": "http://127.0.0.1:5000/api", | ||
"blocklist": {"twitter.com": {}, "finance.yahoo.com": {}, "*.google.com": {}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.