Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@closeio/use-abortable-effect
Advanced tools
Readme
Super simple React hook for running abortable effects based on the AbortController
API.
Interested in working on projects like this? Close is looking for great engineers to join our team!
yarn add @closeio/use-abortable-effect
AbortController
API and it is compatible with the fetch
API.AbortController
API then the hook behaves exactly like a regular useEffect
hook. See Can I Use for browser support overview.useEffect
useEffect
,
where the effect function you pass-in accepts an AbortSignal
instance as a param and you
can return a cleanup function that accepts an AbortController
instance.fetch
requests.useEffect(() => {
// do something
return () => {
/* cleanup */
};
}, [deps]);
const abortControllerRef = useAbortableEffect(
(abortSignal) => {
// do something
return (abortController) => {
/* do cleanup, you should probably abort */
};
},
[deps],
);
fetch
requestsimport React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyAbortableFetchComponent() {
const abortControllerRef = useAbortableEffect((abortSignal) =>
fetch(url, { signal: abortSignal })
.then(/* … */)
.catch((rejection) => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
}),
);
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
import React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyAbortableComputationComponent() {
const abortControllerRef = useAbortableEffect(abortSignal => {
new Promise((resolve, reject) => {
// Should be a DOMException per spec.
const abortRejection = new DOMException(
'Calculation aborted by the user',
'AbortError',
);
// Handle when abort was requested before starting the computation.
if (abortSignal.aborted) {
return reject(abortRejection);
}
// This simulates an expensive computation.
const timeout = setTimeout(() => resolve(1), 5000);
// Listen for abort request.
abortSignal.addEventListener('abort', () => {
clearTimeout(timeout);
reject(abortRejection);
});
})
.then(/* … */)
.catch(rejection => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
}),
});
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
import React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyCustomCleanupComponent() {
const [gotAborted, setGotAborted] = useState(false);
const abortControllerRef = useAbortableEffect((abortSignal) => {
fetch(url, { signal: abortSignal })
.then(/* … */)
.catch((rejection) => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
});
// Just return a function like in `useEffect`, with the difference that you
// get the abort controller (not a ref) as a param.
return (controller) => {
controller.abort();
setGotAborted(true);
};
});
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
MIT © Close
FAQs
Super simple React hook for running abortable effects
The npm package @closeio/use-abortable-effect receives a total of 1,064 weekly downloads. As such, @closeio/use-abortable-effect popularity was classified as popular.
We found that @closeio/use-abortable-effect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.