-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Storage API #1483
Storage API #1483
Conversation
const binding = overrideStorage ? | ||
new ViewerStorageBinding(window) : | ||
new LocalStorageBinding(window); | ||
return new Storage(window, viewer, binding).start_(); |
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.
Won't this return undefined?
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.
Fixed. Didn't actually test this code yet :)
9db7622
to
590d727
Compare
@ericfs requesting the formal review. |
} | ||
|
||
/** | ||
* Saves the specified property. Returns the promise that's resolved when |
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.
s/Saves/Removes/
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.
Done
7c5f5cc
to
0e5ed25
Compare
saveStore(origin, store) { | ||
return new Promise(resolve => { | ||
this.win.localStorage.setItem(this.getKey_(origin), store); | ||
resolve(); |
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.
Why not use Promise.resolve() here?
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.
This gives me automatic rejection if setItem
method fails without needing to use try/catch directly.
LGTM |
I did some additional research today and need a little time to process it. I will hold off merging this PR in the meantime. @ericfs please hold off viewer work on this for a little bit as well. |
Ok, let me know if you're updating the PR. |
if (this.store_) { | ||
return Promise.resolve(this.store_); | ||
} | ||
return this.whenStarted_.then(() => { |
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.
Why not save this as a storePromise
to avoid the extra allocations above?
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.
Good idea. Done.
Assigning to @cramforce for additional review. |
Promise.resolve() : | ||
Promise.reject(`Enable experiment ${EXPERIMENT}`); | ||
|
||
/** @private {!Promise<!JSONObject>} */ |
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.
Can you use !
with a null promise?
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.
Nope. Fixed. (Where are you closure compiler?)
@cramforce PTAL. The format of the storage changed considerably to allow limits. In general, though, it looks like a good idea. |
this.maxValues_ = opt_maxValues || MAX_VALUES_PER_ORIGIN; | ||
|
||
/** @private @const {!Array<!JSONObject>} */ | ||
this.values_ = obj['vv'] || []; |
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.
Why not use an Object<string, JSONObject>
? That'll avoid the O(n)
lookup times.
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.
Possible of course. The set is very small - forced to be under 8 elements. It simplifies search and removal of old values. And it also seemed a bit better as a format. But I can definitely switch to a map if you feel that format would improve.
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.
Oh, I see why you're doing it now (purging old values). Different comments incoming. 😉
Switched the new version to use hash format. |
@cramforce PTAL. |
* @override | ||
*/ | ||
set(name, value) { | ||
assert(typeof value == 'boolean', 'Only boolean values accepted'); |
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 need to make sure to not provide a "read all keys" API. That could be used to put values into keys :)
LGTM |
@ericfs this is merged. I don't think viewer bindings have really changed at all. |
No description provided.