Browse Source

refactor: storage factory + singleton

- Make storage easier.

(cherry picked from commit ed84a4732d7dd3ce6b2c22f30553ab5c59f85dbb)
20230320
Jixun Wu 3 years ago
committed by MengYX
parent
commit
10aa05c244
No known key found for this signature in database GPG Key ID: E63F9C7303E8F604
  1. 2
      src/component/ConfigDialog.vue
  2. 2
      src/decrypt/joox.ts
  3. 8
      src/utils/storage.ts
  4. 2
      src/utils/storage/BaseStorage.ts
  5. 18
      src/utils/storage/BrowserNativeStorage.ts
  6. 12
      src/utils/storage/ChromeExtensionStorage.ts
  7. 32
      src/utils/storage/InMemoryStorage.ts
  8. 13
      src/utils/storage/StorageFactory.ts

2
src/component/ConfigDialog.vue

@ -13,7 +13,7 @@
</template>
<script>
import storage from '../utils/storage';
import { storage } from '@/utils/storage';
export default {
components: {},

2
src/decrypt/joox.ts

@ -2,8 +2,8 @@ import { DecryptResult } from './entity';
import { AudioMimeType, GetArrayBuffer, SniffAudioExt } from './utils';
import jooxFactory from '@unlock-music/joox-crypto';
import storage from '@/utils/storage';
import { MergeUint8Array } from '@/utils/MergeUint8Array';
import { storage } from '@/utils/storage';
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
const uuid = await storage.loadJooxUUID('');

8
src/utils/storage.ts

@ -1,7 +1,3 @@
import BaseStorage from './storage/BaseStorage';
import BrowserNativeStorage from './storage/BrowserNativeStorage';
import ChromeExtensionStorage from './storage/ChromeExtensionStorage';
import storageFactory from './storage/StorageFactory';
const storage: BaseStorage = ChromeExtensionStorage.works ? new ChromeExtensionStorage() : new BrowserNativeStorage();
export default storage;
export const storage = storageFactory();

2
src/utils/storage/BaseStorage.ts

@ -3,6 +3,8 @@ const KEY_JOOX_UUID = 'joox.uuid';
export default abstract class BaseStorage {
protected abstract save<T>(name: string, value: T): Promise<void>;
protected abstract load<T>(name: string, defaultValue: T): Promise<T>;
public abstract getAll(): Promise<Record<string, any>>;
public abstract setAll(obj: Record<string, any>): Promise<void>;
public saveJooxUUID(uuid: string): Promise<void> {
return this.save(KEY_JOOX_UUID, uuid);

18
src/utils/storage/BrowserNativeStorage.ts

@ -1,6 +1,10 @@
import BaseStorage from './BaseStorage';
export default class BrowserNativeStorage extends BaseStorage {
public static get works() {
return typeof localStorage !== 'undefined' && localStorage.getItem;
}
protected async load<T>(name: string, defaultValue: T): Promise<T> {
const result = localStorage.getItem(name);
if (result === null) {
@ -12,4 +16,18 @@ export default class BrowserNativeStorage extends BaseStorage {
protected async save<T>(name: string, value: T): Promise<void> {
localStorage.setItem(name, JSON.stringify(value));
}
public async getAll(): Promise<Record<string, any>> {
const result = {};
for (const [key, value] of Object.entries(localStorage)) {
Object.assign(result, { [key]: JSON.parse(value) });
}
return result;
}
public async setAll(obj: Record<string, any>): Promise<void> {
for (const [key, value] of Object.entries(obj)) {
localStorage.setItem(key, JSON.stringify(value));
}
}
}

12
src/utils/storage/ChromeExtensionStorage.ts

@ -24,4 +24,16 @@ export default class ChromeExtensionStorage extends BaseStorage {
chrome.storage.local.set({ [name]: value }, resolve);
});
}
public async getAll(): Promise<Record<string, any>> {
return new Promise((resolve) => {
chrome.storage.local.get(null, resolve);
});
}
public async setAll(obj: Record<string, any>): Promise<void> {
return new Promise((resolve) => {
chrome.storage.local.set(obj, resolve);
});
}
}

32
src/utils/storage/InMemoryStorage.ts

@ -0,0 +1,32 @@
import BaseStorage from './BaseStorage';
export default class InMemoryStorage extends BaseStorage {
private values = new Map<string, any>();
protected async load<T>(name: string, defaultValue: T): Promise<T> {
if (this.values.has(name)) {
return this.values.get(name);
}
return defaultValue;
}
protected async save<T>(name: string, value: T): Promise<void> {
this.values.set(name, value);
}
public async getAll(): Promise<Record<string, any>> {
const result = {};
this.values.forEach((value, key) => {
Object.assign(result, {
[key]: value,
});
});
return result;
}
public async setAll(obj: Record<string, any>): Promise<void> {
for (const [key, value] of Object.entries(obj)) {
this.values.set(key, value);
}
}
}

13
src/utils/storage/StorageFactory.ts

@ -0,0 +1,13 @@
import BaseStorage from './BaseStorage';
import BrowserNativeStorage from './BrowserNativeStorage';
import ChromeExtensionStorage from './ChromeExtensionStorage';
import InMemoryStorage from './InMemoryStorage';
export default function storageFactory(): BaseStorage {
if (ChromeExtensionStorage.works) {
return new ChromeExtensionStorage();
} else if (BrowserNativeStorage.works) {
return new BrowserNativeStorage();
}
return new InMemoryStorage();
}
Loading…
Cancel
Save