types.nope.IPubSubSystem - ZeMA-gGmbH/NoPE-JS GitHub Wiki
The default Publish and Subscribe System.
The System contains of publishers and subscribers which are linked using topics (based on strings).
- To add new
publishersorsubscribersuse the function:registerand provide the required options (see register) - To add new
subscriberyou can use the function :registerSubscriptionwhich will receive a topic and acallback(see registerSubscription) - After adding
publishersorsubscribersyou can change the behavior usingupdateOptions(see updateOptions) - To remove
publishersorsubscribersuseunregister(see unregister) - to
emitdata useemit(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
publishersandsubscribersare present, checkout the corresponding properties. - You can subscribe to incremental changes using the eventEmitter onIncrementalDataChange
- If the pub-sub-system isnt needed any more
disposeit!
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,
}// 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
| Name | Type |
|---|---|
AD |
extends ITopicSetContentOptions = ITopicSetContentOptions
|
I |
extends INopeEventEmitter<unknown, unknown, unknown, AD> = INopeEventEmitter<unknown, unknown, unknown, AD> |
O |
extends INopeTopic = INopeTopic
|
-
IPubSubSystem
Readonly emitters: Object
List all known Emitters in the System.
| Name | Type |
|---|---|
publishers |
{ name: string ; schema: INopeDescriptor }[] |
subscribers |
{ name: string ; schema: INopeDescriptor }[] |
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
Readonly options: IPubSubOptions
Options which describe the Behavior
Author
M.Karkowski
Memberof
IPubSubSystem
Readonly publishers: IMapBasedMergeData<O, IPubSubEmitterOptions<AD>, O, string>
List containing all publishers.
Author
M.Karkowski
Memberof
IPubSubSystem
Readonly subscriptions: IMapBasedMergeData<O, IPubSubEmitterOptions<AD>, O, string>
List, containing all subscribers.
Author
M.Karkowski
Memberof
IPubSubSystem
dispose(): void
Disposes the Pub-Sub-System.
Author
M.Karkowski
Memberof
IPubSubSystem
void
emit(path, data, options?): void
Emits an Events and all subscribes, where the pattern matches will be informed
Author
M.Karkowski
Memberof
IPubSubSystem
| Name | Type | Description |
|---|---|---|
path |
string |
The Topic. |
data |
any |
The Data of the Event. |
options? |
Partial<AD> |
void
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
| Name | Type | Description |
|---|---|---|
emitter |
I |
The Emitter to consider |
options |
IEventOptions |
O
{O}
registerSubscription<T>(path, subscription): INopeObserver
A Helper, that allows the user to subscribe to changes. Therfore he must transmit
Author
M.Karkowski
Memberof
IPubSubSystem
| Name | Type | Description |
|---|---|---|
T |
unknown |
Expected Type of the content |
| Name | Type | Description |
|---|---|---|
path |
string |
The |
subscription |
IEventCallback<T, AD> |
{INopeObserver}
toDescription(): Object
Lists all publishers and subscribers of the system.
Object
| Name | Type |
|---|---|
publishers |
{ name: string ; schema: INopeDescriptor }[] |
subscribers |
{ name: string ; schema: INopeDescriptor }[] |
unregister(emitter): boolean
Removes an observable of the Pub-Sub-System.
Author
M.Karkowski
Memberof
IPubSubSystem
| Name | Type |
|---|---|
emitter |
I |
boolean
{boolean}
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
void
updateOptions(emitter, options): void
Function to update the options and there by the topics of an observable.
Author
M.Karkowski
Memberof
IPubSubSystem
| Name | Type | Description |
|---|---|---|
emitter |
I |
The Emitter to consider |
options |
IEventOptions |
The modified options |
void