-
Notifications
You must be signed in to change notification settings - Fork 97
/
redirector.js
48 lines (46 loc) · 1.24 KB
/
redirector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
/**
* 对wx.navigateTo、wx.redirectTo、wx.navigateBack的包装,在它们的基础上添加了事件
*/
var Message = require('./message')
var conf = require('./conf')
var exportee = module.exports = new Message()
var timer, readyTimer, pending
exportee.on('page:ready', function () {
readyTimer = setTimeout(function () {
pending = false
}, 100)
})
function route(type, cfg, args) {
if (pending) return
pending = true
clearTimeout(timer)
clearTimeout(readyTimer)
var routeFrozenTime = conf.get('routeFrozenTime')
/**
* 避免重复的跳转
*/
timer = setTimeout(function () {
pending = false
}, routeFrozenTime || routeFrozenTime === 0 ? routeFrozenTime : 1000)
exportee.emit('navigateTo', cfg.url)
// 会存在不兼容接口,例如:reLaunch
if (wx[type]) {
return wx[type].apply(wx, args)
}
}
exportee.navigateTo = function (cfg) {
return route('navigateTo', cfg, arguments)
}
exportee.redirectTo = function (cfg) {
return route('redirectTo', cfg, arguments)
}
exportee.switchTab = function (cfg) {
return route('switchTab', cfg, arguments)
}
exportee.reLaunch = function (cfg) {
return route('reLaunch', cfg, arguments)
}
exportee.navigateBack = function () {
return wx.navigateBack.apply(wx, arguments)
}