All files / src/Ice RetryQueue.js

86.58% Statements 71/82
83.33% Branches 10/12
88.88% Functions 8/9
86.58% Lines 71/82

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 8341x 41x 41x 41x 41x 41x 1039x 1039x 1039x 1039x 41x 41x 1038x 1038x 1038x 41x 41x             41x 41x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 67x 67x 67x 41x 41x 1039x     1039x 1039x 1039x 1039x 1039x 41x 41x 65x     65x 65x 65x 65x 65x 41x 41x 1038x 1038x 1038x 1038x 1038x 41x 41x 1x 1x 1x 1x 1x   1x 41x  
// Copyright (c) ZeroC, Inc.
 
import { CommunicatorDestroyedException } from "./LocalExceptions.js";
 
class RetryTask {
    constructor(instance, queue, outAsync) {
        this._instance = instance;
        this._queue = queue;
        this._outAsync = outAsync;
    }
 
    run() {
        this._outAsync.retry();
        this._queue.remove(this);
    }
 
    destroy() {
        try {
            this._outAsync.abort(new CommunicatorDestroyedException());
        } catch {
            // Abort shouldn't throw if there's no callback, ignore.
        }
    }
 
    asyncRequestCanceled(outAsync, ex) {
        console.assert(
            outAsync === this._outAsync,
            "RetryTask.asyncRequestCanceled: outAsync does not match this._outAsync",
        );
        if (this._queue.cancel(this)) {
            if (this._instance.traceLevels().retry >= 1) {
                this._instance
                    .initializationData()
                    .logger.trace(this._instance.traceLevels().retryCat, "operation retry canceled\n" + ex.toString());
            }
            outAsync.completedEx(ex);
        }
    }
}
 
export class RetryQueue {
    constructor(instance) {
        this._instance = instance;
        this._requests = [];
    }
 
    add(outAsync, interval) {
        if (this._instance === null) {
            throw new CommunicatorDestroyedException();
        }
        const task = new RetryTask(this._instance, this, outAsync);
        outAsync.cancelable(task); // This will throw if the request is canceled
        task.token = this._instance.timer().schedule(() => task.run(), interval);
        this._requests.push(task);
    }
 
    destroy() {
        this._requests.forEach(request => {
            this._instance.timer().cancel(request.token);
            request.destroy();
        });
 
        this._requests = [];
        this._instance = null;
    }
 
    remove(task) {
        const idx = this._requests.indexOf(task);
        if (idx >= 0) {
            this._requests.splice(idx, 1);
        }
    }
 
    cancel(task) {
        const idx = this._requests.indexOf(task);
        if (idx >= 0) {
            this._requests.splice(idx, 1);
            return this._instance.timer().cancel(task.token);
        }
        return false;
    }
}