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

side-effect-manager

v1.2.2

Published

A tiny library to encapsulate side effects in a compact, reusable and testable style.

Downloads

11,260

Readme

side-effect-manager

Build Status npm-version Coverage Status minified-size tree-shakable

Commitizen friendly Conventional Commits code style: prettier

A tiny library to encapsulate side effects in a compact, reusable, testable and TypeScript-friendly style.

Install

npm add side-effect-manager

Why

Conventionally we write side effects like this:

class MyClass {
  constructor() {
    this.handleResize = () => {
      console.log("resize");
    };
    window.addEventListener("resize", this.handleResize);
  }

  destroy() {
    // cleanup side effects
    window.removeEventListener("resize", this.handleResize);
  }
}

This code style is scattered and hard-to-follow. The side effect handler has to be exposed to this which leaves us many unwanted and uncompressible properties.

With side-effect-manager we may write the same logic like this instead:

import { SideEffectManager } from "side-effect-manager";

class MyClass {
  constructor() {
    this.sideEffect = new SideEffectManager();

    this.sideEffect.add(() => {
      const handleResize = () => {
        console.log("resize");
      };
      window.addEventListener("resize", handleResize);
      return () => window.removeEventListener("resize", handleResize);
    });
  }

  destroy() {
    this.sideEffect.flushAll();
  }
}

Or simply like this:

this.sideEffect.addEventListener(window, "resize", () => {
  console.log("resize");
});

Not only the code is more compact and readable, variables can now be compressed as they are not properties.

Also the typing of listener can now be inferred from method so it is also more TypeScript friendly without the need to declare listener type specifically.

Usage

Add a side effect:

sideEffect.add(() => {
  const subscription = observable$.subscribe(value => {
    console.log(value);
  });
  return () => subscription.unsubscribe();
});

If the side effect returns a disposer function directly you can also:

import Emittery from "emittery";
import { Remitter } from "remitter";

const emittery = new Emittery();
const remitter = new Remitter();

sideEffect.addDisposer(
  remitter.on("event1", eventData => console.log(eventData))
);

// Or an array of disposers
sideEffect.addDisposer([
  remitter.on("event1", eventData => console.log(eventData)),
  remitter.on("event2", eventData => console.log(eventData)),
  emittery.on("event3", eventData => console.log(eventData)),
]);

There are also sugars for addEventListener, setTimeout and setInterval.

sideEffect.setTimeout(() => {
  console.log("timeout");
}, 2000);

Adding a side effect returns a disposerID which can be used to remove or flush a side effect.

const disposerID = sideEffect.addEventListener(window, "resize", () => {
  console.log("resize");
});

// Remove the side effect without running the disposer callback
sideEffect.remove(disposerID);

// Remove the side effect then run the disposer callback
sideEffect.flush(disposerID);

A disposerID can also be set deliberately. Side effects with the same ID will be flushed before adding a new one.

function debounce(handler, timeout) {
  sideEffect.setTimeout(handler, timeout, "my-timeout");
}

Async Side Effects

Similar to SideEffectManager, AsyncSideEffectManager can also handle async side effect cleanup nicely.

import { AsyncSideEffectManager } from "side-effect-manager";

const asyncSideEffect = new AsyncSideEffectManager();

asyncSideEffect.add(async () => {
  // async side effect

  return async () => {
    // async cleanup
  };
});

Yon can add or flush side effects with the same ID repeatably. AsyncSideEffectManager will correctly schedule tasks and skip unnecessary tasks automatically.

const disposerID = asyncSideEffect.add(async () => {
  // async side effect

  return async () => {
    // async cleanup
  };
});

// Add side effect with same ID
asyncSideEffect.add(async () => {
  // async side effect

  return async () => {
    // async cleanup
  };
}, disposerID);

asyncSideEffect.flush(disposerID);

You can always await asyncSideEffect.finished which will be updated and resolved after all tasks are finished.

await asyncSideEffect.finished;

Helpers

  • joinDisposers Join multiple disposers into on disposer
  • joinAsyncDisposers Join multiple disposers into on disposer and wait until all disposers are resolved.