summaryrefslogtreecommitdiff
path: root/js/src/Ice/AsyncResult.js
blob: 43d0d2f0fc75e7fb034cebcd233d23c2f7e8a69f (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

(function(global){
    require("Ice/Class");
    require("Ice/AsyncResultBase");
    require("Ice/Debug");
    require("Ice/Promise");
    require("Ice/Protocol");
    require("Ice/Exception");
    require("Ice/BasicStream");

    var Ice = global.Ice || {};

    var AsyncResultBase = Ice.AsyncResultBase;
    var Debug = Ice.Debug;
    var Promise = Ice.Promise;
    var Protocol = Ice.Protocol;
    var UserException = Ice.UserException;
    var BasicStream = Ice.BasicStream;

    var AsyncResult = Ice.Class(AsyncResultBase, {
        __init__: function(communicator, op, connection, proxy, adapter, completedFn, sentFn)
        {
            //
            // AsyncResult can be constructed by a sub-type's prototype, in which case the
            // arguments are undefined.
            //
            AsyncResultBase.call(this, communicator, op, connection, proxy, adapter);
            if(communicator !== undefined)
            {
                this._completed = completedFn;
                this._sent = sentFn;
                this._is = null;
                this._os = communicator !== null ?
                    new BasicStream(this._instance, Protocol.currentProtocolEncoding, false) : null;
                this._state = 0;
                this._exception = null;
                this._timeoutRequestHandler = null;
                this._timeoutToken = null;
            }
        },
        __os: function()
        {
            return this._os;
        },
        __is: function()
        {
            return this._is;
        },
        __startReadParams: function()
        {
            this._is.startReadEncaps();
            return this._is;
        },
        __endReadParams: function()
        {
            this._is.endReadEncaps();
        },
        __readEmptyParams: function()
        {
            this._is.skipEmptyEncaps(null);
        },
        __readParamEncaps: function()
        {
            return this._is.readEncaps(null);
        },
        __throwUserException: function()
        {
            Debug.assert((this._state & AsyncResult.Done) !== 0);
            if((this._state & AsyncResult.OK) === 0)
            {
                try
                {
                    this._is.startReadEncaps();
                    this._is.throwException();
                }
                catch(ex)
                {
                    if(ex instanceof UserException)
                    {
                        this._is.endReadEncaps();
                    }
                    throw ex;
                }
            }
        },
        __invokeException: function(ex)
        {
            this._state |= AsyncResult.Done;
            this._exception = ex;
            this._os.resize(0);
            this.fail(ex, this);
        },
        __invokeCompleted: function()
        {
            //
            // Note: no need to change the state here, specializations are responsible for
            // changing the state.
            //

            if(this.proxy !== null && this.proxy.ice_isTwoway())
            {
                Debug.assert(this._completed !== null);
                this._completed(this);
            }
        },
        __runTimerTask: function()
        {
            if(this._timeoutRequestHandler)
            {
                this._timeoutRequestHandler.asyncRequestTimedOut(this);
                this._timeoutRequestHnalder = null;
            }
        }
    });
    
    AsyncResult.OK = 0x1;
    AsyncResult.Done = 0x2;
    AsyncResult.Sent = 0x4;

    Ice.AsyncResult = AsyncResult;
    global.Ice = Ice;
}(typeof (global) === "undefined" ? window : global));