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

definit

v0.1.1

Published

'Diff and deinit'. Compares data structures, calling destructors on 'removed' objects.

Downloads

5

Readme

Overview

Diff and deinit to definitely clean up resources. Compares two data structures and calls a destructor method on every inner object that's been "removed".

A small subset of Espo, extracted as a separate library.

Useful for automatic cleanup when you store stuff in "immutable" structures, creating new versions instead of mutating them. Works well with ES2018 object spread or Emerge.

Why

Automatic resource cleanup: store and remove objects without worrying about forgetting to call destructors.

Installation

npm i -E definit

Usage

deinitDiff(prev, next)

Diffs prev and next, deiniting any objects that implement isDeinitable (see below) and exist in prev but not in next. The diff algorithm recursively traverses plain data structures, but stops at non-plain objects, allowing you to safely include objects of arbitrary size and structure.

Definition of “plain data”:

  • primitive: number, string, boolean, symbol, null, undefined
  • {} or Object.create(null)
  • array

Everything else is considered non-data and is not traversed.

Resilient to exceptions: if a deiniter or a property accessor produces an exception, deinitDiff will still traverse the rest of the tree, delaying exceptions until the end.

Avoids cyclic references.

class Resource {
  constructor (name) {this.name = name}
  deinit () {console.info('deiniting:', this.name)}
}

class BlackBox {
  constructor (inner) {this.inner = inner}
}

const prev = {
  root: new Resource('Sirius'),
  dict: {
    inner: new Resource('Arcturus'),
  },
  list: [new Resource('Rigel')],
  // Sun is untouchable to deinitDiff because it's wrapped
  // into a non-plain object that doesn't implement isDeinitable
  blackBox: new BlackBox(new Resource('Sun'))
}

const next = {
  root: prev.root,
  dict: {
    inner: new Resource('Bellatrix')
  },
  list: null,
}

deinitDiff(prev, next)

// 'deiniting: Arcturus'
// 'deiniting: Rigel'

deinitDiff(next, null)

// 'deiniting: Sirius'
// 'deiniting: Bellatrix'

deinitDeep(value)

Same as deinitDiff(value, undefined). Deeply deinits the entire outgoing value.

const tree = {
  one: {deinit() {console.info('cleanup!')}},
  two: {deinit() {console.info('cleanup!')}},
}

deinitDeep(tree)

isDeinitable(value)

True if value has a .deinit method.

isDeinitable({deinit() {}})
// true

isDeinitable({})
// false

State Container

definit is useful for adding automatic resource cleanup to a state container: a popular pattern for storing immutable data on a mutable reference. Redux is a popular example, but you don't actually need a library for this.

This state container is copied from my production apps:

import {deinitDiff} from 'definit'

class Ptr {
  constructor(value) {
    this.$ = value
  }

  swap(fun) {
    const prev = arguments[0] = this.$
    deinitDiff(prev, this.$ = fun.apply(undefined, arguments))
  }

  reset(next) {
    deinitDiff(this.$, this.$ = next)
  }

  deinit() {
    deinitDiff(this.$, undefined)
  }
}

Usage:

const env = new Ptr({})

const xhr = new XMLHttpRequest()
const request = {xhr, deinit() {xhr.abort()}}

env.swap(state => ({...state, request}))

env.$
// {request: {xhr, deinit}}

// Removing the request also aborts it
env.swap(state => ({...state, request: undefined}))

env.$
// {request: undefined}

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts