Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SubscribersMap<T, U>

This class is a generic map of generic subscribers sets (SubscribersSet<T>). This is an encapsulation of Map<T, SubscribersSet<U>>.

import { SubscribersMap } from '@hubhazard/core';

Type parameters

  • T

    Key of the map.

  • U

    Type of the single subscriber.

Hierarchy

  • SubscribersMap

Index

Methods

clear

  • clear(): void

getSet

  • Returns a subscribers set for specified key. Returns undefined if there's no subscribers set for that key.

    example
    const subscribers = subscribersMap.getSet('vegetables');

    Parameters

    • key: T

    Returns SubscribersSet<U> | undefined

getSubscribers

  • getSubscribers(allowDuplicates?: boolean): U[]
  • Returns a list of all subscribers from all subscribers sets.

    example
    import { SubscribersMap } from '@hubhazard/core';
    const subscribersMap = new SubscribersMap<number, string>();
    // Create a subscribers set containing 'hello' and 'world' with the key of 33
    subscribersMap.subscribe(33, 'hello');
    subscribersMap.subscribe(33, 'world');
    // Create a subscribers set containing 'bread', 'and' adn 'butter' with the key of 12
    subscribersMap.subscribe(35, 'hello');
    subscribersMap.subscribe(35, 'bread');
    const withDuplicates = subscribersMap.getSubscribers();
    // withDuplicates equals: ['hello', 'world', 'hello', 'bread']
    const withoutDuplicates = subscribersMap.getSubscribers(false);
    // withoutDuplicates equals: ['hello', 'world', 'bread']

    Parameters

    • Default value allowDuplicates: boolean = true

      A value specifying if duplicates of subscribers are allowed. When set to false additional step of filtering duplicates out is performed before returning.

    Returns U[]

hasSet

  • hasSet(key: T): boolean
  • Returns a value whether there is a subscribers set for specified key.

    example
    if (subscribersMap.hasSet('vegetables')) {
      console.log('There are subscribers!');
    }

    Parameters

    • key: T

    Returns boolean

subscribe

  • subscribe(key: T, subscriber: U): void
  • Adds provided subscriber to the subscribers set specified with by key argument.

    example
    import { SubscribersMap } from '@hubhazard/core';
    const subscribersMap = new SubscribersMap<number, string>();
    // Create a subscribers set containing 'hello' and 'world' with the key of 33
    subscribersMap.subscribe(33, 'hello');
    subscribersMap.subscribe(33, 'world');
    // Create a subscribers set containing 'bread', 'and' adn 'butter' with the key of 12
    subscribersMap.subscribe(12, 'bread');
    subscribersMap.subscribe(12, 'and');
    subscribersMap.subscribe(12, 'butter');

    Parameters

    • key: T
    • subscriber: U

    Returns void

unsubscribe

  • unsubscribe(key: T, subscriber: U): boolean
  • Unsubscribes provided subscriber if already subscribed to specified key.

    example
    subscribersMap.unsubscribe(33, 'world');

    Parameters

    • key: T
    • subscriber: U

    Returns boolean

    true if unsubscribed existing subscriber or false if there was no subscriber to unsubscribe.

Generated using TypeDoc