3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

perf(automod): bundle automod regex patterns for potentially increased performance

This commit is contained in:
Dragory 2021-10-05 21:30:35 +03:00
parent 5e25792734
commit 392e2da2d1
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1

View file

@ -13,6 +13,8 @@ interface MatchResultType {
type: MatchableTextType;
}
const combinedRegexCache: WeakMap<any, RegExp> = new WeakMap();
export const MatchRegexTrigger = automodTrigger<MatchResultType>()({
configType: t.type({
patterns: t.array(TRegex),
@ -53,19 +55,23 @@ export const MatchRegexTrigger = automodTrigger<MatchResultType>()({
str = normalizeText(str);
}
for (const sourceRegex of trigger.patterns) {
const regex = new RegExp(sourceRegex.source, trigger.case_sensitive && !sourceRegex.ignoreCase ? "" : "i");
if (!combinedRegexCache.has(trigger)) {
const combinedPattern = trigger.patterns.map((p) => `(?:${p.source})`).join("|");
const combinedRegex = new RegExp(combinedPattern, trigger.case_sensitive ? "" : "i");
combinedRegexCache.set(trigger, combinedRegex);
}
const regex = combinedRegexCache.get(trigger)!;
const matches = await pluginData.state.regexRunner.exec(regex, str).catch(allowTimeout);
if (matches?.length) {
return {
extra: {
pattern: sourceRegex.source,
pattern: "<temporarily hidden>",
type,
},
};
}
}
}
return null;
},