-
Notifications
You must be signed in to change notification settings - Fork 25
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
#7670: Simplify injection of widgets onto the page (renderWidget
)
#8150
Changes from all commits
c864a2b
6f74672
28519c3
c60e2c6
9fcb54d
e24b61a
10128a0
7576889
062ce3c
bd77692
ee5cf32
c82c591
b74d7f9
a6de965
88e004b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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(); | ||
}); |
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; |
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); | ||
}); |
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, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, we can use |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this file:
attachShadow()
call withrenderWidget()
's ownuseScrollLock()