-
Notifications
You must be signed in to change notification settings - Fork 3
Rename HashingStore interface methods for consistency and clarity. #17
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 |
---|---|---|
|
@@ -18,14 +18,14 @@ export default function StateProvider(Private, $rootScope, $location, config) { | |
const Events = Private(EventsProvider); | ||
|
||
_.class(State).inherits(Events); | ||
function State(urlParam, defaults, { hashingStore, notify } = {}) { | ||
function State(urlParam, defaults, { hashingStore, notifier } = {}) { | ||
State.Super.call(this); | ||
|
||
let self = this; | ||
self.setDefaults(defaults); | ||
self._urlParam = urlParam || '_s'; | ||
this._notify = notify || new Notifier(); | ||
self._hasher = hashingStore || new HashingStore({ | ||
this._notifier = notifier || new Notifier(); | ||
self._hashingStore = hashingStore || new HashingStore({ | ||
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. Same ideas here -- use a lower-cased version of the class name for the instance name. I think it helps make the relationship clearer. |
||
store: new LazyLruStore({ | ||
id: `${this._urlParam}:state`, | ||
store: window.sessionStorage, | ||
|
@@ -67,7 +67,7 @@ export default function StateProvider(Private, $rootScope, $location, config) { | |
return null; | ||
} | ||
|
||
if (this._hasher.isHash(urlVal)) { | ||
if (this._hashingStore.isHash(urlVal)) { | ||
return this._parseQueryParamValue(urlVal); | ||
} | ||
|
||
|
@@ -80,7 +80,7 @@ export default function StateProvider(Private, $rootScope, $location, config) { | |
} | ||
|
||
if (unableToParse) { | ||
this._notify.error('Unable to parse URL'); | ||
this._notifier.error('Unable to parse URL'); | ||
search[this._urlParam] = this.toQueryParam(this._defaults); | ||
$location.search(search).replace(); | ||
} | ||
|
@@ -194,13 +194,13 @@ export default function StateProvider(Private, $rootScope, $location, config) { | |
* @return {any} - the stored value, or null if hash does not resolve | ||
*/ | ||
State.prototype._parseQueryParamValue = function (queryParam) { | ||
if (!this._hasher.isHash(queryParam)) { | ||
if (!this._hashingStore.isHash(queryParam)) { | ||
return rison.decode(queryParam); | ||
} | ||
|
||
const stored = this._hasher.lookup(queryParam); | ||
const stored = this._hashingStore.getItemAtHash(queryParam); | ||
if (stored === null) { | ||
this._notify.error('Unable to completely restore the URL, be sure to use the share functionality.'); | ||
this._notifier.error('Unable to completely restore the URL, be sure to use the share functionality.'); | ||
} | ||
|
||
return stored; | ||
|
@@ -228,10 +228,11 @@ export default function StateProvider(Private, $rootScope, $location, config) { | |
} | ||
|
||
try { | ||
return this._hasher.add(state); | ||
const hash = this._hashingStore.hashAndSetItem(state); | ||
return hash; | ||
} catch (err) { | ||
this._notify.log('Unable to create hash of State due to error: ' + (state.stack || state.message)); | ||
this._notify.fatal( | ||
this._notifier.log('Unable to create hash of State due to error: ' + (state.stack || state.message)); | ||
this._notifier.fatal( | ||
new Error( | ||
'Kibana is unable to store history items in your session ' + | ||
'because it is full and there don\'t seem to be items any items safe ' + | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ const setup = (opts = {}) => { | |
return { lru, store }; | ||
}; | ||
|
||
describe('State Management LazyLruStore', () => { | ||
describe('LazyLruStore', () => { | ||
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 kept getting confused when reading the test error output in the terminal, because the |
||
describe('#getItem()', () => { | ||
it('returns null when item not found', () => { | ||
const { lru } = setup(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,13 @@ import { Sha256 } from 'ui/crypto'; | |
import StubBrowserStorage from 'test_utils/stub_browser_storage'; | ||
import { LazyLruStore } from './lazy_lru_store'; | ||
|
||
const TAG = 'h@'; | ||
|
||
/** | ||
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 would be OK keeping this here instead of as a static property on the class, but I think a name like |
||
* The HashingStore is a wrapper around a browser store object | ||
* that hashes the items added to it and stores them by their | ||
* hash. This hash is then returned so that the item can be received | ||
* at a later time. | ||
*/ | ||
export default class HashingStore { | ||
class HashingStore { | ||
constructor({ store, createHash, maxItems } = {}) { | ||
this._store = store || window.sessionStorage; | ||
if (createHash) this._createHash = createHash; | ||
|
@@ -22,11 +20,11 @@ export default class HashingStore { | |
/** | ||
* Determine if the passed value looks like a hash | ||
* | ||
* @param {string} hash | ||
* @param {string} str | ||
* @return {boolean} | ||
*/ | ||
isHash(hash) { | ||
return String(hash).slice(0, TAG.length) === TAG; | ||
isHash(str) { | ||
return String(str).indexOf(HashingStore.HASH_TAG) === 0; | ||
} | ||
|
||
/** | ||
|
@@ -35,7 +33,7 @@ export default class HashingStore { | |
* @param {string} hash | ||
* @return {any} | ||
*/ | ||
lookup(hash) { | ||
getItemAtHash(hash) { | ||
try { | ||
return JSON.parse(this._store.getItem(hash)); | ||
} catch (err) { | ||
|
@@ -50,23 +48,13 @@ export default class HashingStore { | |
* @param {any} the value to hash | ||
* @return {string} the hash of the value | ||
*/ | ||
add(object) { | ||
hashAndSetItem(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 the actions of |
||
const json = angular.toJson(object); | ||
const hash = this._getShortHash(json); | ||
this._store.setItem(hash, json); | ||
return hash; | ||
} | ||
|
||
/** | ||
* Remove a value identified by the hash from the store | ||
* | ||
* @param {string} hash | ||
* @return {undefined} | ||
*/ | ||
remove(hash) { | ||
this._store.removeItem(hash); | ||
} | ||
|
||
// private api | ||
|
||
/** | ||
|
@@ -89,7 +77,7 @@ export default class HashingStore { | |
* @param {string} shortHash | ||
*/ | ||
_getShortHash(json) { | ||
const fullHash = `${TAG}${this._createHash(json)}`; | ||
const fullHash = `${HashingStore.HASH_TAG}${this._createHash(json)}`; | ||
|
||
let short; | ||
for (let i = 7; i < fullHash.length; i++) { | ||
|
@@ -101,3 +89,7 @@ export default class HashingStore { | |
return short; | ||
} | ||
} | ||
|
||
HashingStore.HASH_TAG = 'h@'; | ||
|
||
export default HashingStore; |
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.
I think this works better as a noun as a verb, since it's an object. Also, I think it's a more common convention to see a class instance assume the name of the class but with a lowercase first letter.
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.
I agree, only used notify to be consistent with the rest of the code base, but I'm fine changing it.