Skip to content

Commit

Permalink
feat(useHistory): export clear function
Browse files Browse the repository at this point in the history
  • Loading branch information
lmhcoding committed Sep 26, 2020
1 parent 0a009de commit cbfd197
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/useHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ function buildState(): IHistoryState {
}
}

export function useHistory(): ToRefs<IHistoryState> {
export interface IHistoryResult extends ToRefs<IHistoryState> {
clear: () => void
}

export function useHistory(): IHistoryResult {
const state: UnwrapRef<IHistoryState> = reactive(buildState())
useEvent('popstate', buildState)
useEvent('pushstate' as any, buildState)
useEvent('replacestate' as any, buildState)
return toRefs(state)
const [, clearPopStateListener] = useEvent('popstate', buildState)
const [, clearPushStateListener] = useEvent('pushstate' as any, buildState)
const [, clearReplaceStateListener] = useEvent('replacestate' as any, buildState)
const clear = () => {
clearPopStateListener()
clearPushStateListener()
clearReplaceStateListener()
}
return {
...toRefs(state),
clear
}
}

0 comments on commit cbfd197

Please sign in to comment.