Skip to content

Enhancement/adding ability to reset idle #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,67 @@ const vm = new Vue({
</div>
```

### Methods

A build in method is extended through the use of the global mixin, allowing you to reset the idle timer from anywhere in your application.

#### Example
// assumes you are using the plugin method

```html
<template>
<div @click="resetIdleTimer">
Click to reset
</div>
</template>

<script>
export default {
methods: {
resetIdleTimer(){
this.$resetIdle() // stops timer, resets values to default, starts again
}
}
}
</script>
```

#### Use Cases

This can be handy when writing unit tests that rely on the idle timer being reset after an assertion.

```js
// using jest and vue test utils
import Vue from 'vue'
import IdleVue from 'idle-vue'
import { createLocalVue, shallowMount } from '@vue/test-utils'

const eventEmitter = new Vue()

const idleVueMock = localVue.use(IdleVue, {
eventEmitter,
idleTime: 1000 // 1/10th of a second
})
beforeEach(() => {
wrapper = shallowMount(App, {
// other plugins
idleVueMock
})
// resetting the timer before each test runs
wrapper.vm.$resetIdle()
})

describe('The idle timer doing what it\'s supposed to', () => {
it('Something happened after idle', done => {
setTimeout(() => {
const yourTestCase = 'something that happened because the timer ran out'
expect(yourTestCase).toBe(true)
done()
}, 1000)
})
})
```

### `isAppIdle`

The plug-in adds a computed value `isAppIdle` to every Vue object.
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export default {
isAppIdle () {
return store && store.state[moduleName].isIdle
}
},
methods: {
$resetIdle(){
idle.stop()
.reset()
.start()
}
}
})
}
Expand Down