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

cross-context-events

v1.0.0

Published

A lightweight event library for cross context messaging and events synchronization

Downloads

82

Readme

Cross Context Events

Cross context events is a robust, lightweight package providing the option to send and receive events across JS execution context.

You asked, what does this mean, exactly? Well, it means that when you emit an event in your script, the same event will also be emitted in all other linked execution contexts (i.e. other tabs, windows, processes), even on different computers if you want to! All you need to do is writing a few lines of code to link them together.

demo

Try the demo here       Source code for the demo

Documentations

See here.

Features

  • Lightweight
  • No runtime dependency

Comprehensive documentations

  • Containerization
    • Support for named and anonymous containers to provide isolation if you need
  • Unified interface
    • Works the same way whether you are using browser, service worker, node, or even electron.
  • Cross Context
    • Capable of sending events across execution context (e.g. from one tab to another tab in browser or from one process to another process in Node) with minimal setup, so long as an IPC channel can be established between the sending context and receiving context.
    • Relay support: events can be optionally relayed across the network if you have one.
  • Typescript support
    • Written completely in typescript completely with strong type inferences.
  • Comprehensive testing
    • All core functions are unittested
  • Namespaced events and bubbling
    • If you emit an event for event.context.new, then listeners for event.context and event are also notified (but event.context2 is not). This behavior can be disabled if desired. See event bubbling.
  • Event relaying
    • Even if you have a network of nested iframes like the image below, and you emit an event in iframe 9 (or any frame or the parent window for that matter), it will be emitted in all frames and the parent window. This also applies to a chain or child processes or workers or any other combinations of communication channels. See event relaying.

Installation

Unpkg


<script src="https://unpkg.com/cross-context-events/dist/cross-context-events.min.js"></script>

jsDelivr


<script src="https://cdn.jsdelivr.net/npm/cross-context-events/dist/cross-context-events.min.js"></script>

yarn

yarn add cross-context-events

npm

npm install cross-context-events

Getting started

This example uses the WebWorker API , but it can be easily switched to Node processes, iFrames, or others by simply changing the transport used. For details, please refer to transports.

// index.js

import {
    useGlobalTransport,
    createDefaultTransport,
    createEvent
} from "cross-context-events";

const OnlineEvent = createEvent("worker.online")
OnlineEvent.addListener(() => console.log("Worker is now online \(^▽^)/"))

let worker = new Worker("worker.js")

useGlobalTransport(createDefaultTransport({
    type: "worker",
    target: worker
}))
// worker.js

import {
    useGlobalTransport,
    createDefaultTransport,
    createEvent
} from "cross-context-events";

useGlobalTransport(createDefaultTransport({
    type: "worker",
    target: self
}))

const OnlineEvent = createEvent("worker.online")
new OnlineEvent().emit()

As you can see, Worker is now online \(^▽^)/ has been logged to the console from the main thread. Yay!

Please refer to the documentation for more information.