Skip to content

Instantly share code, notes, and snippets.

@pomle
Last active July 26, 2021 09:42
Show Gist options
  • Save pomle/a5da98085230dcb81de9486772d63b99 to your computer and use it in GitHub Desktop.
Save pomle/a5da98085230dcb81de9486772d63b99 to your computer and use it in GitHub Desktop.
// Setup and clean up are logically coupled together but defined separatedly and
// the implementing developer must know that the functionalty comes in pairs.
// We also have the problem where we must store any reference needed by the clean up in the component itself.
function setupFirstThing() {
// Setup is in this function
const handle = setupSomething();
return handle;
}
function teardownFirstThing(handle) {
// Cleanup is in another function
teardownSomething(handle);
}
function setupSecondThing() {
// Setup is in this function
}
function teardownSecondThing() {
// Cleanup is in another function
}
function setupThirdThing() {
// Setup is in this function
const handle = setupSomething();
return handle;
}
function teardownThirdThing(handle) {
// Cleanup is in another function
teardownSomething(handle);
}
new Vue({
created() {
this.firstHandle = setupFirstThing();
setupSecondThing();
this.thirdHandle = setupThirdThing();
},
destroyed() {
teardownFirstThing(this.firstHandle);
teardownSecondThing();
teardownThirdThing(this.thirdHandle);
}
})
function useFirstThing(x, y) {
useEffect(() => {
// Setup is in the beginning
const handle = setupSomething();
// Cleanup is in the end
return () => teardownSomething(handle);
}, [x, y]);
}
function useSecondThing(x, y) {
useEffect(() => {
// Setup is in the beginning
const handle = setupSomething();
// Cleanup is in the end
return () => teardownSomething(handle);
}, [x, y]);
}
function useThirdThing(x, y) {
useEffect(() => {
// Setup is in the beginning
const handle = setupSomething();
// Cleanup is in the end
return () => teardownSomething(handle);
}, [x, y]);
}
export function MyComponent() {
// All these calls a self contained and non-contaminant.
useFirstThing();
useSecondThing();
useThirdThing();
return <div/>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment