-
Notifications
You must be signed in to change notification settings - Fork 56
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 #28 from Saba-Sabato/chrome-backed-stores
Chrome backed stores
- Loading branch information
Showing
8 changed files
with
86 additions
and
73 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
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
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,13 +1,58 @@ | ||
type IStorage = { | ||
count: number; | ||
}; | ||
|
||
const defaultStorage: IStorage = { | ||
count: 0, | ||
}; | ||
|
||
export const storage = { | ||
get: (): Promise<IStorage> => | ||
chrome.storage.sync.get(defaultStorage) as Promise<IStorage>, | ||
set: (value: IStorage): Promise<void> => chrome.storage.sync.set(value), | ||
}; | ||
import { writable, type Writable } from 'svelte/store'; | ||
|
||
/** | ||
* Creates a persistent Svelte store backed by Chrome's sync storage. | ||
* @template T The type of the store's value | ||
* @param key The key to use in Chrome's storage | ||
* @param initialValue The initial value of the store | ||
* @returns A writable Svelte store | ||
*/ | ||
export function persistentStore<T>(key: string, initialValue: T): Writable<T> { | ||
const store = writable(initialValue); | ||
// Ensure each value is updated exactly once in store and in chrome storage | ||
let storeValueQueue: T[] = []; | ||
let chromeValueQueue: T[] = []; | ||
|
||
function watchStore() { | ||
store.subscribe((value) => { | ||
if (chromeValueQueue.length > 0 && value === chromeValueQueue[0]) { | ||
chromeValueQueue.shift(); | ||
return; | ||
} | ||
|
||
storeValueQueue.push(value); | ||
chrome.storage.sync.set({ [key]: value }); | ||
}); | ||
} | ||
|
||
function watchChrome() { | ||
chrome.storage.sync.onChanged.addListener((changes) => { | ||
if (!(Object.hasOwn(changes, key))) return; | ||
|
||
const value = changes[key].newValue as T; | ||
if (storeValueQueue.length > 0 && value === storeValueQueue[0]) { | ||
storeValueQueue.shift(); | ||
return; | ||
} | ||
|
||
chromeValueQueue.push(value); | ||
store.set(value); | ||
}); | ||
} | ||
|
||
// Initialize the store with the value from Chrome storage | ||
chrome.storage.sync.get(key).then((result) => { | ||
let value = Object.hasOwn(result, key) ? result[key] : initialValue; | ||
if (!Object.hasOwn(result, key)) { | ||
console.log(`Persistent store: couldn't find key [${key}] in chrome storage. Default to initial value [${initialValue}]`) | ||
} | ||
chromeValueQueue.push(value); | ||
store.set(value); | ||
watchStore(); | ||
watchChrome(); | ||
}); | ||
|
||
return store; | ||
} | ||
|
||
export const count = persistentStore("count", 10); |