1 | class ViewableBuffer {
|
2 | constructor(sizeOrBuffer) {
|
3 | if (typeof sizeOrBuffer === "number") {
|
4 | this.rawBinaryData = new ArrayBuffer(sizeOrBuffer);
|
5 | } else if (sizeOrBuffer instanceof Uint8Array) {
|
6 | this.rawBinaryData = sizeOrBuffer.buffer;
|
7 | } else {
|
8 | this.rawBinaryData = sizeOrBuffer;
|
9 | }
|
10 | this.uint32View = new Uint32Array(this.rawBinaryData);
|
11 | this.float32View = new Float32Array(this.rawBinaryData);
|
12 | }
|
13 | get int8View() {
|
14 | if (!this._int8View) {
|
15 | this._int8View = new Int8Array(this.rawBinaryData);
|
16 | }
|
17 | return this._int8View;
|
18 | }
|
19 | get uint8View() {
|
20 | if (!this._uint8View) {
|
21 | this._uint8View = new Uint8Array(this.rawBinaryData);
|
22 | }
|
23 | return this._uint8View;
|
24 | }
|
25 | get int16View() {
|
26 | if (!this._int16View) {
|
27 | this._int16View = new Int16Array(this.rawBinaryData);
|
28 | }
|
29 | return this._int16View;
|
30 | }
|
31 | get uint16View() {
|
32 | if (!this._uint16View) {
|
33 | this._uint16View = new Uint16Array(this.rawBinaryData);
|
34 | }
|
35 | return this._uint16View;
|
36 | }
|
37 | get int32View() {
|
38 | if (!this._int32View) {
|
39 | this._int32View = new Int32Array(this.rawBinaryData);
|
40 | }
|
41 | return this._int32View;
|
42 | }
|
43 | view(type) {
|
44 | return this[`${type}View`];
|
45 | }
|
46 | destroy() {
|
47 | this.rawBinaryData = null;
|
48 | this._int8View = null;
|
49 | this._uint8View = null;
|
50 | this._int16View = null;
|
51 | this._uint16View = null;
|
52 | this._int32View = null;
|
53 | this.uint32View = null;
|
54 | this.float32View = null;
|
55 | }
|
56 | static sizeOf(type) {
|
57 | switch (type) {
|
58 | case "int8":
|
59 | case "uint8":
|
60 | return 1;
|
61 | case "int16":
|
62 | case "uint16":
|
63 | return 2;
|
64 | case "int32":
|
65 | case "uint32":
|
66 | case "float32":
|
67 | return 4;
|
68 | default:
|
69 | throw new Error(`${type} isn't a valid view type`);
|
70 | }
|
71 | }
|
72 | }
|
73 |
|
74 | export { ViewableBuffer };
|
75 |
|