03.09.2015 Views

Design Patterns

Download - Assembla

Download - Assembla

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8 ■ THE BRIDGE PATTERN 115<br />

DED.Queue = function() {<br />

// Queued requests.<br />

this.queue = [];<br />

// Observable Objects that can notify the client of interesting moments<br />

// on each DED.Queue instance.<br />

this.onComplete = new DED.util.Observer;<br />

this.onFailure = new DED.util.Observer;<br />

this.onFlush = new DED.util.Observer;<br />

// Core properties that set up a frontend queueing system.<br />

this.retryCount = 3;<br />

this.currentRetry = 0;<br />

this.paused = false;<br />

this.timeout = 5000;<br />

this.conn = {};<br />

this.timer = {};<br />

};<br />

DED.Queue.<br />

method('flush', function() {<br />

if (!this.queue.length > 0) {<br />

return;<br />

}<br />

if (this.paused) {<br />

this.paused = false;<br />

return;<br />

}<br />

var that = this;<br />

this.currentRetry++;<br />

var abort = function() {<br />

that.conn.abort();<br />

if (that.currentRetry == that.retryCount) {<br />

that.onFailure.fire();<br />

that.currentRetry = 0;<br />

} else {<br />

that.flush();<br />

}<br />

};<br />

this.timer = window.setTimeout(abort, this.timeout);<br />

var callback = function(o) {<br />

window.clearTimeout(that.timer);<br />

that.currentRetry = 0;<br />

that.queue.shift();<br />

that.onFlush.fire(o.responseText);<br />

if (that.queue.length == 0) {<br />

that.onComplete.fire();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!