Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 Commits

Repository files navigation

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.

classResource{constructor(name){this.name=name}deinit(){console.info('deiniting:',this.name)}}classBlackBox{constructor(inner){this.inner=inner}}constprev={root: newResource('Sirius'),dict: {inner: newResource('Arcturus'),},list: [newResource('Rigel')],// Sun is untouchable to deinitDiff because it's wrapped// into a non-plain object that doesn't implement isDeinitableblackBox: newBlackBox(newResource('Sun'))}constnext={root: prev.root,dict: {inner: newResource('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.

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

isDeinitable(value)

True if value has a .deinit method.

isDeinitable({deinit(){}})// trueisDeinitable({})// 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'classPtr{constructor(value){this.$=value}swap(fun){constprev=arguments[0]=this.$deinitDiff(prev,this.$=fun.apply(undefined,arguments))}reset(next){deinitDiff(this.$,this.$=next)}deinit(){deinitDiff(this.$,undefined)}}

Usage:

constenv=newPtr({})constxhr=newXMLHttpRequest()constrequest={xhr,deinit(){xhr.abort()}}env.swap(state=>({...state, request}))env.$// {request: {xhr, deinit}}// Removing the request also aborts itenv.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

About

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

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages