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

wormhole.js

v0.10.1

Published

Wormhole - is EventEmitter for communication between tabs.

Downloads

44

Readme

Wormhole

Is EventEmitter for communication between tabs.

npm i --save-dev wormhole.js

Features


Basic example

// All tabs
wormhole().on('coords', function (x, y) {
	console.log(x, y);
});

// Some tab
wormhole().emit('coords', [5, 10]);

// Master tab
if (wormhole().master) {
	// ..
}

wormhole().on('master', function () {
	console.log('Wow!');
});

One connection on WebSocket for all tabs

Module wormhole.js/ws implements WebSocket-like interface: https://rubaxa.github.io/wormhole/?ws=y

import WS from 'wormhole.js/ws';

// Create WebScoket (wormhole-socket)
const socket = new WS('ws://echo.websocket.org'); // OR new WS('...', null, hole);

socket.onopen = () => console.log('Connected');
socket.onmessage = ({data}) => console.log('Received:', data);

// Unique event
socket.onmaster = () => {
	console.log('Yes, I\'m master!');
};

// Some tab
socket.send({foo: 'bar'})

// All tabs:
//  "Received:" {foo: 'bar'}

CORS example

  1. Create a subdomain, ex.: http://wormhole.youdomain.com/;
  2. Copy-paste universal.html into root;
  3. Check access http://wormhole.youdomain.com/universal.html;
  4. Profit.
// http://foo.youdomain.com/
var hole = new wormhole.Universal('http://wormhole.youdomain.com/universal.html');

hole.on('data', function (data) {
	console.log(data);
});


// http://bar.youdomain.com/
var hole = new wormhole.Universal('http://wormhole.youdomain.com/universal.html');

hole.emit('data', 'any data');

Master/slave example

// All tabs
(function ($) {
	var _cache = {},
		_getCacheKey = function (req) {
			return req.url + JSON.stringify(req.data);
		}
	;


	// Define remote command (master)
	wormhole()['get-data'] = function (req, callback) {
		var key = _getCacheKey(req),
			promise = _cache[key];

		if (!promise) {
			_cache[key] = promise = $.get(req.url, req.data);
		}

		return promise
			.done(function (result) {
				callback(null, result);
			})
			.fail(function (err) {
				delete _cache[key];
				callback(err);
			})
		;
	};


	// Get remote data
	$.getData = function (url, data) {
		var dfd = $.Deferred();

		// Calling command on master (from slave... or the master, is not important)
		wormhole().call('get-data', { url: url, data: data }, function (err, data) {
			if (err) {
				dfd.reject(err);
			} else {
				dfd.resolve(data);
			}
		});

		return dfd.promise();
	};


	// I'm master!
	wormhole().on('master', function () {
		// some code
	});
})(jQuery);



// Tab #X
$.getData('/path/to/api').then(function (result) {
	// Send ajax request
	console.log(result);
});


// Tab #Y
$.getData('/path/to/api').then(function (result) {
	// From master cache
	console.log(result);
});

Peers

wormhole()
	.on('peers', function (peers) {
		console.log('ids:', peers); // ['tab-id-1', 'tab-id-2', ..]
	})
	.on('peers:add', function (id) {
		// ..
	})
	.on('peers:remove', function (id) {
		// ..
	})
;

Executing the command on master

// Register command (all tabs)
wormhole()['foo'] = function (data, next) {
	// bla-bla-bla
	next(null, data.reverse()); // or `next('error')`
};


// Calling the command (some tab)
wormhole().call('foo', [1, 2, 3], function (err, results) {
	console.log(results); // [3, 2, 1]
})

Modules

wormhole.Emitter

Micro event emitter.

  • on(type:String, fn:Function):this
  • off(type:String, fn:Function):this
  • emit(type:String[, args:*|Array]):this
var obj = wormhole.Emitter.apply({}); // or new wormhole.Emitter();

obj.on('foo', function () {
  console.log(arguments);
});


obj.emit('foo'); // []
obj.emit('foo', 1); // [1]
obj.emit('foo', [1, 2, 3]); // [1, 2, 3]

wormhole.cors

Wrapper for postMessage.

// Main-frame
wormhole.cors.on('data', function (data) {
	// ...
});

wormhole.cors['some:command'] = function (value) {
	return value * 2;
};

// IFrame
wormhole.cors(parent).send({foo: 'bar'});
wormhole.cors(parent).call('some:command', 3, function (err, result) {
	console.log(result);
});

wormhole.store

Interface for localStorage.

  • get(key:String):*
  • set(key:String, value:*)
  • remove(key:String):*
  • on(type:String, fn:Function)
  • off(type:String, fn:Function)
wormhole.store.on('change', function (key, data) {
	console.log('change -> ', key, data);
});

wormhole.store.on('change:prop', function (key, value) {
	console.log('change:prop -> ', key, value);
});

wormhole.store.set('foo', {bar: 'baz'});
// change -> foo {bar: 'baz'}

wormhole.store.set('prop', {qux: 'ok'});
// change -> prop {qux: 'ok'}
// change:prop -> prop {qux: 'ok'}

Utils

wormhole.uuid():String

A universally unique identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE) (c) wiki.


wormhole.debounce(fn:Function, delay:Number[, immediate:Boolean]):Function

Development

  • npm test
  • npm run dev — run dev watcher
  • npm run build