Skip to main content

Built and signed on GitHub Actions

A collection of types and features for implementing the mediator 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
2 months ago (0.0.1)
class TAbstractParticipant
implements TParticipant<T, K>
extends TAbstractObserver<ParticipantTopicMessage<T, K>>

Provides an abstract implementation for a participant that can receive from and publish to, topical notifications of a mediator.

Examples

Example 1

import { TAbstractParticipant } from './t_abstract_participant.ts';

import type { ParticipantTopicMessage } from './types/mod.ts';

type MyTopics = {
  TopicA: string;
  TopicB: { value: number };
  TopicC: { timestamp: Date };
};

class MyParticipant extends TAbstractParticipant<MyTopics> {
  next(value: ParticipantTopicMessage<MyTopics>): void {
    const [topic, message] = value;

    if (topic === 'TopicA') {
      console.log(message);
    } else if (topic === 'TopicB') {
      console.log(`Took ${(message as MyTopics["TopicB"]).value} seconds to complete.`);
    } else {
       console.log(`Completed at ${(message as MyTopics["TopicC"]).timestamp}.`);
    }
  }

  error(error: Error): void {
    console.error(error);
  }
}

const participant = new MyParticipant(['TopicA', 'TopicB']);

Constructors

new
TAbstractParticipant(topics?: K[])

Initializes a new instance of the TAbstractSubscriber class with the topics the instance will subscribe to.

Type Parameters

T extends PubSubTopics
  • The topics-to-type map of the types being observed.
K extends keyof T = keyof T
  • The topics this subscriber supports.

Properties

readonly
participantId: symbol

The unique identifier for this participant.

readonly
topics: K[]

The topics this subscriber is subscribed to.

protected
mediators: TBaseMediator<PubSubTopics<T>>[]

The mediators this participant is subscribed to.

Methods

Publishes a message to the mediator.

subscribe(mediator: TBaseMediator<PubSubTopics<T>>): IDisposable

Subscribes the participant to a mediator.

Add Package

deno add jsr:@kz/mediator

Import symbol

import { TAbstractParticipant } from "@kz/mediator";

---- OR ----

Import directly with a jsr specifier

import { TAbstractParticipant } from "jsr:@kz/mediator";

Add Package

npx jsr add @kz/mediator

Import symbol

import { TAbstractParticipant } from "@kz/mediator";

Add Package

yarn dlx jsr add @kz/mediator

Import symbol

import { TAbstractParticipant } from "@kz/mediator";

Add Package

pnpm dlx jsr add @kz/mediator

Import symbol

import { TAbstractParticipant } from "@kz/mediator";

Add Package

bunx jsr add @kz/mediator

Import symbol

import { TAbstractParticipant } from "@kz/mediator";