Skip to content

Commit 009d705

Browse files
author
anthinkingcoder
committed
新增promise代理,优化aoploading
1 parent 157ade2 commit 009d705

File tree

3 files changed

+114
-20
lines changed

3 files changed

+114
-20
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# vuex-loading
2-
简化vuex中异步请求,不得不维护loading state的问题,这不是一个loading ui插件,仅仅只是一个简化。
2+
简化vuex中异步请求,不得不维护loading state的问题,支持代理异步回调与promise
33

44
## Installing
55

@@ -22,10 +22,14 @@ const api = {
2222
}).catch(error => {
2323
errorCb(error)
2424
})
25+
},
26+
productDetail () {
27+
return fetch('/api/productDetail')
2528
}
2629
}
2730
const state = {
28-
products: []
31+
products: [],
32+
productDetail: {}
2933
}
3034

3135
const getters = {
@@ -34,29 +38,39 @@ const getters = {
3438
}
3539
}
3640

41+
//aopLoading(commit,loadingName, fn, isPromise)
42+
//有两种fn可以代理,第一种fn是一个promise,第二种fn是接受两个回调函数参数->成功回调和失败回调,设置isPromise可以选择指定方式代理。
3743
const actions = {
38-
//aopLoading会代理api.listProduct,然后返回一个被代理的api.listProduct
3944
listProduct ({{commit}}) {
4045
let request = vxl.aopLoading(commit, 'productsLoading',api.listProduct)
41-
//代理函数调用的时候,会先执行commit('productsLoading',true),在异步请求执行完成后,会在成功回调和失败回调调用前执行commit('productsLoading',false)
4246
request((result) => {
4347
commit('setProducts', result)
4448
}, error => {
45-
49+
})
50+
},
51+
productDetail ({{commit}}) {
52+
vxl.aopLoading(commit, 'productDetail', api.productDetail,true)
53+
.then((result) => {
54+
commit('setProducts', result)
55+
}).catch(error => {
56+
console.info(error)
4657
})
4758
}
4859
}
4960

5061
const mutations = {
5162
setProducts (state, item) {
5263
state.products = item
64+
},
65+
setProductDetail(state, item) {
66+
state.productDetail = item
5367
}
5468
}
5569

5670
//使用vxl.mixin将productsLoading相关操作注入state,getters,mutations中,这样我们不需要手写大堆的代码
57-
vxl.mixin({state,getters,mutations}, ['products'])
71+
vxl.mixin({state,getters,mutations}, ['products','productDetail'])
5872
//或者可以指定loading名字
59-
vxl.mixin({state,getters,mutations}, [{'products':'productsLoading'}])
73+
vxl.mixin({state,getters,mutations}, [{'products':'productsLoading'},'productDetail'])
6074

