Skip to content

Commit 9c95199

Browse files
committed
refactor: use onScopeDispose instead of onUnmounted. #239
1 parent e64e31e commit 9c95199

File tree

7 files changed

+193
-54
lines changed

7 files changed

+193
-54
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"typescript": "^4.0.5",
111111
"vite": "^2.1.5",
112112
"vite-plugin-vue2": "^1.9.2",
113-
"vue": "^3.3.4",
113+
"vue": "^3.5.13",
114114
"vue-template-compiler": "^2.6.14",
115115
"vue2": "npm:vue@2"
116116
},

src/core/plugins/useCachePlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { onUnmounted, ref } from 'vue-demi';
1+
import { ref } from 'vue-demi';
22

33
import { definePlugin } from '../definePlugin';
4-
import { isFunction } from '../utils';
4+
import { isFunction, onScopeDisposeCompatible } from '../utils';
55
import type { CacheData } from '../utils/cache';
66
import { getCache, setCache } from '../utils/cache';
77
import { getCacheQuery, setCacheQuery } from '../utils/cacheQuery';
@@ -68,7 +68,7 @@ export default definePlugin(
6868
unSubscribe.value = subscribeCache();
6969
}
7070

71-
onUnmounted(() => {
71+
onScopeDisposeCompatible(() => {
7272
unSubscribe.value();
7373
});
7474

src/core/plugins/usePollingPlugin.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { computed, onUnmounted, ref, watch } from 'vue-demi';
1+
import { computed, ref, watch } from 'vue-demi';
22

33
import { definePlugin } from '../definePlugin';
4-
import { isDocumentVisibility, isNil, isOnline, refToRaw } from '../utils';
4+
import {
5+
isDocumentVisibility,
6+
isNil,
7+
isOnline,
8+
onScopeDisposeCompatible,
9+
refToRaw,
10+
} from '../utils';
511
import subscriber from '../utils/listener';
612
import type { Timeout } from '../utils/types';
713

@@ -76,7 +82,7 @@ export default definePlugin(
7682
addUnsubscribeList(subscriber('RECONNECT_LISTENER', rePolling));
7783
}
7884

79-
onUnmounted(() => {
85+
onScopeDisposeCompatible(() => {
8086
unsubscribeList.forEach(unsubscribe => unsubscribe());
8187
});
8288

src/core/plugins/useRefreshOnWindowFocus.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { computed, onUnmounted, watchEffect } from 'vue-demi';
1+
import { computed, watchEffect } from 'vue-demi';
22

33
import { definePlugin } from '../definePlugin';
4-
import { refToRaw } from '../utils';
4+
import { onScopeDisposeCompatible, refToRaw } from '../utils';
55
import limitTrigger from '../utils/limitTrigger';
66
import subscriber from '../utils/listener';
77

@@ -32,7 +32,7 @@ export default definePlugin(
3232
}
3333
});
3434

35-
onUnmounted(() => {
35+
onScopeDisposeCompatible(() => {
3636
unsubscribe();
3737
});
3838

src/core/useQuery.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, onUnmounted } from 'vue-demi';
1+
import { getCurrentInstance, inject } from 'vue-demi';
22

33
import { getGlobalOptions, GLOBAL_OPTIONS_PROVIDE_KEY } from './config';
44
import createQuery from './createQuery';
@@ -9,16 +9,20 @@ import type {
99
QueryResult,
1010
Service,
1111
} from './types';
12+
import { onScopeDisposeCompatible } from './utils';
1213

1314
function useQuery<R, P extends unknown[]>(
1415
service: Service<R, P>,
1516
options: Options<R, P> = {},
1617
plugins: PluginImplementType<R, P>[],
1718
): QueryResult<R, P> {
18-
const injectedGlobalOptions = inject<GlobalOptions>(
19-
GLOBAL_OPTIONS_PROVIDE_KEY,
20-
{},
21-
);
19+
let injectedGlobalOptions = {};
20+
if (getCurrentInstance()) {
21+
injectedGlobalOptions = inject<GlobalOptions>(
22+
GLOBAL_OPTIONS_PROVIDE_KEY,
23+
{},
24+
);
25+
}
2226

2327
const config = {
2428
...getGlobalOptions(),
@@ -38,7 +42,7 @@ function useQuery<R, P extends unknown[]>(
3842
queryInstance.context.run(...params);
3943
}
4044

41-
onUnmounted(() => {
45+
onScopeDisposeCompatible(() => {
4246
queryInstance.context.cancel();
4347
});
4448

src/core/utils/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Ref } from 'vue-demi';
2+
import { onScopeDispose, version } from 'vue-demi';
23
import { isRef } from 'vue-demi';
34

45
export const objectToString = Object.prototype.toString;
@@ -80,3 +81,12 @@ export const shallowCopy = <T>(value: T): T => {
8081
return value;
8182
}
8283
};
84+
85+
export const onScopeDisposeCompatible = (fn: () => void) => {
86+
if (version.startsWith('3.5')) {
87+
// @ts-ignore
88+
onScopeDispose(fn, true);
89+
} else {
90+
onScopeDispose(fn);
91+
}
92+
};

0 commit comments

Comments
 (0)