Skip to content

Commit

Permalink
add skipRender option
Browse files Browse the repository at this point in the history
Co-authored-by: dev-m1-macbook <devm1macbook@gmail.com>
  • Loading branch information
devtempmac committed Sep 4, 2023
1 parent d1f9a3f commit 905ffad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
13 changes: 10 additions & 3 deletions src/buildRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { UmbrellaRoute, RouterLocation, RouterContext } from "./types";
import {
UmbrellaRoute,
RouterLocation,
RouterContext,
NavigateOptions,
} from "./types";
import { preventDefaultLinkClickBehavior } from "./preventDefaultLinkClickBehavior";
import { stringUtils } from "./stringUtils";

Expand Down Expand Up @@ -43,8 +48,10 @@ export function buildRoute({
},
},
action: null,
push: () => navigate({ ...route, action: "push" }, true),
replace: () => navigate({ ...route, action: "replace" }, true),
push: (options?: NavigateOptions) =>
navigate({ ...route, action: "push" }, true, options),
replace: (options?: NavigateOptions) =>
navigate({ ...route, action: "replace" }, true, options),
};

return route;
Expand Down
20 changes: 14 additions & 6 deletions src/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RouterContext,
UmbrellaBlocker,
RouterOpts,
NavigateOptions,
} from "./types";
import { createRouteBuilder } from "./createRouteBuilder";
import {
Expand Down Expand Up @@ -90,7 +91,7 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {
const router: UmbrellaCoreRouter = {
routes,
session: {
push(href, state) {
push(href, state, options: NavigateOptions) {
if (__DEV__) {
assert("[RouterSessionHistory].push", [
assert.numArgs([].slice.call(arguments), 1, 2),
Expand All @@ -105,9 +106,9 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {
getRouterContext()
);

return navigate({ ...route, action: "push" }, primaryPath);
return navigate({ ...route, action: "push" }, primaryPath, options);
},
replace(href, state) {
replace(href, state, options: NavigateOptions) {
if (__DEV__) {
assert("[RouterSessionHistory].replace", [
assert.numArgs([].slice.call(arguments), 1, 2),
Expand All @@ -122,7 +123,7 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {
getRouterContext()
);

return navigate({ ...route, action: "replace" }, primaryPath);
return navigate({ ...route, action: "replace" }, primaryPath, options);
},
back(amount = 1) {
if (__DEV__) {
Expand Down Expand Up @@ -243,13 +244,18 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {
}
}

function navigate(route: UmbrellaRoute, primaryPath: boolean) {
function navigate(
route: UmbrellaRoute,
primaryPath: boolean,
options?: NavigateOptions
) {
debugger;
if (blockerCollection.length > 0) {
blockerCollection.forEach((blocker) => {
blocker({
route,
retry: () => {
route[route.action === "push" ? "push" : "replace"]();
route[route.action === "push" ? "push" : "replace"](options);
},
});
});
Expand All @@ -269,6 +275,8 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {

if (skipHandlingNextApplicationTriggeredNavigation) {
skipHandlingNextApplicationTriggeredNavigation = false;
} else if (options?.skipRender) {
// do nothing
} else {
handleNavigation(route, primaryPath);
}
Expand Down
15 changes: 10 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { noMatch } from "./noMatch";
import { History } from "history";

export type NavigateOptions = {
skipRender?: boolean;
};

export type Compute<A extends any> = A extends Function
? A
: {
Expand Down Expand Up @@ -236,7 +240,8 @@ export type UmbrellaRouteDef = RouteDef<UmbrellaParamDefCollection>;

export type NavigateFunction = (
route: UmbrellaRoute,
primaryPath: boolean
primaryPath: boolean,
options?: NavigateOptions
) => void;

export type OnClickHandler = (event?: any) => void;
Expand Down Expand Up @@ -353,12 +358,12 @@ export type Route<TName, TParamDefCollection> = {
* If there were any entries in the stack after the current one, they are
* lost.
*/
push: () => void;
push: (options?: NavigateOptions) => void;

/**
* Replaces the current route in the history stack with this one.
*/
replace: () => void;
replace: (options?: NavigateOptions) => void;
};

/**
Expand Down Expand Up @@ -404,12 +409,12 @@ export type RouterSession<TRouteDefCollection> = {
/**
* Manually add a new item to the history stack.
*/
push(href: string, state?: any): void;
push(href: string, state?: any, options?: NavigateOptions): void;

/**
* Replace the currently active item in the history stack.
*/
replace(href: string, state?: any): void;
replace(href: string, state?: any, options?: NavigateOptions): void;

/**
* Get the initial route. Useful for bootstrapping your application.
Expand Down
29 changes: 29 additions & 0 deletions test/createRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ describe("createRouter", () => {
expect(route.href).toBe("/");
});

it("should respect 'skipRender' option", () => {
const config: RouterOpts = {
session: {
type: "memory",
},
};

const { routes, session } = createRouter(config, {
foo: defineRoute("/foo"),
bar: defineRoute("/bar"),
bar2: defineRoute("/bar2"),
});

let route = session.getInitialRoute();

session.listen((nextRoute) => (route = nextRoute));

expect(route.href).toBe("/");
routes.bar().push({ skipRender: true });
expect(route.href).toBe("/");
routes.bar().push();
expect(route.href).toBe("/bar");
routes.bar2().push({ skipRender: true });
routes.foo().push();
session.back();
// going back should ignore skipRender
expect(route.href).toBe("/bar2");
});

it("link should work", () => {
const config: RouterOpts = {
session: {
Expand Down
10 changes: 5 additions & 5 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRouter, defineRoute, param } from "../src/core";
import { Any } from "ts-toolbelt";
import { Link, Action, ParamValue } from "../src/types";
import { Link, Action, ParamValue, NavigateOptions } from "../src/types";

function expectTypes<A, B>(_: Any.Equals<Any.Compute<A>, Any.Compute<B>>) {}

Expand All @@ -27,8 +27,8 @@ describe("types", () => {
params: {};
link: Link;
href: string;
push: () => void;
replace: () => void;
push: (options?: NavigateOptions) => void;
replace: (options?: NavigateOptions) => void;
action: Action | null;
}
>(toBeEqual);
Expand Down Expand Up @@ -66,8 +66,8 @@ describe("types", () => {
params: {};
link: Link;
href: string;
push: () => void;
replace: () => void;
push: (options?: NavigateOptions) => void;
replace: (options?: NavigateOptions) => void;
action: Action | null;
}
>(toBeEqual);
Expand Down

0 comments on commit 905ffad

Please sign in to comment.