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

Framework: Add sync-handler wrapper #2843

Merged
merged 10 commits into from
Feb 10, 2016
Merged

Framework: Add sync-handler wrapper #2843

merged 10 commits into from
Feb 10, 2016

Conversation

retrofox
Copy link
Contributor

This adds a wrapper to sync the data gotten from the REST-API GET responses.
The idea with this data storing is allow provide data when the user is offline.

This wrapper works like a middleware that intercepts every GET request from the user and does, in short, three actions:

  1. Search the data locally in response immediately if this exists.
  2. At the same time send the request to WordPress REST-API server.
  3. Update the WP response locally

This approach is not ready to production. This feature is allowed through of its sync-handler config key.

@retrofox retrofox added this to the Calypso Core: Offline 1 milestone Jan 27, 2016
@rralian rralian added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Jan 27, 2016
@retrofox
Copy link
Contributor Author

An screenshot of where and how we are storing the data.
image

@retrofox
Copy link
Contributor Author

chrome console setting localStorage.debug to calypso:sync-handler*

image

@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from 7443c06 to 6f2eb76 Compare January 27, 2016 19:22
const oauthToken = require( 'lib/oauth-token' ),
requestHandler = require( 'lib/wpcom-xhr-wrapper' );
const oauthToken = require( 'lib/oauth-token' );
const requestHandler = config.isEnabled( 'sync-handler' )
Copy link
Contributor

Choose a reason for hiding this comment

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

I would be for splitting it into if {} else
We have 4 different permutations here, so I would do

if( config.isEnabled( 'sync-handler' ) ) {
const req... = ...
} else {
const....
}

and move it to line 16, then we dont have to duplicate the code in line 19 & 25

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thing here is you also need to decide what request handler will be wrapped by sync-handler. It's a double nested condition. And the wpcom instantiation is different accordingly.

if ( config.isEnabled( 'oauth' ) ) {
  // ...
  if( config.isEnabled( 'sync-handler' ) ) {
    const req... = ...
  } else {
    const....
  }
} else {
    // ...
  if( config.isEnabled( 'sync-handler' ) ) {
    const req... = ...
  } else {
    const....
  }
}

I can add a const to get the config key to avoid call the config() function twice.

@retrofox retrofox force-pushed the add/sync-handler-wrapper branch 3 times, most recently from 65a97f9 to 57a7b96 Compare January 28, 2016 20:02
const oauthToken = require( 'lib/oauth-token' ),
requestHandler = require( 'lib/wpcom-xhr-wrapper' );
const oauthToken = require( 'lib/oauth-token' );
const requestHandler = addSyncHandlerWrapper
Copy link
Contributor

Choose a reason for hiding this comment

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

This ternary seems to be duplicated in line 20 and 26 :)
Move it to 17 maybe?

Copy link
Contributor

Choose a reason for hiding this comment

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

It isn't duplication: the requires are different.

Not sure if it's correct, though. The first block has lib/wpcom-xhr-wrapper both times. The second block has wpcom-proxy-request and then lib/wpcom-xhr-wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup. you're right.

@seear
Copy link
Contributor

seear commented Jan 29, 2016

I've given this a quick test with /menus. Client menu data is updated with responses from POST requests, so everything works fine unless the page is refreshed, when an initial population is performed from stale GET data, meaning any previous modifications are not visible.

Maybe we should add a blacklist facility to allow us to indicate that certain API endpoints should never be cached?

@retrofox
Copy link
Contributor Author

Maybe we should add a blacklist facility to allow us to indicate that certain API endpoints should never be cached?

Yes, in fact we did it in our first approach. a143572

And on the other hand we're trying to improve the performance when the local data must be updated and at the same time find a way to notify to client( calypso) of these changes.

@rralian
Copy link
Contributor

rralian commented Feb 1, 2016

Client menu data is updated with responses from POST requests, so everything works fine unless the page is refreshed, when an initial population is performed from stale GET data, meaning any previous modifications are not visible.

