-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrequest.js
More file actions
229 lines (197 loc) · 7.02 KB
/
request.js
File metadata and controls
229 lines (197 loc) · 7.02 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var Base = require("../base.js");
var C = require("./constants.js");
var DataNode = require("./graph.js").DataNode;
/**
* Content of this file:
* Classes to request results from an Xflow graph.
*/
/**
* Abstract Request class.
* Any Request is created from a DataNode to receive the result of that DataNode.
* To allow effective optimization, it is recommended to create only one Request per DataNode and receive all
* results through that Request.
* @abstract
* @param {DataNode} dataNode The DataNode from which to request results
* @param {?Array.<string>} filter A list of names filtering the values to be received (only return values with names inside the filter)
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var Request = function(dataNode, filter, callback){
this._dataNode = dataNode;
this._filter = filter ? filter.slice().sort() : null;
this._listener = callback;
/**
* Cached result of this request
* @type {Result}
*/
this._result = null;
/**
* Cached callback function attached to data node
* @private
*/
this._dataNodeListener = this._onDataNodeChange.bind(this);
this._dataNode.addListener(this._dataNodeListener);
};
Object.defineProperty(Request.prototype, "dataNode", {
set: function(){
throw new Error("dataNode is readonly");
},
get: function(){ return this._dataNode; }
});
Object.defineProperty(Request.prototype, "filter", {
set: function(){
throw new Error("filter is read-only");
},
get: function(){ return this._filter; }
});
/**
* Call this function, whenever the request is not required anymore.
* Cleans up cached data and listeners
*/
Request.prototype.clear = function(){
this._listener = null;
if(this._result) this._result._removeRequest(this);
this._dataNode.removeListener(this._dataNodeListener);
};
/**
* @param {C.RESULT_STATE} data
* @private
*/
Request.prototype._onPostponedResultChanged = function(data){
this._listener && this._listener(this, data);
};
/**
* Change the result of the request and update request list of old and new
* result.
* @private
* @param {Request} request
* @param {Result?} newResult
* @returns {Result}
*/
function swapResultRequest(request, newResult){
if(request._result) request._result._removeRequest(request);
request._result = newResult;
if(newResult) newResult._addRequest(request);
return newResult;
}
/**
* @param {Request} request
* @param {C.RESULT_STATE} notification
* @private
*/
function notifyListeners(request, notification){
Base._queueResultCallback(request, notification);
}
/**
* @param {C.RESULT_STATE} notification
*/
Request.prototype._onDataNodeChange = function(notification){
notifyListeners(this, notification);
};
/**
* A ComputeRequest is a Request for a ComputeResult, which contains a named map of typed values.
* @constructor
* @extends {Request}
* @param {DataNode} dataNode The DataNode from which to request results
* @param {?Array.<string>} filter A list of names filtering the values to be received (only return values with names inside the filter)
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var ComputeRequest = function(dataNode, filter, callback){
Request.call(this, dataNode, filter, callback);
};
Base.createClass(ComputeRequest, Request);
/**
* @returns {Result}
*/
ComputeRequest.prototype.getResult = function(){
// swapResultRequest is called here because the result object of the request may change, e.g.
// different forward node.
return swapResultRequest(this, this._dataNode._getResult(C.RESULT_TYPE.COMPUTE, this._filter));
};
ComputeRequest.prototype._onResultChanged = function(notification){
this._onDataNodeChange(notification);
};
var c_vsConnectNodeCount = {},
c_vsConnectNodeKey = {},
c_vsConnectNodeCache = {};
/**
* A VertexShaderRequest is a Request for a VSDataResult, used to generate a VertexShader that includes
* dataflow processing.
* @constructor
* @extends {Request}
* @param {DataNode} dataNode
* @param {VSConfig} vsConfig Configuration for the output of the generated vertex shader
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var VertexShaderRequest = function(dataNode, vsConfig, callback){
var filter = vsConfig.getFilter();
if(filter.length == 0)
throw new Error("vsConfig requires at least one attribute entry.");
Request.call(this, dataNode, filter, callback);
this._vsConfig = vsConfig;
this._vsConnectNode = getVsConnectNode(dataNode, vsConfig);
};
Base.createClass(VertexShaderRequest, Request);
VertexShaderRequest.prototype.getConfig = function(){
return this._vsConfig;
};
/**
* @see ComputeRequest.getResult
* @returns {Result}
*/
VertexShaderRequest.prototype.getResult = function(){
return swapResultRequest(this, this._vsConnectNode._getResult(C.RESULT_TYPE.VS, this._filter));
};
VertexShaderRequest.prototype._onDataNodeChange = function(notification){
if(notification == C.RESULT_STATE.CHANGED_STRUCTURE){
var newVSConnectedNode = getVsConnectNode(this._dataNode, this._vsConfig, this._filter);
if(newVSConnectedNode != this._vsConnectNode){
clearVsConnectNode(this._vsConnectNode);
this._vsConnectNode = newVSConnectedNode;
}
}
Request.prototype._onDataNodeChange.call(this, notification);
};
VertexShaderRequest.prototype.getVertexShader = function(){
this.getResult(); // Update the result first
if(!this._vertexShader){
this._vertexShader = this._result.getVertexShader(this._vsConfig);
}
return this._vertexShader;
};
VertexShaderRequest.prototype._onResultChanged = function(notification){
this._onDataNodeChange(notification);
};
function getVsConnectNode(dataNode, vsConfig, filter){
var forwardNode = dataNode._getForwardNode(filter);
var key = getDataNodeShaderKey(forwardNode, vsConfig);
var connectNode;
if(!(connectNode = c_vsConnectNodeCache[key])){
connectNode = new DataNode(false);
connectNode.appendChild(forwardNode);
connectNode.computeOperator = vsConfig.getOperator();
connectNode.computeInputMapping = null;
connectNode.computeOutputMapping = null;
c_vsConnectNodeCache[key] = connectNode;
c_vsConnectNodeCount[connectNode.id] = 1;
c_vsConnectNodeKey[connectNode.id] = key;
}
else{
c_vsConnectNodeCount[connectNode.id]++;
}
return connectNode;
}
function clearVsConnectNode(connectNode){
c_vsConnectNodeCount[connectNode.id]--;
if(!c_vsConnectNodeCount[connectNode.id]){
var key = c_vsConnectNodeKey[connectNode.id];
c_vsConnectNodeCache[key] = null;
connectNode.clear();
}
}
function getDataNodeShaderKey(dataNode, vsConfig){
return dataNode.id + "|" + vsConfig.getKey();
}
module.exports = {
ComputeRequest: ComputeRequest,
VertexShaderRequest: VertexShaderRequest
};