用flow替代setTimeout,请求更实时

This commit is contained in:
TENCENT\ivinwu
2018-05-23 16:44:57 +08:00
parent a5cc4156c7
commit e2e2e933ec
5 changed files with 55 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@@ -5,5 +5,8 @@
"main": "./src/weRequest.js",
"dependencies": {
"webpack": "latest"
},
"devDependencies": {
"webpack-cli": "^2.1.3"
}
}

35
src/lib/flow.js Normal file
View File

@@ -0,0 +1,35 @@
var store = {};
function emit (key,data){
var flow = getFlow(key);
flow.result = data || true;
flow.waitingList.forEach(function(callback){
callback(data);
});
flow.waitingList.length = 0 ;
}
function wait (key,callback){
var flow = getFlow(key);
if(flow.result){
callback(flow.result)
}else{
flow.waitingList.push(callback)
}
}
function getFlow(key){
if(!store[key]){
store[key] = {
waitingList:[],
result:null
}
}
return store[key];
}
module.exports = {
wait: wait,
emit: emit
}

View File

@@ -1,4 +1,5 @@
const loading = require('./loading');
const flow = require('./lib/flow');
//params
var sessionName = "session";
@@ -33,14 +34,16 @@ var isCheckingSession = false;
function checkSession(callback, obj) {
if (isCheckingSession) {
setTimeout(function () {
flow.wait('checkSessionFinished', function () {
checkSession(callback, obj)
}, 500);
})
} else if (!sessionIsFresh && session) {
isCheckingSession = true;
obj.count++;
// 如果还没检验过session是否有效则需要检验一次
obj._checkSessionStartTime = new Date().getTime();
console.log('wx.checkSession');
wx.checkSession({
success: function () {
// 登录态有效,且在本生命周期内无须再检验了
@@ -58,6 +61,7 @@ function checkSession(callback, obj) {
reportCGI('wx_checkSession', obj._checkSessionStartTime, obj._checkSessionEndTime, request);
}
doLogin(callback, obj);
flow.emit('checkSessionFinished');
}
})
} else {
@@ -72,15 +76,16 @@ function doLogin(callback, obj) {
typeof callback === "function" && callback();
} else if (logining) {
// 正在登录中,请求轮询稍后,避免重复调用登录接口
setTimeout(function () {
flow.wait('doLoginFinished', function () {
doLogin(callback, obj);
}, 500)
})
} else {
// 缓存中无session
logining = true;
obj.count++;
// 记录调用wx.login前的时间戳
obj._loginStartTime = new Date().getTime();
console.log('wx.login');
wx.login({
complete: function () {
obj.count--;
@@ -122,6 +127,7 @@ function doLogin(callback, obj) {
obj.count--;
typeof obj.complete === "function" && obj.count == 0 && obj.complete();
logining = false;
flow.emit('doLoginFinished');
},
fail: codeToSession.fail || null
});
@@ -130,6 +136,7 @@ function doLogin(callback, obj) {
console.error(res);
// 登录失败,解除锁,防止死锁
logining = false;
flow.emit('doLoginFinished');
}
},
fail: function (res) {
@@ -137,6 +144,7 @@ function doLogin(callback, obj) {
console.error(res);
// 登录失败,解除锁,防止死锁
logining = false;
flow.emit('doLoginFinished');
}
})
}

View File

@@ -1,8 +1,8 @@
var path = require('path');
var webpack = require('webpack');
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
module.exports = {
//mode: 'development',
mode: 'production',
entry: './src/weRequest.js',
output: {
path: path.join(__dirname, 'build'),
@@ -10,12 +10,5 @@ module.exports = {
library: "weRequest",
libraryTarget: "commonjs-module"
},
plugins: [
new UglifyJsPlugin({
compress: {
warnings: false
},
except: []
})
]
//devtool: 'inline-source-map'
}