shaun-wild / svelte-chrome-storage Goto Github PK
View Code? Open in Web Editor NEWA lightweight abstraction between Svelte stores and Chrome extension storage.
License: MIT License
A lightweight abstraction between Svelte stores and Chrome extension storage.
License: MIT License
When obtaining a value that was set before the current component initializes, it is possible to first read an undefined
value from the store. Eventually the value is set and the subscribers then receive the correct value.
To reproduce this, set a local value in the background process:
// background.ts
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({"test": 1337})
}
Then in your popup component, immediately subsribe to the value:
<script lang="ts">
import {chromeStorageLocal} from "svelte-chrome-storage/dist/chrome-storage-store-adapter"
let testStore = chromeStorageLocal<number>("test")
$: console.log("$testStore:", $testStore)
</script>
In the console of the popup I get
index.js:6 $testStore: undefined
index.js:6 $testStore: 1337
I ran into this when attempting to do a comparison on a store value and it was not as expected.
I did confirm that if you check the value using setTimeout
after a short wait, the first read on the value is correct, indicating that it is a timing/initialization issue and not an issue with the first read per-se.
This library appeared to be exactly what I was looking for (and trying to implement myself), but as I tried to use it I ran into issues.
My goal is to keep my store strongly typed and provide setters for each field that should be editable. Something like the following:
const Key = "settings"
type Settings = {
recording: boolean;
recordTabId: string;
onSettingsPage: boolean;
settingsUrl: string;
}
function settingsStore() {
// Causes more issues
// const { subscribe, update } = chromeStorageLocal<Settings>(Key)
const settings = chromeStorageLocal<Settings>(Key)
return {
subscribe: settings.subscribe,
startRecording: () => settings.update(state => ({...state, recording: true})),
stopRecording: () => settings.update(state => ({...state, recording: false})),
setRecordTabId: (id: string) => settings.update(state => ({...state, recordTabId: id})),
clearRecordTabId: () => settings.update(state => ({...state, recordTabId: ""})),
}
}
Doing this, I run into issues where this
is not defined when subscribing. If I pull the subscribe
and update
methods off of chromeStorageLocal
, I run into similar issues when calling update
from my setters (i.e. update(...)
instead of settings.update(...)
).
I was going to try extending the ChromeStorageStoreAdapter
class, but it is not exported.
Likely this approach just is not compatible with the library. And assuming it isn't and isn't meant to be, I'm wondering if you have any other guidance on a way to type the store with an object and allow callers to set individual keys without having to pass back in the entire object themselves.
I'm trying to avoid doing the following from the callers:
let settingsStore = chromeStorageLocal<Settings>(Key)
settingsStore.update(state => ({...state, recording: false}))
I'm doing the following import as instructed by the README:
import {chromeStorageLocal} from "svelte-chrome-storage"
But chromeStorageLocal
has type any because of the error:
Could not find a declaration file for module 'svelte-chrome-storage
Things work fine when importing from the dist folder:
import {chromeStorageLocal} from "svelte-chrome-storage/dist/chrome-storage-store-adapter"
vscode is producing the error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.