All files / qlobber/aedes qlobber-sub.js

100% Statements 122/122
100% Branches 24/24
100% Functions 10/10
100% Lines 122/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 10x 10x 10x 8x 8x 14x 14x 8x 2x 2x 1x 1x 1x 1x 1x 20x 20x 20x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 11x 11x 1x 1x 1x 14x 14x 14x 10x 10x 12x 12x 10x 4x 4x 2x 2x 3x 3x 2x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 5x 5x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/*jslint node: true */
"use strict";
 
var util = require('util'),
    qlobber = require('..'),
    Qlobber = qlobber.Qlobber;
 
function QlobberSub (options)
{
    Qlobber.call(this, options);
    this.subscriptionsCount = 0;
}
 
util.inherits(QlobberSub, Qlobber);
 
QlobberSub.prototype._initial_value = function (val)
{
    this.subscriptionsCount += 1;
 
    let r = {
        topic: val.topic,
        clientMap: new Map().set(val.clientId, { qos: val.qos, rh: val.rh, rap: val.rap, nl: val.nl }),
    };
 
    r[Symbol.iterator] = function* (topic)
    {
        if (topic === undefined)
        {
            for (let [clientId, sub] of r.clientMap)
            {
                yield { topic: r.topic, clientId, ...sub };
            }
        }
        else if (r.topic === topic)
        {
            for (let [clientId, sub] of r.clientMap)
            {
                yield { clientId, ...sub };
            }
        }
    };
 
    return r;
};
 
QlobberSub.prototype._add_value = function (existing, val)
{
    var clientMap = existing.clientMap,
        size = clientMap.size;
 
    clientMap.set(val.clientId, { qos: val.qos, rh: val.rh, rap: val.rap, nl: val.nl });
 
    if (clientMap.size > size)
    {
        this.subscriptionsCount += 1;
    }
};
 
QlobberSub.prototype._add_values = function (dest, existing, topic)
{
    if (topic === undefined)
    {
        for (let [clientId, sub] of existing.clientMap)
        {
            dest.push({ clientId, topic: existing.topic, ...sub });
        }
    }
    else if (existing.topic === topic)
    {
        for (let [clientId, sub] of existing.clientMap)
        {
            dest.push({ clientId,...sub });
        }
    }
};
 
QlobberSub.prototype._remove_value = function (existing, val)
{
    var clientMap = existing.clientMap,
        size_before = clientMap.size;
 
    clientMap.delete(val.clientId);
 
    var size_after = clientMap.size;
 
    if (size_after < size_before)
    {
        this.subscriptionsCount -= 1;
    }
 
    return size_after === 0;
};
 
// Returns whether client is last subscriber to topic
QlobberSub.prototype.test_values = function (existing, val)
{
    var clientMap = existing.clientMap;
 
    return (existing.topic === val.topic) &&
           (clientMap.size === 1) &&
           clientMap.has(val.clientId);
};
 
QlobberSub.prototype.match = function (topic, ctx)
{
    return this._match([], 0, topic.split(this._separator), this._trie, ctx);
};
 
QlobberSub.prototype.clear = function ()
{
    this.subscriptionsCount = 0;
    return Qlobber.prototype.clear.call(this);
};
 
QlobberSub.set_native = function (qlobber_native)
{
    const wrap_native = require('../lib/wrap_native.js');
    QlobberSub.native = wrap_native(qlobber_native.QlobberSub, QlobberSub);
    return module.exports;
};
 
module.exports = QlobberSub;