Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
import flushMicroTasks from './flush-microtasks' | ||
|
||
let cleanupCallbacks = [] | ||
let internalCleanupCbs = [] | ||
let cleanupCbs = [] | ||
|
||
async function cleanup() { | ||
await flushMicroTasks() | ||
cleanupCallbacks.forEach((cb) => cb()) | ||
cleanupCallbacks = [] | ||
internalCleanupCbs.forEach((cb) => cb()) | ||
internalCleanupCbs = [] | ||
cleanupCbs.forEach((cb) => cb()) | ||
} | ||
|
||
function addInternalCleanup(callback) { | ||
internalCleanupCbs.push(callback) | ||
} | ||
|
||
function addCleanup(callback) { | ||
cleanupCallbacks.push(callback) | ||
cleanupCbs.push(callback) | ||
} | ||
|
||
function removeCleanup(callback) { | ||
cleanupCallbacks = cleanupCallbacks.filter((cb) => cb !== callback) | ||
function removeInternalCleanup(callback) { | ||
internalCleanupCbs = internalCleanupCbs.filter((cb) => cb !== callback) | ||
} | ||
|
||
export { cleanup, addCleanup, removeCleanup } | ||
export { cleanup, addCleanup, addInternalCleanup, removeInternalCleanup } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect } from 'react' | ||
import { renderHook, addCleanup } from 'src' | ||
|
||
let callSequence = [] | ||
addCleanup(() => { | ||
callSequence.push('cleanup') | ||
}) | ||
addCleanup(() => { | ||
callSequence.push('another cleanup') | ||
}) | ||
|
||
describe('addCleanup tests', () => { | ||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
callSequence.push('unmount') | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(callSequence).toEqual(['unmount', 'cleanup', 'another cleanup']) | ||
}) | ||
}) |