From 9894d216a10e816b826826ce5a3da08b9d45255c Mon Sep 17 00:00:00 2001 From: ShuangYa Date: Thu, 4 Jul 2024 20:06:46 +0800 Subject: [PATCH] fix: record history --- src/pages/background/core/rules.ts | 36 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/pages/background/core/rules.ts b/src/pages/background/core/rules.ts index 8eddaa2..3abb1e7 100644 --- a/src/pages/background/core/rules.ts +++ b/src/pages/background/core/rules.ts @@ -94,6 +94,25 @@ function filter(fromRules: InitdRule[], options: RuleFilterOptions) { return rules; } +function saveRuleHistory(rule: Rule) { + if ( + prefs.get('rule-history') && + !rule.isFunction && + [RULE_TYPE.MODIFY_RECV_HEADER, RULE_TYPE.MODIFY_SEND_HEADER, RULE_TYPE.REDIRECT].includes(rule.ruleType) + ) { + const writeValue = rule.ruleType === RULE_TYPE.REDIRECT ? rule.to : (rule.action as RULE_ACTION_OBJ).value; + const key = `rule_switch_${getVirtualKey(rule)}`; + const engine = getLocal(); + engine.get(key).then((result) => { + const arr = Array.isArray(result[key]) ? [...result[key]] : []; + if (!arr.includes(writeValue)) { + arr.push(writeValue); + engine.set({ [key]: arr }); + } + }); + } +} + async function save(o: Rule) { const tableName = getTableName(o.ruleType); if (!tableName) { @@ -123,22 +142,7 @@ async function save(o: Rule) { updateCache(tableName); notify.other({ method: APIs.ON_EVENT, event: EVENTs.RULE_UPDATE, from: originalRule, target: existsRule }); // Write history - if ( - prefs.get('rule-history') && - !o.isFunction && - [RULE_TYPE.MODIFY_RECV_HEADER, RULE_TYPE.MODIFY_SEND_HEADER, RULE_TYPE.REDIRECT].includes(o.ruleType) - ) { - const writeValue = o.ruleType === RULE_TYPE.REDIRECT ? o.to : (o.action as RULE_ACTION_OBJ).value; - const key = `rule_switch_${getVirtualKey(o)}`; - const engine = getLocal(); - engine.get(key).then((result) => { - const arr = Array.isArray(result[key]) ? [...result[key]] : []; - if (!arr.includes(writeValue)) { - arr.push(writeValue); - engine.set({ [key]: arr }); - } - }); - } + saveRuleHistory(originalRule); resolve(rule); }; };