-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
323 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import AbortSignalGate from "./AbortSignalGate"; | ||
|
||
it("renders the children when active and hides them when aborted", () => { | ||
const controller = new AbortController(); | ||
const { rerender } = render( | ||
<AbortSignalGate signal={controller.signal}> | ||
<div>Content</div> | ||
</AbortSignalGate>, | ||
); | ||
expect(screen.getByText("Content")).toBeInTheDocument(); | ||
|
||
controller.abort(); | ||
rerender( | ||
<AbortSignalGate signal={controller.signal}> | ||
<div>Content</div> | ||
</AbortSignalGate>, | ||
); | ||
expect(screen.queryByText("Content")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("does not render children when the signal is already aborted", () => { | ||
render( | ||
<AbortSignalGate signal={AbortSignal.abort()}> | ||
<div>Content</div> | ||
</AbortSignalGate>, | ||
); | ||
expect(screen.queryByText("Content")).not.toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import useAbortSignal from "@/hooks/useAbortSignal"; | ||
import React from "react"; | ||
|
||
/** | ||
* Render children until the signal is aborted | ||
*/ | ||
const AbortSignalGate: React.FunctionComponent<{ signal: AbortSignal }> = ({ | ||
signal, | ||
children, | ||
}) => { | ||
const aborted = useAbortSignal(signal); | ||
return aborted ? null : <>{children}</>; | ||
}; | ||
|
||
export default AbortSignalGate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { renderHook } from "@testing-library/react-hooks"; | ||
import useAbortSignal from "./useAbortSignal"; | ||
|
||
it("returns the initial state of the signal", () => { | ||
const active = renderHook(() => useAbortSignal(new AbortController().signal)); | ||
expect(active.result.current).toBe(false); | ||
|
||
const aborted = renderHook(() => useAbortSignal(AbortSignal.abort())); | ||
expect(aborted.result.current).toBe(true); | ||
}); | ||
|
||
it("updates the state when the signal is aborted", () => { | ||
const controller = new AbortController(); | ||
const { result } = renderHook(() => useAbortSignal(controller.signal)); | ||
expect(result.current).toBe(false); | ||
|
||
controller.abort(); | ||
expect(result.current).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { useSyncExternalStore } from "use-sync-external-store/shim"; | ||
|
||
export default function useAbortSignal(signal: AbortSignal): boolean { | ||
return useSyncExternalStore( | ||
(callback: () => void) => { | ||
const unsubscribe = new AbortController(); | ||
signal.addEventListener("abort", callback, { | ||
signal: unsubscribe.signal, | ||
once: true, | ||
}); | ||
return () => { | ||
unsubscribe.abort(); | ||
}; | ||
}, | ||
() => signal.aborted, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.