Skip to content

Tiny PubSub (0.3kb) for Browser and Node.js/Deno

Notifications You must be signed in to change notification settings

tuhuynh27/pubsub

Repository files navigation

Node CI

Tiny PubSub (0.3KB)

Lightweight (335 Bytes gzipped) publish/subscribe library written in TypeScript, use for Browser and Node.js / Deno

Features

  • No dependency
  • Tiny size, just 335 Bytes (less than 1KB) minified and gzipped
  • TypeScript with static types
  • Functional style with Closure

Install

npm install @reactif/pubsub

Usage

import usePubSub from '@reactif/pubsub'

const eventBus = usePubSub()

function publisher() {
    let count = 0
    setInterval(() => {
        eventBus.publish('hello', count)
        count += 1
    }, 1000)
}

function subscriber() {
    const unsubscribe = eventBus.subscribe('hello', (d1: string, d2: number) => {
        console.log(d1)
        console.log(d2)
    })

    eventBus.subscribeOnce('hello', (d1: string) => {
        console.log(d1)
    })

    setTimeout(() => {
        unsubscribe()
    }, 3000)
}

// Clear all subscribers
eventBus.clearAllSubscriptions('hello')

// Count all subscribers
const count = eventBus.countSubscription('hello') // count: number

Usage for Deno

import usePubSub from 'https://raw.githubusercontent.com/oddx-team/pubsub/master/src/index.ts'

const eventBus = usePubSub()
eventBus.subscribe('time', (data: number) => {
  console.log(data)
})

setInterval(() => {
  eventBus.publish('time', new Date().getTime())
}, 100)

About

Tiny PubSub (0.3kb) for Browser and Node.js/Deno

Topics

Resources

Stars

Watchers

Forks