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

jquery-small-pubsub

v0.2.0

Published

A really small pub/sub implementation for jQuery.

Downloads

160

Readme

jQuery Small Pub/Sub

A really small pub/sub implementation for jQuery.

This project is derived from Ben Alman's jQuery Tiny Pub/Sub and a solution by Jud Stephenson for getting the list of event listeners that were created in jQuery. The difference between "jQuery Tiny Pub/Sub" and this, is that you can now "filter" through some context that is passed to all subscribers matching an event. This is based on the way WordPress does it's "filter" hooks. Along with setting a priority for the order in which the subscribers are executed. Again, similar to how WordPress has a priority for its hooks. In other words, I built this to be a very small and lightweight JavaScript pub/sub that functioned similar to the way WordPress' action & filter hooks work.

Getting Started

Load JavaScript

Option #1: In the browser

<!-- In the browser -->
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script src="https://unpkg.com/jquery-small-pubsub/dist/pubsub.min.js"></script>

Option #2: In Node for a browser app

# Install from node package manager
npm i jquery-small-pubsub
/* In Node */
const isPubSubLoaded = require('jquery-small-pubsub');

Option #3: In Node for a Node app

# Install from node package manager
npm i jquery-small-pubsub jquery jsdom
/* In Node */
const jsdom = require('jsdom');
global.jQuery = require("jquery")((new jsdom.JSDOM('')).window);
const isPubSubLoaded = require('jquery-small-pubsub');

Examples

You can look at additional examples in the test folder

Basic Pub/Sub

$.subscribe('eventName', () => console.log('Hello World'));
$.subscribe('eventName', () => console.log('Another Listener'));

$.publish('eventName');
// console: 'Hello World'
// console: 'Another Listener'

$.publish('eventName');
// console: 'Hello World'
// console: 'Another Listener'

Pub/Sub Subscribe Once

$.subscribe('eventName', () => console.log('Executes every time'));
$.subscribeOnce('eventName', () => console.log('Executes only once'));

$.publish('eventName');
// console: 'Executes every time'
// console: 'Executes only once'

$.publish('eventName')
// console: 'Executes every time'

Pub/Sub Unsubscribe

$.subscribe('eventName', () => console.log('Hello World'));
const callback = () => console.log('Another Listener');
$.subscribe('eventName', callback);

$.publish('eventName');
// console: 'Hello World'
// console: 'Another Listener'

$.unsubscribe('eventName', callback);
$.publish('eventName');
// console: 'Hello World'

Pub/Sub Unsubscribe All

$.subscribe('eventName', () => console.log('Hello World'));
$.subscribe('eventName', () => console.log('Another Listener'));

$.publish('eventName');
// console: 'Hello World'
// console: 'Another Listener'

$.unsubscribeAll();

$.publish('eventName');
// nothing should get called

Pub/Sub w/ priority (order)

priority is the order the event handlers are executed. Default priority is 10. If you have three subscribers set to priorities of missing or undefined (becomes 10), 99 and -99, then the last subscriber is run first, because its priority is the lowest and the second subscriber runs last, because its priority is the highest. If all the subscribers have the same priority then the handlers execute in the order they were set.

$.subscribe('eventName', () => console.log('Default Priority is 10'));
$.subscribeOnce('eventName', 99, () => console.log('Priority 99, set once'));
$.subscribe('eventName', -99, () => console.log('Priority -99'));

$.publish('eventName');
// console: 'Priority -99'
// console: 'Default Priority is 10'
// console: 'Priority 99, set once'

$.publish('eventName');
// console: 'Priority -99'
// console: 'Default Priority is 10'

const callback = () => console.log('Unsubscribe w/ priority set');
$.subscribe('eventName', 100, () => callback);
$.unsubscribe('eventName', () => callback);
// $.unsubscribe uses jQuery's off() method, so the signature only includes the name and event handler

Pub/Sub w/ context

Context is passed to the event handler as the value of this. If you need to set the priority along with context, then provide a priority key to the context.

$.subscribe('eventName', () => console.log('context empty'));
$.subscribeOnce('eventName', {a: 1, priority: 1}, function () {
	console.log('a = ' + this.a);
});
$.subscribe('eventName', 'some string', function () {
	console.log(this)
});

$.publish('eventName');
// console: 'a = 1' // runs once
// console: 'context empty'
// console: 'some string'

$.publish('eventName');
// console: 'context empty'
// console: 'some string'

Pub/Sub as a WordPress filter hook

$.subscribe('eventName', {compare: 25, priority: 999}, function (isValid, someValue) {
	if (isValid) {
		return someValue > this.compare;
	}

	return false;
});
$.subscribe('eventName', (isValid, someValue) => isValid ? (someValue < 100) : false);
$.subscribeOnce('eventName', -999, (isValid, someValue) => isValid ? (someValue > 75) : false);

let isValid;
isValid = $.publish('eventName', true, 50);
// isValid === false
// 50 !> 75, so false is passed through all subsequent handlers
// NOTE: the first handler that executes (which was the last handler set, having a priority of -999) only runs once

isValid = $.publish('eventName', true, 50);
// isValid === true
// Because the last subscriber was set to subscribeOnce, it's handler doesn't get called this time
// thus 50 < 100 && 50 > 25, so true

isValid = $.publish('eventName', true, 1);
// isValid === false
// 1 < 100, but 1 !> 25, so false