summaryrefslogtreecommitdiff
path: root/js/src/Ice/RequestHandlerFactory.js
blob: e23e0c0d1726d87a0615b88fa716748c07e881f2 (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
// **********************************************************************
//
// Copyright (c) 2003-2016 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.
//
// **********************************************************************

var Ice = require("../Ice/ModuleRegistry").Ice;
Ice.__M.require(module,
    [
        "../Ice/Class",
        "../Ice/Debug",
        "../Ice/HashMap",
        "../Ice/Reference",
        "../Ice/ConnectRequestHandler"
    ]);

var Debug = Ice.Debug;
var HashMap = Ice.HashMap;
var ConnectRequestHandler = Ice.ConnectRequestHandler;

var RequestHandlerFactory = Ice.Class({
    __init__: function(instance)
    {
        this._instance = instance;
        this._handlers = new HashMap(HashMap.compareEquals);
    },
    getRequestHandler: function(ref, proxy)
    {
        var connect = false;
        var handler;
        if(ref.getCacheConnection())
        {
            handler = this._handlers.get(ref);
            if(!handler)
            {
                handler = new ConnectRequestHandler(ref, proxy);
                this._handlers.set(ref, handler);
                connect = true;
            }
        }
        else
        {
            connect = true;
            handler = new ConnectRequestHandler(ref, proxy);
        }

        if(connect)
        {
            ref.getConnection().then(function(connection, compress)
                                     {
                                         handler.setConnection(connection, compress);
                                     },
                                     function(ex)
                                     {
                                         handler.setException(ex);
                                     });
        }
        return proxy.__setRequestHandler(handler.connect(proxy));
    },
    removeRequestHandler: function(ref, handler)
    {
        if(ref.getCacheConnection())
        {
            if(this._handlers.get(ref) === handler)
            {
                this._handlers.delete(ref);
            }
        }
    }
});

Ice.RequestHandlerFactory = RequestHandlerFactory;
module.exports.Ice = Ice;