Use ReadonlyArrays in conversation model and redux

This commit is contained in:
Fedor Indutny 2022-12-21 16:07:02 -08:00 committed by GitHub
parent ecbf84638d
commit dec23725e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 173 additions and 162 deletions

View file

@ -471,7 +471,7 @@ export type DataInterface = {
options: { forceSave?: boolean; ourUuid: UUIDStringType }
) => Promise<void>;
removeMessage: (id: string) => Promise<void>;
removeMessages: (ids: Array<string>) => Promise<void>;
removeMessages: (ids: ReadonlyArray<string>) => Promise<void>;
getTotalUnreadForConversation: (
conversationId: string,
options: {

View file

@ -1509,7 +1509,7 @@ async function updateConversations(
})();
}
function removeConversationsSync(ids: Array<string>): void {
function removeConversationsSync(ids: ReadonlyArray<string>): void {
const db = getInstance();
// Our node interface doesn't seem to allow you to replace one single ? with an array
@ -2046,7 +2046,7 @@ async function removeMessage(id: string): Promise<void> {
db.prepare<Query>('DELETE FROM messages WHERE id = $id;').run({ id });
}
function removeMessagesSync(ids: Array<string>): void {
function removeMessagesSync(ids: ReadonlyArray<string>): void {
const db = getInstance();
db.prepare<ArrayQuery>(
@ -2057,7 +2057,7 @@ function removeMessagesSync(ids: Array<string>): void {
).run(ids);
}
async function removeMessages(ids: Array<string>): Promise<void> {
async function removeMessages(ids: ReadonlyArray<string>): Promise<void> {
batchMultiVarQuery(getInstance(), ids, removeMessagesSync);
}
@ -2091,7 +2091,7 @@ async function getMessagesById(
return batchMultiVarQuery(
db,
messageIds,
(batch: Array<string>): Array<MessageType> => {
(batch: ReadonlyArray<string>): Array<MessageType> => {
const query = db.prepare<ArrayQuery>(
`SELECT json FROM messages WHERE id IN (${Array(batch.length)
.fill('?')
@ -2325,7 +2325,7 @@ async function getUnreadReactionsAndMarkRead({
});
const idsToUpdate = unreadMessages.map(item => item.rowid);
batchMultiVarQuery(db, idsToUpdate, (ids: Array<number>): void => {
batchMultiVarQuery(db, idsToUpdate, (ids: ReadonlyArray<number>): void => {
db.prepare<ArrayQuery>(
`
UPDATE reactions SET
@ -3408,7 +3408,7 @@ async function getAllUnprocessedAndIncrementAttempts(): Promise<
})();
}
function removeUnprocessedsSync(ids: Array<string>): void {
function removeUnprocessedsSync(ids: ReadonlyArray<string>): void {
const db = getInstance();
db.prepare<ArrayQuery>(
@ -4680,7 +4680,7 @@ function modifyStoryDistributionMembersSync(
});
}
batchMultiVarQuery(db, toRemove, (uuids: Array<UUIDStringType>) => {
batchMultiVarQuery(db, toRemove, (uuids: ReadonlyArray<UUIDStringType>) => {
db.prepare<ArrayQuery>(
`
DELETE FROM storyDistributionMembers
@ -5166,7 +5166,7 @@ async function getKnownMessageAttachments(
const messages = batchMultiVarQuery(
db,
rowids,
(batch: Array<number>): Array<MessageType> => {
(batch: ReadonlyArray<number>): Array<MessageType> => {
const query = db.prepare<ArrayQuery>(
`SELECT json FROM messages WHERE rowid IN (${Array(batch.length)
.fill('?')

View file

@ -57,7 +57,7 @@ export default function updateToSchemaVersion42(
}
});
function deleteReactions(rowids: Array<number>) {
function deleteReactions(rowids: ReadonlyArray<number>) {
db.prepare<ArrayQuery>(
`
DELETE FROM reactions

View file

@ -5,7 +5,7 @@ import type { Database } from '@signalapp/better-sqlite3';
import { isNumber, last } from 'lodash';
export type EmptyQuery = [];
export type ArrayQuery = Array<Array<null | number | bigint | string>>;
export type ArrayQuery = Array<ReadonlyArray<null | number | bigint | string>>;
export type Query = {
[key: string]: null | number | bigint | string | Uint8Array;
};
@ -72,21 +72,21 @@ export function getSQLCipherVersion(db: Database): string | undefined {
export function batchMultiVarQuery<ValueT>(
db: Database,
values: Array<ValueT>,
query: (batch: Array<ValueT>) => void
values: ReadonlyArray<ValueT>,
query: (batch: ReadonlyArray<ValueT>) => void
): [];
export function batchMultiVarQuery<ValueT, ResultT>(
db: Database,
values: Array<ValueT>,
query: (batch: Array<ValueT>) => Array<ResultT>
values: ReadonlyArray<ValueT>,
query: (batch: ReadonlyArray<ValueT>) => Array<ResultT>
): Array<ResultT>;
export function batchMultiVarQuery<ValueT, ResultT>(
db: Database,
values: Array<ValueT>,
values: ReadonlyArray<ValueT>,
query:
| ((batch: Array<ValueT>) => void)
| ((batch: Array<ValueT>) => Array<ResultT>)
| ((batch: ReadonlyArray<ValueT>) => void)
| ((batch: ReadonlyArray<ValueT>) => Array<ResultT>)
): Array<ResultT> {
if (values.length > MAX_VARIABLE_COUNT) {
const result: Array<ResultT> = [];
@ -187,7 +187,7 @@ export function removeById<Key extends string | number>(
throw new Error('removeById: No ids to delete!');
}
const removeByIdsSync = (ids: Array<string | number>): void => {
const removeByIdsSync = (ids: ReadonlyArray<string | number>): void => {
db.prepare<ArrayQuery>(
`
DELETE FROM ${table}