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

pedlar

v0.2.2

Published

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

Downloads

2

Readme

Pedlar

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

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.

Pedlar 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 side effect) is run immediately, and the function that you return from the side effect is not run until you tell Pedlar to destroy that side effect.

Additionally, there's also usually an "update" life cycle function (componentDidUpdate() / ngOnChanges()). Oftentimes identical or similar work takes place in the "update" and "init" functions. Pedlar also provides the ability to re-perform a particular effect at any time. It can even skip running the effect if dependencies that you specify have not changed!

Installation

# Install with yarn
yarn add pedlar

# Or install with npm
npm i pedlar

Examples

Simple Usage

import Pedlar from 'pedlar'

let pedlar = new Pedlar()

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

// LOG: 'Side effect 1 performed'

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

// LOG: 'Side effect 2 performed'

pedlar.destroyAll()

// LOG: 'Side effect 1 cleaned up!'
// LOG: 'Side effect 2 cleaned up!'

Add and automatically remove an event

import Pedlar from 'pedlar'

let pedlar = new Pedlar()
let el = document.getElementById('my-button')

// Adds the click event
pedlar.addEvent(el, 'click', () => console.log('My button was clicked'))

// ...

// Removes the click event, along with cleaning up all other side effects
pedlar.destroyAll()

Re-perform an event when dependencies have changed

import {Pedlar, PedlarEffect} from 'pedlar'

let pedlar = new Pedlar()
let consoleEffect: PedlarEffect

consoleEffect = pedlar.perform(() => {
  console.log('Side effect performed')
  return () => console.log('Side effect cleaned up!')
}, ['my-dependency'])

// LOG: 'Side effect performed'
// You can avoid this initial run of the side effect by
// calling `pedlar.create()` instead of `pedlar.perform()`

consoleEffect.perform(['my-dependency'])
// Effect is not performed again, dependencies have not changed

consoleEffect.perform(['my-changed-dependency'])

// LOG: 'Side effect cleaned up!'
// LOG: 'Side effect performed'

Types

PedlarEffect

A Pedlar Effect is the object returned from either Pedlar.perform() or Pedlar.create(). It has two properties:

  • id - string - The ID of this effect. This ID can be passed to Pedlar.destroy() to individually destroy this effect.
  • perform - (currentDependencies?) => void - Run the side effect. If dependencies are passed, the side effect will only be run if the dependencies have changed. If this is not the first time the side effect is being run, the destroyer (if one exists) will be executed before the side effect is re-run.

PedlarSideEffect

The side effect that you wish to run. This is a function that either returns nothing, or another function that cleans up the side effect.

API

create(sideEffect: PedlarSideEffect): PedlarEffect

Create a PedlarEffect without initially running the side effect. You can optionally return a function from the sideEffect that cleans it up. The side effect can be run at any time by invoking PedlarEffect.perform().

perform(sideEffect: PedlarSideEffect, dependencies?: any[]): PedlarEffect

Perform a side effect. You can optionally return a function from the sideEffect that cleans it up. The side effect can be re-run at any time by invoking PedlarEffect.perform().

This is very similar to the Pedlar.create() function, except that this also runs the side effect immediately.

destroy(id)

Clean up a particular side effect that has been performed.

import Pedlar from 'pedlar'

let pedlar = new Pedlar()

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

pedlar.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 side 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 | keyof HTMLElementEventMap (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. |

addCustomEvent(el, eventType, handler, options)

Identical to addEvent() except that the eventType argument accepts any string