Skip to content

Commit 59c1b89

Browse files
authored
Merge pull request #2 from microcipcip/feature/useIntervalFn
Feature/use interval fn
2 parents 4fe5836 + bb71f87 commit 59c1b89

27 files changed

+547
-114
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Vue.use(VueCompositionAPI);
7070
- [`useMouseElement`](./src/components/useMouseElement/stories/useMouseElement.md) — tracks the mouse position relative to given element.
7171
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usemouseelement--demo)
7272
- Animations
73+
- [`useInterval`](./src/components/useInterval/stories/useInterval.md) — updates the `counter` value repeatedly on a fixed time delay.
74+
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useinterval--demo)
75+
- [`useIntervalFn`](./src/components/useIntervalFn/stories/useIntervalFn.md) — calls function repeatedly on a fixed time delay.
76+
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useintervalfn--demo)
7377
- [`useRaf`](./src/components/useRaf/stories/useRaf.md) — returns `elapsedTime` with requestAnimationFrame.
7478
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useraf--demo)
7579
- [`useRafFn`](./src/components/useRafFn/stories/useRafFn.md) — calls function with requestAnimationFrame.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useInterval'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<table class="table is-fullwidth">
3+
<thead>
4+
<tr>
5+
<th>Prop</th>
6+
<th>Value</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<td>counter</td>
12+
<td>
13+
<span>{{ counter }}</span>
14+
</td>
15+
</tr>
16+
<tr>
17+
<td colspan="2">
18+
<button class="button is-primary" @click="start" v-if="!isRunning">
19+
Start Interval
20+
</button>
21+
<button class="button is-danger" @click="stop" v-else>
22+
Stop Interval
23+
</button>
24+
</td>
25+
</tr>
26+
</tbody>
27+
</table>
28+
</template>
29+
30+
<script lang="ts">
31+
import Vue from 'vue'
32+
import { useInterval } from '@src/vue-use-kit'
33+
34+
export default Vue.extend({
35+
name: 'useIntervalDemo',
36+
setup() {
37+
const { isRunning, counter, start, stop } = useInterval(300)
38+
return { isRunning, counter, start, stop }
39+
}
40+
})
41+
</script>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# useInterval
2+
3+
Vue function that updates the `counter` value repeatedly on a fixed time delay.
4+
5+
## Reference
6+
7+
```typescript
8+
useInterval(
9+
ms?: number,
10+
runOnMount?: boolean
11+
): {
12+
isRunning: Ref<boolean>;
13+
counter: Ref<number>;
14+
start: () => void;
15+
stop: () => void;
16+
};
17+
```
18+
19+
### Parameters
20+
21+
- `ms: number` how many milliseconds to wait before updating the counter
22+
- `runOnMount: boolean` whether to run the interval on mount, `true` by default
23+
24+
### Returns
25+
26+
- `isRunning: Ref<boolean>` this value is `true` if the interval is running, `false` otherwise
27+
- `counter: Ref<number>` the number of times the interval has run
28+
- `start: Function` the function used for starting the interval
29+
- `stop: Function` the function used for stopping the interval
30+
31+
## Usage
32+
33+
```html
34+
<template>
35+
<div>
36+
<p>counter: {{ counter }}</p>
37+
38+
<button @click="start" v-if="!isRunning">Start Interval</button>
39+
<button @click="stop" v-else>Stop Interval</button>
40+
</div>
41+
</template>
42+
43+
<script lang="ts">
44+
import Vue from 'vue'
45+
import { useInterval } from 'vue-use-kit'
46+
47+
export default Vue.extend({
48+
name: 'UseIntervalDemo',
49+
setup() {
50+
const ms = 300
51+
52+
const { isRunning, counter, start, stop } = useInterval(ms)
53+
54+
return { isRunning, counter, start, stop }
55+
}
56+
})
57+
</script>
58+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { storiesOf } from '@storybook/vue'
2+
import path from 'path'
3+
import StoryTitle from '@src/helpers/StoryTitle.vue'
4+
import UseIntervalDemo from './UseIntervalDemo.vue'
5+
6+
const functionName = 'useInterval'
7+
const functionPath = path.resolve(__dirname, '..')
8+
const notes = require(`./${functionName}.md`).default
9+
10+
const basicDemo = () => ({
11+
components: { StoryTitle, demo: UseIntervalDemo },
12+
template: `
13+
<div class="container">
14+
<story-title
15+
function-path="${functionPath}"
16+
source-name="${functionName}"
17+
demo-name="UseIntervalDemo.vue"
18+
>
19+
<template v-slot:title></template>
20+
<template v-slot:intro></template>
21+
</story-title>
22+
<demo />
23+
</div>`
24+
})
25+
26+
storiesOf('animations|useInterval', module)
27+
.addParameters({ notes })
28+
.add('Demo', basicDemo)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { mount } from '@src/helpers/test'
2+
import { useInterval } from '@src/vue-use-kit'
3+
4+
beforeEach(() => {
5+
jest.useFakeTimers()
6+
})
7+
8+
afterEach(() => {
9+
jest.clearAllTimers()
10+
})
11+
12+
const testComponent = () => ({
13+
template: `
14+
<div>
15+
<div id="isRunning" v-if="isRunning"></div>
16+
</div>
17+
`,
18+
setup() {
19+
const { isRunning } = useInterval(1000)
20+
return { isRunning }
21+
}
22+
})
23+
24+
describe('useInterval', () => {
25+
it('should show #isRunning when the intervals are called', async () => {
26+
const wrapper = mount(testComponent())
27+
jest.advanceTimersByTime(1500)
28+
29+
// Wait for Vue to append #isReady in the DOM
30+
await wrapper.vm.$nextTick()
31+
expect(wrapper.find('#isRunning').exists()).toBe(true)
32+
})
33+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ref } from '@src/api'
2+
import { useIntervalFn } from '@src/vue-use-kit'
3+
4+
export function useInterval(ms = 0, runOnMount = true) {
5+
const counter = ref(0)
6+
const animHandler = () => {
7+
counter.value = counter.value + 1
8+
}
9+
const { isRunning, start, stop } = useIntervalFn(animHandler, ms, runOnMount)
10+
return { isRunning, counter, start, stop }
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useIntervalFn'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<template>
2+
<table class="table is-fullwidth">
3+
<thead>
4+
<tr>
5+
<th>Prop</th>
6+
<th>Value</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<td>callbackCounter</td>
12+
<td>
13+
<span>{{ callbackCounter }}</span>
14+
</td>
15+
</tr>
16+
<tr>
17+
<td colspan="3">
18+
<button class="button is-primary" @click="start" v-if="!isRunning">
19+
Resume Interval
20+
</button>
21+
<button class="button is-danger" @click="stop" v-else>
22+
Stop Interval
23+
</button>
24+
</td>
25+
</tr>
26+
</tbody>
27+
</table>
28+
</template>
29+
30+
<script lang="ts">
31+
import Vue from 'vue'
32+
import { ref } from '@src/api'
33+
import { useIntervalFn } from '@src/vue-use-kit'
34+
35+
export default Vue.extend({
36+
name: 'UseIntervalFnDemo',
37+
setup() {
38+
const ms = 300
39+
const callbackCounter = ref(0)
40+
const animHandler = () => {
41+
callbackCounter.value = callbackCounter.value + 1
42+
}
43+
44+
const { isRunning, start, stop } = useIntervalFn(animHandler, ms)
45+
46+
return { isRunning, start, stop, callbackCounter }
47+
}
48+
})
49+
</script>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# useIntervalFn
2+
3+
Vue function that calls given callback repeatedly on a fixed time delay.
4+
5+
## Reference
6+
7+
```typescript
8+
useIntervalFn(
9+
callback: Function,
10+
ms?: number,
11+
runOnMount?: boolean
12+
): {
13+
isRunning: Ref<boolean>;
14+
start: () => void;
15+
stop: () => void;
16+
};
17+
```
18+
19+
### Parameters
20+
21+
- `callback: Function` the function to call for each interval finishes
22+
- `ms: number` how many milliseconds to wait before running the callback function
23+
- `runOnMount: boolean` whether to run the interval on mount, `true` by default
24+
25+
### Returns
26+
27+
- `isRunning: Ref<boolean>` this value is `true` if the interval is running, `false` otherwise
28+
- `start: Function` the function used for starting the interval
29+
- `stop: Function` the function used for stopping the interval
30+
31+
## Usage
32+
33+
```html
34+
<template>
35+
<div>
36+
<p>callbackCounter: {{ callbackCounter }}</p>
37+
38+
<button @click="start" v-if="!isRunning">Start Interval</button>
39+
<button @click="stop" v-else>Stop Interval</button>
40+
</div>
41+
</template>
42+
43+
<script lang="ts">
44+
import Vue from 'vue'
45+
import { ref } from '@src/api'
46+
import { useIntervalFn } from 'vue-use-kit'
47+
48+
export default Vue.extend({
49+
name: 'UseIntervalFnDemo',
50+
setup() {
51+
const ms = 300
52+
const callbackCounter = ref(0)
53+
const animHandler = () => {
54+
callbackCounter.value = callbackCounter.value + 1
55+
}
56+
57+
const { isRunning, start, stop } = useIntervalFn(animHandler, ms)
58+
59+
return { isRunning, start, stop, callbackCounter }
60+
}
61+
})
62+
</script>
63+
```

0 commit comments

Comments
 (0)