-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
148 lines (130 loc) · 3.83 KB
/
context.js
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
define(["when", "reconnecting-websocket", 'ws-rpc', 'hub'],
function(when, ReconnectingWebSocket, WsRpc, Hub) {
var Context = function(server, path, port){
var self = this;
this.path = path || 'ws';
this.port = port || parseInt(window.location.port);
this.server = server || window.location.hostname;
this.session = null;
};
/// The server to connect to.
Context.prototype.server = window.location.hostname;
/// The port to connect to.
Context.prototype.port = window.location.port;
/// The path where the WS serever is listening
Context.prototype.path = 'ws';
/// The session name.
Context.prototype.session = null;
/// The installed instance
Context.prototype._instance = null;
/// WsRpc instance
Context.prototype._rpc = null;
/// WsRpc instance
Context.prototype._hub = null;
// Class method
Context.instance = function() {
if (Context.prototype._instance == null) Context.prototype._instance = new Context();
return Context.prototype._instance;
};
Context.prototype.install = function() {
if (Context.prototype._instance) throw new Error("Context already installed");
Context.prototype._instance = this;
};
/**
* The singleton creator of WsRpc
*
* @property rpc
* @return WsRpc instance
*/
Object.defineProperty(Context.prototype, "rpc", {
get: function(){
if (this._rpc === null) {
this._rpc = new WsRpc(this.server, this.path, this.port);
this._rpc.extend(this);
}
return this._rpc;
}
});
/**
* The singleton creator of Hub
*
* @property hub
* @return Hub instance
*/
Object.defineProperty(Context.prototype, "hub", {
get: function(){
if (this._hub === null) {
this._hub = new Hub(this.server, this.port+1, this.rpc, this.session);
}
return this._hub;
}
});
/**
* Modifies in-place the request to add context information
* @fn modifyRequest
* @memberof Context
*
* @param request The JSON-RPC request.
*/
Context.prototype.modifyRequest = function (request) {
if (this.session === null) return;
var _context = {session: this.session};
if (Array.isArray(request.params)) {
request.params = {
_params: request.params,
_context: _context
};
}
else {
request.params._context = _context;
}
};
/**
* Open a new session and include it in the Context so any later
* call will execute in the context of this session
* @fn openSession
* @memberof Context
*
* @param session The session name.
*/
Context.prototype.openSession = function (session) {
var self = this;
var promise = this.rpc.call('SessionSrv.open_session', [session])
.then(function(){self.session = session;});
this.session = session;
return promise;
};
/**
* Open a new session and include it in the session so any later
* call will execute in the context of this session
* @fn closeSession
* @memberof Context
*
*/
Context.prototype.closeSession = function () {
var self = this;
var promise = this.rpc.call('SessionSrv.close_session', [this.session])
.then(function(){self.session = null;});
return promise;
};
/**
* Use a session including it in the Context so any later call
* will execute in the context of this session
* @fn closeSession
* @memberof Context
* @return isNew (Bool) Return if the session requested was
* already open in the server or if was
* created by this call.
*/
Context.prototype.useSession = function (session) {
var self = this;
var promise = this.rpc.call('SessionSrv.use_session', [session])
.then(function(isNew){
self.session = session;
return isNew;
});
return promise;
};
return Context;
}
);