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

@ryanmorr/action-observer

v4.0.1

Published

Declarative event handling to capture unbound action events

Downloads

24

Readme

action-observer

Version Badge License Build Status

Declarative event handling to capture unbound action events

Description

Action observer is a solution for capturing user triggered action events (click, submit) as they bubble up to the document element. This is most useful as a means of maintaining responsiveness pre-initialization of JavaScript heavy apps, allowing action events to be handled immediately or queued for processing once the page has finished initializing. Please refer to the blog post to read more.

Install

Download the CJS, ESM, UMD versions or install via NPM:

npm install @ryanmorr/action-observer

Usage

First, you'll want to add the script to the <head> of your HTML. This is important because it must be loaded before the DOM has started to load and scripts have begun executing so that action events can be captured pre-initialization.

Next, add an action-observe attribute to any element you wish to observe events on. Adding the action-observe attribute to a form will automatically observe submit events for that form. Otherwise, click events will be observed for any other type of element. The value of the action-observe attribute is used as the reference to that element in the JavaScript API for action observer:

<!-- Observe click events -->
<a href="#" action-observe="add">Add Item</a>

<!-- Observe submit events -->
<form method="GET" action="#" action-observe="search">
    <input type="search" name="search" />
    <button type="submit">Submit</button>
</form>

There are two different ways to approach implementation in JavaScript. The first option is to define a callback function to be called for each action event dispatched from the target element, including events dispatched before the handler was defined, allowing you to handle actions immediately:

import { observe } from '@ryanmorr/action-observer';

// Observes the element(s) with the action-observe="add" attribute
observe('add', (event, element) => {
    // Handle the action immediately                  
});

The other option is to check if an action event was dispatched on a target element once your scripts have completed initializing to address the action afterwards:

import { getActions } from '@ryanmorr/action-observer';

// Returns an array of all events dispatched from the action-observe="add" attribute
const actions = getActions('add');

actions.forEach(({event, element}) => {
    // Handle each action after the fact (better late than never)    
});

Once your primary JavaScript has loaded, you can choose to stop observing specific elements or stop functionality entirely for all elements:

import { unobserve, disable } from '@ryanmorr/action-observer';

// Once the application code has initialized the link, stop observing
if (isInitialized('link')) {
    unobserve('link');
}

// Once all initializing code is complete, disable all functionality
if (isAppLoaded()){
    disable();
}

License

This project is dedicated to the public domain as described by the Unlicense.