-
Notifications
You must be signed in to change notification settings - Fork 879
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
New Tab Page: fetch data from JS -> C++ instead of WebUIProperties #2957
Conversation
This is feature complete so ready to look at, but just working through some unit tests that need changing up a bit. |
577e701
to
bc522a8
Compare
@imptrx @cezaraugusto this is ready for review - rebased on the recent NTP changes, and modified tests in light of code refactor so that they pass locally. |
bc522a8
to
72684d0
Compare
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.
left a few comments
state.backgroundImage = backgroundAPI.randomBackgroundImage() | ||
} | ||
console.timeStamp('reducer initial data received') | ||
window.setTimeout(function () { |
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.
what is the reasoning behind this?
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.
I moved it from the removed case types.NEW_TAB_TOP_SITES_DATA_UPDATED:
, which is no longer required as we dispatch a single action for all initial data. I noticed in that removed case, the re-dispatch was not behind a setImmediate
, meaning the action could be dispatched synchronously before this action has reduced to state and rendered.
I forgot that webpack does inject a setImmediate
polyfill for web, if you feel we should use that.
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.
no need to polyfill but seems valid to add a comment referencing why it is needed. for future reference
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.
I ended up changing all the occurances of this anti-pattern of dispatching an action inside a reducer before the reducer has finished. It can introduce weird bugs where state changes go missing. See discussion at https://stackoverflow.com/questions/36730793/can-i-dispatch-an-action-in-reducer for more information.
I wrapped them in a setImmediate
and left a TODO comment to refactor.
state.backgroundImage = backgroundAPI.randomBackgroundImage() | ||
state = getLoadTimeData(state) | ||
return state | ||
const getPersistantData = (state: NewTab.State): NewTab.PersistantState => { |
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/persistant/persistent?
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 no. I always get that wrong. Now I need to change it everywhere 🤦♀
case types.NEW_TAB_PRIVATE_TAB_DATA_UPDATED: | ||
state = { | ||
...state, | ||
useAlternativePrivateSearchEngine: payload.useAlternativePrivateSearchEngine |
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.
nit: you created a const
for all other payloads. maybe create one for this one for consistency?
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 👍
state = { | ||
...state, | ||
...preferences | ||
} | ||
if (positivelyChangedShowBackgroundImage) { |
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 variable name is hard to understand. shouldChangeBackgroundImage
maybe?
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 variable means "Did the value change from false to true". Your suggestion works 👍
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.
Looks great - just left a few questions 😃
Verified affected areas are working as intended on NTP/NTP Private/Welcome Imports, and original issue is addressed 👍
Noticed we have a lot of console.timeStamps
in the code - was wondering if those are just for debugging purposes since it was labeled as a non-standard
isIncognito: boolean | ||
useAlternativePrivateSearchEngine: boolean | ||
isTor: boolean | ||
isQwant: boolean | ||
bookmarks: Record<string, Bookmark> | ||
stats: Stats | ||
backgroundImage?: Image |
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.
out of context but just realized we didn't propagate Image | undefined
down to the actual components using this data - can be done in another PR but quick change if you want to take 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.
@imptrx Can you give me an example? All the places I can see do do a type check first. Typescript should produce a compiler error if they didn't.
d5a2a73
to
53b3a1f
Compare
All feedback has been addressed @cezaraugusto @imptrx - all changes can be viewed at https://github.com/brave/brave-core/compare/d5a2a7394f3b06860763a3a86bf726923f7220b9..53b3a1f05e56f5b0acb2d327d842d81a61af828c |
Fix brave/brave-browser#5249 The previous method of wvh->SetWebUIProperty has issues in that the rvh does not always fire the 'ready' event, especially when navigating 'back'. Using DataSource is a more consistent chromium method for sending properties to the JS context. However, this is usually used for non-changing data. Whilst an UpdateDataSource function does exist, that kind of data is usually pulled from the JS via an explicit call to the C++ WebUI, so we may have to go down that route. The issue with the implementation in this commit is that a page may miss the 'updated' events that fire, for example due to have being navigated away and then back to the page. - Creates JS -> C++ functions to get data for: Preferences, Stats and PrivateProperties (alternativeSearchEngine). loadTimeData is also not a great place for data which can change. Although a DataSource (i.e. strings.js and loadTimeData.data) can be updated, it is complicated and is not normal chromium pattern. Since this data is not required exactly at page creation (since it is not read via static HTML but via React in JS), then we can afford the time it takes to request this data via JS. - Converting to a JS API required refactoring some of the app store / reducer initialization, including creating the concept of 'initial data' which is any data required to render the page. Since we need Preferences to know what to show, and we need Stats since they are shown on the initial page load, then those are contained within. Includes some minor cleanup with regard to 'API' separation and definition. - Fixes storage being loaded multiple times during redux init (the function is called with an empty state param 3 times, so the state was being loaded from storage 3 times). - Only saves to local storage the state which we will be re-used.
Avoids some flashes and jagged loads, bringing in some features implemented in muon: - Dark background only when dark theme is active. - Fade-in whole page when initial data is fetched. - Fade-in image when load is complete, instead of having the image tear down the screen.
promise wasn't being caught, but looks like we don't care
…ucer is finished This is an anti-pattern and could introduce bugs. See discussion at https://stackoverflow.com/questions/36730793/can-i-dispatch-an-action-in-reducer
53b3a1f
to
e3b2ea7
Compare
Merging as CI result was that all the builds passed and just received a deps audit warning, which is unrelated to this PR. |
Adding this and brave/brave-browser#5249 into the |
…2957) New Tab Page: fetch data from JS -> C++ instead of WebUIProperties
Summary
Fix brave/brave-browser#5249 (data is more reliable)
Fix brave/brave-browser#3575 (as much as possible, by removing the flash from light -> dark, it now just stays light until the image / gradient comes int)
...and brings in the detection of the poster image loading (and then fading it in) that we had in muon last year!
Updates for New Tab Page
New Tab Page: Serve data via JS -> C++ get APIs
Includes some minor cleanup with regard to 'API' separation and definition.
New Tab Page WebUI: Graceful page loading
Avoids some flashes and jagged loads, bringing in some features implemented in muon:
Submitter Checklist:
npm run lint
)git rebase master
(if needed).git rebase -i
to squash commits (if needed).Test Plan:
Reviewer Checklist:
After-merge Checklist:
changes has landed on.