New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

optimized-resize-observer

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optimized-resize-observer

ResizeObservers can be tricky to throttle and debounce. This package delivers both in a very easy to use format!

latest
Source
npmnpm
Version
0.0.2-7
Version published
Maintainers
1
Created
Source

optimized-resize-observer

Build Status

ResizeObservers can be tricky to throttle and debounce. This package delivers both in a very easy to use format!

debouncedResizeObserver

Debouncing is delaying the execution of a function until a long enough gap in time (the debounce time) has elapsed without any additional interactions.

Usage of the debouncedResizeObserver:

import { debouncedResizeObserver } from 'optimized-resize-observer';

const resizeObserver = debouncedResizeObserver({
    singleEntryCallback: ({ entry }) => console.log('ResizeObserverEntry', entry),
    debounceTimeInMs: 250
});

resizeObserver.observe(document.querySelector('#my-element-id'));

throttledResizeObserver

Throttling is limiting the number of times a function is executed for a given time interval (throttle time).

For the throttledResizeObserver we will execute on both the leading edge of the throttling window, and the trailing edge. In other words, the callback will be invoked immediately as well as at the end of the throttleTimeInMs.

There are a few reasons for this:

  • If we only invoke on the trailing edge (end of throttle window), then interactions can appear to lag behind
  • If we only invoke on the leading edge (immediately on first call), then we could miss some of the changes and have a broken layout

Invoking on both the leading and trailing edge solves both of these issues.

Usage of the throttledResizeObserver:

import { throttledResizeObserver } from 'optimized-resize-observer';

const resizeObserver = throttledResizeObserver({
    singleEntryCallback: ({ entry }) => console.log('ResizeObserverEntry', entry),
    throttleTimeInMs: 250
});

resizeObserver.observe(document.querySelector('#my-element-id'));

Testing

Until the ResizeObserver class is supported by jsdom, here's an option for testing:

beforeEach(() => {
    Object.defineProperty(global, "ResizeObserver", {
        writable: true,
        value: jest.fn().mockImplementation(() => ({
            observe: jest.fn(),
            unobserve: jest.fn(),
            disconnect: jest.fn(),
        })),
    });
});

Keywords

resizeobserver

FAQs

Package last updated on 24 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts