Skip to content
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

Merged
merged 1 commit into from
Jan 28, 2016
Merged

Storage API #1483

merged 1 commit into from
Jan 28, 2016

Conversation

dvoytenko
Copy link
Contributor

No description provided.

const binding = overrideStorage ?
new ViewerStorageBinding(window) :
new LocalStorageBinding(window);
return new Storage(window, viewer, binding).start_();
Copy link

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?

Copy link
Contributor Author

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 :)

@dvoytenko dvoytenko force-pushed the storage1 branch 2 times, most recently from 9db7622 to 590d727 Compare January 21, 2016 23:03
@dvoytenko dvoytenko changed the title [POC] Storage API Storage API Jan 21, 2016
@dvoytenko
Copy link
Contributor Author

@ericfs requesting the formal review.

}

/**
* Saves the specified property. Returns the promise that's resolved when
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Saves/Removes/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@dvoytenko dvoytenko force-pushed the storage1 branch 2 times, most recently from 7c5f5cc to 0e5ed25 Compare January 21, 2016 23:47
saveStore(origin, store) {
return new Promise(resolve => {
this.win.localStorage.setItem(this.getKey_(origin), store);
resolve();
Copy link

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?

Copy link
Contributor Author

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.

@ericfs
Copy link

ericfs commented Jan 22, 2016

LGTM

@dvoytenko
Copy link
Contributor Author

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.

@ericfs
Copy link

ericfs commented Jan 22, 2016

Ok, let me know if you're updating the PR.

if (this.store_) {
return Promise.resolve(this.store_);
}
return this.whenStarted_.then(() => {
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

@dvoytenko dvoytenko assigned cramforce and unassigned ericfs Jan 22, 2016
@dvoytenko
Copy link
Contributor Author

Assigning to @cramforce for additional review.

Promise.resolve() :
Promise.reject(`Enable experiment ${EXPERIMENT}`);

/** @private {!Promise<!JSONObject>} */
Copy link
Contributor

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?

Copy link
Contributor Author

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?)

@dvoytenko
Copy link
Contributor Author

@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'] || [];
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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. 😉

@dvoytenko
Copy link
Contributor Author

Switched the new version to use hash format.

@dvoytenko
Copy link
Contributor Author

@cramforce PTAL.

* @override
*/
set(name, value) {
assert(typeof value == 'boolean', 'Only boolean values accepted');
Copy link
Member

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 :)

@cramforce
Copy link
Member

LGTM

dvoytenko added a commit that referenced this pull request Jan 28, 2016
@dvoytenko dvoytenko merged commit 208e2eb into ampproject:master Jan 28, 2016
@dvoytenko dvoytenko deleted the storage1 branch January 28, 2016 06:50
@dvoytenko
Copy link
Contributor Author

@ericfs this is merged. I don't think viewer bindings have really changed at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants