UNPKG

2.23 kBJavaScriptView Raw
1class ViewableBuffer {
2 constructor(sizeOrBuffer) {
3 typeof sizeOrBuffer == "number" ? this.rawBinaryData = new ArrayBuffer(sizeOrBuffer) : sizeOrBuffer instanceof Uint8Array ? this.rawBinaryData = sizeOrBuffer.buffer : this.rawBinaryData = sizeOrBuffer, this.uint32View = new Uint32Array(this.rawBinaryData), this.float32View = new Float32Array(this.rawBinaryData);
4 }
5 /** View on the raw binary data as a `Int8Array`. */
6 get int8View() {
7 return this._int8View || (this._int8View = new Int8Array(this.rawBinaryData)), this._int8View;
8 }
9 /** View on the raw binary data as a `Uint8Array`. */
10 get uint8View() {
11 return this._uint8View || (this._uint8View = new Uint8Array(this.rawBinaryData)), this._uint8View;
12 }
13 /** View on the raw binary data as a `Int16Array`. */
14 get int16View() {
15 return this._int16View || (this._int16View = new Int16Array(this.rawBinaryData)), this._int16View;
16 }
17 /** View on the raw binary data as a `Uint16Array`. */
18 get uint16View() {
19 return this._uint16View || (this._uint16View = new Uint16Array(this.rawBinaryData)), this._uint16View;
20 }
21 /** View on the raw binary data as a `Int32Array`. */
22 get int32View() {
23 return this._int32View || (this._int32View = new Int32Array(this.rawBinaryData)), this._int32View;
24 }
25 /**
26 * Returns the view of the given type.
27 * @param type - One of `int8`, `uint8`, `int16`,
28 * `uint16`, `int32`, `uint32`, and `float32`.
29 * @returns - typed array of given type
30 */
31 view(type) {
32 return this[`${type}View`];
33 }
34 /** Destroys all buffer references. Do not use after calling this. */
35 destroy() {
36 this.rawBinaryData = null, this._int8View = null, this._uint8View = null, this._int16View = null, this._uint16View = null, this._int32View = null, this.uint32View = null, this.float32View = null;
37 }
38 static sizeOf(type) {
39 switch (type) {
40 case "int8":
41 case "uint8":
42 return 1;
43 case "int16":
44 case "uint16":
45 return 2;
46 case "int32":
47 case "uint32":
48 case "float32":
49 return 4;
50 default:
51 throw new Error(`${type} isn't a valid view type`);
52 }
53 }
54}
55export {
56 ViewableBuffer
57};
58//# sourceMappingURL=ViewableBuffer.mjs.map