react-avatar
Advanced tools
Comparing version 1.2.2 to 2.0.0
@@ -17,2 +17,4 @@ 'use strict'; | ||
var _utils = require('./utils.js'); | ||
var _Gravatar = require('./sources/Gravatar.js'); | ||
@@ -79,3 +81,6 @@ | ||
instance.get(function (state) { | ||
if (state) { | ||
var failedBefore = state && state.hasOwnProperty('src') && (0, _utils.hasSourceFailedBefore)(state.src); | ||
if (!failedBefore && state) { | ||
_this.setState(state); | ||
@@ -95,3 +100,6 @@ return; | ||
// other social ID available to try | ||
if (event && event.type === 'error') _this.setState({ src: null }); | ||
if (event && event.type === 'error') { | ||
(0, _utils.cacheFailingSource)(_this.state.src); | ||
_this.setState({ src: null }); | ||
} | ||
@@ -98,0 +106,0 @@ if (SOURCES.length === _this.state._internal.sourcePointer) return; |
@@ -29,2 +29,7 @@ 'use strict'; | ||
if ((0, _utils.hasSourceFailedBefore)(url)) { | ||
setState(null); | ||
return; | ||
} | ||
(0, _utils.fetch)(url, function (data) { | ||
@@ -38,2 +43,3 @@ var src = data.entry.gphoto$thumbnail.$t; | ||
// on error | ||
(0, _utils.cacheFailingSource)(url); | ||
setState(null); | ||
@@ -40,0 +46,0 @@ }); |
@@ -9,2 +9,4 @@ 'use strict'; | ||
exports.getRandomColor = getRandomColor; | ||
exports.cacheFailingSource = cacheFailingSource; | ||
exports.hasSourceFailedBefore = hasSourceFailedBefore; | ||
@@ -41,3 +43,2 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
window[callbackName] = function (data) { | ||
console.log('CALLBACK', data, successCb); | ||
delete window[callbackName]; | ||
@@ -75,2 +76,31 @@ document.body.removeChild(script); | ||
return colors[colorIndex]; | ||
} | ||
function _hasLocalStorage() { | ||
return typeof Storage !== 'undefined'; | ||
} | ||
var CACHE_KEY = 'react-avatar'; | ||
function cacheFailingSource(source) { | ||
// cache not available | ||
if (!_hasLocalStorage) return; | ||
var cache = localStorage.getItem(CACHE_KEY) || ''; | ||
// already in cache | ||
if (cache.indexOf(source) > -1) return; | ||
var cacheList = cache.split(';'); | ||
cacheList.push(source); | ||
// only keep the last 20 results so we don't fill up local storage | ||
cacheList = cacheList.slice(-20); | ||
localStorage.setItem(CACHE_KEY, cacheList.join(';')); | ||
} | ||
function hasSourceFailedBefore(source) { | ||
var cache = localStorage.getItem(CACHE_KEY) || ''; | ||
return cache.indexOf(source) > -1; | ||
} |
{ | ||
"name": "react-avatar", | ||
"version": "1.2.2", | ||
"version": "2.0.0", | ||
"description": "Universal React avatar component makes it possible to generate avatars based on user information.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,3 @@ 'use strict'; | ||
import shallowCompare from 'react-addons-shallow-compare'; | ||
import {cacheFailingSource, hasSourceFailedBefore} from './utils.js'; | ||
@@ -135,3 +136,8 @@ import gravatarSource from './sources/Gravatar.js'; | ||
instance.get((state) => { | ||
if(state) { | ||
const failedBefore = state && | ||
state.hasOwnProperty('src') && | ||
hasSourceFailedBefore(state.src); | ||
if(!failedBefore && state) { | ||
this.setState(state); | ||
@@ -151,4 +157,6 @@ return; | ||
// other social ID available to try | ||
if( event && event.type === 'error' ) | ||
if( event && event.type === 'error' ) { | ||
cacheFailingSource(this.state.src); | ||
this.setState({src: null}); | ||
} | ||
@@ -155,0 +163,0 @@ if(SOURCES.length === this.state._internal.sourcePointer) |
'use strict'; | ||
import {fetch} from '../utils'; | ||
import {fetch,cacheFailingSource,hasSourceFailedBefore} from '../utils'; | ||
@@ -20,2 +20,7 @@ export default | ||
if(hasSourceFailedBefore(url)) { | ||
setState(null); | ||
return; | ||
} | ||
fetch(url, (data) => { | ||
@@ -29,2 +34,3 @@ const src = data.entry.gphoto$thumbnail.$t; | ||
// on error | ||
cacheFailingSource(url); | ||
setState(null); | ||
@@ -31,0 +37,0 @@ }); |
@@ -33,3 +33,2 @@ 'use strict'; | ||
window[callbackName] = function(data) { | ||
console.log('CALLBACK', data, successCb); | ||
delete window[callbackName]; | ||
@@ -74,1 +73,36 @@ document.body.removeChild(script); | ||
} | ||
function _hasLocalStorage() | ||
{ | ||
return typeof Storage !== 'undefined'; | ||
} | ||
const CACHE_KEY = 'react-avatar'; | ||
export | ||
function cacheFailingSource(source) | ||
{ | ||
// cache not available | ||
if(!_hasLocalStorage) | ||
return; | ||
const cache = localStorage.getItem(CACHE_KEY) || ''; | ||
// already in cache | ||
if(cache.indexOf(source) > -1) | ||
return; | ||
let cacheList = cache.split(';'); | ||
cacheList.push(source); | ||
// only keep the last 20 results so we don't fill up local storage | ||
cacheList = cacheList.slice(-20); | ||
localStorage.setItem(CACHE_KEY, cacheList.join(';')); | ||
} | ||
export | ||
function hasSourceFailedBefore(source) | ||
{ | ||
const cache = localStorage.getItem(CACHE_KEY) || ''; | ||
return cache.indexOf(source) > -1; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1574999
38623