Skip to content

Commit

Permalink
add onChanged listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamlinerm committed Jan 9, 2025
1 parent dc2c132 commit d153f86
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/composables/useBrowserStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ function mergeDeep(defaults: any, source: any): any {
const output = { ...defaults } // Start with defaults

Object.keys(defaults).forEach((key) => {
if (isObject(defaults[key]) && source?.[key] != null) {
const defaultValue = defaults[key]
const sourceValue = source?.[key]

if (isObject(defaultValue) && sourceValue != null) {
// Recursively merge nested objects
output[key] = mergeDeep(defaults[key], source[key])
} else if (checkType(defaults[key], source?.[key])) {
output[key] = source[key]
output[key] = mergeDeep(defaultValue, sourceValue)
} else if (checkType(defaultValue, sourceValue)) {
output[key] = sourceValue
} else {
// If the type is different, use the default value
output[key] = defaults[key]
output[key] = defaultValue
}
})

Expand All @@ -33,6 +36,8 @@ function isObject(value: any): boolean {

export function useBrowserSyncStorage<T>(key: string, defaultValue: T) {
const data = ref<T>(defaultValue)
// Blocking setting storage if it is updating from storage
let isUpdatingFromStorage = false
// Initialize storage with the value from chrome.storage.sync
chrome.storage.sync.get(key, (result) => {
if (result?.[key] !== undefined) {
Expand All @@ -48,15 +53,29 @@ export function useBrowserSyncStorage<T>(key: string, defaultValue: T) {
watch(
data,
(newValue) => {
chrome.storage.sync.set({ [key]: toRaw(newValue) })
if (!isUpdatingFromStorage)
chrome.storage.sync.set({ [key]: toRaw(newValue) })
},
{ deep: true },
)
// Add the onChanged listener here
chrome.storage.sync.onChanged.addListener(function (changes) {
if (changes?.[key]) {
isUpdatingFromStorage = true
const { oldValue, newValue } = changes.settings
data.value = newValue
setTimeout(() => {
isUpdatingFromStorage = false
}, 5)
}
})
return data
}

export function useBrowserLocalStorage<T>(key: string, defaultValue: T) {
const data = ref<T>(defaultValue)
// Blocking setting storage if it is updating from storage
let isUpdatingFromStorage = false
// Initialize storage with the value from chrome.storage.local
chrome.storage.local.get(key, (result) => {
if (result?.[key] !== undefined) {
Expand All @@ -72,9 +91,21 @@ export function useBrowserLocalStorage<T>(key: string, defaultValue: T) {
watch(
data,
(newValue) => {
chrome.storage.local.set({ [key]: toRaw(newValue) })
if (!isUpdatingFromStorage)
chrome.storage.local.set({ [key]: toRaw(newValue) })
},
{ deep: true },
)
// Add the onChanged listener here
chrome.storage.local.onChanged.addListener(function (changes) {
if (changes?.[key]) {
isUpdatingFromStorage = true
const { oldValue, newValue } = changes.settings
data.value = newValue
setTimeout(() => {
isUpdatingFromStorage = false
}, 5)
}
})
return data
}

0 comments on commit d153f86

Please sign in to comment.