npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modum

v0.0.1

Published

A utility for logically grouping the performance and destruction of side effects

Downloads

2

Readme

Modum

A utility for logically grouping the performance and destruction of side effects. Inspired by React's useEffect hook.

Why the name you ask? Modum is Latin for effect.

Problem

Most JavaScript frameworks have some type of "init" and "destroy" life cycle functions. For example, React has componentDidMount() and componentWillUnmount(); and similarly Angular has ngOnInit() and ngOnDestroy().

The problem with these functions is that often times the work that you have to do on "init" closely mirrors the work that you have to do on "destroy". So you end up with this highly coupled code split between two different functions.

Modum addresses that problem by letting you declare both the "init" and "destroy" logic at the same time in the same place. It's just not all run at the same time. The function that you pass to perform() (the effect) is run immediately, and the function that you return from the effect is not run until you tell Modum to destroy that side effect.

Example usage

import Modum from 'modum'

let modum = new Modum()

modum.perform(() => {
  console.log('Side effect 1 performed')
  return () => console.log('Side effect 1 cleaned up!')
})

modum.perform(() => {
  console.log('Side effect 2 performed')
  return () => console.log('Side effect 2 cleaned up!')
})

modum.destroyAll()

API

perform(effect)

Perform a side effect. You can optionally return a function from the effect that cleans up the effect. This ensures that these highly related functions are logically grouped in your code.

destroy(id)

Clean up a particular side effect that has been performed.

let modum = new Modum()

let id = modum.perform(() => {
  console.log('Side effect 1 performed')
  return () => console.log('Side effect 1 cleaned up!')
})

modum.destroy(id)

destroyAll()

Clean up all side effects that have been performed since the last time this function was called.

addEvent(el, eventType, handler, options)

Perform the specific effect of adding an event listener to an element. This event is then automatically removed when the side effect is destroyed.

| Argument name | Type | Description | | ------------- | ------------------------------------ | --------------------------------------------------------------------------------------- | | element | Element | The element to add the event listener to | | eventType | string | The type of event to add | | handler | EventListenerOrEventListenerObject | The event handler | | options | boolean \| EventListenerOptions | Optional. These options get passed directly through to the addEventListener function. |