-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[state] store actual state value in session storage #8022
Changes from all commits
8f7af7d
55a923d
df8e5ac
ed12206
b51b5cb
df2c116
06ed933
6f9c708
445ceba
a26b850
bf35d8a
458630d
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Sha256 } from './sha256'; | ||
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 just remove this file. IMHO, it's an extra indirection, and really doesn't add any abstraction (e.g. if it were to have a default export, and then a bunch of named ones) 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 talked about this and agreed that this file is fine. |
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.
Is this a constant? IIRC there were ways to adjust this in some browsers. There also doesn't seem to be a way to use JS to get at it :(
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.
It's basically just the minimum standard size browsers use. Since this is a stub it's not really used outside of the tests, and really it's only used to enforce a limit when necessary.