Skip to content

Commit

Permalink
refactor: use onScopeDispose instead of onUnmounted. #239
Browse files Browse the repository at this point in the history
  • Loading branch information
John60676 committed Dec 10, 2024
1 parent e64e31e commit 9c95199
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"typescript": "^4.0.5",
"vite": "^2.1.5",
"vite-plugin-vue2": "^1.9.2",
"vue": "^3.3.4",
"vue": "^3.5.13",
"vue-template-compiler": "^2.6.14",
"vue2": "npm:vue@2"
},
Expand Down
6 changes: 3 additions & 3 deletions src/core/plugins/useCachePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { onUnmounted, ref } from 'vue-demi';
import { ref } from 'vue-demi';

import { definePlugin } from '../definePlugin';
import { isFunction } from '../utils';
import { isFunction, onScopeDisposeCompatible } from '../utils';
import type { CacheData } from '../utils/cache';
import { getCache, setCache } from '../utils/cache';
import { getCacheQuery, setCacheQuery } from '../utils/cacheQuery';
Expand Down Expand Up @@ -68,7 +68,7 @@ export default definePlugin(
unSubscribe.value = subscribeCache();
}

onUnmounted(() => {
onScopeDisposeCompatible(() => {
unSubscribe.value();
});

Expand Down
12 changes: 9 additions & 3 deletions src/core/plugins/usePollingPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { computed, onUnmounted, ref, watch } from 'vue-demi';
import { computed, ref, watch } from 'vue-demi';

import { definePlugin } from '../definePlugin';
import { isDocumentVisibility, isNil, isOnline, refToRaw } from '../utils';
import {
isDocumentVisibility,
isNil,
isOnline,
onScopeDisposeCompatible,
refToRaw,
} from '../utils';
import subscriber from '../utils/listener';
import type { Timeout } from '../utils/types';

Expand Down Expand Up @@ -76,7 +82,7 @@ export default definePlugin(
addUnsubscribeList(subscriber('RECONNECT_LISTENER', rePolling));
}

onUnmounted(() => {
onScopeDisposeCompatible(() => {
unsubscribeList.forEach(unsubscribe => unsubscribe());
});

Expand Down
6 changes: 3 additions & 3 deletions src/core/plugins/useRefreshOnWindowFocus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { computed, onUnmounted, watchEffect } from 'vue-demi';
import { computed, watchEffect } from 'vue-demi';

import { definePlugin } from '../definePlugin';
import { refToRaw } from '../utils';
import { onScopeDisposeCompatible, refToRaw } from '../utils';
import limitTrigger from '../utils/limitTrigger';
import subscriber from '../utils/listener';

Expand Down Expand Up @@ -32,7 +32,7 @@ export default definePlugin(
}
});

onUnmounted(() => {
onScopeDisposeCompatible(() => {
unsubscribe();
});

Expand Down
16 changes: 10 additions & 6 deletions src/core/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { inject, onUnmounted } from 'vue-demi';
import { getCurrentInstance, inject } from 'vue-demi';

import { getGlobalOptions, GLOBAL_OPTIONS_PROVIDE_KEY } from './config';
import createQuery from './createQuery';
Expand All @@ -9,16 +9,20 @@ import type {
QueryResult,
Service,
} from './types';
import { onScopeDisposeCompatible } from './utils';

function useQuery<R, P extends unknown[]>(
service: Service<R, P>,
options: Options<R, P> = {},
plugins: PluginImplementType<R, P>[],
): QueryResult<R, P> {
const injectedGlobalOptions = inject<GlobalOptions>(
GLOBAL_OPTIONS_PROVIDE_KEY,
{},
);
let injectedGlobalOptions = {};
if (getCurrentInstance()) {
injectedGlobalOptions = inject<GlobalOptions>(
GLOBAL_OPTIONS_PROVIDE_KEY,
{},
);
}

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

onUnmounted(() => {
onScopeDisposeCompatible(() => {
queryInstance.context.cancel();
});

Expand Down
10 changes: 10 additions & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Ref } from 'vue-demi';
import { onScopeDispose, version } from 'vue-demi';
import { isRef } from 'vue-demi';

export const objectToString = Object.prototype.toString;
Expand Down Expand Up @@ -80,3 +81,12 @@ export const shallowCopy = <T>(value: T): T => {
return value;
}
};

export const onScopeDisposeCompatible = (fn: () => void) => {
if (version.startsWith('3.5')) {
// @ts-ignore
onScopeDispose(fn, true);
} else {
onScopeDispose(fn);
}
};
Loading

0 comments on commit 9c95199

Please sign in to comment.