All files / src/Ice AsyncResult.js

90.74% Statements 98/108
90.9% Branches 20/22
92.85% Functions 13/14
90.74% Lines 98/108

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 10941x 41x 41x 41x 41x 41x 41x 3803x 3803x 3803x 3803x 3803x 3803x 3803x 3803x 3803x 3803x 3803x 41x 41x 4x 4x 41x 41x 11x 11x 41x 41x 2427x 2427x 41x 41x         41x 41x 54x 54x 41x 41x 2471x 2471x 2471x 38x 38x 38x 38x 2471x 41x 41x 2653x 2653x 2653x 2587x 2587x 2653x 2653x 2593x 2653x 60x 60x 2653x 41x 41x 1112x 1112x 1112x 1112x 1112x 1112x 41x 41x 8x 6x 8x 2x 2x 8x 41x 41x 7066x             7066x 7066x 41x 41x 2x 2x 41x 41x 2x 2x 41x 41x 4x 4x 41x 41x 41x 41x 41x  
// Copyright (c) ZeroC, Inc.
 
import { Promise } from "./Promise.js";
import { InvocationCanceledException } from "./LocalExceptions.js";
 
export class AsyncResult extends Promise {
    constructor(communicator, op, proxy, completed) {
        super();
        this._communicator = communicator;
        this._instance = communicator ? communicator.instance : null;
        this._operation = op;
        this._proxy = proxy;
        this._completed = completed;
        this._is = null;
        this._state = 0;
        this._sentSynchronously = false;
        this._exception = null;
    }
 
    cancel() {
        this.cancelWithException(new InvocationCanceledException());
    }
 
    isCompleted() {
        return (this._state & AsyncResult.Done) > 0;
    }
 
    isSent() {
        return (this._state & AsyncResult.Sent) > 0;
    }
 
    throwLocalException() {
        if (this._exception !== null) {
            throw this._exception;
        }
    }
 
    sentSynchronously() {
        return this._sentSynchronously;
    }
 
    markSent(done) {
        console.assert((this._state & AsyncResult.Done) === 0);
        this._state |= AsyncResult.Sent;
        if (done) {
            this._state |= AsyncResult.Done | AsyncResult.Ok;
            this._cancellationHandler = null;
            this.resolve();
        }
    }
 
    markFinished(ok, completed) {
        console.assert((this._state & AsyncResult.Done) === 0);
        this._state |= AsyncResult.Done;
        if (ok) {
            this._state |= AsyncResult.Ok;
        }
        this._cancellationHandler = null;
        if (completed) {
            completed(this);
        } else {
            this.resolve();
        }
    }
 
    markFinishedEx(ex) {
        console.assert((this._state & AsyncResult.Done) === 0);
        this._exception = ex;
        this._state |= AsyncResult.Done;
        this._cancellationHandler = null;
        this.reject(ex);
    }
 
    cancelWithException(ex) {
        if (this._cancellationHandler) {
            this._cancellationHandler.asyncRequestCanceled(this, ex);
        } else {
            this._cancellationException = ex;
        }
    }
 
    cancelable(handler) {
        if (this._cancellationException) {
            try {
                throw this._cancellationException;
            } finally {
                this._cancellationException = null;
            }
        }
        this._cancellationHandler = handler;
    }
 
    get communicator() {
        return this._communicator;
    }
 
    get proxy() {
        return this._proxy;
    }
 
    get operation() {
        return this._operation;
    }
}
 
AsyncResult.Ok = 0x1;
AsyncResult.Done = 0x2;
AsyncResult.Sent = 0x4;