1 | class 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 |
|
6 | get int8View() {
|
7 | return this._int8View || (this._int8View = new Int8Array(this.rawBinaryData)), this._int8View;
|
8 | }
|
9 |
|
10 | get uint8View() {
|
11 | return this._uint8View || (this._uint8View = new Uint8Array(this.rawBinaryData)), this._uint8View;
|
12 | }
|
13 |
|
14 | get int16View() {
|
15 | return this._int16View || (this._int16View = new Int16Array(this.rawBinaryData)), this._int16View;
|
16 | }
|
17 |
|
18 | get uint16View() {
|
19 | return this._uint16View || (this._uint16View = new Uint16Array(this.rawBinaryData)), this._uint16View;
|
20 | }
|
21 |
|
22 | get int32View() {
|
23 | return this._int32View || (this._int32View = new Int32Array(this.rawBinaryData)), this._int32View;
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | view(type) {
|
32 | return this[`${type}View`];
|
33 | }
|
34 |
|
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 | }
|
55 | export {
|
56 | ViewableBuffer
|
57 | };
|
58 |
|