Package Exports
- @kettek/pubsub
- @kettek/pubsub/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kettek/pubsub) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@kettek/pubsub
This library provides a relatively simple PubSub implementation that supports glob matching for both subscriptions and for publishing.
Installation
npm i @kettek/pubsubDocumentation
See the TypeDoc generated documentation.
Usage
import { Publisher } from '@kettek/pubsub'
const p = new Publisher()
let s = p.subscribe('topic.golang', async ({ message }) => {
console.log(message)
})
p.publish('topic.golang', 'Hello from topic.golang!')
// Hello from topic.golang!
p.unsubscribe(s)Wildcards can be used for subscriptions:
let s = p.subscribe('topic.*', async ({ message }) => {
console.log(message)
})
p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!Wildcards can also be used for publishing:
let s = p.subscribe('topic.golang', async ({ message }) => {
console.log(message)
})
let s2 = p.subscribe('topic.rust', async ({ message }) => {
console.log(message)
})
p.publish('topic.*', 'Hello from topic.*!')
// Hello from topic.*!
// Hello from topic.*!Await can be used to wait for all subscribers to be published to:
let s = p.subscribe('topic.golang', async ({ message }) => {
console.log(message)
})
await p.publish('topic.golang', 'Hello from topic.golang!')A single subscriber can be used for multiple topic subscriptions:
let s = p.subscribe('topic.golang', async ({ message }) => {
console.log(message)
})
p.subscribe('topic.rust', s)
p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!A subscriber can be unsubscribed from a topic:
p.unsubscribe('topic.rust', s)
// s is still subscribed to topic.golangA subscriber can also be unsubscribed from all subscribed topics:
p.unsubscribe(s)
// s is no longer subscribed to any topics.A handler/callback can be used instead of a subscriber:
let h = async ({message}) => {
console.log('handled', message)
}
p.subscribe('topic.golang', h)
p.subscribe('topic.rust', h)
p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!
p.unsubscribe('topic.rust', h)
// h is now only subscribed to topic.golang.
p.unsubscribe(h)
// h is now unsubscribed from all subscriptions.