8000 add skipRender option by devtempmac · Pull Request #131 · zilch/type-route · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add skipRender option #131

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

Open
wants to merge 1 8000 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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