Skip to content
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

Allow adding blockers that don't create a beforeUnload event #949

Open
wants to merge 3 commits into
base: dev
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import expect from "expect";

import { execSteps } from "./utils.js";

export default (history, done) => {
let steps = [
({ location }) => {
expect(location).toMatchObject({
pathname: "/",
});

let unblock = history.noUnloadBlock();

history.push("/home");

expect(history.location).toMatchObject({
pathname: "/",
});

unblock();
},
];

execSteps(steps, history, done);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import expect from "expect";

import { execSteps } from "./utils.js";

export default (history, done) => {
let steps = [
({ location }) => {
expect(location).toMatchObject({
pathname: "/",
});

let unblock = history.block();
let noUnloadUnblock = history.noUnloadBlock();

history.push("/home");

expect(history.location).toMatchObject({
pathname: "/",
});

unblock();
noUnloadUnblock();
},
];

execSteps(steps, history, done);
};
14 changes: 14 additions & 0 deletions packages/history/__tests__/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import EncodedReservedCharacters from "./TestSequences/EncodedReservedCharacters
import GoBack from "./TestSequences/GoBack.js";
import GoForward from "./TestSequences/GoForward.js";
import BlockEverything from "./TestSequences/BlockEverything.js";
import BlockEverythingNoUnload from "./TestSequences/BlockEverythingNoUnload";
import BlockPopWithoutListening from "./TestSequences/BlockPopWithoutListening.js";
import BlockSupersedesNoUnloadBlock from "./TestSequences/BlockSupersedesNoUnloadBlock";

describe("a browser history", () => {
let history;
Expand Down Expand Up @@ -135,9 +137,21 @@ describe("a browser history", () => {
});
});

describe("blockNoUnload", () => {
it("blocks all transitions with no beforeunload event", (done) => {
BlockEverythingNoUnload(history, done);
});
});

describe("block a POP without listening", () => {
it("receives the next ({ action, location })", (done) => {
BlockPopWithoutListening(history, done);
});
});

describe("blockSupersedesNoUnload", () => {
it("normal block supersedes no unload block", (done) => {
BlockSupersedesNoUnloadBlock(history, done);
});
});
});
44 changes: 42 additions & 2 deletions packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ export interface History {
* @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.block
*/
block(blocker: Blocker): () => void;

/**
* Prevents the current location from changing and sets up a listener that
* will be called instead, but without setting a beforeUnload listener.
*
* @param blocker - A function that will be called when a transition is blocked
* @returns unblock - A function that may be used to stop blocking
*
* @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#history.block
*/
noUnloadBlock(blocker: Blocker): () => void;
}

/**
Expand Down Expand Up @@ -386,12 +397,15 @@ export function createBrowserHistory(
function handlePop() {
if (blockedPopTx) {
blockers.call(blockedPopTx);
if (!blockers.length) {
noUnloadBlockers.call(blockedPopTx);
}
blockedPopTx = null;
} else {
let nextAction = Action.Pop;
let [nextIndex, nextLocation] = getIndexAndLocation();

if (blockers.length) {
if (blockers.length || noUnloadBlockers.length) {
if (nextIndex != null) {
let delta = index - nextIndex;
if (delta) {
Expand Down Expand Up @@ -433,6 +447,7 @@ export function createBrowserHistory(
let [index, location] = getIndexAndLocation();
let listeners = createEvents<Listener>();
let blockers = createEvents<Blocker>();
let noUnloadBlockers = createEvents<Blocker>();

if (index == null) {
index = 0;
Expand Down Expand Up @@ -470,8 +485,16 @@ export function createBrowserHistory(
}

function allowTx(action: Action, location: Location, retry: () => void) {
const allow =
!blockers.length || (blockers.call({ action, location, retry }), false);

if (!allow) {
noUnloadBlockers.clear();
}

return (
!blockers.length || (blockers.call({ action, location, retry }), false)
allow &&
(!noUnloadBlockers.length || (noUnloadBlockers.call({ action, location, retry }), false))
);
}

Expand Down Expand Up @@ -546,6 +569,13 @@ export function createBrowserHistory(
listen(listener) {
return listeners.push(listener);
},
noUnloadBlock(blocker) {
let unblock = noUnloadBlockers.push(blocker);

return function () {
unblock();
};
},
block(blocker) {
let unblock = blockers.push(blocker);

Expand Down Expand Up @@ -809,6 +839,9 @@ export function createHashHistory(
listen(listener) {
return listeners.push(listener);
},
noUnloadBlock() {
return function () {}
},
block(blocker) {
let unblock = blockers.push(blocker);

Expand Down Expand Up @@ -992,6 +1025,9 @@ export function createMemoryHistory(
listen(listener) {
return listeners.push(listener);
},
noUnloadBlock() {
return function () {};
},
block(blocker) {
return blockers.push(blocker);
},
Expand Down Expand Up @@ -1019,6 +1055,7 @@ type Events<F> = {
length: number;
push: (fn: F) => () => void;
call: (arg: any) => void;
clear: () => void;
};

function createEvents<F extends Function>(): Events<F> {
Expand All @@ -1037,6 +1074,9 @@ function createEvents<F extends Function>(): Events<F> {
call(arg) {
handlers.forEach((fn) => fn && fn(arg));
},
clear() {
handlers = [];
},
};
}

Expand Down