Skip to content

Commit

Permalink
add skipNextRender() function
Browse files Browse the repository at this point in the history
  • Loading branch information
devtempmac committed Sep 5, 2023
1 parent d1f9a3f commit 1787818
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter {

history.go(amount);
},
skipNextRender() {
skipHandlingNextApplicationTriggeredNavigation = true;
},
getInitialRoute() {
if (__DEV__) {
assert("[RouterSessionHistory].getInitialRoute", [
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ export type RouterSession<TRouteDefCollection> = {
*/
reset(options: SessionOpts): void;

/**
* Skips next render (but allows history change)
* - push()/replace() after calling this function will skip render
* - going back/forward won't skip render
*/
skipNextRender(): void;

/**
* Blocks navigation and registers a listener that is called when
* navigation is blocked. Returns a function to unblock navigation.
Expand Down
31 changes: 31 additions & 0 deletions test/createRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ describe("createRouter", () => {
expect(route.href).toBe("/");
});

it("should work with skipRender() function ", () => {
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("/");
session.skipNextRender();
routes.bar().push();
expect(route.href).toBe("/");
routes.bar().push();
expect(route.href).toBe("/bar");
session.skipNextRender();
routes.bar2().push();
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

0 comments on commit 1787818

Please sign in to comment.