Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
A collection of types and features for implementing the publisher/subscriber pattern.
pubsub
kz is a collection of easy-to-use utility and feature libraries for creating anything you want with the Deno runtime.
@kz/pubsub
The @kz/pubsub module provides types and features for implementing the publisher/subscriber pattern.
Within integereleven the publisher/subscriber pattern is a software design pattern similar to the observer pattern, but with a key difference: topics. In the publisher/subscriber pattern, the publisher sends messages to subscribers based on topics. This allows for more granular control over which messages are sent to which subscribers.
Examples
import { type PubSubTopicMessage, TAbstractSubscriber, TBasePublisher, } from './mod.ts'; import { TAbstractObserver } from '@kz/observe'; interface TChange<T> { model: string; user: string; value: T; } interface IUser { id: string; name: string; } interface IDevice { id: string; name: string; type: string; } type ChangeMap = { 'user': TChange<IUser>; 'device': TChange<IDevice>; }; class Updater extends TBasePublisher<ChangeMap> { } class UserUpdater extends TAbstractSubscriber<ChangeMap, 'user'> { public next(change: PubSubTopicMessage<ChangeMap, 'user'>): void { const [, message] = change; console.log( `User ${message.user} updated ${message.model} to ${message.value.name}`, ); } public error(error: Error): void { console.error(error); } } class DeviceUpdater extends TAbstractSubscriber<ChangeMap, 'device'> { public next(change: PubSubTopicMessage<ChangeMap, 'device'>): void { const [, message] = change; console.log( `User ${message.user} updated ${message.model} to ${message.value.name}`, ); } public error(error: Error): void { console.error(error); } } // Yes! You can use regular observers to subscribe to a publisher. // Because they cannot specify a topic, they will receive all messages from // the publisher. class Logger extends TAbstractObserver<PubSubTopicMessage<ChangeMap>> { public next(change: PubSubTopicMessage<ChangeMap>): void { const [topic, message] = change; console.log(`${topic}: Changed by ${message.user}`); } public error(error: Error): void { console.error(error); } } const updater = new Updater(); const userUpdater = new UserUpdater(['user']); const deviceUpdater = new DeviceUpdater(['device']); const logger = new Logger(); updater.subscribe(userUpdater); updater.subscribe(deviceUpdater); updater.subscribe(logger); updater.publish(['user', { model: 'user', user: 'admin', value: { id: '1', name: 'admin' }, }]); updater.publish(['device', { model: 'device', user: 'admin', value: { id: '1', name: 'MKSENSE', type: 'sensor' }, }]);
Contributing
Contributions are welcome! Take a look at our contributing guidelines to get started.
License
The MIT License (MIT) 2020-2024 integereleven. Refer to LICENSE for details.
Built with ❤ by integereleven
Add Package
deno add jsr:@kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";
Import directly with a jsr specifier
import * as pubsub from "jsr:@kz/pubsub";
Add Package
pnpm i jsr:@kz/pubsub
pnpm dlx jsr add @kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";
Add Package
yarn add jsr:@kz/pubsub
yarn dlx jsr add @kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";
Add Package
vlt install jsr:@kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";
Add Package
npx jsr add @kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";
Add Package
bunx jsr add @kz/pubsub
Import symbol
import * as pubsub from "@kz/pubsub";