12 * Internal storage for established contracts and execution responses.
22 initialize : function()
24 // Nothing to do here.
31 /* ------------------------------------- TASK HANDLING ------------------------------------- */
33 * Establish a Task Contract with server.
40 * @param errorCallback
42 getTask : function( id, workspace, task, jobId, data, callback, errorCallback )
44 this.taskContracts[id] = {
45 "Callback" : callback,
46 "ErrorCallback" : errorCallback
56 "url" : 'workspace/contract-task',
57 "data" : [{"Workspace": workspace, "Task": task, "JobId": jobId, "Packet": data}]
59 $.proxy(this._getTaskSuccess, this),
60 $.proxy(this._getTaskError, this)
65 * Retrieve a Task Contract established with server.
67 * @returns object|null
69 taskContract : function( id )
71 return this.taskContracts[id]
72 ? this.taskContracts[id]
76 _getTaskSuccess : function( id, response )
78 response = response[0];
79 if (!this.taskContracts[id])
83 this.taskContracts[id]["Response"] = response;
84 if (response.Status && 'Success' != response.Status)
86 if (this.taskContracts[id]['ErrorCallback'])
88 this.taskContracts[id]['ErrorCallback'](response);
93 this.taskContracts[id]["Hash"] = response.Hash;
94 this.taskContracts[id]["LifeTime"] = response.LifeTime;
95 if (this.taskContracts[id]['Callback'])
97 this.taskContracts[id]['Callback'](response);
102 _getTaskError : function( id, error )
104 this.taskContracts[id]["Response"] = error;
105 if (this.taskContracts[id]['ErrorCallback'])
107 this.taskContracts[id]['ErrorCallback'](error);
112 * Establish multiple Task Contracts with server.
115 getTasks : function( contracts, callback )
117 var id = this.requestId;
120 this.requests.push({});
121 this.requestCallback.push({
124 for (var i in contracts)
126 this.requests[id][i] = contracts[i].id;
127 this.taskContracts[contracts[i].id] = {
128 "Callback" : contracts[i].callback,
129 "ErrorCallback" : contracts[i].errorCallback
131 jobId = (contracts[i].jobId)
134 data = (contracts[i].data)
138 "Workspace": contracts[i].workspace,
139 "Task": contracts[i].task,
146 "url" : 'workspace/contract-task',
149 $.proxy(this._getTasksSuccess, this),
150 $.proxy(this._getTaskError, this)
154 _getTasksSuccess : function( gid, responses )
156 for (var i in responses)
158 response = responses[i];
159 var id = this.requests[gid][i];
160 if (!this.taskContracts[id])
164 this.taskContracts[id]["Response"] = response;
165 if (undefined != response.Status && 'Success' != response.Status)
167 if (this.taskContracts[id]['ErrorCallback'])
169 this.taskContracts[id]['ErrorCallback'](response);
174 this.taskContracts[id]["Hash"] = response.Hash;
175 this.taskContracts[id]["LifeTime"] = response.LifeTime;
176 if (this.taskContracts[id]['Callback'])
178 this.taskContracts[id]['Callback'](response);
182 if (undefined != this.requestCallback[gid])
184 this.requestCallback[gid].callback();
186 delete this.requests[gid];
187 delete this.requestCallback[gid];
191 * Execute a Task Contract against server.
196 * @param errorCallback
197 * @returns boolean|void
199 execTask: function( id, data, options, callback, errorCallback, method, direct )
201 if (!this.taskContracts[id])
203 console.log('! No contract for: ' + id);
206 this.taskContracts[id]["Callback"] = callback;
207 this.taskContracts[id]["ErrorCallback"] = errorCallback;
220 "url" : 'workspace/execute-task',
221 "data" : [{"Contract" : this.taskContracts[id]["Hash"], "Packet": data, "Options": options}]
225 packet.direct = true;
229 $.proxy(this._execTaskSuccess, this),
230 $.proxy(this._execTaskError, this)
234 _execTaskSuccess : function( id, response )
236 response = response[0];
237 this.taskExecutions[id] = response;
238 if (!this.taskContracts[id])
240 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
242 console.log(this.taskContracts);
244 if (response.Status && 'Success' != response.Status)
246 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
248 this.taskContracts[id]['ErrorCallback'](response);
253 var callback = this.taskContracts[id]['Callback']
254 ? this.taskContracts[id]['Callback']
256 if (this.taskContracts[id] && this.taskContracts[id]['LifeTime'] != 'Recurring')
258 delete this.taskContracts[id];
267 _execTaskError : function( id, error )
269 this.taskExecutions[id] = error;
270 if (this.taskContracts[id]['ErrorCallback'])
272 this.taskContracts[id]['ErrorCallback'](error);
279 * Execute a Task Contract against server.
284 * @param errorCallback
285 * @returns boolean|void
287 execTasks: function( contracts, callback )
288 //id, data, options, callback, errorCallback, method, direct )
290 var gid = this.requestId;
292 this.requests.push({});
293 this.requestCallback.push({
297 for (var i in contracts)
299 this.requests[gid][i] = contracts[i].id;
300 this.taskContracts[contracts[i].id]["Callback"] = contracts[i].callback;
301 this.taskContracts[contracts[i].id]["ErrorCallback"] = contracts[i].errorCallback;
303 "Contract" : this.taskContracts[contracts[i].id]["Hash"],
304 "Packet": contracts[i].data,
305 "Options": contracts[i].options
311 "url" : 'workspace/execute-task',
314 $.proxy(this._execTasksSuccess, this),
315 $.proxy(this._execTaskError, this)
319 _execTasksSuccess : function( gid, responses )
321 for (var i in responses)
323 response = responses[i];
324 var id = this.requests[gid][i];
325 this.taskExecutions[id] = response;
326 if (!this.taskContracts[id])
328 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
330 console.log(this.taskContracts);
332 if (response.Status && 'Success' != response.Status)
334 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
336 this.taskContracts[id]['ErrorCallback'](response);
341 if (this.taskContracts[id]['Callback'])
343 this.taskContracts[id]['Callback'](response);
345 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
347 delete this.taskContracts[id];
351 if (undefined != this.requestCallback[gid])
353 this.requestCallback[gid].callback(responses);
355 delete this.requests[gid];
356 delete this.requestCallback[gid];
360 * Execute a Task Contract against server.
365 * @param errorCallback
366 * @returns boolean|void
368 execTaskMulti: function( id, data, options, callback, errorCallback, method, direct )
370 if (!this.taskContracts[id])
372 console.log('! No contract for: ' + id);
375 this.taskContracts[id]["Callback"] = callback;
376 this.taskContracts[id]["ErrorCallback"] = errorCallback;
389 for (var i = 0; i < data.length; i++)
392 "Contract" : this.taskContracts[id]["Hash"],
394 "Options": options[i] ? options[i] : {}
399 "url" : 'workspace/execute-task',
404 packet.direct = true;
408 $.proxy(this._execTaskSuccessMulti, this),
409 $.proxy(this._execTaskError, this)
413 _execTaskSuccessMulti : function( id, responses )
416 this.taskExecutions[id] = [];
417 for (var i = 0; i < responses.length; i++)
419 response = responses[i];
420 this.taskExecutions[id].push(response);
421 if (!this.taskContracts[id])
423 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
425 console.log(this.taskContracts);
427 if (response.Status && 'Success' != response.Status)
434 if (this.taskContracts[id]['ErrorCallback'])
436 this.taskContracts[id]['ErrorCallback'](response);
441 if (this.taskContracts[id]['Callback'])
443 this.taskContracts[id]['Callback'](this.taskExecutions[id]);
445 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
447 delete this.taskContracts[id];
452 _execTaskError : function( id, error )
454 this.taskExecutions[id] = error;
455 if (this.taskContracts[id]['ErrorCallback'])
457 this.taskContracts[id]['ErrorCallback'](error);
470 /* ------------------------------------- ROUTE HANDLING ------------------------------------- */
472 * Directly route an item to a new sate.
479 * @param errorCallback
481 directRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
483 this.routeContracts[id] = {
484 "Callback" : callback,
485 "ErrorCallback" : errorCallback
495 "url" : 'workspace/direct-route',
496 "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
498 $.proxy(this._directRouteSuccess, this),
499 $.proxy(this._directRouteError, this)
503 _directRouteSuccess : function( id, response )
505 response = response[0];
506 this.routeExecutions[id] = response;
507 if (response.Status && 'Success' != response.Status)
509 if (this.routeContracts[id]['ErrorCallback'])
511 this.routeContracts[id]['ErrorCallback'](response);
516 if (this.routeContracts[id]['Callback'])
518 this.routeContracts[id]['Callback'](response);
520 delete this.routeContracts[id];
524 _directRouteError : function( id, error )
526 this.routeExecutions[id] = error;
527 if (this.routeContracts[id]['ErrorCallback'])
529 this.routeContracts[id]['ErrorCallback'](error);
534 * Establish a Route Contract with server.
541 * @param errorCallback
543 getRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
545 this.routeContracts[id] = {
546 "Callback" : callback,
547 "ErrorCallback" : errorCallback
557 "url" : 'workspace/contract-route',
558 "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
560 $.proxy(this._getRouteSuccess, this),
561 $.proxy(this._getRouteError, this)
566 * Retrieve a Route Contract established with server.
568 * @returns object|null
570 routeContract : function( id )
572 return this.routeContracts[id]
573 ? this.routeContracts[id]
577 _getRouteSuccess : function( id, response )
579 response = response[0];
580 this.routeContracts[id]["Response"] = response;
581 if (response.Status && 'Success' != response.Status)
583 if (this.routeContracts[id]['ErrorCallback'])
585 this.routeContracts[id]['ErrorCallback'](response);
590 this.routeContracts[id]["Hash"] = response.Hash;
591 this.routeContracts[id]["LifeTime"] = response.LifeTime;
592 if (this.routeContracts[id]['Callback'])
594 this.routeContracts[id]['Callback'](response);
599 _getRouteError : function( id, error )
601 this.routeContracts[id]["Response"] = error;
602 if (this.routeContracts[id]['ErrorCallback'])
604 this.routeContracts[id]['ErrorCallback'](error);
609 * Execute a Route Contract against server.
614 * @param errorCallback
615 * @returns boolean|void
617 execRoute: function( id, data, options, callback, errorCallback )
619 if (!this.routeContracts[id])
623 this.routeContracts[id]["Callback"] = callback;
624 this.routeContracts[id]["ErrorCallback"] = errorCallback;
633 "url" : 'workspace/execute-route',
634 "data" : [{"Contract" : this.routeContracts[id]["Hash"], "Packet": data, "Options": options}]
636 $.proxy(this._execRouteSuccess, this),
637 $.proxy(this._execRouteError, this)
641 _execRouteSuccess : function( id, response )
643 response = response[0];
644 this.routeExecutions[id] = response;
645 if (response.Status && 'Success' != response.Status)
647 if (this.routeContracts[id]['ErrorCallback'])
649 this.routeContracts[id]['ErrorCallback'](response);
654 if (this.routeContracts[id]['Callback'])
656 this.routeContracts[id]['Callback'](response);
658 if (this.routeContracts[id]['LifeTime'] != 'Recurring')
660 delete this.routeContracts[id];
665 _execRouteError : function( id, error )
667 this.routeExecutions[id] = error;
668 if (this.routeContracts[id]['ErrorCallback'])
670 this.routeContracts[id]['ErrorCallback'](error);