All files mqtt-plus-subscription.ts

99.51% Statements 206/207
85.36% Branches 35/41
100% Functions 19/19
99.51% Lines 206/207

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 2081x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 1x 1x 82x 82x 82x 82x 82x 82x 82x 82x 1x 1x 1x 9x 9x 9x    
/*
**  MQTT+ -- MQTT Communication Patterns
**  Copyright (c) 2018-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
**  Permission is hereby granted, free of charge, to any person obtaining
**  a copy of this software and associated documentation files (the
**  "Software"), to deal in the Software without restriction, including
**  without limitation the rights to use, copy, modify, merge, publish,
**  distribute, sublicense, and/or sell copies of the Software, and to
**  permit persons to whom the Software is furnished to do so, subject to
**  the following conditions:
**
**  The above copyright notice and this permission notice shall be included
**  in all copies or substantial portions of the Software.
**
**  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
**  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
**  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
**  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
**  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
**  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
**  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
 
/*  external requirements  */
import type { IClientSubscribeOptions } from "mqtt"
 
/*  internal requirements  */
import type { APISchema }               from "./mqtt-plus-api"
import { BaseTrait }                    from "./mqtt-plus-base"
import { run, Spool }                   from "./mqtt-plus-error"
 
/*  reference-counted subscription helper  */
class RefCountedSubscription {
    /*  internal state  */
    private counts    = new Map<string, number>()
    private pending   = new Map<string, Promise<void>>()
    private lingers   = new Map<string, ReturnType<typeof setTimeout>>()
    private unsubbing = new Map<string, Promise<void>>()
 
    /*  initial construction with configuration  */
    constructor (
        private subscribeFn:   (topic: string, options: IClientSubscribeOptions) => Promise<void>,
        private unsubscribeFn: (topic: string) => Promise<void>,
        private lingerMs:      number = 30 * 1000
    ) {}
 
    /*  increment reference count for a topic  */
    private incrementCount (topic: string) {
        const count = this.counts.get(topic) ?? 0
        this.counts.set(topic, count + 1)
        return count
    }
 
    /*  decrement reference count for a topic  */
    private decrementCount (topic: string): number | undefined {
        const count = this.counts.get(topic)
        if (count !== undefined) {
            if (count <= 1) {
                this.counts.delete(topic)
                return 0
            }
            else {
                this.counts.set(topic, count - 1)
                return count - 1
            }
        }
        return undefined
    }
 
    /*  subscribe to a topic (reference-counted)  */
    async subscribe (topic: string, options: IClientSubscribeOptions = { qos: 2 }): Promise<void> {
        /*  increment count first to reserve our interest  */
        const count = this.incrementCount(topic)
 
        /*  optionally just cancel a pending linger unsubscription
            (subscription is still kept active on the broker)  */
        const linger = this.lingers.get(topic)
        if (linger) {
            clearTimeout(linger)
            this.lingers.delete(topic)
            return
        }
 
        /*  if we are the first, we must perform the actual subscription  */
        if (count === 0) {
            /*  create a deferred promise and store it in pending immediately,
                so concurrent subscribers arriving during the await below
                will find and await it instead of returning prematurely  */
            let resolve: () => void
            let reject:  (err: Error) => void
            const deferred = new Promise<void>((res, rej) => {
                resolve = res
                reject  = rej
            })
            deferred.catch(() => {}) /*  avoid unhandled promise rejection  */
            this.pending.set(topic, deferred)
 
            /*  await any in-flight linger unsubscription to avoid a race
                where the broker processes UNSUBSCRIBE after our SUBSCRIBE  */
            const inflight = this.unsubbing.get(topic)
            if (inflight)
                await inflight
 
            /*  perform the actual subscription  */
            const promise = this.subscribeFn(topic, options).then(() => {
                this.pending.delete(topic)
                resolve()
            }).catch((err: Error) => {
                this.pending.delete(topic)
                this.decrementCount(topic)
                reject(err)
                throw err
            })
            return promise
        }
        else {
            /*  perhaps still need to wait for a pending subscription  */
            const pending = this.pending.get(topic)
            if (pending)
                return pending.catch((err: Error) => {
                    this.decrementCount(topic)
                    throw err
                })
        }
    }
 
    /*  unsubscribe from a topic (reference-counted)  */
    async unsubscribe (topic: string): Promise<void> {
        const count = this.decrementCount(topic)
        if (count === 0) {
            if (this.lingerMs > 0) {
                /*  defer the actual broker unsubscription  */
                const timer = setTimeout(() => {
                    this.lingers.delete(topic)
                    const promise = this.unsubscribeFn(topic).catch(() => {}).finally(() => {
                        this.unsubbing.delete(topic)
                    })
                    this.unsubbing.set(topic, promise)
                }, this.lingerMs)
                this.lingers.set(topic, timer)
            }
            else {
                /*  perform the unsubscription immediately, but still store the
                    promise in unsubbing so a concurrent subscribe can await it  */
                const promise = this.unsubscribeFn(topic).catch(() => {}).finally(() => {
                    this.unsubbing.delete(topic)
                })
                this.unsubbing.set(topic, promise)
                await promise
            }
        }
    }
 
    /*  flush all pending linger timers and unsubscribe  */
    async flush (): Promise<void> {
        /*  determine all topics with potentially active subscriptions  */
        const topics = new Set<string>([
            ...this.counts.keys(),
            ...this.lingers.keys(),
            ...this.pending.keys(),
            ...this.unsubbing.keys()
        ])
 
        /*  cancel all pending linger timers first (synchronously)  */
        for (const timer of this.lingers.values())
            clearTimeout(timer)
        this.lingers.clear()
        this.counts.clear()
 
        /*  wait for any in-flight subscribe/unsubscribe operations to settle first  */
        await Promise.allSettled([ ...this.pending.values(), ...this.unsubbing.values() ])
 
        /*  then unsubscribe from all potentially active topics  */
        await Promise.allSettled([ ...topics ].map((topic) =>
            this.unsubscribeFn(topic).catch(() => {})))
 
        /*  clear remaining internal state  */
        this.pending.clear()
        this.unsubbing.clear()
    }
}
 
/*  Subscription trait with shared MQTT subscription management  */
export class SubscriptionTrait<T extends APISchema = APISchema> extends BaseTrait<T> {
    protected subscriptions = new RefCountedSubscription(
        (topic, options) => this.subscribeTopic(topic, options),
        (topic)          => this.unsubscribeTopic(topic)
    )
 
    /*  subscribe to an MQTT topic (reference-counted) and spool the unsubscription  */
    protected async subscribeTopicAndSpool (
        spool:   Spool,
        topic:   string,
        options: Partial<IClientSubscribeOptions> = {}
    ) {
        await run(`subscribe to MQTT topic "${topic}"`, spool, () =>
            this.subscriptions.subscribe(topic, { qos: 2, ...options }))
        spool.roll(() => this.subscriptions.unsubscribe(topic))
    }
 
    /*  destroy subscription trait  */
    override async destroy () {
        await this.subscriptions.flush()
        await super.destroy()
    }
}