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

extension-event-bus

v0.5.0

Published

Event bus for browser extensions

Downloads

18

Readme

browser-extension-event-bus

Description

Why this library was created?

After Google announced Manifest V3 for browser extensions, then we as developers could not use persistent background page anymore. What worse we have to use Service worker, which could be inactive from time to time. So simple in-memory event bus implementation is not enough.

TL;DR:

This tool allows (again) to decouple business logic hidden in orthogonal modules written for MV3 extensions.

How it works?

Browser extension event bus is a cross-browser library that is utilizing browser local storage in order to buffer and dispatch all events to multiple subscribers if needed.

Events are persisted as soon as possible, then all subscribers receive them.

Features

  • Sending events
  • Subscribing for events (from many places if needed)
  • Buffering events (local storage implementation)
  • Cross context communication *

Technical side of things

Installation

NPM - use command presented below

npm i extension-event-bus

YARN - use command presented below

yarn add extension-event-bus

Dependencies

For now library is using only:

  • Typescript
  • webextension-polyfill

API documentation

Sending an event

In order to send event you need to use method presented below.

async send(topic: string, data: any): Promise<boolean>

We simply pass topic name and data we want to include in event.

Subscribing or receiving an event

In order to subscribe to an event we need to use method shown below.

subscribe(topic: string, f: Function): Promise<void>

Again, we are passing topic and handler function that would take data that were sent as an argument.

Usage example

import { EventBusFactory } from 'extension-event-bus';
import { IEventBus } from 'extension-event-bus/dist/cjs/iEventBus';

// Define custom handler
const multiply = (n: number) => {
  const result = n * n;
  console.log(result);
}

const browser = null; // import it from webextension-polyfill library
const config = Config.default();

// Create an event bus by using factory class
const eventBus: IEventBus = EventBusFactory.getEventBus(browser, config);

// Define all subsribents
await eventBus.subscribe('numbers', console.log);
await eventBus.subscribe('numbers', multiply);

// Send event
await eventBus.send('numbers',10);

// Result in the console should be like ...
// 10
// 100

Other things

Contribution

Library is still on development stage. So please create an issues if you have some ideas or problems.

Unit tests

If you want implement some new features or test something locally you can use Jest framework to do it. Take a look at __tests__ directory. In order to run all tests please execute npm run test command.

Known issues and limitations

As you know we could distinguish a couple of sub-packages in browser extension:

  • service worker
  • content scripts
  • popup
  • devtools

We need to initialize our event bus in each context itself. Because an event bus is utilizing local storage (which is shared between contexts) we could send events between these contexts.

WARN: THIS FEATURE IS IN TEST STAGE

TODOs

  • Reviewing events from given topic
  • Supporting cross context communication
  • Implement in-memory version if needed
  • Implement preloading events from storage if needed
  • Define event as a type
  • Add prop that defines trusted level
  • and more :)

Licensing

MIT - Check LICENSE file.

ONE MORE WARN

It's implementation of beta version. So please watch out while using it. All insights, tips and bug reports are welcome!

Changelog

Check CHANGELOG.md file for more details.

Hall of Fame