Skip to content

Commit

Permalink
feat(useMutationObserver): add takeRecords function (#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Nov 9, 2023
1 parent 038666b commit f9136e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/useMutationObserver/index.test.ts
Expand Up @@ -166,4 +166,25 @@ describe('useMutationObserver', () => {
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)
})

it('should work with takeRecords', async () => {
const target = document.createElement('div')
const cb = vi.fn()

const { takeRecords } = useMutationObserver(target, cb, {
attributes: true,
})

target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)

target.setAttribute('id', 'header')
const records = takeRecords()

await promiseTimeout(10)
expect(records).toHaveLength(1)
expect(records![0].target).toBe(target)
expect(cb).toHaveBeenCalledTimes(1)
})
})
5 changes: 5 additions & 0 deletions packages/core/useMutationObserver/index.ts
Expand Up @@ -46,6 +46,10 @@ export function useMutationObserver(
{ immediate: true },
)

const takeRecords = () => {
return observer?.takeRecords()
}

const stop = () => {
cleanup()
stopWatch()
Expand All @@ -56,6 +60,7 @@ export function useMutationObserver(
return {
isSupported,
stop,
takeRecords,
}
}

Expand Down

0 comments on commit f9136e8

Please sign in to comment.