@global-savings-group/extension-storage
Advanced tools
Comparing version 0.2.2 to 0.3.0
import { LocalStorage } from './storage/local.storage.js'; | ||
import { SessionStorage } from './storage/session.storage.js'; | ||
export { AsyncStorage } from './interfaces.js'; | ||
export type { AsyncStorage, StorageListener } from './interfaces.js'; | ||
export { MemoryStorage } from './storage/memory.storage.js'; | ||
export declare const localStorageService: LocalStorage; | ||
export declare const sessionStorageService: SessionStorage; |
@@ -6,1 +6,6 @@ export interface AsyncStorage { | ||
} | ||
export type Listener = <T>(newValue?: T, oldValue?: T) => void; | ||
export interface StorageListener { | ||
addEventListener(eventName: string, listener: Listener): void; | ||
removeEventListener(eventName: string, listener: Listener): void; | ||
} |
/// <reference types="chrome" /> | ||
import { AsyncStorage } from '../interfaces.js'; | ||
export declare abstract class ChromeStorage implements AsyncStorage { | ||
import type { AsyncStorage, Listener, StorageListener } from '../interfaces.js'; | ||
export declare abstract class ChromeStorage implements AsyncStorage, StorageListener { | ||
protected areaName: chrome.storage.AreaName; | ||
private eventListeners; | ||
constructor(areaName: chrome.storage.AreaName); | ||
protected get storage(): chrome.storage.StorageArea; | ||
protected get manifestVersion(): number; | ||
private storageListener; | ||
get<T = any>(name: string): Promise<T | null>; | ||
remove(key: string): Promise<void>; | ||
set<T = any>(key: string, value: T): Promise<void>; | ||
addEventListener: (eventName: string, listener: Listener) => void; | ||
removeEventListener: (eventName: string, listener: Listener) => void; | ||
onChanged: (key: string, callback: (newValue: any, oldValue: any) => void) => void; | ||
protected getStorage(): chrome.storage.StorageArea; | ||
} |
export class ChromeStorage { | ||
areaName; | ||
eventListeners = new Map(); | ||
constructor(areaName) { | ||
this.areaName = areaName; | ||
} | ||
async get(name) { | ||
if (chrome.runtime.getManifest().manifest_version === 3) { | ||
const data = await this.getStorage().get([name]); | ||
return data?.[name] ?? null; | ||
get storage() { | ||
return chrome.storage[this.areaName]; | ||
} | ||
get manifestVersion() { | ||
return chrome.runtime.getManifest().manifest_version; | ||
} | ||
storageListener = (changes) => { | ||
for (const [eventName, { oldValue, newValue }] of Object.entries(changes)) { | ||
const listeners = this.eventListeners.get(eventName); | ||
if (listeners && JSON.stringify(oldValue) !== JSON.stringify(newValue)) { | ||
listeners.forEach((listener) => listener(newValue, oldValue)); | ||
} | ||
} | ||
}; | ||
get(name) { | ||
if (this.manifestVersion === 3) { | ||
return this.storage.get([name]).then((data) => data[name] ?? null); | ||
} | ||
return new Promise((resolve) => { | ||
this.getStorage().get([name], (data) => resolve(data?.[name] ?? null)); | ||
this.storage.get([name], (data) => resolve(data?.[name] ?? null)); | ||
}); | ||
} | ||
async remove(key) { | ||
if (chrome.runtime.getManifest().manifest_version === 3) { | ||
return this.getStorage().remove(key); | ||
remove(key) { | ||
if (this.manifestVersion === 3) { | ||
return this.storage.remove(key); | ||
} | ||
return new Promise((resolve) => { | ||
this.getStorage().remove(key, () => resolve()); | ||
this.storage.remove(key, () => resolve()); | ||
}); | ||
} | ||
async set(key, value) { | ||
if (chrome.runtime.getManifest().manifest_version === 3) { | ||
return this.getStorage().set({ [key]: value }); | ||
set(key, value) { | ||
if (this.manifestVersion === 3) { | ||
return this.storage.set({ [key]: value }); | ||
} | ||
return new Promise((resolve) => { | ||
this.getStorage().set({ [key]: value }, () => resolve()); | ||
this.storage.set({ [key]: value }, () => resolve()); | ||
}); | ||
} | ||
addEventListener = (eventName, listener) => { | ||
const listeners = this.eventListeners.get(eventName) || []; | ||
this.eventListeners.set(eventName, [...listeners, listener]); | ||
if (!this.storage.onChanged.hasListener(this.storageListener)) { | ||
this.storage.onChanged.addListener(this.storageListener); | ||
} | ||
}; | ||
removeEventListener = (eventName, listener) => { | ||
const listeners = this.eventListeners.get(eventName)?.filter((eventListener) => eventListener !== listener); | ||
if (listeners && listeners.length > 0) { | ||
this.eventListeners.set(eventName, listeners); | ||
} | ||
else { | ||
this.eventListeners.delete(eventName); | ||
if (this.eventListeners.size === 0) { | ||
this.storage.onChanged.removeListener(this.storageListener); | ||
} | ||
} | ||
}; | ||
onChanged = (key, callback) => { | ||
@@ -38,5 +71,2 @@ chrome.storage.onChanged.addListener((changes, areaName) => { | ||
}; | ||
getStorage() { | ||
return chrome.storage[this.areaName]; | ||
} | ||
} |
{ | ||
"name": "@global-savings-group/extension-storage", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Cross-browser storage service for browser extensions", | ||
@@ -10,4 +10,4 @@ "author": "Global Savings Group", | ||
"engines": { | ||
"node": ">=18" | ||
"node": ">=20" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16571
24
428
1