-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from MetaMask/CacheIdenticons
Cache identicons
- Loading branch information
Showing
4 changed files
with
91 additions
and
11 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
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,31 @@ | ||
const assert = require('assert') | ||
const sinon = require('sinon') | ||
|
||
const path = require('path') | ||
const IconFactoryGen = require(path.join(__dirname, '..', '..', '..', 'ui', 'lib', 'icon-factory.js')) | ||
|
||
describe('icon-factory', function() { | ||
let iconFactory, address, diameter | ||
|
||
beforeEach(function() { | ||
iconFactory = IconFactoryGen((d,n) => 'stubicon') | ||
address = '0x012345671234567890' | ||
diameter = 50 | ||
}) | ||
|
||
it('should return a data-uri string for any address and diameter', function() { | ||
const output = iconFactory.iconForAddress(address, diameter) | ||
assert.ok(output.indexOf('data:image/svg') === 0) | ||
assert.equal(output, iconFactory.cache[address][diameter]) | ||
}) | ||
|
||
it('should default to cache first', function() { | ||
const testOutput = 'foo' | ||
const mockSizeCache = {} | ||
mockSizeCache[diameter] = testOutput | ||
iconFactory.cache[address] = mockSizeCache | ||
|
||
const output = iconFactory.iconForAddress(address, diameter) | ||
assert.equal(output, testOutput) | ||
}) | ||
}) |
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,52 @@ | ||
var iconFactory | ||
|
||
module.exports = function(jazzicon) { | ||
if (!iconFactory) { | ||
iconFactory = new IconFactory(jazzicon) | ||
} | ||
return iconFactory | ||
} | ||
|
||
function IconFactory(jazzicon) { | ||
this.jazzicon = jazzicon | ||
this.cache = {} | ||
} | ||
|
||
IconFactory.prototype.iconForAddress = function(address, diameter) { | ||
if (this.isCached(address, diameter)) { | ||
return this.cache[address][diameter] | ||
} | ||
|
||
const dataUri = this.generateNewUri(address, diameter) | ||
this.cacheIcon(address, diameter, dataUri) | ||
return dataUri | ||
} | ||
|
||
IconFactory.prototype.generateNewUri = function(address, diameter) { | ||
var numericRepresentation = jsNumberForAddress(address) | ||
var identicon = this.jazzicon(diameter, numericRepresentation) | ||
var identiconSrc = identicon.innerHTML | ||
var dataUri = 'data:image/svg+xml;charset=utf-8,'+encodeURIComponent(identiconSrc) | ||
return dataUri | ||
} | ||
|
||
IconFactory.prototype.cacheIcon = function(address, diameter, icon) { | ||
if (!(address in this.cache)) { | ||
var sizeCache = {} | ||
sizeCache[diameter] = icon | ||
return this.cache[address] = sizeCache | ||
|
||
} else { | ||
return this.cache[address][diameter] = icon | ||
} | ||
} | ||
|
||
IconFactory.prototype.isCached = function(address, diameter) { | ||
return address in this.cache && diameter in this.cache[address] | ||
} | ||
|
||
function jsNumberForAddress(address) { | ||
var addr = address.slice(2, 10) | ||
var seed = parseInt(addr, 16) | ||
return seed | ||
} |