From 516b4f0ff82c31d325d9d899091ed6165d45c9b1 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Wed, 21 Sep 2022 20:06:05 +0200 Subject: [PATCH] Add array concat util (#9306) --- src/audio/VoiceMessageRecording.ts | 6 ++---- src/utils/arrays.ts | 9 +++++++++ test/utils/arrays-test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/audio/VoiceMessageRecording.ts b/src/audio/VoiceMessageRecording.ts index 39951ff278e..2e101f903a5 100644 --- a/src/audio/VoiceMessageRecording.ts +++ b/src/audio/VoiceMessageRecording.ts @@ -18,6 +18,7 @@ import { IEncryptedFile, MatrixClient } from "matrix-js-sdk/src/matrix"; import { SimpleObservable } from "matrix-widget-api"; import { uploadFile } from "../ContentMessages"; +import { concat } from "../utils/arrays"; import { IDestroyable } from "../utils/IDestroyable"; import { Singleflight } from "../utils/Singleflight"; import { Playback } from "./Playback"; @@ -148,10 +149,7 @@ export class VoiceMessageRecording implements IDestroyable { private onDataAvailable = (data: ArrayBuffer) => { const buf = new Uint8Array(data); - const newBuf = new Uint8Array(this.buffer.length + buf.length); - newBuf.set(this.buffer, 0); - newBuf.set(buf, this.buffer.length); - this.buffer = newBuf; + this.buffer = concat(this.buffer, buf); }; private get audioBuffer(): Uint8Array { diff --git a/src/utils/arrays.ts b/src/utils/arrays.ts index a0fddde45cd..b82be21443a 100644 --- a/src/utils/arrays.ts +++ b/src/utils/arrays.ts @@ -304,3 +304,12 @@ export class GroupedArray { return new ArrayUtil(a); } } + +export const concat = (...arrays: Uint8Array[]): Uint8Array => { + return arrays.reduce((concatenatedSoFar: Uint8Array, toBeConcatenated: Uint8Array) => { + const concatenated = new Uint8Array(concatenatedSoFar.length + toBeConcatenated.length); + concatenated.set(concatenatedSoFar, 0); + concatenated.set(toBeConcatenated, concatenatedSoFar.length); + return concatenated; + }, new Uint8Array(0)); +}; diff --git a/test/utils/arrays-test.ts b/test/utils/arrays-test.ts index 7dabbb2981a..f0cc52e0a99 100644 --- a/test/utils/arrays-test.ts +++ b/test/utils/arrays-test.ts @@ -28,6 +28,7 @@ import { arrayIntersection, ArrayUtil, GroupedArray, + concat, } from "../../src/utils/arrays"; type TestParams = { input: number[], output: number[] }; @@ -375,5 +376,32 @@ describe('arrays', () => { expect(result.value).toEqual(output); }); }); + + describe("concat", () => { + const emptyArray = () => new Uint8Array(0); + const array1 = () => new Uint8Array([1, 2, 3]); + const array2 = () => new Uint8Array([4, 5, 6]); + const array3 = () => new Uint8Array([7, 8, 9]); + + it("should work for empty arrays", () => { + expect(concat(emptyArray(), emptyArray())).toEqual(emptyArray()); + }); + + it("should concat an empty and non-empty array", () => { + expect(concat(emptyArray(), array1())).toEqual(array1()); + }); + + it("should concat an non-empty and empty array", () => { + expect(concat(array1(), emptyArray())).toEqual(array1()); + }); + + it("should concat two arrays", () => { + expect(concat(array1(), array2())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6])); + }); + + it("should concat three arrays", () => { + expect(concat(array1(), array2(), array3())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])); + }); + }); });