Skip to content

Commit d688bee

Browse files
committed
Adds active request protection to *Once queries
1 parent f2d4fc5 commit d688bee

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

firestore/useCollection.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const useCollectionInternal = <T = DocumentData>(
6666
return;
6767
}
6868
if (listen) {
69-
const listener = options?.snapshotListenOptions
69+
const unsubscribe = options?.snapshotListenOptions
7070
? onSnapshot(
7171
ref.current,
7272
options.snapshotListenOptions,
@@ -76,11 +76,28 @@ const useCollectionInternal = <T = DocumentData>(
7676
: onSnapshot(ref.current, setValue, setError);
7777

7878
return () => {
79-
listener();
79+
unsubscribe();
8080
};
8181
} else {
82+
let effectActive = true;
83+
8284
const get = getDocsFnFromGetOptions(options?.getOptions);
83-
get(ref.current).then(setValue).catch(setError);
85+
86+
get(ref.current)
87+
.then((result) => {
88+
if (effectActive) {
89+
setValue(result);
90+
}
91+
})
92+
.catch((error) => {
93+
if (effectActive) {
94+
setError(error);
95+
}
96+
});
97+
98+
return () => {
99+
effectActive = false;
100+
};
84101
}
85102
}, [ref.current]);
86103

firestore/useDocument.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const useDocumentInternal = <T = DocumentData>(
6565
return;
6666
}
6767
if (listen) {
68-
const listener = options?.snapshotListenOptions
68+
const unsubscribe = options?.snapshotListenOptions
6969
? onSnapshot(
7070
ref.current,
7171
options.snapshotListenOptions,
@@ -75,12 +75,28 @@ const useDocumentInternal = <T = DocumentData>(
7575
: onSnapshot(ref.current, setValue, setError);
7676

7777
return () => {
78-
listener();
78+
unsubscribe();
7979
};
8080
} else {
81+
let effectActive = true;
82+
8183
const get = getDocFnFromGetOptions(options?.getOptions);
8284

83-
get(ref.current).then(setValue).catch(setError);
85+
get(ref.current)
86+
.then((doc) => {
87+
if (effectActive) {
88+
setValue(doc);
89+
}
90+
})
91+
.catch((error) => {
92+
if (effectActive) {
93+
setError(error);
94+
}
95+
});
96+
97+
return () => {
98+
effectActive = false;
99+
};
84100
}
85101
}, [ref.current]);
86102

0 commit comments

Comments
 (0)