-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: wrong previousRoute after back journey #57
fix: wrong previousRoute after back journey #57
Conversation
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.
Nice and quick job @bchelkowski, well done 👏 thank you 🙇
Left some suggestions.
src/components/router/Router.brs
Outdated
|
||
if (isBackJourney OR m.top.activatedRoute.shouldSkip) | ||
previousRoute = CreateObject("roSGNode", "ActivatedRoute") | ||
previousRouteData = m._history[m._history.count() - 1] |
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.
previousRouteData = m._history[m._history.count() - 1] | |
previousRouteData = m._history.peek() |
src/components/router/Router.brs
Outdated
sub _deleteKeys(assocArray as Object, keys as Object) | ||
for each key in keys | ||
assocArray.delete(key) | ||
end for | ||
end sub |
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.
that looks like a utility function that could be added to ObjectUtils
, something like omit from lodash
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 agree, but such a function does not exist yet, so I left a TODO comment.
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 have ObjectUtils().pick()
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 don't want to use the pick, as it will require you to modify it each time you would like to add something.
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 the opposite of ObjectUtils().pick()
here 😅
src/components/router/Router.brs
Outdated
historyItem = route.getFields() | ||
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"]) | ||
|
||
return historyItem |
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.
having the omit
function added to the ObjectUtils
we could simply do here as per below
historyItem = route.getFields() | |
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"]) | |
return historyItem | |
return ObjectUtils().omit(route.getFields(), ["change", "focusable", "focusedChild", "id"]) |
src/components/router/Router.brs
Outdated
rootRoute = CreateObject("roSGNode", "ActivatedRoute") | ||
rootRoute.setFields({ path: rootPath }) | ||
|
||
m._history.push(rootRoute.getFields()) |
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.
_createHistoryItem
seems to be missing
m._history.push(rootRoute.getFields()) | |
m._history.push(_createHistoryItem(rootRoute.getFields())) |
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.
Not missing, but yes, it would be better to use _createHistoryItem.
m._history.push(rootRoute.getFields()) | |
m._history.push(_createHistoryItem (rootRoute)) |
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.
by missing I meant to keep the consistent interface of the objects stored in the history 😊
src/components/router/Router.brs
Outdated
rootRoute = CreateObject("roSGNode", "ActivatedRoute") | ||
rootRoute.setFields({ path: rootPath }) |
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 could add helper function like _createActivatedRoute(data)
and use here and in _getPreviousRoute
src/components/router/Router.brs
Outdated
function _getPreviousRoute(data as Object) as Object | ||
isBackJourney = getProperty(data, "isBackJourney", false) | ||
|
||
if (isBackJourney OR m.top.activatedRoute.shouldSkip) |
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 wonder if we really need to apply this logic when m.top.activatedRoute.shouldSkip
is true. This is a different flag than skipInHistory
and it is not set on back journey, but could be set by the middleware. Meaning that it is possible to skip routes only in the non-back journey (skipped routes are not in the history), and in that case previousRoute
should stay as is.
E.g. we navigate from route A to route B, middleware updates route B to be skipped and redirects to route C, then we should not set route B as the previousRoute
and route A should be still there. If we apply the below logic, then we would update the previousRoute
with the route A, but it seems to be redundant as it is already there.
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 is basically what I mean we could do:
if (isBackJourney OR m.top.activatedRoute.shouldSkip) | |
if (m.top.activatedRoute.shouldSkip) | |
return m.top.previousRoute | |
end if | |
if (isBackJourney) |
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.
now both solutions point to the same object, so I would just skip that additional check
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.
👌
src/components/router/Router.brs
Outdated
|
||
m.top.activatedRoute.path = data.path | ||
m.top.activatedRoute.params = ternary(data.params <> Invalid, data.params, {}) | ||
' Needs to be set before activatedRoute as setPreviousRoute uses the previous value of activatedRoute. |
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.
' Needs to be set before activatedRoute as setPreviousRoute uses the previous value of activatedRoute. | |
' Needs to be set before activatedRoute as getPreviousRoute uses the previous value of activatedRoute. |
BTW this comment makes sense only if you put an empty line after setting m.top.previousRoute
src/components/router/Router.brs
Outdated
sub _updateHistory() | ||
if (m.top.url = "" OR m.top.activatedRoute.shouldSkip) | ||
return | ||
function _getPreviousRoute(data as Object) as Object |
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 lacks context on what kind of data it is. You could pass here only isBackJourney
, but it's still a bit strange why anything about the back journey should have an impact on the previous route. That's why I think the if (isBackJourney OR m.top.activatedRoute.shouldSkip)
check should be part of the navigate
method and this one should only return indeed the previous route
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.
Changed to navigateData
.
I don't agree, the function _getPreviousRoute
is responsible for returning the previous route, so it shouldn't be shocking that it has all checks for that purpose.
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 could argue that during navigation the current route is not the previous route until we switch it to the new one 😅
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.
The output of the _getPreviousRoute
is set to the m.top.previousRoute
always.
So in my opinion whole logic should be there, even if previousRoute would be the current activatedRoute.
src/components/router/Router.brs
Outdated
previousRoute = CreateObject("roSGNode", "ActivatedRoute") | ||
previousRouteData = m._history[m._history.count() - 1] | ||
|
||
if (previousRouteData = Invalid) then return Invalid | ||
|
||
previousRoute.setFields(previousRouteData) |
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.
previousRoute = CreateObject("roSGNode", "ActivatedRoute") | |
previousRouteData = m._history[m._history.count() - 1] | |
if (previousRouteData = Invalid) then return Invalid | |
previousRoute.setFields(previousRouteData) | |
previousRouteData = m._history[m._history.count() - 1] | |
if (previousRouteData = Invalid) then return Invalid | |
previousRoute = CreateObject("roSGNode", "ActivatedRoute") | |
previousRoute.setFields(previousRouteData) |
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.
changed that according to Radek's comment
src/components/router/Router.brs
Outdated
historyItem = route.getFields() | ||
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"]) | ||
|
||
return historyItem |
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 is an interface change - for some purpose fields like renderedUrl
or isBackJourney
aren't stored (especially the renderedUrl
makes no sense), so I think we shouldn't change 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 believe it is needed to recreate properly the ActivatedRoute instance from the history item
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.
How could we live without this then? 🤔 Anyway, makes sense 🙈
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 is an interface change, but non-breaking, as it added fields that didn't exist before and didn't change the existing ones.
And it would be better in my opinion to keep all data related to the route instead of selecting some of it, we should not decide what would be useful to the user.
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 were not recreating the ActivatedRoute from the history item before, it was introduced by Błazej within this PR 😄
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.
So maybe we could just switch to storing the ActivatedRoute node? 🤔
src/components/router/Router.brs
Outdated
function _getPreviousRoute(navigationData as Object) as Object | ||
isBackJourney = getProperty(navigationData, "isBackJourney", false) |
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.
how about passing boolean parameter instead, ie isBackJourney
? currently we use various naming for parameters across various functions, and it does not seem to be straightforward what are they. We have data
, navigationData
, and routeData
👀
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.
Wouldn't it be even clearer?
if isBackJourney
m.top.previousRoute = _getPreviousRoute()
else
m.top.previousRoute = NodeUtils().cloneNode(m.top.activatedRoute)
end if
Anyway, no hard feelings, up to you @bchelkowski
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.
and rename _getPreviousRoute
to _getPreviousRouteFromTheHistory
then
but we could still pass the boolean parameter to the _getPreviousRoute
and apply proper logic inside as it is right now
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.
sorry, but I don't understand why I should introduce yet another if statement in the navigate function if I could move it to the _getPreviousRoute
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.
my idea was to extract the isBackJourney
in the navigate
function and pass it as a param, so the if stays in the _getPreviousRoute
in navigate
:
m.top.previousRoute = _getPreviousRoute(getProperty(data, "isBackJourney", false))`
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.
So like it is now?
b7d068e
to
6b8e5d6
Compare
## [2.1.5](v2.1.4...v2.1.5) (2023-12-05) ### Bug Fixes * wrong previousRoute after back journey ([#57](#57)) ([cdd6652](cdd6652))
🎉 This PR is included in version 2.1.5 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What did you implement:
Closes #56
How did you implement it:
Save the latest item from kept history items instead of the current
activeRoute
.It will also do that if the
activeRoute
should be skipped in the history.How can we verify it:
The
previousRoute
should have the correct value always.Todos:
Is this ready for review?: YES
Is it a breaking change?: NO