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

papillon

v1.5.9

Published

Change detection library

Downloads

79

Readme

Papillon

Build Status Coverage Status

Papillon is a smart change detection library.

The library is using the fact, that changes of data in the context of the browser cannot be provided more often than the refresh rate of the browser.

Instead of watching every change, the library creates a frozen states of objects between repaints and provides the difference between these states.

Getting started

npm install papillon
import { Observer, State } from 'papillon';

NPM package contains UMD built version of the library in dist/ path.

State

State provides an singleton interface for storing state of object. Only last state and changes between this and the previous state are stored.

Singleton pattern ensures low memory usage and consistent states for objects that reference to themselves.

Module uses a state counter that increase only before next repaint. Checking state multiply times before counter is changed yields the same results.

Static methods

State.now()

let statestamp = State.now()

Returns the current value of the state counter.

Instance

Constructor

let state = new State({ one: 'two' });

Params:

  • Object target object

Properties

  • state.target - reference to object, which state is stored
  • state.lastCheck - value of state counter when target was checked
  • state.lastChange - value of state counter when target has changed
  • state.changelog - list of changes between previous and current state

state.changelog

{
 one: { type: 'set', oldKey: 'five' },
 two: { type: 'set', oldValue: 'some'},
 three: { type: 'delete', oldValue: 'before removed'},
 four: { type: 'modify', changelog: {...} }
}
  • set - Set reference or primitive value.
  • delete - Deleted property.
  • modify - Nested changes of Object property in changelog.
  • oldValue - Property value from last state.
  • oldKey - Property key from last state which value was moved to this property. Paremeter is set only when new value of old property has changed. In another words, it is set only for relocated properties, not copied.

state.isChanged()

Checks if target object has changed from last state. If it is true, method regenerates changelog.

Observer

This module connects changes of observed object properties with callback action. Callback method is called with changelog state property.

Observed property is redefined as getter/setter. Getting or setting that property will trigger request for state change detection before next repaint.

Constructor

let host = {test: 'one'};
let observer = new Observer(host, 'test', changelog => {
  console.dir(changelog.test);
});

// Will trigger log in console before next repaint
host.test = 'two';

Params:

  • Object host object
  • String | Array<String> properties - one or more properties
  • Function callback - takes changelog object as argument

observer.check()

Schedule change detection with window.requestAnimationFrame.

This method is called when the observed property is set or get. You do not have to use this method directly. If your code changes object by other reference then observed property, you have to schedule checking manually.

observer.destroy()

Cancel scheduled checking request and revert observed properties to original definition. If your code do not requires object with original definition of observed properties you do not have to call this method.

After destroying Observer instance, accessing properties will not trigger check() method.

Contribution

Feel free to contribute project. Fork repository, clone and run:

npm install && npm run develop

Write some code, provide proper tests and pull request to this repository.

License

Papillon is released under the MIT License.