Skip to content

Commit f831a98

Browse files
author
jrtaylor
committed
feat(usertimeout): timer functionality
1 parent deb59f0 commit f831a98

File tree

6 files changed

+161
-28
lines changed

6 files changed

+161
-28
lines changed

.github/workflows/npmpublish-alpha.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ jobs:
3232
env:
3333
NODE_AUTH_TOKEN: ${{secrets.NPM_REGISTRY_TOKEN}}
3434

35-
publish-gpr:
36-
needs: build
37-
runs-on: ubuntu-latest
38-
steps:
39-
- uses: actions/checkout@v1
40-
- uses: actions/setup-node@v1
41-
with:
42-
node-version: 12
43-
registry-url: https://npm.pkg.github.com/
44-
scope: '@jrtnq514'
45-
- run: npm ci
46-
- run: npm publish
47-
env:
48-
NODE_AUTH_TOKEN: ${{secrets.GITHUB_PACKAGES_TOKEN}}
35+
# publish-gpr:
36+
# needs: build
37+
# runs-on: ubuntu-latest
38+
# steps:
39+
# - uses: actions/checkout@v1
40+
# - uses: actions/setup-node@v1
41+
# with:
42+
# node-version: 12
43+
# registry-url: https://npm.pkg.github.com/
44+
# scope: '@jrtnq514'
45+
# - run: npm ci
46+
# - run: npm publish
47+
# env:
48+
# NODE_AUTH_TOKEN: ${{secrets.GITHUB_PACKAGES_TOKEN}}

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
11
# vue-user-timeout
2+
A simple plugin to help with timeouts.
23

34

5+
6+
## Getting started
7+
Install
8+
```
9+
npm install --save vue-user-timeout
10+
```
11+
Add plugin to your entry file
12+
```
13+
import VueUserTimeout from 'vue-user-timeout'
14+
15+
Vue.use(VueUserTimeout, [options])
16+
```
17+
18+
## Usage
19+
20+
21+
## Methods
22+
23+
## Options
24+
| property | description | default | values |
25+
|:------------|-------------|---------------|-------------------|
26+
| **timeout** | Time in milliseconds before timeout event. | 60000ms (10 min) | `Number`<br>*Min*: must be greater than (>) `updateInterval`<br> *Max*: must be less than or equal to (<=) `Number.MAX_SAFE_INTEGER` |
27+
| **updateInterval** | How often to check if the timeout has been exceeded in milliseconds. | 500ms | `Number`<br>*Min*: must be greater than (>) 100ms<br> *Max*: must be less than (<) `timeout` |
28+
| **events** | Array of events that will reset the timeout. *Events are currently listened for on `document`*. | `['resize', 'scroll', 'keydown', 'click', 'mousemove']` | `Array<String>`<br> |
29+
| **startOnload** | When `true` the timeout will start once the plugin has been loaded. | `false` | `Boolean` |
30+
| **destroyOnTimeout** | When `true` all listeners will be removed upon completion of the timeout event. *They can be added again using `init()`*. | `true` | `Boolean` |
31+
32+
## Events
33+
434
## TODO
5-
* Listen for events. Handle only events user specifies
35+
- [ ] Listen for events. Handle only events user specifies
636
* Click
737
* Mouse move
838
* etc
9-
* Directive to be added to elements
10-
* Store (Vuex) integration
11-
* SessionStorage support
12-
* Emit events/call a defined function that the user can extend/override
13-
* Linting
14-
* Tests
15-
* Transpiling...? or let the user's project decide
39+
- [ ] Directive to be added to elements
40+
- [ ] Store (Vuex) integration
41+
- [ ] SessionStorage support
42+
- [ ] Emit events/call a defined function that the user can extend/override
43+
- [ ] Linting
44+
- [ ] Tests
45+
- [ ] Warning option/event

