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

@alshdavid/reactive

v1.2.8

Published

Generic object mutation observation

Downloads

39

Readme

This project introduces a tiny, unopinionated implementation that deeply watches JavaScript objects and arrays for mutations, additions, deletions.

import Reactive from '@alshdavid/reactive';

const foo = Reactive.create({
  values = []
})

Reactive.observe(foo).subscribe(() => console.log('Updated!'))

// Will trigger the callback
foo.values.push(0)
foo.values[0]++

Install

# npm
npm install --save @alshdavid/reactive

# yarn
yarn add @alshdavid/reactive

Usage

Object Instances

Reactive will produce an observable proxy of an object instance, watching deeply for changes.

import Reactive from '@alshdavid/reactive';

const foo = Reactive.create({
  values = []
})

Reactive.observe(foo).subscribe(() => console.log('Updated!'))

// Will trigger the callback
foo.values.push(0)
foo.values[0]++

Patch Classes

Reactive can patch JavaScript class constructors so that the resulting instance will be deeply observable for changes.

import Reactive from '@alshdavid/reactive';

class Foo {
  values = []
}

const $Foo = Reactive.create(Foo)
const foo = new $Foo()

Reactive.observe(foo).subscribe(() => console.log('Updated!'))

// Will trigger the callback
foo.values.push(0)
foo.values[0]++

Deep initialization

Reactive will automatically traverse an object/array tree and initialize the properties as observable.

import Reactive from '@alshdavid/reactive';

class Foo {
  title = 'Hello World'
}

class Bar {
  foo

  constructor(
    foo
  ) {
    this.foo = foo
  }
}

const $Bar = Reactive.create(Bar)

const foo = new Foo()
const bar = new $Bar(foo)

Reactive.observe(bar).subscribe(() => console.log('Updated!'))
Reactive.observe(bar.foo).subscribe(() => console.log('Updated!'))

bar.foo.title = 'updated'

In the above case, foo is not observable directly and must be observed through bar via Reactive.observe(bar.foo). If a property has been set as observable externally, then it will attach to it's existing mutation watchers.

import Reactive from '@alshdavid/reactive';

class Foo {
  title = 'Hello World'
}

class Bar {
  foo

  constructor(
    foo
  ) {
    this.foo = foo
  }
}

const $Foo = Reactive.create(Foo)
const $Bar = Reactive.create(Bar)

const foo = new $Foo()
const bar = new $Bar(foo)

Reactive.observe(bar).subscribe(() => console.log('Updated!'))
Reactive.observe(foo).subscribe(() => console.log('Updated!'))

// Will trigger the observers for both "foo" and "bar"
foo.title = 'updated'

Ignore Specified Instances

In some cases, you will want to avoid setting up on certain objects or types, for this reason use Reactive.ignore, or Reactive.ignoreInstanceOf

import Reactive from '@alshdavid/reactive';

Reactive.ignore(window)
Reactive.ignoreInstanceOf(HTMLElement)

const foo = Reactive.create({
  _window: window,
  element: document.getElementById('app'),
  value: 0,
})

Reactive.observe(foo).subscribe(() => console.log('Updated!'))

// Won't trigger the callback
window.custom = 'update 1'
foo._window.custom = 'update 2'
foo.element.name = 'something'

// Will trigger the callback
foo.value = 1

React / Preact

This library provides hooks for React and Preact to use the class observation implementation to create a view model for React or Preact.

import React from 'react'
import VM from '@alshdavid/reactive/react'

export class AppComponent {
  value

  constructor(
    initialValue = 0,
  ) {
    this.value = initialValue
  }

  inc() {
    this.value++
  }

  dec() {
    this.value--
  }
}

export const App = () => {
  const vm = VM(AppComponent, [10])
  return <div>
    <button onClick={() => vm.inc()}>+</button>
    <button onClick={() => vm.dec()}>-</button>
    <h1>{vm.value}</h1>
  </div>
} 

Testing

This component's view model contains state and methods that describes its behavior entirely. Testing the presentation of the component is not required to ensure the behaviors are correct.

The tests would look like:

import { AppComponent } from './app'

describe('AppComponent', () => {
  describe('constructor', () => {
    it('Should not throw', () => {
      const testFunc = () => new AppComponent()
      expect(testFunc).not.toThrow()
    })
  })
  
  describe('inc', () => {
    it('Should increment the value', () => {
      const appComponent = new AppComponent()
      appComponent.inc()
      expect(appComponent.value).toBe(1)
    })
  })
  
  describe('dec', () => {
    it('Should decrement the value', () => {
      const appComponent = new AppComponent()
      appComponent.dec()
      expect(appComponent.value).toBe(-1)
    })
  })
})

These tests verify that the component's logic is sound. Additional tests would be required to ensure the methods are connected to the correct buttons and the values are rendered into the correct elements.