fix storage bug

This commit is contained in:
Gabe Yuan
2023-08-19 13:48:03 +08:00
parent 2773a76af8
commit c2fd1fe9e0
2 changed files with 8 additions and 11 deletions

View File

@@ -3,7 +3,6 @@ import { browser, isExt, isGm, isWeb } from "../libs/browser";
import {
STOKEY_SETTING,
STOKEY_RULES,
STOKEY_MSAUTH,
STOKEY_SYNC,
DEFAULT_SETTING,
DEFAULT_RULES,
@@ -15,12 +14,13 @@ import storage from "../libs/storage";
* 默认配置
*/
export const defaultStorage = {
[STOKEY_MSAUTH]: null,
[STOKEY_SETTING]: DEFAULT_SETTING,
[STOKEY_RULES]: DEFAULT_RULES,
[STOKEY_SYNC]: DEFAULT_SYNC,
};
const activeKeys = Object.keys(defaultStorage);
const StoragesContext = createContext(null);
export function StoragesProvider({ children }) {
@@ -38,7 +38,10 @@ export function StoragesProvider({ children }) {
}
const newStorages = {};
Object.entries(changes)
.filter(([_, { oldValue, newValue }]) => oldValue !== newValue)
.filter(
([key, { oldValue, newValue }]) =>
activeKeys.includes(key) && oldValue !== newValue
)
.forEach(([key, { newValue }]) => {
newStorages[key] = JSON.parse(newValue);
});
@@ -51,8 +54,7 @@ export function StoragesProvider({ children }) {
// 首次从storage同步配置到内存
(async () => {
const curStorages = {};
const keys = Object.keys(defaultStorage);
for (const key of keys) {
for (const key of activeKeys) {
const val = await storage.get(key);
if (val) {
curStorages[key] = JSON.parse(val);

View File

@@ -6,7 +6,6 @@ import {
EVENT_KISS,
MSG_TRANS_CURRULE,
} from "../config";
import { StoragesProvider } from "../hooks/Storage";
import { queryEls } from ".";
import Content from "../views/Content";
import { fetchUpdate, fetchClear } from "./fetch";
@@ -144,10 +143,6 @@ export class Translator {
"-webkit-line-clamp: unset; max-height: none; height: auto;";
const root = createRoot(span);
root.render(
<StoragesProvider>
<Content q={q} translator={this} />
</StoragesProvider>
);
root.render(<Content q={q} translator={this} />);
};
}