aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx')
-rw-r--r--FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx23
1 files changed, 23 insertions, 0 deletions
diff --git a/FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx b/FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx
new file mode 100644
index 00000000..78e21151
--- /dev/null
+++ b/FrontEnd/src/components/hooks/useAutoUnsubscribePromise.tsx
@@ -0,0 +1,23 @@
+import { useEffect, DependencyList } from "react";
+
+export default function useAutoUnsubscribePromise<T>(
+ promiseGenerator: () => Promise<T> | null | undefined,
+ resultHandler: (data: T) => void,
+ dependencies?: DependencyList | undefined,
+) {
+ useEffect(() => {
+ let subscribe = true;
+ const promise = promiseGenerator();
+ if (promise) {
+ void promise.then((data) => {
+ if (subscribe) {
+ resultHandler(data);
+ }
+ });
+
+ return () => {
+ subscribe = false;
+ };
+ }
+ }, dependencies);
+}