All Projects β†’ robinpokorny β†’ dead-simple

robinpokorny / dead-simple

Licence: MIT license
πŸ’€πŸ’‘ Dead simple PubSub and EventEmitter in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to dead-simple

pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (+138.1%)
Mutual labels:  events, pubsub, publish, subscribe
angular-PubSub
Angular 1.x implementation of the Publish–Subscribe pattern.
Stars: ✭ 32 (+52.38%)
Mutual labels:  events, pubsub, publish, subscribe
tiny-typed-emitter
Fully type-checked NodeJS EventEmitter
Stars: ✭ 96 (+357.14%)
Mutual labels:  events, event, eventemitter, eventemitter3
fastapi websocket pubsub
A fast and durable Pub/Sub channel over Websockets. FastAPI + WebSockets + PubSub == ⚑ πŸ’ͺ ❀️
Stars: ✭ 255 (+1114.29%)
Mutual labels:  pubsub, publish, subscribe
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-28.57%)
Mutual labels:  events, event, pubsub
Mitt
πŸ₯Š Tiny 200 byte functional event emitter / pubsub.
Stars: ✭ 6,945 (+32971.43%)
Mutual labels:  event, pubsub, eventemitter
Twitchlib
C# Twitch Chat, Whisper, API and PubSub Library. Allows for chatting, whispering, stream event subscription and channel/account modification. Supports .NET Core 2.0
Stars: ✭ 519 (+2371.43%)
Mutual labels:  events, pubsub
Opendataday
Open Data Day website
Stars: ✭ 70 (+233.33%)
Mutual labels:  events, event
Samples
Community driven repository for Dapr samples
Stars: ✭ 104 (+395.24%)
Mutual labels:  events, pubsub
Rocketman
πŸš€ Rocketman help build event-based/pub-sub code in Ruby
Stars: ✭ 139 (+561.9%)
Mutual labels:  events, pubsub
Go Sdk
Dapr SDK for go
Stars: ✭ 149 (+609.52%)
Mutual labels:  events, pubsub
Noel
A universal, human-centric, replayable javascript event emitter.
Stars: ✭ 158 (+652.38%)
Mutual labels:  events, event
Pg Listen
πŸ“‘ PostgreSQL LISTEN & NOTIFY for node.js that finally works.
Stars: ✭ 348 (+1557.14%)
Mutual labels:  events, pubsub
Eventemitter
Evented JavaScript for the browser
Stars: ✭ 3,107 (+14695.24%)
Mutual labels:  events, eventemitter
Ease
It's magic.
Stars: ✭ 1,213 (+5676.19%)
Mutual labels:  events, event
Wisper
A micro library providing Ruby objects with Publish-Subscribe capabilities
Stars: ✭ 3,014 (+14252.38%)
Mutual labels:  events, pubsub
watermill-http
HTTP Pub/Sub for the Watermill project.
Stars: ✭ 16 (-23.81%)
Mutual labels:  events, pubsub
micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (+85.71%)
Mutual labels:  events, pubsub
trainmanjs
TrainmanJS - Cross-Origin Communication Library
Stars: ✭ 16 (-23.81%)
Mutual labels:  events, eventemitter
events
Tiny type-safe event emitter
Stars: ✭ 25 (+19.05%)
Mutual labels:  events, eventemitter

πŸ’€πŸ’‘ Dead simple PubSub and EventEmitter

Small, readable, almost-tweetable modules utilising classic patterns in modern language.

GitHub Workflow Status license git3moji code style: prettier Managed by Yarn

Features

  • Small: PubSub: 127 bytes, EventEmitter: 138 bytes, together: 191 bytes (all gzipped)
  • Best practices: Subscribe returns unsubscribe. So you can use anonymous functions.
  • Readable: There is nothing magical in the code; except the simplicity.
  • Modern: Uses new, yet well-supported features.
  • Efficient: No memory leaks, no duplicate calls, no extra looping.
  • Clean API: sub and pub (or on and emit), need no more, can't go less.
  • Modular: Use only what you need.
  • Typed TypeScript definitions are bundled

Install

Install using yarn or npm:

yarn add dead-simple
# or via npm
npm install --save dead-simple

Usage

import pubsub from "dead-simple/pubsub";
import eventEmitter from "dead-simple/eventEmitter";
// Alternatively:
// import { pubsub, eventEmitter } from 'dead-simple'

// === PubSub ======
const clicks = pubsub();

const unSub = clicks.sub((target) => console.log(`Clicked on ${target}!`));

clicks.pub("button");
// -> Clicked on button!

unSub();

clicks.pub("link");
// nothing

// === eventEmitter ===
// eventEmitter = named PubSub
const events = eventEmitter();

events.on("click", (target) => console.log(`Clicked on ${target}!`));

const unSubChange = events.on("change", (newValue) =>
  console.log(`Value is now ${newValue}!`)
);

events.emit("change", 1968);
// -> Value is now 1968!

unSubChange();

events.emit("change", 1968);
// nothing

events.emit("click", "button");
// -> Clicked on button!

Browser compatibility

Used ES6: const, arrow functions, Map, Set, object shorthand

Chrome* Edge FF IE Opera Safari iOS Node
38 12 13 -* 25 7.1 8 4

Notes:

  • Chrome includes mobile Chrome (Android 4+).
  • IE 11 does not support only syntax feature, arrow functions and object shorthand.
  • The module needs to be bundled, of course.

Super small versions

This project started a pair of gists which included a hand minified version, too.

We were able to get down to 91B for PubSub:

export default (s = new Set()) => ({
  pub: (d) => s.forEach((f) => f(d)),
  sub: (f) => s.add(f).delete.bind(s, f),
});

And 139B for EventEmitter (p is PubSub):

export default (e) => (
  (e = new Map()),
  Object.freeze({
    on: (n, f) => (e.has(name) || e.set(n, p()), e.get(n).sub(f)),
    emit: (n, d) => e.has(n) && e.get(n).pub(d),
  })
);

These versions are for fun, more like a proof of concept and may not work in some browsers.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].