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

@brycemarshall/event-throttle

v1.0.3

Published

A helper class that throttles events streaming from a specific event source.

Downloads

7

Readme

@brycemarshall/event-throttle

A helper class that throttles events streaming from a specific event source.

EventThrottle can be used to decrease the "density" of events from any upstream event source and therefore reduce the processing burden on attached downstream event handlers, OR to guanrantee that a downstream event is raised to handle ONLY the LAST in each sequence of upstream source events.

A sequence is defined as any series of events with no more than a throttle duration interval between each sequential event.

Conceptually, EventThrottle can be be loosely considered to "slow down" events from an upstream event source, although in practise it actually ensures that downstream events are dispatched to EventThrottle clients with a maximum frequency (the throttle duration, specified in milliseconds). It does this by dispatching only one upstream event per throttle duration period whilst also guaranteeing that the last event in each upstream sequence will ALWAYS be dispatched to the downstream handler.

Demo

http://plnkr.co/nC6ter

Installation

npm install @brycemarshall/event-throttle

##The module exports the following types:

/**
 * The callback function that is invoked by an EventThrottle instance to dispatch a throttled upstream event to a downstream handler.
 * @param sender The EventThrottle that dispatched the event.
 * @param sourceEvent The source  event.
 * @param state Optional state to be passed to the downstream event handler.
 */
export declare type EventThrottleCallbackFunction = (sender: EventThrottle, sourceEvent?: any, state?: any) => void;
/**
 * Specifies EventThrottle configuration options.
 */
export interface EventThrottleOptions {
    /**
     * When specified, defines the duration (in milliseconds) of the minimum delay in between processing downstream events (the default is 150 milliseconds).
     */
    throttleDuration?: number;
    /**
     * Specifies EventThrottle configuration options.
     * If true, results in only the last of a sequence of upstream (source) events being fired.
     * In practise, this means that the EventThrottle instance wil only dispatch a throttled event if no additional upstream events
     * were raised after the active event was queued for processing.
     */
    suppressActive?: boolean;
}
/**
 * A helper class that throttles events streaming from a specific event source.
 * EventThrottle can be used to decrease the "density" of events from any upstream event source and therefore reduce the processing burden on attached downstream event handlers,
 * OR to guanrantee that a downstream event is raised to handle ONLY the LAST in each sequence of upstream source events.
 *
 * A sequence is defined as any series of events with no more than a throttle duration interval between each sequential event.
 *
 * Remarks:
 *
 * Conceptually, EventThrottle can be be loosely considered to "slow down" events from an upstream event source, although in practise it actually ensures that downstream events are
 * dispatched to EventThrottle clients with a maximum frequency (the throttle duration, specified in milliseconds). It does this by dispatching only one upstream event
 * per throttle duration period whilst also guaranteeing that the last event in each upstream sequence will ALWAYS be dispatched to the downstream handler.
 */
export declare class EventThrottle {
    /** @internal */
    private _fn;
    /** @internal */
    private _throtDur;
    /** @internal */
    private _suppressActive;
    /** @internal */
    private _backlog;
    /** @internal */
    private _enabled;
    /** @internal */
    private _last;
    /** @internal */
    private _lastState;
    /**
     * Creates a new instance of the EventThrottle class.
     * @param callbackFunction - the function for handling throttled downstream events.
     * @param options - Optional configuration values in the form of an object implementing EventThrottleOptions.
     */
    constructor(callbackFunction: EventThrottleCallbackFunction, options?: EventThrottleOptions);
    /**
     * Gets the number of source events that have been suppressed since the last downstream event was dispatched by this instance.
     */
    readonly throttled: number;
    /**
     * Returns true if 1 or more source events have been suppressed since the last downstream event was dispatched by this instance.
     */
    readonly isThrottling: boolean;
    /**
     * Gets or sets the enabled state of the EventThrottle instance. Setting enabled to 'false' will automatically flush the EventThrottle.
     */
    enabled: boolean;
    /**
     * Flushes any suppressed source events that have not yet been processed.
     */
    flush(): void;
    /**
     * Registers an upstream source event to be potentially queued for downstream processing.
     * @param sourceEvent - The source Event.
     * @param state - Optional state to be passed to the downstream event handler.
     */
    registerEvent(e?: any, state?: any): void;
    /** @internal */
    private queueEvent(e, state);
    /** @internal */
    private processEvent(backlog, e, state);
}

Usage - General

EventThrottle instances are created by invoking the EventThrottle constructor and passing (at a minimum) a callback function to be invoked in response to scroll events. Further configuration is possible by passing an object implementing EventThrottleOptions.

Usage - Default Configuration

An example of throttling an upstream event source using the default configuration.

class EventThrottleDefault
{
    private throttle: EventThrottle;

    constructor() {
        this.throttle = new EventThrottle((s, e) => { this.onDownstreamEvent(s, e) });
        document.getElementById("sourceElement").addEventListener("keydown", (e) => { this.throttle.registerEvent(e); });
    }
    
    onDownstreamEvent (sender: EventThrottleOptions, sourceEvent: Event) {
        console.log("Downstream event fired.");
    }
}

Usage - Passing State

An example of passing state to the downstream event handler.

class EventThrottleState
{
    private throttle: EventThrottle;
    private _state = "My State";

    constructor() {
        this.throttle = new EventThrottle((source, evt, state) => { this.onDownstreamEvent(source, evt, state) });
        document.getElementById("sourceElement").addEventListener("keydown", (evt) => { this.throttle.registerEvent(evt, this._state); });
    }
    
    onDownstreamEvent (sender: EventThrottleOptions, sourceEvent: Event, state: string) {
        console.log("Downstream event fired - state = '" + state + "'");
    }
}

Usage - Custom Throttle Duration

An example of throttling an upstream event source using a custom throttle duration of 300ms.

class EventThrottleCustomDuration
{
    private throttle: EventThrottle;

    constructor() {
        this.throttle = new EventThrottle(target, (s, e) => { this.onDownstreamEvent(s, e) }, { throttleDuration: 300 });
        document.getElementById("sourceElement").addEventListener("keydown", (e) => { this.throttle.registerEvent(e); });
    }
    
    onDownstreamEvent (sender: EventThrottleOptions, sourceEvent: Event) {
        console.log("Downstream event fired.");
    }
}

Usage - Suppress Active (Last Event in Sequence Only)

An example of throttling an upstream event source to ensure that ONLY the last event in any upstream sequence is dispatched to the downstream handler. In this example, a sequence is defined as any series of events with no more than a 1500ms interval between sequential events.

class EventThrottleSupressActive
{
    private throttle: EventThrottle;

    constructor() {
        this.throttle = new EventThrottle(target, (s, e) => { this.onDownstreamEvent(s, e) }, { throttleDuration: 1500, suppressActive: true });
        document.getElementById("sourceElement").addEventListener("keydown", (e) => { this.throttle.registerEvent(e); });
    }
    
    onDownstreamEvent (sender: EventThrottleOptions, sourceEvent: Event) {
        console.log("Downstream event fired.");
    }
}

Contributors

  • Bryce Marshall

MIT Licenced