Skip to content

Commit 74a9657

Browse files
committed
feat: 新增 timer-store
1 parent 79708b4 commit 74a9657

File tree

5 files changed

+83
-102
lines changed

5 files changed

+83
-102
lines changed

README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm install --save timer-miniprogram
1616

1717
2. 导入小程序适配版本的 timer-miniprogram
1818

19-
```javascript
19+
```js
2020
import { TimerBehavior } from 'timer-miniprogram'
2121
// 在页面中使用
2222
Page({
@@ -45,6 +45,31 @@ Components({
4545
})
4646
```
4747

48+
## eslint 配置
49+
50+
为了让团队更好地遵守定时器使⽤规范,你还可以配置 eslint 增加代码提示,配置如下:
51+
52+
```
53+
// .eslintrc.js
54+
module.exports = {
55+
'rules': {
56+
'no-restricted-globals': ['error', {
57+
'name': 'setTimeout',
58+
'message': 'Please use TimerBehavior and this.$setTimeout instead. see the link: https://github.com/o2team/timer-miniprogram'
59+
}, {
60+
'name': 'setInterval',
61+
'message': 'Please use TimerBehavior and this.$setInterval instead. see the link: https://github.com/o2team/timer-miniprogram'
62+
}, {
63+
'name': 'clearInterval',
64+
'message': 'Please use TimerBehavior and this.$clearInterval instead. see the link: https://github.com/o2team/timer-miniprogram'
65+
}, {
66+
'name': 'clearTimout',
67+
'message': 'Please use TimerBehavior and this.$clearTimout instead. see the link: https://github.com/o2team/timer-miniprogram'
68+
}]
69+
}
70+
}
71+
```
72+
4873
## License
4974

5075

src/behavior.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import { Timer } from './timer'
2+
import { TimerStore } from './timer-store'
23

34
const TimerBehavior = Behavior({
5+
created () { this.$timerStore = new TimerStore() },
6+
detached () { this.$timerStore.hide() },
47
pageLifetimes: {
5-
show () { Timer.pageShow(this.__wxWebviewId__) },
6-
hide () { Timer.pageHide(this.__wxWebviewId__) }
8+
show () { this.$timerStore.show() },
9+
hide () { this.$timerStore.hide() }
710
},
811
methods: {
912
$setTimeout (fn = () => {}, timeout = 0, ...arg) {
10-
const timer = new Timer(false, this.__wxWebviewId__, fn, timeout, ...arg)
11-
return timer.id
13+
const timer = new Timer(false, fn, timeout, ...arg)
14+
return this.$timerStore.addTimer(timer)
1215
},
1316
$setInterval (fn = () => {}, timeout = 0, ...arg) {
14-
const timer = new Timer(true, this.__wxWebviewId__, fn, timeout, ...arg)
15-
return timer.id
17+
const timer = new Timer(true, fn, timeout, ...arg)
18+
return this.$timerStore.addTimer(timer)
1619
},
17-
$clearInterval (id) {
18-
Timer.clear(this.__wxWebviewId__, id)
19-
},
20-
$clearTimeout (id) {
21-
Timer.clear(this.__wxWebviewId__, id)
22-
},
23-
},
24-
created () { Timer.pageLoad(this.__wxWebviewId__) },
25-
detached () { Timer.pageUnLoad(this.__wxWebviewId__) },
20+
$clearInterval (id) { this.$timerStore.clear(id) },
21+
$clearTimeout (id) { this.$timerStore.clear(id) },
22+
}
2623
})
2724

2825
export { TimerBehavior }

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './timer'
22
export * from './behavior'
3+
export * from './timer-store'

src/timer-store.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class TimerStore {
2+
constructor () {
3+
this.store = new Map()
4+
this.isActive = true
5+
}
6+
7+
addTimer (timer) {
8+
this.store.set(timer.id, timer)
9+
this.isActive && timer.start(this.store)
10+
11+
return timer.id
12+
}
13+
14+
show () {
15+
/* 没有隐藏,不需要恢复定时器 */
16+
if (this.isActive) return
17+
18+
this.isActive = true
19+
this.store.forEach(timer => timer.start(this.store))
20+
}
21+
22+
hide () {
23+
this.store.forEach(timer => timer.suspend())
24+
this.isActive = false
25+
}
26+
27+
clear (id) {
28+
const timer = this.store.get(id)
29+
if (!timer) return
30+
31+
clearTimeout(timer.timerId)
32+
timer.timerId = ''
33+
this.store.delete(id)
34+
}
35+
}
36+
37+
export { TimerStore }

src/timer.js

+7-86
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,23 @@
1-
/**
2-
* 定时器管理库
3-
* @author chenzeji
4-
*/
5-
61
class Timer {
7-
/**
8-
* 清除定时器
9-
* @param {String} pageId 页面 id
10-
* @param {String} id 定时器 id
11-
*/
12-
static clear (pageId, id) {
13-
const { timerStore } = Timer.pageStore.get(pageId) || {}
14-
if (!timerStore) return
15-
16-
const timer = timerStore.get(id)
17-
if (!timer) return
18-
19-
clearTimeout(timer.timerId)
20-
timer.timerId = ''
21-
timerStore.delete(id)
22-
}
23-
24-
/**
25-
* 页面加载处理函数
26-
* @param {String} pageId 页面 id
27-
*/
28-
static pageLoad (pageId) {
29-
Timer.pageStore.set(pageId, {
30-
isActive: true,
31-
timerStore: new Map()
32-
})
33-
}
34-
35-
/**
36-
* 页面展示处理函数
37-
* @param {String} pageId 页面 id
38-
*/
39-
static pageShow (pageId) {
40-
const page = Timer.pageStore.get(pageId) || {}
41-
42-
/* 没有隐藏,不需要恢复定时器 */
43-
if (page.isActive) return
44-
45-
page.isActive = true
46-
page.timerStore && page.timerStore.forEach(timer => timer.start())
47-
}
48-
49-
/**
50-
* 页面隐藏处理函数
51-
* @param {String} pageId 页面 id
52-
*/
53-
static pageHide (pageId) {
54-
const page = Timer.pageStore.get(pageId) || {}
55-
page.timerStore && page.timerStore.forEach(timer => timer.suspend())
56-
page.isActive = false
57-
}
58-
59-
/**
60-
* 页面卸载处理函数
61-
* @param {String} pageId 页面 id
62-
*/
63-
static pageUnLoad (pageId) {
64-
Timer.pageHide(pageId)
65-
Timer.pageStore.delete(pageId)
66-
}
67-
682
/**
693
* 构造函数
704
* @param {Boolean} isInterval 是否是 setInterval
71-
* @param {String} pageId 页面 id
725
* @param {Function} fn 回调函数
736
* @param {Number} timeout 定时器执行时间间隔
747
* @param {...any} arg 定时器其他参数
758
*/
76-
constructor (isInterval = false, pageId = '', fn = () => {}, timeout = 0, ...arg) {
9+
constructor (isInterval = false, fn = () => {}, timeout = 0, ...arg) {
7710
this.id = ++Timer.count // 定时器递增 id
7811
this.fn = fn
7912
this.timeout = timeout
8013
this.restTime = timeout // 定时器剩余计时时间
81-
this.pageId = pageId
8214
this.isInterval = isInterval
8315
this.arg = arg
84-
85-
/* 存储定时器 */
86-
const { timerStore } = Timer.pageStore.get(pageId) || {}
87-
timerStore && timerStore.set(this.id, this)
88-
89-
this.start()
9016
}
9117

92-
/**
93-
* 启动定时器
94-
*/
95-
start () {
96-
const { isActive, timerStore } = Timer.pageStore.get(this.pageId) || {}
18+
/* 启动或恢复定时器 */
19+
start (timerStore) {
9720
/* 页面隐藏,不创建定时器 */
98-
if (this.restTime < 0 || !isActive) return
99-
10021
this.startTime = +new Date()
10122

10223
if (this.isInterval) {
@@ -112,7 +33,7 @@ class Timer {
11233
/* setTimeout */
11334
const cb = (...arg) => {
11435
this.fn(...arg)
115-
timerStore && timerStore.delete(this.id)
36+
timerStore.delete(this.id)
11637
}
11738
this.timerId = setTimeout(cb, this.restTime, ...this.arg)
11839
}
@@ -122,15 +43,15 @@ class Timer {
12243
if (this.timeout > 0) {
12344
const now = +new Date()
12445
const nextRestTime = this.restTime - (now - this.startTime)
125-
this.restTime = this.isInterval ? Math.abs(nextRestTime) % this.timeout : nextRestTime
46+
const intervalRestTime = nextRestTime >= 0 ? nextRestTime : this.timeout - (Math.abs(nextRestTime) % this.timeout)
47+
48+
this.restTime = this.isInterval ? intervalRestTime : nextRestTime
12649
}
12750
clearTimeout(this.timerId)
12851
}
12952
}
13053

13154
/* 定时器增量 id */
13255
Timer.count = 0
133-
/* 存储页面定时器和页面显示或隐藏状态 */
134-
Timer.pageStore = new Map()
13556

13657
export { Timer }

0 commit comments

Comments
 (0)