Skip to content

Commit 238c5de

Browse files
author
nullagent
committed
interanl subscribe
1 parent 332caef commit 238c5de

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/topics/host-topic.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const Path = require('path')
44
const debug = require('debug')('dataparty.topics.host-topic')
5+
const EventEmitter = require('eventemitter3')
56

6-
class HostTopic {
7+
8+
class HostTopic extends EventEmitter {
79
constructor(path, type){
10+
super()
811
this.path = Path.normalize(path)
912
this.type = type
1013
this.created = Date.now()
@@ -95,6 +98,12 @@ class HostTopic {
9598
sends.push(node[1].send(this, this.lastMessage, sender))
9699
}
97100

101+
this.emit('publish', {
102+
topic: this,
103+
data: this.lastMessage,
104+
sender: sender
105+
})
106+
98107
if(sends.length > 0){
99108
debug('publishing', this.path)
100109
this.sending = true

src/topics/local-topic-host.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class LocalTopicHost {
1818
*/
1919
constructor(){
2020
debug('constructor')
21-
this.nodesByUuid = new Map()
22-
this.topicsByPath = new Map()
21+
this.nodesByUuid = new Map() // <string, PeerNode>
22+
this.topicsByPath = new Map() // <string, HostTopic>
2323
this.party = null
2424
}
2525

@@ -146,6 +146,18 @@ class LocalTopicHost {
146146

147147
}
148148

149+
async subscribeInternal(path, cb, create=false){
150+
const topic = this.getTopic(path, create)
151+
152+
debug('subscribeInternal', path)
153+
154+
if(topic){
155+
156+
topic.on('publish', cb)
157+
}
158+
159+
}
160+
149161
async destroyNode(peer){
150162
const node = this.getNodeByPeer(peer)
151163

0 commit comments

Comments
 (0)