Giter Site home page Giter Site logo

wofwca / safe-init-destroy Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 15 KB

The right way to write `destroy()` functions

Home Page: https://www.npmjs.com/package/safe-init-destroy

License: Apache License 2.0

JavaScript 100.00%
destroy init async constructor deinit destructor library refactoring utils observer-pattern

safe-init-destroy's Introduction

safe-init-destroy

If you have a function called destroy (or deinit) and it is longer than 3 lines, you may want to rewrite it using this approach.

UDP: I just learned that this is an "observer" design pattern, so I guess the takeaway is "the observer pattern is good in this case".

Example

Before After
// ...





function init() {
  doSomething();

  // ...
  doSomethingElse();

  // ...
  if (cond) {
    doSomeOtherThing();

  }
}
function destroy() {
  undoSomething();
  undoSomethingElse();
  if (didDoSomeOtherThing) {
    undoSomeOtherThing();
  }
}
// ...
// ...
+const {
+  onDestroy,
+  destroy
+} = createDestructionManager();
 
 function init() {
   doSomething();
+  onDestroy(() => undoSomething());
   // ...
   doSomethingElse();
+  onDestroy(() => undoSomethingElse());
   // ...
   if (cond) {
     doSomeOtherThing();
+    onDestroy(() => undoSomeOtherThing());
   }
 }
-function destroy() {
-  undoSomething();
-  undoSomethingElse();
-  if (didDoSomeOtherThing) {
-    undoSomeOtherThing();
-  }
-}
 // ...

Such code is much more maintainable because all the related things are grouped together. If you change one piece of code, you won't forget to update its corresponding destroy() part (or vice versa), because it's immediately next to it (perhaps even inside the same nested code block).

Feedback wanted

I would like to hear feedback on this approach. I don't know why I havent's seen it implemented in the wild, yet I have seen bloated destroy methods that look like they can break from a breath of wind (no offence). If you have seen similar code (even in different languages), or code that solves the same problem, or code that manages to avoid this problem, please let me know.

safe-init-destroy's People

Contributors

wofwca avatar

Watchers

 avatar

safe-init-destroy's Issues

Execute callbacks in reverse order?

This is the case for a lot of similar pieces of software. For example, in Rust references are dropped in reverse order. And in general destruction should go in reverse order. Example use case:

const url = URL.createObjectURL(mediaSource);
onDestroy(() => URL.revokeObjectURL(url));
el.src = url;
onDestroy(() => el.src = "");

Here it makes sense that we first must get rid of usages of the URL, and only then invalidate it.

But we currently execute them in the same order as they were added:

* Callbacks are executed in the same order they were added.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.