11# ` vue-loadable `
2+
23[ ![ Build Status] ( https://travis-ci.org/VitorLuizC/vue-loadable.svg?branch=master )] ( https://travis-ci.org/VitorLuizC/vue-loadable )
34[ ![ License] ( https://badgen.net/github/license/VitorLuizC/vue-loadable )] ( ./LICENSE )
45[ ![ Library minified size] ( https://badgen.net/bundlephobia/min/vue-loadable )] ( https://bundlephobia.com/result?p=vue-loadable )
1617</template >
1718
1819<script >
19- import { loadable , mapLoadableActions } from ' vue-loadable' ;
20+ import { mapActions } from ' vuex' ;
21+ import { loadable , mapLoadableMethods } from ' vue-loadable' ;
2022
2123export default {
2224 ... ,
@@ -39,10 +41,12 @@ export default {
3941 await this .$store .dispatch (' users/fetchUsers' );
4042 }, ' initialize' ),
4143
42- // An there's `mapLoadableActions` to map Vuex actions into loadable methods.
43- ... mapLoadableActions (' users' , {
44- initialize: ' fetchUsers'
45- })
44+ // An there's `mapLoadableMethods` to map methods into loadable methods (works with Vuex too).
45+ ... mapLoadableMethods (
46+ mapActions (' users' , {
47+ initialize: ' fetchUsers'
48+ })
49+ )
4650 },
4751 mounted () {
4852 this .initialize ();
@@ -97,7 +101,7 @@ You can install it locally instead with `LoadableMixin` mixin.
97101import { LoadableMixin } from ' vue- loadable' ;
98102
99103export default {
100- mixins: [ LoadableMixin ]
104+ mixins: [LoadableMixin],
101105};
102106</script>
103107```
@@ -111,12 +115,12 @@ export default {
111115 ` ` ` js
112116 Vue.component('SignInForm', {
113117 methods: {
114- signIn: loadable(async function (name) {
118+ signIn: loadable(async function(name) {
115119 // ...
116120 }, 'signIn'),
117121 },
118122
119- async mounted () {
123+ async mounted() {
120124 this.$isLoading('signIn');
121125 //=> false
122126
@@ -129,11 +133,12 @@ export default {
129133
130134 this.$isLoading('signIn');
131135 //=> false
132- }
136+ },
133137 });
134138 ` ` `
135139
136140 > It passes down the function arguments, rejects the errors and resolves the returned value.
141+ >
137142 > ```ts
138143 > async function confirmUsername (username : string ): Promise<boolean> {
139144 > // ...
@@ -142,16 +147,16 @@ export default {
142147 > export default {
143148 > methods: {
144149 > // Returns a function with same signature, but handling loading states.
145- > confirm: loadable (confirmUsername, ' confirmation' )
150+ > confirm: loadable (confirmUsername, ' confirmation' ),
146151 > },
147- > async mounted (): Promise < void > {
152+ > async mounted (): Promise < void > {
148153 > try {
149154 > const isConfirmed = await this .confirm (' VitorLuizC' );
150155 > this .$router .push (isConfirmed ? ' /checkout' : ' /confirmation' );
151156 > } catch (error) {
152157 > new Rollbar.Error (error).send ();
153158 > }
154- > }
159+ > },
155160 > };
156161 > ` ` `
157162
@@ -161,14 +166,28 @@ export default {
161166 <br />
162167
163168 ` ` ` ts
164- export default function loadable <Return, Params extends any[]> (
165- λ: (this : Vue & LoadableMixin , ... params : Params ) => Return | Promise < Return > ,
169+ type Method =
170+ | ((... args : any []) => any)
171+ | ((this : Vue , ... args : any []) => any);
172+
173+ type LoadableMethod< T extends Method> = (
174+ this: Vue,
175+ ... args: Parameters< T > ,
176+ ) => (
177+ ReturnType< T > extends Promise < any>
178+ ? ReturnType< T >
179+ : Promise < ReturnType< T >>
180+ );
181+
182+ const loadable: <T extends Method >(
183+ method: T ,
166184 state?: string,
167- ): (this: Vue & LoadableMixin, ...params: Params) => Promise<Return >;
185+ ) => LoadableMethod < T > ;
168186 ` ` `
187+
169188 </details>
170189
171- - **`mapLoadableActions `** map Vuex actions into Vue component's methods to trigger loading states.
190+ - **` mapLoadableMethods ` ** maps methods into loadable ones that triggers loading states, it works pretty well with Vuex .
172191
173192 > It uses method's names as loading state name.
174193
@@ -183,38 +202,38 @@ export default {
183202 < / template>
184203
185204 < script>
186- import { mapLoadableActions } from ' vue-loadable' ;
205+ import { mapActions } from ' vuex' ;
206+ import { mapLoadableMethods } from ' vue-loadable' ;
187207
188208 export default {
189- methods: mapLoadableActions ([
190- ' signInUser' ,
191- ' signUpUser'
192- ])
209+ methods: mapLoadableMethods (
210+ mapActions ([
211+ ' signInUser' ,
212+ ' signUpUser'
213+ ])
214+ )
193215 };
194216 ` ` `
195217
196- > It supports Vuex module namespaces too.
197- > ` ` ` js
198- > Vue .component (' SignInForm' , {
199- > methods: mapLoadableActions (' user' , [
200- > ' signIn' ,
201- > ' signUp'
202- > ])
203- > });
204- > ` ` `
205-
206218 <details>
207219 <summary>TypeScript type definitions.</summary>
208220
209221 <br />
210222
211223 ` ` ` ts
212- import { mapActions } from ' vuex' ;
224+ // `Method` and `LoadableMethod` are defined at `loadable` type definitions.
225+
226+ type Methods = Record< string, Method> ;
213227
214- declare const mapLoadableActions: typeof mapActions ;
228+ type LoadableMethods< T extends Methods> = {
229+ [K in keyof T ]: LoadableMethod< T [K ]> ;
230+ };
215231
216- export default mapLoadableActions ;
232+ const mapLoadableMethods: <T extends Methods >>(
233+ methods: T ,
234+ ) => LoadableMethods< T > ;
217235 ` ` `
236+
218237 </details>
219238
220239- **` $isLoading` ** is a method to check if a state is loading.
@@ -253,6 +272,7 @@ export default {
253272 $isLoading (state?: string): boolean;
254273 }
255274 ` ` `
275+
256276 </details>
257277
258278- **` $isLoadingAny` ** is a method to check if any state is loading.
@@ -297,6 +317,7 @@ export default {
297317 $isLoadingAny (): boolean;
298318 }
299319 ` ` `
320+
300321 </details>
301322
302323- **` $setLoading` ** is a method to set state as loading.
@@ -326,6 +347,7 @@ export default {
326347 $setLoading (state?: string): void ;
327348 }
328349 ` ` `
350+
329351 </details>
330352
331353- **` $unsetLoading` ** is a method to unset state as loading.
@@ -357,11 +379,11 @@ export default {
357379 $unsetLoading (state?: string): void ;
358380 }
359381 ` ` `
382+
360383 </details>
361384
362385## License
363386
364387Released under [MIT License](./LICENSE).
365388
366-
367- [](https://app.fossa.io/projects/git%2Bgithub.com%2FVitorLuizC%2Fvue-loadable?ref=badge_large)
389+ [](https://app.fossa.io/projects/git%2Bgithub.com%2FVitorLuizC%2Fvue-loadable?ref=badge_large)
0 commit comments