Skip to content

Commit

Permalink
Custom handling of a leave hook in case of path is
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Jan 17, 2021
1 parent ac68bc6 commit ba19b45
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.6.5

Custom handling of a leave hook in case of path is `*`.

## 8.6.4

Refactoring the leave hook implementation. The leave hook check now run a bit earlier and once per navigating. So far it was running for each of the matched routes. Also the hook now may accept an array of matches. Not only a single Match object.
Expand Down
16 changes: 12 additions & 4 deletions lib/es/middlewares/checkForLeaveHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ export default function checkForLeaveHook(context, done) {
return;
}

var someOfTheLastOnesMatch = context.matches ? context.matches.find(function (match) {
return oldMatch.route.path === match.route.path;
}) : false;
var runHook = false;
var newLocationVSOldMatch = context.instance.matchLocation(oldMatch.route.path, context.currentLocationPath);

if (undefinedOrTrue(context.navigateOptions, "callHooks") && !someOfTheLastOnesMatch) {
if (oldMatch.route.path !== "*") {
runHook = !newLocationVSOldMatch;
} else {
var someOfTheLastOnesMatch = context.matches ? context.matches.find(function (match) {
return oldMatch.route.path === match.route.path;
}) : false;
runHook = !someOfTheLastOnesMatch;
}

if (undefinedOrTrue(context.navigateOptions, "callHooks") && runHook) {
Q(oldMatch.route.hooks.leave.map(function (f) {
// just so we match the Q interface
return function (_, d) {
Expand Down
16 changes: 12 additions & 4 deletions lib/navigo.amd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.amd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js.map

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions lib/navigo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navigo",
"version": "8.6.4",
"version": "8.6.5",
"description": "A simple vanilla JavaScript router",
"main": "lib/navigo.js",
"browser": "lib/navigo.min.js",
Expand Down
25 changes: 16 additions & 9 deletions src/middlewares/checkForLeaveHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ export default function checkForLeaveHook(context: QContext, done) {
leaveLoopDone();
return;
}
const someOfTheLastOnesMatch = context.matches
? context.matches.find((match) => {
return oldMatch.route.path === match.route.path;
})
: false;
if (
undefinedOrTrue(context.navigateOptions, "callHooks") &&
!someOfTheLastOnesMatch
) {
let runHook = false;
const newLocationVSOldMatch = context.instance.matchLocation(
oldMatch.route.path as string,
context.currentLocationPath
);
if (oldMatch.route.path !== "*") {
runHook = !newLocationVSOldMatch;
} else {
const someOfTheLastOnesMatch = context.matches
? context.matches.find((match) => {
return oldMatch.route.path === match.route.path;
})
: false;
runHook = !someOfTheLastOnesMatch;
}
if (undefinedOrTrue(context.navigateOptions, "callHooks") && runHook) {
Q(
oldMatch.route.hooks.leave
.map((f) => {
Expand Down

0 comments on commit ba19b45

Please sign in to comment.