Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to Datafeed class to enable specific topics #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/lib/datafeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ datafeed.subscribe(topic, (message) => {
* Datafeed connect/subscribe manager
*/
class Datafeed {
constructor(privateBullet = false) {
constructor(privateBullet = false, specific = false) {
/** public */
// use specific topics or just topicPrefix's
this.specific = !!specific;
// use private bullet link
this.privateBullet = !!privateBullet;
// real connected status
Expand All @@ -47,7 +49,7 @@ class Datafeed {
this.topicState = [];
// topic listener record
this.topicListener = {
// topicPrefix => [...hooks],
// topic(Prefix) => [...hooks],
};
// subscribed id, auto inc
this.incrementSubscribeId = 0;
Expand Down Expand Up @@ -218,7 +220,8 @@ class Datafeed {

const hookId = this.incrementSubscribeId;
const listener = { hook, id: hookId };
const prefix = getTopicPrefix(topic);
const prefix = this.specific?topic:getTopicPrefix(topic);

if (this.topicListener[prefix]) {
this.topicListener[prefix].push(listener);
} else {
Expand Down Expand Up @@ -247,7 +250,7 @@ class Datafeed {
* @param {boolean} _private is close topic that push private data
*/
unsubscribe(topic, hookId, _private = false) {
const prefix = getTopicPrefix(topic);
const prefix = this.specific?topic:getTopicPrefix(topic);
if (this.topicListener[prefix]) {
const deleted = this.topicListener[prefix].filter(item => item.id !== hookId);
if (deleted.length === 0) {
Expand All @@ -265,7 +268,7 @@ class Datafeed {
_distribute(message) {
const { topic } = message;
if (topic) {
const prefix = getTopicPrefix(topic);
const prefix = this.specific?topic:getTopicPrefix(topic);
const listeners = this.topicListener[prefix];
if (listeners) {
_.each(listeners, ({ hook }) => {
Expand Down