home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / google3 / thread-queue.js < prev   
Text File  |  2006-08-07  |  3KB  |  85 lines

  1. function TH_ThreadQueue(opt_config) {
  2. this.runtime_ = 100;
  3. this.delay_ = 25;
  4. this.interleave_ = true;
  5. if (opt_config) {
  6. this.interleave_ = (opt_config.interleave === true);
  7. if (opt_config.runtime) this.runtime_ = opt_config.runtime;
  8. if (opt_config.delay) this.delay_ = opt_config.delay;
  9. }
  10. this.workers_ = [];
  11. this.guid_ = 0;
  12. this.iterate_ = Function.prototype.bind.call(this.runIteration_, this);
  13. }
  14. TH_ThreadQueue.prototype.addWorker = function(worker, opt_isComplete) {
  15. if (worker.__workerId) {
  16. throw new Error("Cannot add a worker to a thread while it is currently " +
  17. "running in the thread.");
  18. }
  19. worker.__isComplete = opt_isComplete || function() { return false; };
  20. worker.__workerId = this.guid_++;
  21. this.workers_.push(worker);
  22. if (this.workers_.length == 1) {
  23. this.setCurrentWorkerByIndex_(0);
  24. this.iterate_();
  25. }
  26. }
  27. TH_ThreadQueue.prototype.removeWorker = function(worker) {
  28. for (var i = 0; i < this.workers_.length; i++) {
  29. if (worker.__workerId == this.workers_[i].__workerId) {
  30. this.removeWorkerByIndex_(i);
  31. }
  32. }
  33. }
  34. TH_ThreadQueue.prototype.removeWorkerByIndex_ = function(workerIndex) {
  35. var worker = this.workers_[workerIndex];
  36. delete(worker.__isComplete);
  37. delete(worker.__workerId);
  38. this.workers_.splice(workerIndex, 1);
  39. var newWorkerIndex = this.currentWorkerIndex_ % this.workers_.length;
  40. this.setCurrentWorkerByIndex_(newWorkerIndex);
  41. }
  42. TH_ThreadQueue.prototype.setDelay = function(delay) {
  43. this.delay_ = delay;
  44. }
  45. TH_ThreadQueue.prototype.setRuntime = function(runtime) {
  46. this.runtime_ = runtime;
  47. }
  48. TH_ThreadQueue.prototype.run = function() {
  49. this.running_ = true;
  50. this.iterate_();
  51. }
  52. TH_ThreadQueue.prototype.pause = function() {
  53. this.running_ = false;
  54. }
  55. TH_ThreadQueue.prototype.runIteration_ = function() {
  56. if (!this.running_ || this.workers_.length == 0) {
  57. return;
  58. }
  59. var startTime = (new Date()).getTime();
  60. while ((new Date()).getTime() - startTime < this.runtime_) {
  61. if (this.currentWorker_.__isComplete()) {
  62. this.removeWorkerByIndex_(this.currentWorkerIndex_);
  63. break;
  64. }
  65. this.currentWorker_();
  66. if (this.interleave_) this.nextWorker_();
  67. }
  68. if (typeof top != "undefined" && top.setTimeout) {
  69. top.setTimeout(this.iterate_, this.delay_);
  70. } else if (G_Alarm) {
  71. new G_Alarm(this.iterate_, this.delay_);
  72. } else {
  73. throw new Error("Could not find a mechanism to start a timeout. Need " +
  74. "window.setTimeout or G_Alarm.");
  75. }
  76. }
  77. TH_ThreadQueue.prototype.setCurrentWorkerByIndex_ = function(workerIndex) {
  78. this.currentWorkerIndex_ = workerIndex;
  79. this.currentWorker_ = this.workers_[workerIndex];
  80. }
  81. TH_ThreadQueue.prototype.nextWorker_ = function() {
  82. var nextWorkerIndex = (this.currentWorkerIndex_ + 1) % this.workers_.length;
  83. this.setCurrentWorkerByIndex_(nextWorkerIndex);
  84. }
  85.