Skip to content

Commit

Permalink
fix infinit loop if map is used in useResult
Browse files Browse the repository at this point in the history
  • Loading branch information
tcastelly committed Aug 27, 2024
1 parent 8215deb commit ac2d774
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-use-async",
"version": "1.8.6",
"version": "1.8.7",
"description": "Xhr and async helpers",
"repository": {
"type": "git",
Expand Down
15 changes: 11 additions & 4 deletions src/useResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ export default function <T, Z extends T, U = Res<T, Z>>(
_res.value = map(defaultRes as Res<T, Z>);
}

// prevent infinite loop because of _res/res changes
let disableWatch = false;

watchEffect(() => {
if (res) {
const unWrapRes = unref<any>(res);

if (unWrapRes !== undefined) {
disableWatch = true;
_res.value = map?.(unWrapRes);
}
}
Expand All @@ -50,14 +54,17 @@ export default function <T, Z extends T, U = Res<T, Z>>(
watch(
() => _res.value,
(v) => {
if (res.value === undefined) {
if (v === undefined || res.value === undefined) {
return;
}

if (v !== res.value) {
// @ts-ignore
res.value = new Result(v);
if (disableWatch) {
disableWatch = false;
return;
}

// @ts-ignore
res.value = new Result(v);
},
);

Expand Down

0 comments on commit ac2d774

Please sign in to comment.