6175
export {
6276
state,getters,actions,mutations

index.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,26 @@ function mixin({state, getters, mutations}, stateNameArray = []) {
2626
}
2727

2828
/**
29-
* 生成loading action
30-
* @param commit
31-
* @param loading
32-
* @param fn
33-
* @returns {function(*=, *=)}
29+
* 代理loading
30+
* @param commit vuex commit
31+
* @param loading loading name
32+
* @param fn be proxy function
33+
* @param isPromise fn是否返回promise,false表示fn不返回promise, 而是以回调的形式返回
3434
*/
35-
function aopLoading(commit, loading, fn) {
35+
36+
function aopLoading(commit, loading, fn, isPromise = false) {
3637
loading = normalizeMutationName(loading)
38+
return isPromise ? aopPromise(commit, loading, fn) : aopCallback(commit, loading, fn);
39+
}
40+
41+
/**
42+
* 生成loading action(回调式)
43+
* @param commit vuex commit
44+
* @param loading loading name
45+
* @param fn be proxy function
46+
* @returns {function(*=, *=)} proxy function
47+
*/
48+
function aopCallback(commit, loading, fn) {
3749
return (success, error, ...otherArg) => {
3850
commit(loading, true)
3951
success = pointer(commit, loading, success)
@@ -45,6 +57,31 @@ function aopLoading(commit, loading, fn) {
4557
}
4658
}
4759

60+
/**
61+
* 生成loading action(promise式)
62+
* @param commit vuex commit
63+
* @param loading loading name
64+
* @param fn be proxy function
65+
* @returns {function(*=, *=)} proxy function
66+
*/
67+
function aopPromise(commit, loading, fn) {
68+
let showLoading = () => {
69+
commit(loading, true)
70+
}
71+
let hideLoading = (result) => {
72+
commit(loading, false)
73+
return result
74+
}
75+
return (...arg) => {
76+
let promise = Promise.resolve(loading)
77+
let chain = [showLoading, undefined, fn.bind(null, ...arg), undefined, hideLoading, hideLoading]
78+
while (chain.length > 0) {
79+
promise = promise.then(chain.shift(), chain.shift())
80+
}
81+
return promise
82+
}
83+
}
84+
4885
/**
4986
* 生成loading state
5087
* mapLoadingState({datasA:[],datasB:[]}) => {datasA:[],datasLoading:false,datasB:[],datasBLoading:false}

lib/vuex-loading.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,28 @@ function mixin(_ref) {
4141
}
4242

4343
/**
44-
* 生成loading action
45-
* @param commit
46-
* @param loading
47-
* @param fn
48-
* @returns {function(*=, *=)}
44+
* 代理loading
45+
* @param commit vuex commit
46+
* @param loading loading name
47+
* @param fn be proxy function
48+
* @param isPromise fn是否返回promise,false表示fn不返回promise, 而是以回调的形式返回
4949
*/
50+
5051
function aopLoading(commit, loading, fn) {
52+
var isPromise = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
53+
5154
loading = normalizeMutationName(loading);
55+
return isPromise ? aopPromise(commit, loading, fn) : aopCallback(commit, loading, fn);
56+
}
57+
58+
/**
59+
* 生成loading action(回调式)
60+
* @param commit vuex commit
61+
* @param loading loading name
62+
* @param fn be proxy function
63+
* @returns {function(*=, *=)} proxy function
64+
*/
65+
function aopCallback(commit, loading, fn) {
5266
return function (success, error) {
5367
for (var _len = arguments.length, otherArg = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
5468
otherArg[_key - 2] = arguments[_key];
@@ -73,6 +87,35 @@ function aopLoading(commit, loading, fn) {
7387
};
7488
}
7589

90+
/**
91+
* 生成loading action(promise式)
92+
* @param commit vuex commit
93+
* @param loading loading name
94+
* @param fn be proxy function
95+
* @returns {function(*=, *=)} proxy function
96+
*/
97+
function aopPromise(commit, loading, fn) {
98+
var showLoading = function showLoading() {
99+
commit(loading, true);
100+
};
101+
var hideLoading = function hideLoading(result) {
102+
commit(loading, false);
103+
return result;
104+
};
105+
return function () {
106+
for (var _len4 = arguments.length, arg = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
107+
arg[_key4] = arguments[_key4];
108+
}
109+
110+
var promise = Promise.resolve(loading);
111+
var chain = [showLoading, undefined, fn.bind.apply(fn, [null].concat(arg)), undefined, hideLoading, hideLoading];
112+
while (chain.length > 0) {
113+
promise = promise.then(chain.shift(), chain.shift());
114+
}
115+
return promise;
116+
};
117+
}
118+
76119
/**
77120
* 生成loading state
78121
* mapLoadingState({datasA:[],datasB:[]}) => {datasA:[],datasLoading:false,datasB:[],datasBLoading:false}
@@ -194,8 +237,8 @@ function pointer(commit, loading, fn) {
194237
return function () {
195238
commit(loading, false);
196239

197-
for (var _len4 = arguments.length, arg = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
198-
arg[_key4] = arguments[_key4];
240+
for (var _len5 = arguments.length, arg = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
241+
arg[_key5] = arguments[_key5];
199242
}
200243

201244
fn.apply(null, arg);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy