Skip to content

Commit

Permalink
feat(system): add createRenderEffectWithCleaning function
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed May 15, 2022
1 parent f001a37 commit 1678375
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-sloths-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suid/system": minor
---

Add `createRenderEffectWithCleaning` function
15 changes: 15 additions & 0 deletions packages/system/src/createRenderEffectWithCleaning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createRenderEffect, onCleanup } from "solid-js";

function createRenderEffectWithCleaning(effect: () => () => void) {
let lastCleanup: (() => void) | undefined;
onCleanup(() => lastCleanup?.());
createRenderEffect<(() => void) | undefined>((prevCleanup) => {
if (prevCleanup) {
lastCleanup = undefined;
prevCleanup?.();
}
return (lastCleanup = effect());
});
}

export default createRenderEffectWithCleaning;

0 comments on commit 1678375

Please sign in to comment.