Skip to content

Commit

Permalink
Merge branch 'feat-useTimeoutFn'
Browse files Browse the repository at this point in the history
  • Loading branch information
lmhcoding committed Sep 20, 2020
2 parents 26b5c37 + 7324143 commit 079a6c8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './useInterval'
export * from './useHash'
export * from './useHistory'
export * from './useTimeout'
export * from './useTimeoutFn'
10 changes: 7 additions & 3 deletions src/useTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { ref } from 'vue'
export function useTimeout(delay = 1000, immediate = true) {
const ready = ref(false)
let timer: any
const initTimeout = () => {
ready.value = false
const stop = () => {
if (timer) {
clearTimeout(timer)
}
}
const initTimeout = () => {
ready.value = false
stop()
timer = window.setTimeout(() => {
ready.value = true
timer = null
Expand All @@ -17,6 +20,7 @@ export function useTimeout(delay = 1000, immediate = true) {

return {
ready,
start: () => initTimeout()
start: () => initTimeout(),
stop
}
}
24 changes: 24 additions & 0 deletions src/useTimeoutFn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { watch, WatchStopHandle } from 'vue'
import { useTimeout } from './useTimeout'

export function useTimeoutFn(fn: Function, delay = 1000, immediate = true, clearEffectWhenStop = false) {
const { start, ready, stop } = useTimeout(delay, immediate)
let stopEffect: WatchStopHandle | undefined
const startEffect = () => {
stopEffect = watch(ready, (hasReady) => {
hasReady && fn()
})
}
const _stop = () => {
clearEffectWhenStop && stopEffect!()
stop()
}
startEffect()
return {
start: () => {
clearEffectWhenStop && startEffect()
start()
},
stop: _stop
}
}
8 changes: 7 additions & 1 deletion tests/useTimeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useTimeout } from '../src/useTimeout'
import { useTimeout } from '../src/useTimeout'

beforeEach(() => {
jest.useFakeTimers()
Expand Down Expand Up @@ -33,3 +33,9 @@ test('setTimeout should be called after calling start when immediate is false',
jest.advanceTimersByTime(1000)
expect(ready.value).toBeTruthy()
})

test('setTimeout should be clear after calling stop', () => {
const { stop } = useTimeout()
stop()
expect(clearTimeout).toHaveBeenCalledTimes(1)
})
55 changes: 55 additions & 0 deletions tests/useTimeoutFn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useTimeoutFn } from '../src/useTimeoutFn'
import { nextTick } from 'vue'

let callback: Function | undefined

beforeEach(() => {
callback = jest.fn()
jest.useFakeTimers()
})

afterEach(() => {
jest.clearAllTimers()
jest.clearAllMocks()
})

test('setTimeout should be called with default delay', async () => {
useTimeoutFn(callback!)
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000)
jest.advanceTimersByTime(1000)
await nextTick()
expect(callback!).toHaveBeenCalledTimes(1)
})

test('setTimeout should not be called when immediate is set to false', () => {
useTimeoutFn(callback!, 1000, false)
expect(setTimeout).not.toBeCalled()
})

test('setTimeout should be called after calling start when immediate is false', async () => {
const { start } = useTimeoutFn(callback!, 1000, false)
expect(setTimeout).not.toBeCalled()
start()
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000)
jest.advanceTimersByTime(1000)
await nextTick()
expect(callback!).toHaveBeenCalledTimes(1)
})

test('setTimeout should be clear after calling stop', () => {
const { stop } = useTimeoutFn(callback!, 1000)
stop()
expect(clearTimeout).toHaveBeenCalledTimes(1)
})

test('callback should be called 1 time when clearEffectWhenStop is true and calling start after calling stop', async () => {
const { start, stop } = useTimeoutFn(callback!, 1000, true, true)
stop()
expect(clearTimeout).toHaveBeenCalledTimes(1)
start()
jest.advanceTimersByTime(1000)
await nextTick()
expect(callback!).toHaveBeenCalledTimes(1)
})

0 comments on commit 079a6c8

Please sign in to comment.