Description | Author | ||||||||||||||||||||||
This package can register and call handlers of events by name. |
|
A simple and flexible publish-subscribe pattern implementation for PHP, JavaScript, Python
PublishSubscribe
version: 2.0.0
Supports nested topics, tagged topics and namespaced topics.
Topic structure:
[Topic1[/SubTopic11/SubTopic111 ...]][#Tag1[#Tag2 ...]][@NAMESPACE1[@NAMESPACE2 ...]]
Namespaces work similarly to jQuery
namespaces (so handlers can be unsubscribed based on namespaces etc..).
The difference, between tags and namespaces, is that when a topic is triggered without tags and namespaces, handlers which match the topic will be called regardless if they have namespaces or not, while handlers that match the topic but also have tags will not be called.
All topic separators (i.e "/", "#", "@") are configurable per instance.
During the publishing process, an event can be stopped and/or cancel the bubble propagation.
API
see /test
folder
const pb = new PublishSubscribe();
// set topic/tag/namespace separators for this pb instance
// defaults are:
// Topic separator = "/"
// Tag separator = "#"
// Namespace separator = "@"
pb.option('topic_separator', "/");
pb.option('tag_separator', "#");
pb.option('namespace_separator', "@");
// subscribe a handler for a topic with optional tags and optional namespaces
pb.on(topic_with_tags_namespaces, handler);
// subscribe a handler only once for a topic with optional tags and optional namespaces
// handler automatically is unsubscribed after it is called once
pb.one(topic_with_tags_namespaces, handler);
// subscribe a handler on top (first) for a topic with optional tags and optional namespaces
pb.on1(topic_with_tags_namespaces, handler);
// subscribe a handler only once on top (first) for a topic with optional tags and optional namespaces
// handler automatically is unsubscribed after it is called once
pb.one1(topic_with_tags_namespaces, handler);
// unsubscribe a specific handler or all handlers matching topic with optional tags and optional namespaces
pb.off(topic_with_tags_namespaces [, handler = null]);
// publish a topic with optional tags and optional namespaces and pass any data as well
pb.emit(topic_with_tags_namespaces, data);
// pipeline allows to call subscribers of given topic/message asynchronously via a pipeline
// each subscriber calls next subscriber via the passed event's .next() method
// pipeline can be aborted via the passed event's .abort() method
// optional finish_callback will be called when the pipeline finishes the chain or event is aborted
pb.pipeline(topic_with_tags_namespaces, data [, abort_callback [, finish_callback]]);
// dispose PublishSubscribe instance
pb.disposePubSub();
see also:
File | Role | Description | ||
---|---|---|---|---|
Files folder imagesrc (3 directories) | ||||
Files folder imagetest (3 directories) | ||||
Accessible without login Image file publishsubscribe.jpg | Icon | Icon image | ||
Accessible without login Plain text file README.md | Doc. | Documentation |
File | Role | Description |
---|---|---|
Accessible without login Plain text file PublishSubscribe.js | Data | Auxiliary data |
Accessible without login Plain text file PublishSubscribe.min.js | Data | Auxiliary data |
File | Role | Description |
---|---|---|
Plain text file PublishSubscribe.php | Class | Class source |
File | Role | Description |
---|---|---|
Accessible without login Plain text file PublishSubscribe.py | Data | Auxiliary data |
Accessible without login Plain text file __init__.py | Data | Auxiliary data |
File | Role | Description |
---|---|---|
Accessible without login Plain text file out.txt | Doc. | Documentation |
Accessible without login Plain text file test.js | Data | Auxiliary data |
Accessible without login Plain text file test_filter.js | Data | Auxiliary data |
Accessible without login Plain text file test_pipeline.js | Data | Auxiliary data |
File | Role | Description |
---|---|---|
Accessible without login Plain text file out.txt | Doc. | Documentation |
Accessible without login Plain text file test.php | Example | Example script |
Accessible without login Plain text file test_filter.php | Example | Example script |
Accessible without login Plain text file test_pipeline.php | Example | Example script |
Add link image If you know an application of this package, send a message to the author to add a link here.