feat: improve performance for mute

This commit is contained in:
Shusui MOYATANI
2024-02-22 21:13:35 +09:00
parent 0e384b8c4b
commit 00fd1d71e3
4 changed files with 30 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
import { createRoot, type Accessor, type Setter } from 'solid-js';
import { createRoot, type Accessor, type Setter, createMemo } from 'solid-js';
import sortBy from 'lodash/sortBy';
import uniq from 'lodash/uniq';
@@ -21,6 +21,7 @@ import {
} from '@/hooks/createSignalWithStorage';
import { useTranslation } from '@/i18n/useTranslation';
import { genericEvent } from '@/nostr/event';
import { asCaseInsensitive, wordsRegex } from '@/utils/regex';
export type CustomEmojiConfig = {
shortcode: string;
@@ -218,11 +219,13 @@ const useConfig = (): UseConfig => {
[(e) => e.shortcode.length],
);
const isPubkeyMuted = (pubkey: string) => config.mutedPubkeys.includes(pubkey);
const mutedPubkeySet = createMemo(() => new Set(config.mutedPubkeys));
const isPubkeyMuted = (pubkey: string) => mutedPubkeySet().has(pubkey);
const mutedKeywordsRegex = createMemo(() => asCaseInsensitive(wordsRegex(config.mutedKeywords)));
const hasMutedKeyword = (event: NostrEvent) => {
if (event.kind === Kind.ShortTextNote) {
return config.mutedKeywords.some((keyword) => event.content.includes(keyword));
return mutedKeywordsRegex().test(event.content);
}
return false;
};

View File

@@ -28,6 +28,7 @@ const useEvent = (propsProvider: () => UseEventProps | null): UseEvent => {
const promise = task.firstEventPromise().catch(() => {
throw new Error(`event not found: ${eventId}`);
});
console.log('useEvent', props());
registerTask({ task, signal });
return timeout(15000, `useEvent: ${eventId}`)(promise);
},

15
src/utils/regex.test.ts Normal file
View File

@@ -0,0 +1,15 @@
import assert from 'assert';
import { describe, it } from 'vitest';
import { escapeRegExpSymbols } from '@/utils/regex';
describe('escapeRegExpString', () => {
it.each<{ given: string; expected: string }>([
{ given: '^hello$', expected: '\\^hello\\$' },
{ given: '.*+?^${}()|[]\\', expected: '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\' },
])('should return $expected for $given', ({ given, expected }) => {
const actual = escapeRegExpSymbols(given);
assert.deepStrictEqual(actual, expected);
});
});

8
src/utils/regex.ts Normal file
View File

@@ -0,0 +1,8 @@
export const escapeRegExpSymbols = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
export const wordsRegex = (words: string[]) => new RegExp(words.map(escapeRegExpSymbols).join('|'));
export const asCaseInsensitive = (regex: RegExp) => {
if (regex.flags.indexOf('i') >= 0) return regex;
return new RegExp(regex.source, `${regex.flags}i`);
};