Skip loading videos into memory for performance
This commit is contained in:
parent
c6904476f4
commit
5d0469adef
1 changed files with 38 additions and 5 deletions
|
@ -2,7 +2,9 @@
|
||||||
* @prettier
|
* @prettier
|
||||||
*/
|
*/
|
||||||
import is from '@sindresorhus/is';
|
import is from '@sindresorhus/is';
|
||||||
|
import { partition, sortBy } from 'lodash';
|
||||||
|
|
||||||
|
import * as MIME from '../../../../types/MIME';
|
||||||
import { arrayBufferToObjectURL } from '../../../../util/arrayBufferToObjectURL';
|
import { arrayBufferToObjectURL } from '../../../../util/arrayBufferToObjectURL';
|
||||||
import { Attachment } from '../../../../types/Attachment';
|
import { Attachment } from '../../../../types/Attachment';
|
||||||
import { MapAsync } from '../../../../types/MapAsync';
|
import { MapAsync } from '../../../../types/MapAsync';
|
||||||
|
@ -16,24 +18,55 @@ export type Message = {
|
||||||
const DEFAULT_CONTENT_TYPE: MIMEType = 'application/octet-stream' as MIMEType;
|
const DEFAULT_CONTENT_TYPE: MIMEType = 'application/octet-stream' as MIMEType;
|
||||||
|
|
||||||
export const loadWithObjectURL = (loadMessage: MapAsync<Message>) => async (
|
export const loadWithObjectURL = (loadMessage: MapAsync<Message>) => async (
|
||||||
media: Array<Message>
|
messages: Array<Message>
|
||||||
): Promise<Array<Message>> => {
|
): Promise<Array<Message>> => {
|
||||||
if (!is.function_(loadMessage)) {
|
if (!is.function_(loadMessage)) {
|
||||||
throw new TypeError("'loadMessage' must be a function");
|
throw new TypeError("'loadMessage' must be a function");
|
||||||
}
|
}
|
||||||
if (!is.array(media)) {
|
if (!is.array(messages)) {
|
||||||
throw new TypeError("'media' must be a function");
|
throw new TypeError("'messages' must be an array");
|
||||||
}
|
}
|
||||||
|
|
||||||
const mediaWithAttachmentData = await Promise.all(media.map(loadMessage));
|
// Messages with video are too expensive to load into memory, so we don’t:
|
||||||
return mediaWithAttachmentData.map(withObjectURL);
|
const [messagesWithVideo, messagesWithoutVideo] = partition(
|
||||||
|
messages,
|
||||||
|
hasVideoAttachment
|
||||||
|
);
|
||||||
|
const loadedMessagesWithoutVideo = await Promise.all(
|
||||||
|
messagesWithoutVideo.map(loadMessage)
|
||||||
|
);
|
||||||
|
const loadedMessages = sortBy(
|
||||||
|
[...messagesWithVideo, ...loadedMessagesWithoutVideo],
|
||||||
|
message => -message.received_at
|
||||||
|
);
|
||||||
|
|
||||||
|
return loadedMessages.map(withObjectURL);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hasVideoAttachment = (message: Message): boolean =>
|
||||||
|
message.attachments.some(
|
||||||
|
attachment =>
|
||||||
|
!is.undefined(attachment.contentType) &&
|
||||||
|
MIME.isVideo(attachment.contentType)
|
||||||
|
);
|
||||||
|
|
||||||
const withObjectURL = (message: Message): Message => {
|
const withObjectURL = (message: Message): Message => {
|
||||||
if (message.attachments.length === 0) {
|
if (message.attachments.length === 0) {
|
||||||
throw new TypeError('`message.attachments` cannot be empty');
|
throw new TypeError('`message.attachments` cannot be empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
const attachment = message.attachments[0];
|
const attachment = message.attachments[0];
|
||||||
|
if (typeof attachment.contentType === 'undefined') {
|
||||||
|
throw new TypeError('`attachment.contentType` is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MIME.isVideo(attachment.contentType)) {
|
||||||
|
return {
|
||||||
|
...message,
|
||||||
|
objectURL: 'images/video.svg',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const objectURL = arrayBufferToObjectURL({
|
const objectURL = arrayBufferToObjectURL({
|
||||||
data: attachment.data,
|
data: attachment.data,
|
||||||
type: attachment.contentType || DEFAULT_CONTENT_TYPE,
|
type: attachment.contentType || DEFAULT_CONTENT_TYPE,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue