首次提交
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
'use strict'
|
||||
|
||||
const mqemitter = require('.')
|
||||
|
||||
const mq = mqemitter()
|
||||
|
||||
function subscribe (topic, obj) {
|
||||
mq.on(topic, callback)
|
||||
obj.close = close
|
||||
|
||||
function callback (value, cb) {
|
||||
obj.push(value)
|
||||
cb()
|
||||
}
|
||||
|
||||
function close () {
|
||||
mq.removeListener(topic, callback)
|
||||
}
|
||||
}
|
||||
|
||||
class MyQueue {
|
||||
push (value) {
|
||||
console.log(value)
|
||||
}
|
||||
}
|
||||
|
||||
const a = new MyQueue()
|
||||
const b = new MyQueue()
|
||||
const c = new MyQueue()
|
||||
|
||||
subscribe('hello', a)
|
||||
subscribe('hello', b)
|
||||
subscribe('hello', c)
|
||||
|
||||
mq.emit({ topic: 'hello', payload: 'world' })
|
||||
|
||||
a.close()
|
||||
b.close()
|
||||
c.close()
|
||||
|
||||
mq.emit({ topic: 'hello', payload: 'world' })
|
||||
|
||||
// const listeners = new Map()
|
||||
//
|
||||
//
|
||||
// const queues = new Map()
|
||||
//
|
||||
// function subscribe (topic, queue) {
|
||||
// if (listeners.has(topic)) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// function callback (err) {
|
||||
//
|
||||
// for (var value of queues) {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// listeners.set(topic, callback)
|
||||
// queues.set(topic, [queue])
|
||||
// }
|
||||
Reference in New Issue
Block a user