Skip to main content
Home

@kz/pubsub@0.0.1
Built and signed on GitHub Actions

A collection of types and features for implementing the publisher/subscriber pattern.

This package works with Node.js, DenoIt is unknown whether this package works with Cloudflare Workers, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
a year ago (0.0.1)

kz logo 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.

Overview | API Docs

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.

Contributor Covenant

License

The MIT License (MIT) 2020-2024 integereleven. Refer to LICENSE for details.

Built with ❤ by integereleven

@kz logo

GitHub commit activity GitHub issues

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@kz/pubsub

Import symbol

import * as pubsub from "@kz/pubsub";
or

Import directly with a jsr specifier

import * as pubsub from "jsr:@kz/pubsub";

Add Package

pnpm i jsr:@kz/pubsub
or (using pnpm 10.8 or older)
pnpm dlx jsr add @kz/pubsub

Import symbol

import * as pubsub from "@kz/pubsub";

Add Package

yarn add jsr:@kz/pubsub
or (using Yarn 4.8 or older)
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";