[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | var util = require('util')
|
---|
| 4 | var isNode = require('detect-node')
|
---|
| 5 |
|
---|
| 6 | // Node.js 0.8, 0.10 and 0.12 support
|
---|
| 7 | Object.assign = (process.versions.modules >= 46 || !isNode)
|
---|
| 8 | ? Object.assign // eslint-disable-next-line
|
---|
| 9 | : util._extend
|
---|
| 10 |
|
---|
| 11 | function QueueItem () {
|
---|
| 12 | this.prev = null
|
---|
| 13 | this.next = null
|
---|
| 14 | }
|
---|
| 15 | exports.QueueItem = QueueItem
|
---|
| 16 |
|
---|
| 17 | function Queue () {
|
---|
| 18 | QueueItem.call(this)
|
---|
| 19 |
|
---|
| 20 | this.prev = this
|
---|
| 21 | this.next = this
|
---|
| 22 | }
|
---|
| 23 | util.inherits(Queue, QueueItem)
|
---|
| 24 | exports.Queue = Queue
|
---|
| 25 |
|
---|
| 26 | Queue.prototype.insertTail = function insertTail (item) {
|
---|
| 27 | item.prev = this.prev
|
---|
| 28 | item.next = this
|
---|
| 29 | item.prev.next = item
|
---|
| 30 | item.next.prev = item
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | Queue.prototype.remove = function remove (item) {
|
---|
| 34 | var next = item.next
|
---|
| 35 | var prev = item.prev
|
---|
| 36 |
|
---|
| 37 | item.next = item
|
---|
| 38 | item.prev = item
|
---|
| 39 | next.prev = prev
|
---|
| 40 | prev.next = next
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | Queue.prototype.head = function head () {
|
---|
| 44 | return this.next
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | Queue.prototype.tail = function tail () {
|
---|
| 48 | return this.prev
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | Queue.prototype.isEmpty = function isEmpty () {
|
---|
| 52 | return this.next === this
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | Queue.prototype.isRoot = function isRoot (item) {
|
---|
| 56 | return this === item
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | function LockStream (stream) {
|
---|
| 60 | this.locked = false
|
---|
| 61 | this.queue = []
|
---|
| 62 | this.stream = stream
|
---|
| 63 | }
|
---|
| 64 | exports.LockStream = LockStream
|
---|
| 65 |
|
---|
| 66 | LockStream.prototype.write = function write (chunks, callback) {
|
---|
| 67 | var self = this
|
---|
| 68 |
|
---|
| 69 | // Do not let it interleave
|
---|
| 70 | if (this.locked) {
|
---|
| 71 | this.queue.push(function () {
|
---|
| 72 | return self.write(chunks, callback)
|
---|
| 73 | })
|
---|
| 74 | return
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | this.locked = true
|
---|
| 78 |
|
---|
| 79 | function done (err, chunks) {
|
---|
| 80 | self.stream.removeListener('error', done)
|
---|
| 81 |
|
---|
| 82 | self.locked = false
|
---|
| 83 | if (self.queue.length > 0) { self.queue.shift()() }
|
---|
| 84 | callback(err, chunks)
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | this.stream.on('error', done)
|
---|
| 88 |
|
---|
| 89 | // Accumulate all output data
|
---|
| 90 | var output = []
|
---|
| 91 | function onData (chunk) {
|
---|
| 92 | output.push(chunk)
|
---|
| 93 | }
|
---|
| 94 | this.stream.on('data', onData)
|
---|
| 95 |
|
---|
| 96 | function next (err) {
|
---|
| 97 | self.stream.removeListener('data', onData)
|
---|
| 98 | if (err) {
|
---|
| 99 | return done(err)
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | done(null, output)
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | for (var i = 0; i < chunks.length - 1; i++) { this.stream.write(chunks[i]) }
|
---|
| 106 |
|
---|
| 107 | if (chunks.length > 0) {
|
---|
| 108 | this.stream.write(chunks[i], next)
|
---|
| 109 | } else { process.nextTick(next) }
|
---|
| 110 |
|
---|
| 111 | if (this.stream.execute) {
|
---|
| 112 | this.stream.execute(function (err) {
|
---|
| 113 | if (err) { return done(err) }
|
---|
| 114 | })
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // Just finds the place in array to insert
|
---|
| 119 | function binaryLookup (list, item, compare) {
|
---|
| 120 | var start = 0
|
---|
| 121 | var end = list.length
|
---|
| 122 |
|
---|
| 123 | while (start < end) {
|
---|
| 124 | var pos = (start + end) >> 1
|
---|
| 125 | var cmp = compare(item, list[pos])
|
---|
| 126 |
|
---|
| 127 | if (cmp === 0) {
|
---|
| 128 | start = pos
|
---|
| 129 | end = pos
|
---|
| 130 | break
|
---|
| 131 | } else if (cmp < 0) {
|
---|
| 132 | end = pos
|
---|
| 133 | } else {
|
---|
| 134 | start = pos + 1
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | return start
|
---|
| 139 | }
|
---|
| 140 | exports.binaryLookup = binaryLookup
|
---|
| 141 |
|
---|
| 142 | function binaryInsert (list, item, compare) {
|
---|
| 143 | var index = binaryLookup(list, item, compare)
|
---|
| 144 |
|
---|
| 145 | list.splice(index, 0, item)
|
---|
| 146 | }
|
---|
| 147 | exports.binaryInsert = binaryInsert
|
---|
| 148 |
|
---|
| 149 | function binarySearch (list, item, compare) {
|
---|
| 150 | var index = binaryLookup(list, item, compare)
|
---|
| 151 |
|
---|
| 152 | if (index >= list.length) {
|
---|
| 153 | return -1
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if (compare(item, list[index]) === 0) {
|
---|
| 157 | return index
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return -1
|
---|
| 161 | }
|
---|
| 162 | exports.binarySearch = binarySearch
|
---|
| 163 |
|
---|
| 164 | function Timeout (object) {
|
---|
| 165 | this.delay = 0
|
---|
| 166 | this.timer = null
|
---|
| 167 | this.object = object
|
---|
| 168 | }
|
---|
| 169 | exports.Timeout = Timeout
|
---|
| 170 |
|
---|
| 171 | Timeout.prototype.set = function set (delay, callback) {
|
---|
| 172 | this.delay = delay
|
---|
| 173 | this.reset()
|
---|
| 174 | if (!callback) { return }
|
---|
| 175 |
|
---|
| 176 | if (this.delay === 0) {
|
---|
| 177 | this.object.removeListener('timeout', callback)
|
---|
| 178 | } else {
|
---|
| 179 | this.object.once('timeout', callback)
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | Timeout.prototype.reset = function reset () {
|
---|
| 184 | if (this.timer !== null) {
|
---|
| 185 | clearTimeout(this.timer)
|
---|
| 186 | this.timer = null
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | if (this.delay === 0) { return }
|
---|
| 190 |
|
---|
| 191 | var self = this
|
---|
| 192 | this.timer = setTimeout(function () {
|
---|
| 193 | self.timer = null
|
---|
| 194 | self.object.emit('timeout')
|
---|
| 195 | }, this.delay)
|
---|
| 196 | }
|
---|