-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,13 @@ | ||
import { WebsiteIds } from "@/data/websites"; | ||
|
||
import BrowserStorage from "./browser"; | ||
import BrowserStorageTool from "./browser"; | ||
|
||
export interface UnivCommonConfig { | ||
export type UnivCommonConfig = { | ||
rainbow?: boolean; | ||
dark?: boolean; | ||
} | ||
}; | ||
|
||
export type CommonConfig = Record<WebsiteIds, UnivCommonConfig>; | ||
|
||
export const CommonStorage = new (class { | ||
#storage: BrowserStorage<CommonConfig>; | ||
constructor() { | ||
this.#storage = new BrowserStorage("common"); | ||
} | ||
|
||
async get(id: WebsiteIds, key: keyof UnivCommonConfig) { | ||
const isRainbowEnabled = (await this.#storage.get(id))[key]; | ||
return isRainbowEnabled; | ||
} | ||
|
||
async set(id: WebsiteIds, value: Partial<UnivCommonConfig>) { | ||
const data = await this.#storage.getAll(); | ||
|
||
await this.#storage.set({ ...data, [id]: { ...data[id], ...value } }); | ||
} | ||
|
||
async toggle(id: WebsiteIds, key: keyof UnivCommonConfig) { | ||
const data = await this.#storage.get(id); | ||
const newdata = !data[key]; | ||
await this.#storage.set({ [id]: newdata }); | ||
} | ||
})(); | ||
export const CommonStorage = BrowserStorageTool.fromId<CommonConfig>("common", { | ||
area: "sync", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,57 @@ | ||
import { StorageIds } from "./type"; | ||
|
||
export default interface StorageTool<T> { | ||
id: StorageIds; | ||
|
||
get(value: keyof T): Promise<T[keyof T]>; | ||
set(value: Partial<T>): Promise<void>; | ||
toggle(key: keyof T): Promise<void>; | ||
getAll(): Promise<T>; | ||
} | ||
|
||
export class ObjectStorage<T> implements StorageTool<T> { | ||
#data: T; | ||
|
||
constructor(data: T) { | ||
this.#data = data; | ||
} | ||
|
||
async get(value: keyof T) { | ||
return this.#data[value]; | ||
} | ||
async set(value: Partial<T>) { | ||
this.#data = { ...this.#data, ...value }; | ||
} | ||
async toggle(key: keyof T): Promise<void> { | ||
const data = this.#data; | ||
const newdata = !data[key]; | ||
this.#data = { ...data, [key]: newdata }; | ||
} | ||
async getAll(): Promise<T> { | ||
return this.#data; | ||
} | ||
} | ||
|
||
type ValueOf<T> = T[keyof T]; | ||
type ChildKeys<T> = keyof ValueOf<T>; | ||
export class RecursiveStorageTool<T> implements StorageTool<ValueOf<T>> { | ||
#parent: StorageTool<T>; | ||
#key: keyof T; | ||
|
||
constructor(parent: StorageTool<T>, key: keyof T) { | ||
this.#parent = parent; | ||
this.#key = key; | ||
} | ||
async get(value: keyof ValueOf<T>): Promise<ValueOf<T>[ChildKeys<T>]> { | ||
const data = await this.#parent.get(this.#key); | ||
return data[value]; | ||
} | ||
async getAll(): Promise<ValueOf<T>> { | ||
return await this.#parent.get(this.#key); | ||
} | ||
async set(value: Partial<ValueOf<T>>): Promise<void> { | ||
const data = await this.#parent.getAll(); | ||
await this.#parent.set({ ...data, value }); | ||
} | ||
async toggle(key: ChildKeys<T>): Promise<void> { | ||
const data = await this.#parent.getAll(); | ||
const dataValue = !data[this.#key][key]; | ||
await this.#parent.set({ ...data, [this.#key]: { ...data[this.#key], [key]: dataValue } }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters