types.nope.IPubSubSystem - ZeMA-gGmbH/NoPE-JS GitHub Wiki

Interface: IPubSubSystem<AD, I, O>

types.nope.IPubSubSystem

The default Publish and Subscribe System.

The System contains of publishers and subscribers which are linked using topics (based on strings).

  • To add new publishers or subscribers use the function: register and provide the required options (see register)
  • To add new subscriber you can use the function : registerSubscription which will receive a topic and a callback (see registerSubscription)
  • After adding publishers or subscribers you can change the behavior using updateOptions (see updateOptions)
  • To remove publishers or subscribers use unregister (see unregister)
  • to emit data use emit (see emit)
  • internally, if a subscriber / publisher is added, its options are changed or its removed, the pub sub system updates an matching structure. In the case you want to perform this manually execute updateMatching
  • to check which publishers and subscribers are present, checkout the corresponding properties.
  • You can subscribe to incremental changes using the eventEmitter onIncrementalDataChange
  • If the pub-sub-system isnt needed any more dispose it!

The publisher might be observabes or eventEmitters.

The IPubSubSystem is implemented by the PubSubSystemBase-Class

The Behavior may differ based on the settings. Your are not able to change these options, after the instance has been created. (see IPubSubOptions for details) The Default of these options are settings are.

{
  mqttPatternBasedSubscriptions: true,
  forwardChildData: true,
  forwardParentData: true,
  matchTopicsWithoutWildcards: true,
}

Example 1

// Describe the required Test:
let pubSubSystem = new PubSubSystemBase({
  generateEmitterType: function () {
    return new NopeEventEmitter() as INopeEventEmitter;
  },
});

// Create a Publisher for the system:
let publisher: INopeEventEmitter = new NopeEventEmitter();

pubSubSystem.register(publisher, {
  mode: "publish",
  schema: {},
  topic: "this/is/a/test",
});

// Create a Subscriber
let subscriber: INopeEventEmitter = new NopeEventEmitter();

subscriber = pubSubSystem.register(subscriber, {
  mode: "subscribe",
  schema: {},
  topic: "this/#",
});

subscriber.subscribe(console.log);
publisher.emit("Hello World!"); // Logs the following => "Hello World!", {...}

Author

M.Karkowski

Export

Type parameters

Name Type
AD extends ITopicSetContentOptions = ITopicSetContentOptions
I extends INopeEventEmitter<unknown, unknown, unknown, AD> = INopeEventEmitter<unknown, unknown, unknown, AD>
O extends INopeTopic = INopeTopic

Hierarchy

Implemented by

Properties

emitters

Readonly emitters: Object

List all known Emitters in the System.

Type declaration

Name Type
publishers { name: string ; schema: INopeDescriptor }[]
subscribers { name: string ; schema: INopeDescriptor }[]

onIncrementalDataChange

Readonly onIncrementalDataChange: INopeEventEmitter<IIncrementalChange, IIncrementalChange, IIncrementalChange, IEventAdditionalData>

An observable which holds the incremental data change. this will be triggered, if the an emitter (publisher) changes its data. Contains only the last emitted data and the topic

// Describe the required Test:
let pubSubSystem = new PubSubSystemBase({
  generateEmitterType: function () {
    return new NopeEventEmitter() as INopeEventEmitter;
  },
});

// Create a Publisher for the system:
let publisher: INopeEventEmitter = new NopeEventEmitter();

pubSubSystem.register(publisher, {
  mode: "publish",
  schema: {},
  topic: "this/is/a/test",
});

pubSubSystem.onIncrementalDataChange.subscribe(console.log);
publisher.emit("Hello World!"); // Logs the following => {path: "this/is/a/test", data: "Hello World!"}

Author

M.Karkowski

Memberof

IPubSubSystem


options

Readonly options: IPubSubOptions

Options which describe the Behavior

Author

M.Karkowski

Memberof

IPubSubSystem


publishers

Readonly publishers: IMapBasedMergeData<O, IPubSubEmitterOptions<AD>, O, string>

List containing all publishers.

Author

M.Karkowski

Memberof

IPubSubSystem


subscriptions

Readonly subscriptions: IMapBasedMergeData<O, IPubSubEmitterOptions<AD>, O, string>

List, containing all subscribers.

Author

M.Karkowski

Memberof

IPubSubSystem

Methods

dispose

dispose(): void

Disposes the Pub-Sub-System.

Author

M.Karkowski

Memberof

IPubSubSystem

Returns

void


emit

emit(path, data, options?): void

Emits an Events and all subscribes, where the pattern matches will be informed

Author

M.Karkowski

Memberof

IPubSubSystem

Parameters

Name Type Description
path string The Topic.
data any The Data of the Event.
options? Partial<AD>

Returns

void


register

register(emitter, options): O

Function to register an Observable. Please define the Options, to decide whether the data of the observable should be published or subscribed.

Author

M.Karkowski

Memberof

IPubSubSystem

Parameters

Name Type Description
emitter I The Emitter to consider
options IEventOptions

Returns

O

{O}


registerSubscription

registerSubscription<T>(path, subscription): INopeObserver

A Helper, that allows the user to subscribe to changes. Therfore he must transmit

Author

M.Karkowski

Memberof

IPubSubSystem

Type parameters

Name Type Description
T unknown Expected Type of the content

Parameters

Name Type Description
path string The
subscription IEventCallback<T, AD>

Returns

INopeObserver

{INopeObserver}


toDescription

toDescription(): Object

Lists all publishers and subscribers of the system.

Returns

Object

Name Type
publishers { name: string ; schema: INopeDescriptor }[]
subscribers { name: string ; schema: INopeDescriptor }[]

unregister

unregister(emitter): boolean

Removes an observable of the Pub-Sub-System.

Author

M.Karkowski

Memberof

IPubSubSystem

Parameters

Name Type
emitter I

Returns

boolean

{boolean}


updateMatching

updateMatching(): void

Helper to manually Trigger an update of the Matching. This will update subscribers and publishers and link them. Normally this is not necessary.

This will build an internal linking (based on the settings) between publishers and subscribers.

Author

M.Karkowski

Memberof

IPubSubSystem

Returns

void


updateOptions

updateOptions(emitter, options): void

Function to update the options and there by the topics of an observable.

Author

M.Karkowski

Memberof

IPubSubSystem

Parameters

Name Type Description
emitter I The Emitter to consider
options IEventOptions The modified options

Returns

void

⚠️ **GitHub.com Fallback** ⚠️