Skip to content

Commit 8b511ad

Browse files
committed
🔥 remove LoadableMixinInstance and other helper types
1 parent d76fc43 commit 8b511ad

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

src/LoadableMixin.ts

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
import Vue from 'vue';
22

3-
export type LoadableMixinInstance = Vue & LoadableMixinState & LoadableMixinMethods;
4-
5-
export type LoadableMixinState = {
6-
LOADING_STATES: Record<string, number>;
7-
};
8-
9-
export type LoadableMixinMethods = {
10-
/**
11-
* Check if a state is loading.
12-
* @param [state] - Loading state name.
13-
*/
14-
$isLoading (this: LoadableMixinInstance, state?: string): boolean;
15-
16-
/**
17-
* Check if any state is loading.
18-
*/
19-
$isLoadingAny (this: LoadableMixinInstance): boolean;
20-
21-
/**
22-
* Set state as loading.
23-
* @param [state] - Loading state name.
24-
*/
25-
$setLoading (this: LoadableMixinInstance, state?: string): void;
26-
27-
/**
28-
* Unset state as loading.
29-
* @param [state] - Loading state name.
30-
*/
31-
$unsetLoading (this: LoadableMixinInstance, state?: string): void;
32-
};
33-
343
/**
354
* A mixin which adds loading states and helpers to Vue components.
365
* @example ```js
@@ -44,31 +13,31 @@ export type LoadableMixinMethods = {
4413
* })```
4514
*/
4615
const LoadableMixin = Vue.extend({
47-
data (): LoadableMixinState {
16+
data() {
4817
return {
49-
LOADING_STATES: Object.create(null)
18+
LOADING_STATES: Object.create(null) as Record<string, number>,
5019
};
5120
},
5221
methods: {
53-
$isLoading (state = 'unknown') {
22+
$isLoading(state = 'unknown') {
5423
const value = this.LOADING_STATES[state];
5524
return !!value && value > 0;
5625
},
5726

58-
$isLoadingAny () {
27+
$isLoadingAny() {
5928
return Object.keys(this.LOADING_STATES).some(this.$isLoading);
6029
},
6130

62-
$setLoading (state = 'unknown') {
31+
$setLoading(state = 'unknown') {
6332
const value = this.LOADING_STATES[state];
6433
this.$set(this.LOADING_STATES, state, value ? value + 1 : 1);
6534
},
6635

67-
$unsetLoading (state = 'unknown') {
36+
$unsetLoading(state = 'unknown') {
6837
const value = this.LOADING_STATES[state];
6938
this.$set(this.LOADING_STATES, state, value ? value - 1 : 0);
70-
}
71-
} as LoadableMixinMethods
39+
},
40+
},
7241
});
7342

7443
export default LoadableMixin;

src/loadable.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
import Vue from 'vue';
12
import callWithHooks from './callWithHooks';
2-
import { LoadableMixinInstance } from './LoadableMixin';
33

44
/**
55
* An union of any function and functions that have access to `this`
66
* (Vue instance).
77
*/
88
export type Method =
99
| ((...args: any[]) => any)
10-
| ((this: LoadableMixinInstance, ...args: any[]) => any);
10+
| ((this: Vue, ...args: any[]) => any);
1111

1212
/**
1313
* A Higher-order type to trasnform a method into loadable method that have
1414
* access to `this` (Vue instance) and returns a Promise.
1515
*/
16-
export type LoadableMethod <T extends Method> = (
17-
this: LoadableMixinInstance,
16+
export type LoadableMethod<T extends Method> = (
17+
this: Vue,
1818
...args: Parameters<T>
19-
) =>
20-
ReturnType<T> extends Promise<any>
21-
? ReturnType<T>
22-
: Promise<ReturnType<T>>;
19+
) => ReturnType<T> extends Promise<any>
20+
? ReturnType<T>
21+
: Promise<ReturnType<T>>;
2322

2423
/**
2524
* Decorate a method to causes loading states changes during its execution. It
@@ -36,13 +35,13 @@ export type LoadableMethod <T extends Method> = (
3635
* @param method - A method, commonly async, which causes loading state changes.
3736
* @param [state] - Loading state name. It's "unknown" if not defined.
3837
*/
39-
const loadable = <T extends Method> (method: T, state: string = 'unknown') =>
40-
function () {
38+
const loadable = <T extends Method>(method: T, state: string = 'unknown') =>
39+
function() {
4140
this.$setLoading(state);
4241

4342
return callWithHooks(
4443
() => method.apply(this, arguments as any),
45-
() => this.$unsetLoading(state)
44+
() => this.$unsetLoading(state),
4645
);
4746
} as LoadableMethod<T>;
4847

src/mapLoadableMethods.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type Methods = Record<string, Method>;
99
* A Higher-order type to transform methods into loadable methods. It keeps keys
1010
* as-is, but values have access to `this` (Vue instance) and returns a Promise.
1111
*/
12-
export type LoadableMethods <T extends Methods> = {
12+
export type LoadableMethods<T extends Methods> = {
1313
[K in keyof T]: LoadableMethod<T[K]>;
1414
};
1515

@@ -35,8 +35,8 @@ export type LoadableMethods <T extends Methods> = {
3535
* }
3636
* });
3737
*/
38-
const mapLoadableMethods = <T extends Methods> (
39-
methods: T
38+
const mapLoadableMethods = <T extends Methods>(
39+
methods: T,
4040
): LoadableMethods<T> => {
4141
const names = Object.keys(methods) as (string & keyof T)[];
4242

@@ -45,7 +45,7 @@ const mapLoadableMethods = <T extends Methods> (
4545
loadableMethods[name] = loadable(methods[name], name);
4646
return loadableMethods;
4747
},
48-
Object.create(null) as LoadableMethods<T>
48+
Object.create(null) as LoadableMethods<T>,
4949
);
5050
};
5151

src/vue-loadable.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ declare module 'vue/types/vue' {
77
* Check if a state is loading.
88
* @param [state] - Loading state name.
99
*/
10-
$isLoading (state?: string): boolean;
10+
$isLoading(state?: string): boolean;
1111

1212
/**
1313
* Check if any state is loading.
1414
*/
15-
$isLoadingAny (): boolean;
15+
$isLoadingAny(): boolean;
1616

1717
/**
1818
* Set state as loading.
1919
* @param [state] - Loading state name.
2020
*/
21-
$setLoading (state?: string): void;
21+
$setLoading(state?: string): void;
2222

2323
/**
2424
* Unset state as loading.
2525
* @param [state] - Loading state name.
2626
*/
27-
$unsetLoading (state?: string): void;
27+
$unsetLoading(state?: string): void;
2828
}
2929
}
3030

@@ -35,7 +35,7 @@ export { default as loadable, Method, LoadableMethod } from './loadable';
3535
export {
3636
default as mapLoadableMethods,
3737
Methods,
38-
LoadableMethods
38+
LoadableMethods,
3939
} from './mapLoadableMethods';
4040

4141
/**
@@ -44,7 +44,7 @@ export {
4444
* Vue.use(install)```
4545
* @param Vue - The Vue constructor.
4646
*/
47-
export function install (Vue: VueConstructor): void {
47+
export function install(Vue: VueConstructor): void {
4848
Vue.mixin(LoadableMixin);
4949
}
5050

0 commit comments

Comments
 (0)