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

propserver

v0.1.0

Published

Responsive property observation for JavaScript

Downloads

152

Readme

Responsive property observation for JavaScript

propserver Travis

What is it good for

Sometimes you want to be notified on each frame about a property change so you can handle things in a more responsive way.
Consider when you have an element on a page, and you want to check its offsetTop property so you know when the element is inside/outside the viewport.
You can observe the element offetTop with propserver and be notified when it changes.

This module should be used on the client side

Install

Install via npm with

$ npm install --save propserver

What makes it responsive

As you know, calling requestAnimationFrame to request an animation frame help us to optimize operations and queries on the DOM, since the browser waits for the next frame to render in order to run our callbacks. However, when requesting many animation frames synchronously, the browser will stack all the repeated request animation frames.
When you creates more than one observer, all animation frames are managed in a timeline so only one requestAnimationFrame gets called.

Creating property observer

import { createObserver } from "propserver";

// get notified when $anything.offsetTop is changes
function callback(value, prevValue) {
    console.log(value, prevValue);
}

// create property ovserver
const observer = createObserver($anything, "offsetTop", callback);

// start observing for $anything.offsetTop changes
observer.observe();

// disconnect the observer when you finish
observer.disconnect();

API

createObserver(target, property, callback)

Factory function to create property observer.

| Name | Type | Required | Description | | - | - | - | - | | target | Object | true | the target that holds the property | | property | String | true | the property to observe on the target | | callback | Function | true | a callback function to be called when the target property changes function (newValue, oldValue) {} |

createObserver(propertyGetter, callback)

| Name | Type | Required | Description | | - | - | - | - | | property getter | Function | true | a function to get the property | | callback | Function | true | a callback function to be called when the property changes function (newValue, oldValue) {} |

The second option to create observer with property getter function. this method is for cases where you want to run a function to get the value, like where the property is not a direct property of your target.
Here is a property getter example:

() => {
    const { top } = target.getBoundingClientRect();
    return top;
}

License

This project is licensed under the MIT License - see the LICENSE file for details