dist/vue-user-timeout.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"webpack-stream": "^5.2.1"
5555
},
5656
"peerDependencies": {
57-
"vue": ">= 1.0.0"
57+
"vue": ">= 2.0.0"
5858
},
5959
"config": {
6060
"commitizen": {

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const VueUserTimeout = {
44
// eslint-disable-next-line no-unused-vars
55
install(Vue, options = {}) {
66
console.log('VueUserTimeout Installed');
7+
userTimeout.init(Vue, options);
78
// eslint-disable-next-line no-param-reassign
89
Vue.prototype.$userTimeout = userTimeout;
910
// eslint-disable-next-line no-param-reassign

src/lib/userTimeout.js

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,113 @@
1-
export default {
1+
const userTimeout = {
22
// properties
3-
test: 'Test',
3+
Vue: null,
4+
mergedOptions: null,
5+
timerInterval: null,
6+
startTime: null,
7+
currentTime: null,
8+
active: false,
49

510
// methods
6-
init: options => {
11+
init: (Vue, options) => {
12+
userTimeout.Vue = Vue;
13+
const defaultOptions = {
14+
timeout: 10000, // 10 min
15+
updateInterval: 500, // 1 sec
16+
events: ['resize', 'scroll', 'keydown', 'click', 'mousemove'],
17+
startOnload: false,
18+
destroyOnTimeout: true,
19+
};
20+
721
if (options) {
8-
console.log('yay');
22+
userTimeout.mergedOptions = {
23+
...defaultOptions,
24+
...options,
25+
};
26+
userTimeout.active = userTimeout.mergedOptions.startOnload;
927
}
28+
29+
userTimeout
30+
.addListeners()
31+
.then(() => {
32+
// start the time
33+
if (userTimeout.mergedOptions.startOnload) {
34+
userTimeout.start();
35+
}
36+
})
37+
.catch(() => {
38+
console.log('There was an error adding event listeners');
39+
});
40+
},
41+
42+
addListeners: () => {
43+
return new Promise((resolve, reject) => {
44+
try {
45+
userTimeout.mergedOptions.events.forEach(evnt => {
46+
document.addEventListener(evnt, userTimeout.reset);
47+
});
48+
resolve();
49+
} catch (err) {
50+
reject(err);
51+
}
52+
});
53+
},
54+
55+
start: () => {
56+
if (userTimeout.startTime === null) {
57+
userTimeout.startTime = Date.now();
58+
}
59+
userTimeout.active = true;
60+
userTimeout.timerInterval = setInterval(() => {
61+
userTimeout.currentTime = Date.now();
62+
const elapsedTime = userTimeout.currentTime - userTimeout.startTime;
63+
console.log(`tick tick ${(elapsedTime / 1000).toFixed(3)}`);
64+
if (elapsedTime >= userTimeout.mergedOptions.timeout) {
65+
// emit event
66+
console.log(`your time is up ${(elapsedTime / 1000).toFixed(3)}`);
67+
userTimeout.stop();
68+
if (userTimeout.mergedOptions.destroyOnTimeout) {
69+
userTimeout.active = false;
70+
userTimeout.destroy();
71+
}
72+
}
73+
}, userTimeout.mergedOptions.updateInterval);
74+
},
75+
76+
reset: () => {
77+
console.log('reset');
78+
userTimeout.stop();
79+
if (userTimeout.active) {
80+
userTimeout.start();
81+
}
82+
},
83+
84+
stop: () => {
85+
clearInterval(userTimeout.timerInterval);
86+
userTimeout.startTime = null;
87+
},
88+
89+
pause: () => {
90+
clearInterval(userTimeout.timerInterval);
91+
console.log(
92+
`currently paused at ${(
93+
(userTimeout.currentTime - userTimeout.startTime) /
94+
1000
95+
).toFixed(3)}`
96+
);
97+
},
98+
99+
destroy: () => {
100+
return new Promise((resolve, reject) => {
101+
try {
102+
userTimeout.mergedOptions.events.forEach(evnt => {
103+
document.removeEventListener(evnt, userTimeout.reset);
104+
});
105+
resolve();
106+
} catch (err) {
107+
reject(err);
108+
}
109+
});
10110
},
11111
};
112+
113+
export default userTimeout;

0 commit comments

Comments
 (0)