-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[state] store url states into session storage
- Loading branch information
spalger
committed
Oct 11, 2016
1 parent
bdd39ea
commit 7fbd389
Showing
24 changed files
with
1,414 additions
and
227 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
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,92 +1,109 @@ | ||
const keys = Symbol('keys'); | ||
const values = Symbol('values'); | ||
const remainingSize = Symbol('remainingSize'); | ||
|
||
export default class StubBrowserStorage { | ||
constructor() { | ||
this[keys] = []; | ||
this[values] = []; | ||
this[remainingSize] = 5000000; // 5mb, minimum browser storage size | ||
this._keys = []; | ||
this._values = []; | ||
this._size = 0; | ||
this._sizeLimit = 5000000; // 5mb, minimum browser storage size | ||
} | ||
|
||
// ----------------------------------------------------------------------------------------------- | ||
// Browser-specific methods. | ||
// ----------------------------------------------------------------------------------------------- | ||
|
||
get length() { | ||
return this[keys].length; | ||
return this._keys.length; | ||
} | ||
|
||
key(i) { | ||
return this[keys][i]; | ||
return this._keys[i]; | ||
} | ||
|
||
getItem(key) { | ||
key = String(key); | ||
|
||
const i = this[keys].indexOf(key); | ||
const i = this._keys.indexOf(key); | ||
if (i === -1) return null; | ||
return this[values][i]; | ||
return this._values[i]; | ||
} | ||
|
||
setItem(key, value) { | ||
key = String(key); | ||
value = String(value); | ||
this._takeUpSpace(this._calcSizeOfAdd(key, value)); | ||
const sizeOfAddition = this._getSizeOfAddition(key, value); | ||
this._updateSize(sizeOfAddition); | ||
|
||
const i = this[keys].indexOf(key); | ||
const i = this._keys.indexOf(key); | ||
if (i === -1) { | ||
this[keys].push(key); | ||
this[values].push(value); | ||
this._keys.push(key); | ||
this._values.push(value); | ||
} else { | ||
this[values][i] = value; | ||
this._values[i] = value; | ||
} | ||
} | ||
|
||
removeItem(key) { | ||
key = String(key); | ||
this._takeUpSpace(this._calcSizeOfRemove(key)); | ||
const sizeOfRemoval = this._getSizeOfRemoval(key); | ||
this._updateSize(sizeOfRemoval); | ||
|
||
const i = this[keys].indexOf(key); | ||
const i = this._keys.indexOf(key); | ||
if (i === -1) return; | ||
this[keys].splice(i, 1); | ||
this[values].splice(i, 1); | ||
this._keys.splice(i, 1); | ||
this._values.splice(i, 1); | ||
} | ||
|
||
// non-standard api methods | ||
_getKeys() { | ||
return this[keys].slice(); | ||
// ----------------------------------------------------------------------------------------------- | ||
// Test-specific methods. | ||
// ----------------------------------------------------------------------------------------------- | ||
|
||
getStubbedKeys() { | ||
return this._keys.slice(); | ||
} | ||
|
||
_getValues() { | ||
return this[values].slice(); | ||
getStubbedValues() { | ||
return this._values.slice(); | ||
} | ||
|
||
_setSizeLimit(limit) { | ||
if (this[keys].length) { | ||
throw new Error('You must call _setSizeLimit() before setting any values'); | ||
setStubbedSizeLimit(sizeLimit) { | ||
// We can't reconcile a size limit with the "stored" items, if the stored items size exceeds it. | ||
if (sizeLimit < this._size) { | ||
throw new Error(`You can't set a size limit smaller than the current size.`); | ||
} | ||
|
||
this[remainingSize] = limit; | ||
this._sizeLimit = sizeLimit; | ||
} | ||
|
||
getStubbedSizeLimit() { | ||
return this._sizeLimit; | ||
} | ||
|
||
getStubbedSize() { | ||
return this._size; | ||
} | ||
|
||
_calcSizeOfAdd(key, value) { | ||
const i = this[keys].indexOf(key); | ||
_getSizeOfAddition(key, value) { | ||
const i = this._keys.indexOf(key); | ||
if (i === -1) { | ||
return key.length + value.length; | ||
} | ||
return value.length - this[values][i].length; | ||
// Return difference of what's been stored, and what *will* be stored. | ||
return value.length - this._values[i].length; | ||
} | ||
|
||
_calcSizeOfRemove(key) { | ||
const i = this[keys].indexOf(key); | ||
_getSizeOfRemoval(key) { | ||
const i = this._keys.indexOf(key); | ||
if (i === -1) { | ||
return 0; | ||
} | ||
return 0 - (key.length + this[values][i].length); | ||
// Return negative value. | ||
return -(key.length + this._values[i].length); | ||
} | ||
|
||
_takeUpSpace(delta) { | ||
if (this[remainingSize] - delta < 0) { | ||
_updateSize(delta) { | ||
if (this._size + delta > this._sizeLimit) { | ||
throw new Error('something about quota exceeded, browsers are not consistent here'); | ||
} | ||
|
||
this[remainingSize] -= delta; | ||
this._size += delta; | ||
} | ||
} |
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
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 @@ | ||
export { Sha256 } from './sha256'; |
Oops, something went wrong.