@ianwalter/subpub
v3.0.0
Published
A super-simple JavaScript Subscribe/Publish (pubsub) implementation
Downloads
325
Maintainers
Readme
@ianwalter/subpub
A super-simple JavaScript Subscribe/Publish (pubsub) implementation
Installation
yarn add @ianwalter/subpub
Usage
import { Subpub } from '@ianwalter/subpub'
// Create a Subpub instance.
const sp = new Subpub()
// Create a subscription to a topic.
const usubscribe = sp.sub('alerts', msg => console.log(msg))
// Publish a message on the topic.
sp.pub('alerts', 'We ran out of coffee!')
// Unsubscribe the subscriber from the topic.
usubscribe()
// Create a subscriber with an object key that can be pattern-matched.
sp.sub({ key: '1d', name: 'BIBA' }, async msg => msg.toUpperCase())
// Send a message to the subscriber using only the key property to match and
// wait for the returned promise to resolve with the transformed value.
await sp.pub({ key: '1d' }, 'Te biba nachdi') // => TE BIBA NACHDI
License
Apache 2.0 with Commons Clause - See LICENSE
Created by Ian Walter