@retrofox I think the double-callback branch should be merged into this PR. The stale data issue is likely enough of a blocker that this PR should not be merged without it, even in development. The odd behavior with stale data is otherwise likely to confuse developers as they are working on their features. We should also add a blacklist mechanism if/when we find an endpoint for which the double-callback approach runs into problems.

@retrofox
Copy link
Contributor Author

retrofox commented Feb 1, 2016

ok @rralian

@retrofox retrofox added [Status] Needs Author Reply and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Feb 1, 2016
@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from 1337f3f to 8d2b642 Compare February 1, 2016 17:17
@retrofox
Copy link
Contributor Author

retrofox commented Feb 1, 2016

image

This is an example of a malfunction using the double callback. Repro: go to http://calypso.localhost:3000/menus/your-testing-site

@gwwar
Copy link
Contributor

gwwar commented Feb 2, 2016

@retrofox you may want to rebase with master to work around build breakages.

@retrofox
Copy link
Contributor Author

retrofox commented Feb 2, 2016

thanks @gwwar

@retrofox
Copy link
Contributor Author

retrofox commented Feb 9, 2016

I can look into that. I'll see if it makes sense to do so in this PR or against master.

I really would like try to do it on this PR.

@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from bee8fd9 to 33069b8 Compare February 9, 2016 23:10
@gwwar
Copy link
Contributor

gwwar commented Feb 9, 2016

It occurs to me that we may need schema validation for this cached GET request. While we require a version bump for an endpoint whenever attributes change or are removed, we allow attributes to be added to an endpoint for a given version.

Hmm, additive properties that we expect could be a mess. Since this is a generic low level cache, I'm not sure if we could target all the endpoints. Probably ~1d max age might make more sense.

@rralian
Copy link
Contributor

rralian commented Feb 10, 2016

👍 Looking good, but here's one case:

Ha! After a fair bit of debugging I figured out the issue was not quite what it seemed. The dirty cache to clear the post-list and the second callback where all working as expected. The issue is that receivePage always expected we would be appending new posts to the end of the list as we scroll down. So your post was getting added, just at the end of your post list, rather than the top. So we just needed to sort the list. Added a commit to do so. 😄

@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from 5e1869e to 4206d49 Compare February 10, 2016 19:47
@rralian
Copy link
Contributor

rralian commented Feb 10, 2016

@retrofox I would prefer that we not add the reader endpoint until/unless we can confirm it doesn't have the same corner case issues we found for the post-list. Let's just keep this one simple, we can add the reader endpoints separately.

@retrofox
Copy link
Contributor Author

@retrofox I would prefer that we not add the reader endpoint until/unless we can confirm it doesn't have the same corner case issues we found for the post-list. Let's just keep this one simple, we can add the reader endpoints separately.

👍

@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from 4206d49 to 7ad16b6 Compare February 10, 2016 20:34
@rralian
Copy link
Contributor

rralian commented Feb 10, 2016

This is working well for me, though it looks like this needs to be rebased again. There are still some issues we need to figure out, but I think we can merge this now, especially considering this is limited to development. 👍

retrofox and others added 10 commits February 10, 2016 18:26
previously we were skipping the second callback from the sync-handler because setDefaultMenu() inside menu-data.js did not gracefully handle a double-callback. I've updated it to more gracefully handle the double-callback, so the sync-handler works as expected for this case as well.
because the receivePage method gets run for both pagination queries as well as sync-queries, we can no longer assume that all new posts can go at the end of the list. So we need to sort posts after we receive them.
@retrofox retrofox force-pushed the add/sync-handler-wrapper branch from 7ad16b6 to 0aacbb8 Compare February 10, 2016 21:27
@retrofox
Copy link
Contributor Author

rebased. waiting for circle cli as well.

@retrofox retrofox added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Feb 10, 2016
retrofox added a commit that referenced this pull request Feb 10, 2016
@retrofox retrofox merged commit f635f42 into master Feb 10, 2016
@retrofox retrofox deleted the add/sync-handler-wrapper branch February 10, 2016 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants