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

Use synctrhough to process things #14

Merged
merged 1 commit into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"process-nextick-args": "^1.0.6",
"pump": "^1.0.1",
"qlobber": "^0.7.0",
"syncthrough": "^0.3.1",
"through2": "^2.0.0",
"throughv": "^1.0.3"
}
Expand Down
12 changes: 6 additions & 6 deletions persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var Redis = require('ioredis')
var through = require('through2')
var throughv = require('throughv')
var syncthrough = require('syncthrough')
var fs = require('fs')
var path = require('path')
var lua = fs.readFileSync(path.join(__dirname, 'lib/cursor.lua'))
Expand All @@ -21,6 +22,7 @@ var qlobberOpts = {
}
var offlineClientsCountKey = 'counter:offline:clients'
var offlineSubscriptionsCountKey = 'counter:offline:subscriptions'
var retainedRegexp = /[#+]/

function RedisPersistence (opts) {
if (!(this instanceof RedisPersistence)) {
Expand All @@ -35,7 +37,7 @@ function RedisPersistence (opts) {

var that = this
this._decodeAndAugment = function decodeAndAugment (chunk, enc, cb) {
that._getPipeline().getBuffer(chunk, function decodeMessage (err, result) {
that._db.getBuffer(chunk, function decodeMessage (err, result) {
var decoded
if (result) {
decoded = msgpack.decode(result)
Expand Down Expand Up @@ -75,31 +77,29 @@ function checkAndSplit (prefix, pattern) {
var qlobber = new Qlobber(qlobberOpts)
qlobber.add(pattern, true)

// TODO use ctor
var instance = through.obj(splitArray)
var instance = syncthrough(splitArray)

instance._qlobber = qlobber
instance._prefix = prefix

return instance
}

function splitArray (keys, enc, cb) {
function splitArray (keys, enc) {
var prefix = this._prefix.length
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i].slice(prefix)
if (this._qlobber.match(key).length > 0) {
this.push(keys[i])
}
}
cb()
}

RedisPersistence.prototype.createRetainedStream = function (pattern) {
return new MatchStream({
objectMode: true,
redis: this._db,
match: 'retained:' + pattern.split(/[#+]/)[0] + '*'
match: 'retained:' + pattern.split(retainedRegexp)[0] + '*'
}).pipe(checkAndSplit('retained:', pattern))
.pipe(throughv.obj(this._decodeAndAugment))
}
Expand Down