Skip to content

Commit b922f7f

Browse files
committed
improvement: Make properties more consistent and remove cyclic import
1 parent 9114402 commit b922f7f

File tree

7 files changed

+40
-44
lines changed

7 files changed

+40
-44
lines changed

src/components/useTimeout/stories/UseTimeoutDemo.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
<td colspan="3">
1818
<button
1919
class="button is-primary"
20-
@click="resetTimer"
20+
@click="reset"
2121
v-text="btnResetMsg"
2222
/>
23-
<button class="button is-danger" @click="cancelTimer">
23+
<button class="button is-danger" @click="cancel">
2424
Cancel Timer
2525
</button>
2626
</td>
@@ -38,7 +38,7 @@ export default Vue.extend({
3838
name: 'UseTimeoutDemo',
3939
setup() {
4040
const timerDuration = 3000
41-
const { isReady, isIdle, cancelTimer, resetTimer } = useTimeout(
41+
const { isReady, isIdle, cancel, reset } = useTimeout(
4242
timerDuration,
4343
false
4444
)
@@ -54,7 +54,7 @@ export default Vue.extend({
5454
return 'Completed'
5555
})
5656
57-
return { btnResetMsg, timerStatus, cancelTimer, resetTimer }
57+
return { btnResetMsg, timerStatus, cancel, reset }
5858
}
5959
})
6060
</script>

src/components/useTimeout/stories/useTimeout.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ useTimeout(
1111
): {
1212
isReady: Ref<boolean | null>;
1313
isIdle: Ref<boolean>;
14-
cancelTimer: () => void;
15-
resetTimer: () => void;
14+
cancel: () => void;
15+
reset: () => void;
1616
}
1717
```
1818

@@ -28,8 +28,8 @@ useTimeout(
2828
- `true` when the timer is completed
2929
- `null` when the timer is cancelled
3030
- `isIdle: boolean` this value is `true` if the timer has ever been called, `false` otherwise
31-
- `cancelTimer: Function` the function used to cancel the timer
32-
- `resetTimer: Function` the function used for resetting the timer
31+
- `cancel: Function` the function used to cancel the timer
32+
- `reset: Function` the function used for resetting the timer
3333

3434
## Usage
3535

@@ -38,8 +38,8 @@ useTimeout(
3838
<div>
3939
<p>Timer status: {{ isReady ? 'Called!' : 'Pending...' }}</p>
4040

41-
<button @click="resetTimer">Reset Timer</button>
42-
<button @click="cancelTimer">Cancel Timer</button>
41+
<button @click="reset">Reset Timer</button>
42+
<button @click="cancel">Cancel Timer</button>
4343
</div>
4444
</template>
4545

@@ -51,8 +51,8 @@ useTimeout(
5151
name: 'UseTimeoutDemo',
5252
setup() {
5353
const timerDuration = 3000
54-
const { isReady, cancelTimer, resetTimer } = useTimeout(timerDuration)
55-
return { isReady, cancelTimer, resetTimer }
54+
const { isReady, cancel, reset } = useTimeout(timerDuration)
55+
return { isReady, cancel, reset }
5656
}
5757
})
5858
</script>
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { useTimeoutFn } from '../../vue-use-kit'
1+
import { useTimeoutFn } from '../useTimeoutFn'
22

33
const noop = () => null
44
export function useTimeout(ms = 0, runOnMount = true) {
5-
const { isReady, isIdle, cancelTimer, resetTimer } = useTimeoutFn(
6-
noop,
7-
ms,
8-
runOnMount
9-
)
10-
return { isReady, isIdle, cancelTimer, resetTimer }
5+
const { isReady, isIdle, cancel, reset } = useTimeoutFn(noop, ms, runOnMount)
6+
return { isReady, isIdle, cancel, reset }
117
}

src/components/useTimeoutFn/stories/UseTimeoutFnDemo.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
<td colspan="3">
2424
<button
2525
class="button is-primary"
26-
@click="resetTimer"
26+
@click="reset"
2727
v-text="btnResetMsg"
2828
/>
29-
<button class="button is-danger" @click="cancelTimer">
29+
<button class="button is-danger" @click="cancel">
3030
Cancel Timer
3131
</button>
3232
</td>
@@ -48,7 +48,7 @@ export default Vue.extend({
4848
const timerHandler = () => {
4949
timerCallbackMsg.value = 'Timer completed!'
5050
}
51-
const { isReady, isIdle, cancelTimer, resetTimer } = useTimeoutFn(
51+
const { isReady, isIdle, cancel, reset } = useTimeoutFn(
5252
timerHandler,
5353
timerDuration,
5454
false
@@ -74,8 +74,8 @@ export default Vue.extend({
7474
timerCallbackMsg,
7575
btnResetMsg,
7676
timerStatus,
77-
cancelTimer,
78-
resetTimer
77+
cancel,
78+
reset
7979
}
8080
}
8181
})

src/components/useTimeoutFn/stories/useTimeoutFn.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ useTimeoutFn(
1212
): {
1313
isReady: Ref<boolean | null>;
1414
isIdle: Ref<boolean>;
15-
cancelTimer: () => void;
16-
resetTimer: () => void;
15+
cancel: () => void;
16+
reset: () => void;
1717
}
1818
```
1919

@@ -30,8 +30,8 @@ useTimeoutFn(
3030
- `true` when the timer is completed
3131
- `null` when the timer is cancelled
3232
- `isIdle: boolean` this value is `true` if the timer has ever been called, `false` otherwise
33-
- `cancelTimer: Function` the function used to cancel the timer
34-
- `resetTimer: Function` the function used for resetting the timer
33+
- `cancel: Function` the function used to cancel the timer
34+
- `reset: Function` the function used for resetting the timer
3535

3636
## Usage
3737

@@ -41,8 +41,8 @@ useTimeoutFn(
4141
<p>Timer status: {{ isReady ? 'Called!' : 'Pending...' }}</p>
4242
<p>Timeout Callback msg: {{ timerFnMsg }}</p>
4343

44-
<button @click="resetTimer">Reset Timer</button>
45-
<button @click="cancelTimer">Cancel Timer</button>
44+
<button @click="reset">Reset Timer</button>
45+
<button @click="cancel">Cancel Timer</button>
4646
</div>
4747
</template>
4848

@@ -60,12 +60,12 @@ useTimeoutFn(
6060
timerFnMsg.value = 'Timer completed!'
6161
}
6262
63-
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(
63+
const { isReady, cancel, reset } = useTimeoutFn(
6464
timerHandler,
6565
timerDuration
6666
)
6767
68-
return { timerFnMsg, isReady, cancelTimer, resetTimer }
68+
return { timerFnMsg, isReady, cancel, reset }
6969
}
7070
})
7171
</script>

src/components/useTimeoutFn/useTimeoutFn.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ const testComponent = (onMount = true) => ({
1515
<div>
1616
<div id="isIdle" v-if="isIdle"></div>
1717
<div id="isReady" v-if="isReady">
18-
<button id="cancelTimer" @click="cancelTimer"></button>
19-
<button id="resetTimer" @click="resetTimer"></button>
18+
<button id="cancel" @click="cancel"></button>
19+
<button id="reset" @click="reset"></button>
2020
<div id="isCallbackCalled" v-if="isCallbackCalled"></div>
2121
</div>
2222
</div>
2323
`,
2424
setup() {
2525
const isCallbackCalled = ref(false)
26-
const { isReady, isIdle, cancelTimer, resetTimer } = useTimeoutFn(
26+
const { isReady, isIdle, cancel, reset } = useTimeoutFn(
2727
() => {
2828
isCallbackCalled.value = true
2929
},
3030
1000,
3131
onMount
3232
)
33-
return { isReady, isIdle, cancelTimer, resetTimer, isCallbackCalled }
33+
return { isReady, isIdle, cancel, reset, isCallbackCalled }
3434
}
3535
})
3636

@@ -41,26 +41,26 @@ describe('useTimeoutFn', () => {
4141
expect(setTimeout).toHaveBeenCalledTimes(1)
4242
})
4343

44-
it('should call setTimeout again when resetTimer is called', async () => {
44+
it('should call setTimeout again when reset is called', async () => {
4545
expect(setTimeout).toHaveBeenCalledTimes(0)
4646
const wrapper = mount(testComponent())
4747
expect(setTimeout).toHaveBeenCalledTimes(1)
4848
jest.runAllTimers()
4949

50-
// Wait for Vue to append #resetTimer in the DOM
50+
// Wait for Vue to append #reset in the DOM
5151
await wrapper.vm.$nextTick()
52-
wrapper.find('#resetTimer').trigger('click')
52+
wrapper.find('#reset').trigger('click')
5353
expect(setTimeout).toHaveBeenCalledTimes(2)
5454
})
5555

56-
it('should hide all elements when cancelTimer is called', async () => {
56+
it('should hide all elements when cancel is called', async () => {
5757
const wrapper = mount(testComponent())
5858
jest.runAllTimers()
5959

6060
// Wait for Vue to append #isReady in the DOM
6161
await wrapper.vm.$nextTick()
6262
expect(wrapper.find('#isReady').exists()).toBe(true)
63-
wrapper.find('#cancelTimer').trigger('click')
63+
wrapper.find('#cancel').trigger('click')
6464
jest.runAllTimers()
6565

6666
// Wait for Vue to remove #isReady from the DOM

src/components/useTimeoutFn/useTimeoutFn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function useTimeoutFn(callback: Function, ms = 0, runOnMount = true) {
55
const isIdle = ref(!runOnMount)
66
let timeout: any = null
77

8-
const cancelTimer = () => {
8+
const cancel = () => {
99
isReady.value = null
1010
if (timeout) {
1111
clearTimeout(timeout)
@@ -26,7 +26,7 @@ export function useTimeoutFn(callback: Function, ms = 0, runOnMount = true) {
2626
}
2727

2828
onMounted(() => runOnMount && setTimer())
29-
onUnmounted(cancelTimer)
29+
onUnmounted(cancel)
3030

31-
return { isReady, isIdle, cancelTimer, resetTimer: setTimer }
31+
return { isReady, isIdle, cancel, reset: setTimer }
3232
}

0 commit comments

Comments
 (0)