-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from Dreamlinerm/master
storage, added promise, nextTick, fixed bugs
- Loading branch information
Showing
4 changed files
with
75 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,88 @@ | ||
import { ref, watch } from "vue" | ||
import { ref, watch, nextTick } from "vue" | ||
function mergeDeep(defaults: any, source: any): any { | ||
Check warning on line 2 in src/composables/useBrowserStorage.ts
|
||
// Merge the default options with the stored options | ||
const output = { ...defaults } // Start with defaults | ||
// Merge the default options with the stored options | ||
const output = { ...defaults } // Start with defaults | ||
|
||
Object.keys(defaults).forEach((key) => { | ||
const defaultValue = defaults[key] | ||
const sourceValue = source?.[key] | ||
Object.keys(defaults).forEach((key) => { | ||
const defaultValue = defaults[key] | ||
const sourceValue = source?.[key] | ||
|
||
if (isObject(defaultValue) && sourceValue != null) { | ||
// Recursively merge nested objects | ||
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] = defaultValue | ||
} | ||
}) | ||
if (isObject(defaultValue) && sourceValue != null) { | ||
// Recursively merge nested objects | ||
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] = defaultValue | ||
console.log("Type mismatch", key, sourceValue) | ||
} | ||
}) | ||
|
||
return output | ||
return output | ||
} | ||
|
||
function checkType(defaultValue: any, value: any): boolean { | ||
Check warning on line 25 in src/composables/useBrowserStorage.ts
|
||
// Check if the value type is the same type as the default value or null | ||
// there are only strings, booleans, nulls and arrays as types left | ||
return ( | ||
(typeof value === typeof defaultValue && | ||
Array.isArray(value) == Array.isArray(defaultValue)) || | ||
value === null | ||
) | ||
// Check if the value type is the same type as the default value or null | ||
// there are only strings, booleans, nulls and arrays as types left | ||
return (typeof value === typeof defaultValue && Array.isArray(value) == Array.isArray(defaultValue)) || value === null | ||
} | ||
function isObject(value: any): boolean { | ||
return value !== null && value instanceof Object && !Array.isArray(value) | ||
return value !== null && value instanceof Object && !Array.isArray(value) | ||
} | ||
|
||
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) { | ||
if (isObject(defaultValue) && isObject(result[key])) { | ||
data.value = mergeDeep(defaultValue, result[key]) | ||
} else if (checkType(defaultValue, result[key])) { | ||
data.value = result[key] | ||
} | ||
} | ||
}) | ||
|
||
// Watch for changes in the storage and update chrome.storage.sync | ||
watch( | ||
data, | ||
(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[key] | ||
data.value = newValue | ||
setTimeout(() => { | ||
isUpdatingFromStorage = false | ||
}, 5) | ||
} | ||
}) | ||
return data | ||
return useBrowserStorage(key, defaultValue, "sync") | ||
} | ||
|
||
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) { | ||
if (isObject(defaultValue) && isObject(result[key])) { | ||
data.value = mergeDeep(defaultValue, result[key]) | ||
} else if (checkType(defaultValue, result[key])) { | ||
data.value = result[key] | ||
} | ||
} | ||
}) | ||
return useBrowserStorage(key, defaultValue, "local") | ||
} | ||
|
||
function useBrowserStorage<T>(key: string, defaultValue: T, storageType: "sync" | "local" = "sync") { | ||
const data = ref<T>(defaultValue) | ||
// Blocking setting storage if it is updating from storage | ||
let isUpdatingFromStorage = true | ||
const defaultIsObject = isObject(defaultValue) | ||
// Initialize storage with the value from chrome.storage | ||
const promise = new Promise((resolve) => { | ||
chrome.storage[storageType].get(key, async (result) => { | ||
if (result?.[key] !== undefined) { | ||
if (defaultIsObject && isObject(result[key])) { | ||
data.value = mergeDeep(defaultValue, result[key]) | ||
} else if (checkType(defaultValue, result[key])) { | ||
data.value = result[key] | ||
} | ||
} | ||
await nextTick() | ||
isUpdatingFromStorage = false | ||
resolve(true) | ||
}) | ||
}) | ||
|
||
// Watch for changes in the storage and update chrome.storage.local | ||
watch( | ||
data, | ||
(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[key] | ||
data.value = newValue | ||
setTimeout(() => { | ||
isUpdatingFromStorage = false | ||
}, 5) | ||
} | ||
}) | ||
return data | ||
// Watch for changes in the storage and update chrome.storage | ||
watch( | ||
data, | ||
(newValue) => { | ||
if (!isUpdatingFromStorage) { | ||
if (checkType(defaultValue, newValue)) { | ||
chrome.storage[storageType].set({ [key]: toRaw(newValue) }) | ||
} else { | ||
console.error("not updating " + key + ": type mismatch") | ||
} | ||
} | ||
}, | ||
{ deep: true, flush: "post" }, | ||
) | ||
// Add the onChanged listener here | ||
chrome.storage[storageType].onChanged.addListener(async function (changes) { | ||
if (changes?.[key]) { | ||
isUpdatingFromStorage = true | ||
const { oldValue, newValue } = changes[key] | ||
data.value = newValue | ||
await nextTick() | ||
isUpdatingFromStorage = false | ||
} | ||
}) | ||
return { data, promise } | ||
} |
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