Skip to content

Commit d8a7df5

Browse files
committed
es5classify fifo
1 parent 5c734db commit d8a7df5

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

utils.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,38 +1110,41 @@ export function resizeUint8Array(array, byteSize) {
11101110
return newArray;
11111111
}
11121112

1113-
export function Fifo(capacity) {
1114-
this.buffer = new Uint8Array(capacity);
1115-
this.size = 0;
1116-
this.wPtr = 0;
1117-
this.rPtr = 0;
1113+
export class Fifo {
1114+
constructor(capacity) {
1115+
this._buffer = new Uint8Array(capacity);
1116+
this._size = 0;
1117+
this._wPtr = 0;
1118+
this._rPtr = 0;
1119+
}
1120+
/** @returns {number} */
1121+
get size() { return this._size; }
1122+
/** @returns {boolean} */
1123+
get full() {
1124+
return this._size === this._buffer.length;
1125+
}
1126+
/** @returns {boolean} */
1127+
get empty() {
1128+
return this._size === 0;
1129+
}
1130+
clear() {
1131+
this._size = 0;
1132+
this._wPtr = 0;
1133+
this._rPtr = 0;
1134+
}
1135+
/** @type {Number} b */
1136+
put(b) {
1137+
if (this.full) return;
1138+
this._buffer[this._wPtr % this._buffer.length] = b;
1139+
this._wPtr++;
1140+
this._size++;
1141+
}
1142+
/** @returns {Number} */
1143+
get() {
1144+
if (this.empty) return;
1145+
const res = this._buffer[this._rPtr % this._buffer.length];
1146+
this._rPtr++;
1147+
this._size--;
1148+
return res;
1149+
}
11181150
}
1119-
1120-
Fifo.prototype.full = function () {
1121-
return this.size === this.buffer.length;
1122-
};
1123-
1124-
Fifo.prototype.empty = function () {
1125-
return this.size === 0;
1126-
};
1127-
1128-
Fifo.prototype.clear = function () {
1129-
this.size = 0;
1130-
this.wPtr = 0;
1131-
this.rPtr = 0;
1132-
};
1133-
1134-
Fifo.prototype.put = function (b) {
1135-
if (this.full()) return;
1136-
this.buffer[this.wPtr % this.buffer.length] = b;
1137-
this.wPtr++;
1138-
this.size++;
1139-
};
1140-
1141-
Fifo.prototype.get = function () {
1142-
if (this.empty()) return;
1143-
const res = this.buffer[this.rPtr % this.buffer.length];
1144-
this.rPtr++;
1145-
this.size--;
1146-
return res;
1147-
};

0 commit comments

Comments
 (0)