Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 516b4f0

Browse files
authored
Add array concat util (#9306)
1 parent c182c1c commit 516b4f0

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/audio/VoiceMessageRecording.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { IEncryptedFile, MatrixClient } from "matrix-js-sdk/src/matrix";
1818
import { SimpleObservable } from "matrix-widget-api";
1919

2020
import { uploadFile } from "../ContentMessages";
21+
import { concat } from "../utils/arrays";
2122
import { IDestroyable } from "../utils/IDestroyable";
2223
import { Singleflight } from "../utils/Singleflight";
2324
import { Playback } from "./Playback";
@@ -148,10 +149,7 @@ export class VoiceMessageRecording implements IDestroyable {
148149

149150
private onDataAvailable = (data: ArrayBuffer) => {
150151
const buf = new Uint8Array(data);
151-
const newBuf = new Uint8Array(this.buffer.length + buf.length);
152-
newBuf.set(this.buffer, 0);
153-
newBuf.set(buf, this.buffer.length);
154-
this.buffer = newBuf;
152+
this.buffer = concat(this.buffer, buf);
155153
};
156154

157155
private get audioBuffer(): Uint8Array {

src/utils/arrays.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,12 @@ export class GroupedArray<K, T> {
304304
return new ArrayUtil(a);
305305
}
306306
}
307+
308+
export const concat = (...arrays: Uint8Array[]): Uint8Array => {
309+
return arrays.reduce((concatenatedSoFar: Uint8Array, toBeConcatenated: Uint8Array) => {
310+
const concatenated = new Uint8Array(concatenatedSoFar.length + toBeConcatenated.length);
311+
concatenated.set(concatenatedSoFar, 0);
312+
concatenated.set(toBeConcatenated, concatenatedSoFar.length);
313+
return concatenated;
314+
}, new Uint8Array(0));
315+
};

test/utils/arrays-test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
arrayIntersection,
2929
ArrayUtil,
3030
GroupedArray,
31+
concat,
3132
} from "../../src/utils/arrays";
3233

3334
type TestParams = { input: number[], output: number[] };
@@ -375,5 +376,32 @@ describe('arrays', () => {
375376
expect(result.value).toEqual(output);
376377
});
377378
});
379+
380+
describe("concat", () => {
381+
const emptyArray = () => new Uint8Array(0);
382+
const array1 = () => new Uint8Array([1, 2, 3]);
383+
const array2 = () => new Uint8Array([4, 5, 6]);
384+
const array3 = () => new Uint8Array([7, 8, 9]);
385+
386+
it("should work for empty arrays", () => {
387+
expect(concat(emptyArray(), emptyArray())).toEqual(emptyArray());
388+
});
389+
390+
it("should concat an empty and non-empty array", () => {
391+
expect(concat(emptyArray(), array1())).toEqual(array1());
392+
});
393+
394+
it("should concat an non-empty and empty array", () => {
395+
expect(concat(array1(), emptyArray())).toEqual(array1());
396+
});
397+
398+
it("should concat two arrays", () => {
399+
expect(concat(array1(), array2())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6]));
400+
});
401+
402+
it("should concat three arrays", () => {
403+
expect(concat(array1(), array2(), array3())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));
404+
});
405+
});
378406
});
379407

0 commit comments

Comments
 (0)