-
Notifications
You must be signed in to change notification settings - Fork 3
Simplify HashingStore interface. #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Sha256 } from 'ui/crypto'; | ||
|
||
export default function createStorageHash(json) { | ||
return new Sha256().update(json, 'utf8').digest('hex'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
import angular from 'angular'; | ||
import { sortBy } from 'lodash'; | ||
import { Sha256 } from 'ui/crypto'; | ||
|
||
import StubBrowserStorage from 'test_utils/stub_browser_storage'; | ||
import { LazyLruStore } from './lazy_lru_store'; | ||
|
||
/** | ||
* The HashingStore is a wrapper around a browser store object | ||
|
@@ -12,9 +7,9 @@ import { LazyLruStore } from './lazy_lru_store'; | |
* at a later time. | ||
*/ | ||
class HashingStore { | ||
constructor({ store, createHash, maxItems } = {}) { | ||
this._store = store || window.sessionStorage; | ||
if (createHash) this._createHash = createHash; | ||
constructor(createStorageHash, store) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why get rid of the config object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think config objects are great when dealing with a ton of arguments (more than 3), since they add more meaning for each argument when invoking the method. On the other hand, there's also some meaning in the order in which arguments are passed (in this case, the createStoreHash function is used to set items in the store, so it comes first). And config objects require additional syntax in their creation and in their consumption. Since this constructor only needs two arguments, I don't think this is an appropriate case for a config object. The readability benefits of the config object through named parameters don't exist any more and the additional syntax required to use the config object becomes noise, in my opinion. Also, personally, config objects stand out to me as yellow flags that indicate there may be a way to simplify the code and user fewer arguments. So by removing the config object here, I've removed that yellow flag for myself. |
||
this._createStorageHash = createStorageHash; | ||
this._store = store; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the defaults to make the dependencies explicit. |
||
} | ||
|
||
/** | ||
|
@@ -49,25 +44,13 @@ class HashingStore { | |
* @return {string} the hash of the value | ||
*/ | ||
hashAndSetItem(object) { | ||
// The object may contain Angular $$ properties, so let's ignore them. | ||
const json = angular.toJson(object); | ||
const hash = this._getShortHash(json); | ||
this._store.setItem(hash, json); | ||
return hash; | ||
} | ||
|
||
// private api | ||
|
||
/** | ||
* calculate the full hash of a json object | ||
* | ||
* @private | ||
* @param {string} json | ||
* @return {string} hash | ||
*/ | ||
_createHash(json) { | ||
return new Sha256().update(json, 'utf8').digest('hex'); | ||
} | ||
|
||
/** | ||
* Calculate the full hash for a json blob and then shorten in until | ||
* it until it doesn't collide with other short hashes in the store | ||
|
@@ -77,7 +60,7 @@ class HashingStore { | |
* @param {string} shortHash | ||
*/ | ||
_getShortHash(json) { | ||
const fullHash = `${HashingStore.HASH_TAG}${this._createHash(json)}`; | ||
const fullHash = `${HashingStore.HASH_TAG}${this._createStorageHash(json)}`; | ||
|
||
let short; | ||
for (let i = 7; i < fullHash.length; i++) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify the constructor to the point where it doesn't require a config object.