-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkeybars.js
More file actions
2071 lines (1873 loc) · 65.3 KB
/
monkeybars.js
File metadata and controls
2071 lines (1873 loc) · 65.3 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* MonkeyBars v0.9.14
* http://mcgaryes.github.com/monkeybars/
* MonkeyBars may be freely distributed under the MIT license.
*/
(function() {
"use strict";
// ===================================================================
// === Constants =====================================================
// ===================================================================
var STATE_INITIALIZED = 0;
var STATE_STARTED = 1;
var STATE_CANCELED = 2;
var STATE_FAULTED = 3;
var STATE_COMPLETED = 4;
var TYPE_PARALLEL = "parallel";
var TYPE_SEQUENCE = "sequence";
var TYPE_SIMPLE = "simple";
var LOG_NONE = 0;
var LOG_ERROR = 10;
var LOG_INFO = 20;
var LOG_VERBOSE = 30;
var TID_PREFIX = "task";
var TIMEOUT_INTERVAL = 100;
var OVERRIDE_NEEDED = "Override Needed";
var UNDEFINED_TASK = "Undefined Task";
var MISSING_ATTRIBUTES = "No Attributes";
var UNKNOW_TYPE = "Unknown Task Type";
var INVALID_ARGUMENTS = "Invalid Arguments";
var UNHANDLED_POST_MESSAGE = "Unhandled 'postMessage'";
// ===================================================================
// === Private Variables =============================================
// ===================================================================
/**
* Reference to the global js object (i.e. brower's window)
* @for MonkeyBars
* @property root
* @type Object
* @private
*/
var root = this;
/**
* Counter used to create unique task ids
* @for MonkeyBars
* @property taskIdCounter
* @type Integer
* @private
*/
var taskIdCounter = 0;
/**
* List of all whitelisted properties for a task
* @property taskOptions
* @type Array
* @private
*/
var taskOptions = [
// task
"name", "tid", "data", "type", "concurrent", "worker", "displayName", "state", "logLevel", "timeout", "dependencies", "group", "processed",
// group
"tasks", "max",
// decorators
"count", "interval"];
/**
* Object returned by module. Works as namespace for the task library.
* @property MonkeyBars
* @type Object
*/
var MonkeyBars = root.MonkeyBars = {};
// ===================================================================
// === NodeJS Conditional ============================================
// ===================================================================
if (typeof exports !== 'undefined') {
if (typeof(module) !== 'undefined' && module.exports) {
exports = module.exports = MonkeyBars;
}
}
// ===================================================================
// === Helper Methods ================================================
// ===================================================================
/**
* Creates task based on the options passed.
* @for MonkeyBars
* @method createTaskWithOptions
* @param {Object} options
* @return {Task} Task
* @private
*/
var createTaskWithOptions = function(attributes) {
// check for attributes
if (!attributes) {
if (attributes.logLevel >= LOG_ERROR) {
log(MISSING_ATTRIBUTES);
}
return;
}
var task;
// if the attributes passes already has a tid then we know that
// its an already initialized Task object... else we need to create
// a task from the attributes passed
if (attributes.tid) {
task = attributes;
} else {
var type = attributes.type;
var tasks = attributes.tasks;
// create any subtasks
if (tasks) {
attributes.tasks = createSubTasksFromTaskOptionsArray(tasks);
}
if (type) {
if (type === TYPE_SIMPLE) {
task = new Task(attributes);
} else if (type === TYPE_SEQUENCE) {
task = new SequenceTask(attributes);
} else if (type === TYPE_PARALLEL) {
task = new ParallelTask(attributes);
} else {
throw UNKNOW_TYPE;
}
} else {
if (!tasks) {
task = new Task(attributes);
} else {
task = new SequenceTask(attributes);
}
}
}
return task;
};
/**
* Creates an array of tasks based on the options array passed.
* @for MonkeyBars
* @method createSubTasksFromTaskOptionsArray
* @param {Array} tasks
* @return {Array} Array of tasks
* @private
*/
var createSubTasksFromTaskOptionsArray = function(tasks) {
var tempTasks = [];
if (tasks) {
for (var i = 0; i < tasks.length; i++) {
tempTasks.push(createTaskWithOptions(tasks[i]));
}
}
return tempTasks;
};
/**
* Creates property descriptors from the passes attributes.
* @for MonkeyBars
* @method createPropertyDescriptorsWithAttributes
* @param {Object} attributes
* @return {Object} Property descriptors object
* @private
*/
var createPropertyDescriptorsWithAttributes = function(attributes) {
var descriptors = {};
for (var attribute in attributes) {
descriptors[attribute] = {
value: attributes[attribute],
writable: true
};
}
return descriptors;
};
/**
* Generates a unique id for each task.
* @for MonkeyBars
* @method generateUniqueId
* @param {String} prefix
* @return {String} tid
* @private
*/
var generateUniqueId = function(prefix) {
var id = taskIdCounter++;
var tid = prefix ? prefix + id : TID_PREFIX + id;
return tid;
};
/**
* Determains whether the first task is dependent on the second.
* @for MonkeyBars
* @method isTaskDependentOnTask
* @param {Task} task1
* @param {Task} task2
* @return {Boolean} Whether or not the task is dependent on the other
* @private
*/
var isTaskDependentOnTask = function(task1, task2) {
var dependencies = task1.dependencies;
if (dependencies) {
var totalDependencies = dependencies.length;
for (var i = 0; i < totalDependencies; i++) {
var dependency = dependencies[i];
if (dependency.tid === task2.tid) {
return true;
} else if (dependency === task2.name && task2.name !== "undefined") {
return true;
}
}
}
return false;
};
/**
* Variation of http://blog.stchur.com/2007/04/06/serializing-objects-in-javascript/
* @for MonkeyBars
* @method serialize
* @param {Object} o
* @return {String} Serialized string representation of the passed object
* @private
*/
var serialize = function(o) {
// Let Gecko browsers do this the easy way
if (typeof o.toSource !== 'undefined' && typeof o.callee === 'undefined') {
return o.toSource();
}
// Other browsers must do it the hard way
if (typeof o === "number" || typeof o === "boolean" || typeof o === "function") {
return o;
} else if (typeof o === "string") {
return '\'' + o + '\'';
} else if (typeof o === "object") {
var str;
if (o.constructor === Array || typeof o.callee !== 'undefined') {
str = '[';
var i, len = o.length;
for (i = 0; i < len - 1; i++) {
str += serialize(o[i]) + ',';
}
str += serialize(o[i]) + ']';
} else {
str = '{';
var key;
for (key in o) {
str += key + ':' + serialize(o[key]) + ',';
}
str = str.replace(/\,$/, '') + '}';
}
return str;
} else {
return 'UNKNOWN';
}
};
/**
* Creates a blob string to be used with the web worker for concurrent task execution
* @for MonkeyBars
* @method createBlobWithTask
* @param {Task} task
* @return {Blob} Blob instance
* @private
*/
var createBlobWithTask = function(task) {
// create a console wrapper
var consoleString = "var console = { log: function(msg) { postMessage({ type: 'console', message: msg }); } };";
var workerTask;
if (task.worker !== undefined) {
if (typeof(task.worker) === "function") {
workerTask = new task.worker(task);
} else if (task.worker.constructor !== undefined && typeof(task.worker.constructor) === "function") {
workerTask = new task.worker.constructor(task);
}
} else {
workerTask = new WorkerTask(task);
}
var workerString = "var workerTask = " + serialize(workerTask) + "; workerTask.performTask();";
var blobString = "onmessage = function(e) {" + consoleString + workerString + "};";
return new Blob([blobString], {
type: "text\/javascript"
});
};
/**
* Creates a web Worker instance with the passed arguments
* @for MonkeyBars
* @method createWebWorkerWithBlobAndTask
* @param {Blob} blob
* @param {Task} task
* @return {Worker} WebWorker instance
* @private
*/
var createWebWorkerWithBlobAndTask = function(blob, task) {
// @TODO: Need to figure out what the other browser prefixes for window.URL
var URL = root.URL || root.webkitURL;
// create our worker
var worker = new Worker(URL.createObjectURL(blob));
// assign worker on message callback
worker.onmessage = function(e) {
if (e.data.type === "complete") {
task.complete(e.data.value);
} else if (e.data.type === "fault") {
task.fault(e.data.value);
} else if (e.data.type === "cancel") {
task.cancel();
} else if (e.data.type === "console") {
log(e.data.message);
} else {
if (task.worker !== undefined && typeof(task.worker.handler) === "function") {
task.worker.handler(e);
} else {
if (task.logLevel > LOG_ERROR) {
log(UNHANDLED_POST_MESSAGE + ": " + serialize(e.data));
}
}
}
};
// assign worker onerror callback
worker.onerror = function(e) {
task.fault("WebWorker error.");
};
return worker;
};
/**
* Performs the tasks `performTask` functionality within a web worker
* @for MonkeyBars
* @method performTaskFunctionalityWithWebWorker
* @param {Task} task
* @private
*/
var performTaskFunctionalityWithWebWorker = function(task) {
if (typeof(Worker) === "undefined" || typeof(Blob) === "undefined" || task.type !== TYPE_SIMPLE) {
task.performTask();
return;
}
// create our worker
var worker = createWebWorkerWithBlobAndTask(createBlobWithTask(task), task);
// start the worker
worker.postMessage();
};
/**
* @method decorateTaskBasedOnAttributes
* @for MonkeyBars
* @param {Task} task
* @param {Object} attributes
* @private
*/
var decorateTaskBasedOnAttributes = function(task, attributes) {
if (task.count) {
forTaskDecorator(task);
}
if (task.when) {
whenTaskDecorator(task);
}
if (task.doWhile) {
whileTaskDecorator(task);
}
};
/**
* Extention functionality for various task types.
* @method extend
* @for MonkeyBars
* @param {Object} protoProps
* @return {Function} child Constructor function for extended task type
* @example
* var CustomTask = MonkeyBars.Task.extend({
* name:"CustomTask",
* newMethod:function(){
* console.log("Executing newMethod");
* }
* });
* var instance = new CustomTask();
* @private
*/
var extend = function(protoProps) {
var parent = this;
var child = function() {
parent.apply(this, arguments);
};
var childProto = createPropertyDescriptorsWithAttributes(protoProps);
child.prototype = Object.create(parent.prototype, childProto);
return child;
};
/**
* Simple console.log wrapper
* @for MonkeyBars
* @method log
* @param {Object} msg
* @private
*/
var log = function(msg) {
if (console && console.log) {
console.log(msg);
}
};
// ===================================================================
// === Task Events ===================================================
// ===================================================================
/**
* @proerty TaskEvents
* @type Object
*/
var TaskEvents = {
// ===================================================================
// === Properties ====================================================
// ===================================================================
/**
* Holds all references to event types, callbacks, contexts and configurations.
* @for TaskEvents
* @property _eventMap
* @type Object
* @private
*/
_eventMap: undefined,
// ===================================================================
// === Methods =======================================================
// ===================================================================
/**
* Checks to see if an event is registered to this object with the passed type.
* @for TaskEvents
* @method has
* @param {String} type
* @return {Boolean} Whether or not the object contains the listener type
*/
has: function(type) {
if (this._eventMap === undefined || this._eventMap[type] === undefined) {
return false;
}
return true;
},
/**
* Removes an event to the object.
* @for TaskEvents
* @method off
* @param {String} type
* @param {Function} callback
*/
off: function(type, callbackRef) {
if (this._eventMap === undefined || this._eventMap[type] === undefined) {
return;
}
if (type) {
if (callbackRef) {
var tempArr = [];
for (var i = 0; i < this._eventMap[type].length; i++) {
var item1 = this._eventMap[type][i];
if (item1.callback === callbackRef) {
this._eventMap[type] = this._eventMap[type].splice(i, 0);
}
}
} else {
for (var j = 0; j < this._eventMap[type].length; j++) {
var item2 = this._eventMap[type][j];
if (item2.configurable === true) {
this._eventMap[type] = this._eventMap[type].splice(j, 0);
}
}
}
} else {
// @TODO: need to come up with a way to look through all of the objects
// props as well as any events on the object and then delete only those that
// are not configurable
this._eventMap = {};
}
},
/**
* Attaches an event to the object.
* @for TaskEvents
* @method on
* @param {String} type
* @param {Function} callback
* @param {Object} context
* @param {Boolean} configurable Whether or not you should be able to remove this listener without passing its callback reference
*/
on: function(type, callback, context, configurable) {
if (this._eventMap === undefined) {
this._eventMap = {};
}
if (this._eventMap[type] === undefined) {
this._eventMap[type] = [];
}
if (configurable === undefined) {
configurable = true;
}
this._eventMap[type].push({
callback: callback,
context: context,
configurable: configurable
});
},
/**
* Triggers the firing of an event on an object.
* @for TaskEvents
* @method trigger
* @param {String} type
*/
trigger: function(type) {
if (this._eventMap === undefined || this._eventMap[type] === undefined) {
return;
}
for (var i = 0; i < this._eventMap[type].length; i++) {
var item = this._eventMap[type][i];
item.callback.call(item.context, {
type: type,
target: this,
isConfigurable: item.configurable
});
}
}
};
// ===================================================================
// === Worker Task ===================================================
// ===================================================================
/**
* Creates a new worker representation of the task
* @extends Object
* @constructor
* @class WorkerTask
* @param {Task} task The task we're creating this worker representation from
* @example
* var CustomWorker = MonkeyBars.WorkerTask.extend({
* append:function(data){
* this.postMessage("append",100);
* },
* devide:function(data){
* this.postMessage("devide",2);
* this.complete(data/2);
* }
* });
* var task = new MonkeyBars.Task({
* ...
* concurrent:true,
* worker:{
* constructor:CustomWorker,
* handler:function(e){
* if(e.data.type === "append") {
* ...
* } else if(e.data.type === "devide") {
* ...
* }
* }
* }
* ...
* });
* task.start();
*/
var WorkerTask = MonkeyBars.WorkerTask = function(task) {
if (!task) {
throw INVALID_ARGUMENTS;
}
if (task.data !== undefined) {
this.data = task.data;
}
this.performTask = task.performTask;
};
WorkerTask.prototype = {
// ===================================================================
// === WorkerTask Methods ============================================
// ===================================================================
/**
* Post a complete message along with the data passed stating that the task
* has completed what it needs to.
* @for WorkerTask
* @method complete
*/
complete: function(data) {
this.postMessage("complete", data);
},
/**
* Posts a fault message to the main thread that the task has faulted. Passes
* an error as its value.
* @for WorkerTask
* @method fault
* @param {Object} error
*/
fault: function(error) {
this.postMessage("fault", error);
},
/**
* Posts a cancel message to the main thread that the task has been canceled.
* @for WorkerTask
* @method cancel
*/
cancel: function() {
this.postMessage("cancel");
},
/**
* Convenience method for posting messages to the main thread. You should opt into
* using this as it is how the rest of the WorkerTask core methods communicate with
* the main thread.
* @for WorkerTask
* @method postMessage
* @param {String} type
* @param {Object} value
*/
postMessage: function(type, value) {
var message = {};
if (type !== undefined && typeof(type) === "string") {
message.type = type;
}
if (value !== undefined) {
message.value = value;
}
postMessage(message);
}
};
/**
* Extention functionality for worker tasks. This is different than the core extend
* functionality because we need to make sure that all of the protoprops provided
* are available on the task because of its concurrent nature.
* @method extend
* @for WorkerTask
* @param {Object} protoProps
* @return {Function} child Constructor function for extended task type
*/
WorkerTask.extend = function(protoProps) {
var parent = this;
var child = function() {
parent.apply(this, arguments);
};
var proto = Object.create(parent.prototype);
for (var prop in protoProps) {
proto[prop] = protoProps[prop];
}
child.prototype = proto;
return child;
};
// ===================================================================
// === Simple Task ===================================================
// ===================================================================
/**
* The simplest form of a __MonkeyBars__ task. Once started the task executes all
* functionality located within the `performTask` function block. Set `logLevel`
* to see console logs during task execution.
* @extends Object
* @uses TaskEvents
* @constructor
* @class Task
* @param {Object} attributes List of attributes to apply to the task
* @example
* var task = new MonkeyBars.Task({
* name:"ExampleTask",
* performTask:function(){
* this.complete();
* },
* onComplete:function(){
* alert(this.name + " is complete!");
* }
* });
* task.start();
*/
var Task = MonkeyBars.Task = function(attributes) {
var task = this;
task.tid = generateUniqueId();
// add our attributes
for (var attribute in attributes) {
if (attributes.hasOwnProperty(attribute)) {
var option = true;
for (var i = 0; i < taskOptions.length; i++) {
// @TODO: Need to add functionality here to make sure that the options passed
// match up to their type
if (attribute === taskOptions[i] || typeof(attributes[attribute]) === "function") {
option = false;
break;
}
}
if (option) {
if (task.options === undefined) {
task.options = {};
}
task.options[attribute] = attributes[attribute];
} else {
if (!task.hasOwnProperty(attribute)) {
task[attribute] = attributes[attribute];
}
}
}
}
// decorate our task
decorateTaskBasedOnAttributes(task, attributes);
// initialize the task
task.initialize(task.options);
};
Task.prototype = Object.create(TaskEvents, {
// ===================================================================
// === Task Private Properties =======================================
// ===================================================================
/**
* The current state of the task
* @for Task
* @property _state
* @type Integer
* @private
*/
_state: {
value: STATE_INITIALIZED,
writable: true
},
// ===================================================================
// === Task Public Properties ========================================
// ===================================================================
/**
* Whether or not to run the task concurrently through Web Workers
* @for Task
* @property concurrent
* @type Boolean
* @default false
*/
concurrent: {
value: false,
writable: true
},
/**
* The default logging level for tasks
* @for Task
* @property logLevel
* @type Integer
* @default 0
*/
logLevel: {
value: LOG_NONE,
writable: true
},
/**
* Time in milliseconds in which a task will time out and throw a fault
* @for Task
* @property timeout
* @type Integer
* @default undefined
*/
timeout: {
value: undefined,
writable: true
},
/**
* The kind of task
* @for Task
* @property type
* @type String
* @readonly
*/
type: {
value: TYPE_SIMPLE
},
/**
* This object can either be simply a reference to a custom WorkerTask extention's
* constructor. Or it can be an object with a constructor key/value pair. If it is the
* latter then you also have the option of passing a handler function that will be run
* on the `onMessage` handler of the Worker itself.
* @for Task
* @property worker
* @type Object
* @default undefined
* @example
* var task = new MonkeyBars.Task({
* ...
* worker:{
* constructor:CustomWorker,
* handler:function(e){
* // called when a postMessage is posted from the task
* }
* },
* ...
* });
* var task = new MonkeyBars.Task({
* ...
* worker:CustomWorker,
* ...
* });
*/
worker: {
value: undefined,
writable: true
},
// ===================================================================
// === Task Private Methods ==========================================
// ===================================================================
/**
* This method is called during the execution lifecycle of the task. It is intentionally
* left blank and is up to the instance to describe it functionality.
* @for Task
* @method __onStateChange
* @param {Integer} state The current state of the task
* @param {String} error Message describing error
* @private
*/
__onStateChange: {
value: function(state, error) {},
writable: true
},
// ===================================================================
// === Task Public Methods ===========================================
// ===================================================================
/**
* Calling this method cancels the task. However it is up to the instance to handle
* the canceled state.
* @for Task
* @method cancel
* @example
* var task = new MonkeyBars.Task({
* performTask:function(){
* if(true){
* this.cancel();
* }
* }
* });
* task.start();
*/
cancel: {
value: function() {
if (this._state > STATE_STARTED) {
return;
}
this._state = STATE_CANCELED;
if (this.logLevel >= LOG_INFO) {
log("Canceled: " + this.displayName);
}
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
this.trigger("cancel");
this.__onStateChange(this._state);
this.onCancel();
}
},
/**
* Calling this method says that the tasks execution is now complete.
* @for Task
* @method complete
* @param {Object} data
* @param {String} operation
* @example
* var task = new MonkeyBars.Task({
* performTask:function(){
* this.complete();
* }
* });
* task.start();
*/
complete: {
value: function(data) {
if (this._state > STATE_STARTED) {
return;
}
this._state = STATE_COMPLETED;
if (this.logLevel >= LOG_INFO) {
log("Completed: " + this.displayName);
}
// clear the timeout interval if we actually had one
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
// run the data operation
if (arguments.length !== 0) {
this.operate(data, this);
}
// call completion methods
this.trigger("complete");
this.onComplete();
this.__onStateChange(this._state);
},
writable: true
},
/**
* @for Task
* @function destroy
*/
destroy: {
value: function() {
for (var prop in this) {
if (this.hasOwnProperty(prop)) {
delete this[prop];
}
}
}
},
/**
* Display name for task. Used in logging output.
* @for Task
* @property displayName
* @type String
* @return {String} The display name of the task
* @readonly
*/
displayName: {
get: function() {
if (this.name) {
return this.name;
} else {
return this.type + ":" + this.tid;
}
}
},
/**
* Calling this method to fault a task. If it is part of a group task this will
* also call the groups fault method passing the error up to the group.
* @for Task
* @method fault
* @param {String} error Message associated with the cause of the fault.
* @example
* var task = new MonkeyBars.Task({
* performTask:function(){
* var a = "a";
* if(a != "b") {
* this.fault("a != b");
* }
* }
* });
* task.start();