(val)
| 129 | this._count = 0; |
| 130 | } |
| 131 | add(val) { |
| 132 | // Attempt to reserve an id |
| 133 | |
| 134 | let id; |
| 135 | // Optimized paths |
| 136 | if (this._cur < MAX_CHANNEL) { |
| 137 | id = ++this._cur; |
| 138 | } else if (this._count === 0) { |
| 139 | // Revert and reset back to fast path once we no longer have any channels |
| 140 | // open |
| 141 | this._cur = 0; |
| 142 | id = 0; |
| 143 | } else { |
| 144 | // Slower lookup path |
| 145 | |
| 146 | // This path is triggered we have opened at least MAX_CHANNEL channels |
| 147 | // while having at least one channel open at any given time, so we have |
| 148 | // to search for a free id. |
| 149 | const channels = this._channels; |
| 150 | for (let i = 0; i < MAX_CHANNEL; ++i) { |
| 151 | if (channels[i] === undefined) { |
| 152 | id = i; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (id === undefined) |
| 159 | return -1; |
| 160 | |
| 161 | this._channels[id] = (val || true); |
| 162 | ++this._count; |
| 163 | |
| 164 | return id; |
| 165 | } |
| 166 | update(id, val) { |
| 167 | if (typeof id !== 'number' || id < 0 || id >= MAX_CHANNEL || !isFinite(id)) |
| 168 | throw new Error(`Invalid channel id: ${id}`); |
no outgoing calls
no test coverage detected