Use localforage for persistence
This commit is contained in:
1 parent
b48beccc0e
commit
feba813b38
5 files changed
+129
-35
No files matched your search
@@ -1,12 +1,20 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
|
||||
export default class GreetingComponent extends Component {
|
||||
@service clock;
|
||||
@service storage;
|
||||
|
||||
get name() {
|
||||
return this.storage.cache.get('prefs:name') ?? "Râu";
|
||||
@tracked name = null;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.loadName();
|
||||
}
|
||||
|
||||
async loadName() {
|
||||
this.name = await this.storage.get('preferences', 'name') ?? "Râu";
|
||||
}
|
||||
|
||||
get greeting() {
|
||||
@@ -22,3 +30,4 @@ export default class GreetingComponent extends Component {
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
|
||||
+89
-30
@@ -1,43 +1,102 @@
|
||||
import Service from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
|
||||
const isExtension = typeof browser !== 'undefined' || typeof chrome !== 'undefined';
|
||||
const platform = typeof browser !== 'undefined' ? 'firefox' : typeof chrome !== 'undefined' ? 'chrome' : 'web';
|
||||
|
||||
const storage = isExtension
|
||||
? {
|
||||
get: (key) => (platform === 'firefox' ? browser.storage.local.get(key) : chrome.storage.local.get(key))
|
||||
.then(obj => obj[key]),
|
||||
set: (key, value) => (platform === 'firefox' ? browser.storage.local.set : chrome.storage.local.set)({ [key]: value }),
|
||||
remove: (key) => (platform === 'firefox' ? browser.storage.local.remove : chrome.storage.local.remove)(key)
|
||||
}
|
||||
: {
|
||||
get: (key) => Promise.resolve(localStorage.getItem(key)),
|
||||
set: (key, value) => Promise.resolve(localStorage.setItem(key, JSON.stringify(value))),
|
||||
remove: (key) => Promise.resolve(localStorage.removeItem(key))
|
||||
};
|
||||
import localforage from 'localforage';
|
||||
|
||||
export default class StorageService extends Service {
|
||||
isExtension = isExtension;
|
||||
platform = platform;
|
||||
|
||||
@tracked cache = new Map();
|
||||
|
||||
async get(key) {
|
||||
if (this.cache.has(key)) return this.cache.get(key);
|
||||
const raw = await storage.get(key);
|
||||
const value = this.isExtension ? raw : JSON.parse(raw);
|
||||
this.cache.set(key, value);
|
||||
databases = new Map();
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.initializeDatabases();
|
||||
}
|
||||
|
||||
initializeDatabases() {
|
||||
const dbConfigs = [
|
||||
{ name: 'preferences', storeName: 'prefs' },
|
||||
{ name: 'integrations', storeName: 'integrations' },
|
||||
{ name: 'settings', storeName: 'settings' },
|
||||
];
|
||||
|
||||
dbConfigs.forEach(({ name, storeName }) => {
|
||||
const db = localforage.createInstance({
|
||||
name: 'dashtab',
|
||||
storeName: storeName,
|
||||
driver: [
|
||||
localforage.INDEXEDDB,
|
||||
localforage.WEBSQL,
|
||||
localforage.LOCALSTORAGE
|
||||
]
|
||||
});
|
||||
this.databases.set(name, db);
|
||||
});
|
||||
}
|
||||
|
||||
getDatabase(dbName = 'preferences') {
|
||||
if (!this.databases.has(dbName)) {
|
||||
throw new Error(`Database "${dbName}" not initialized. Available: ${Array.from(this.databases.keys()).join(', ')}`);
|
||||
}
|
||||
return this.databases.get(dbName);
|
||||
}
|
||||
|
||||
getCacheKey(dbName, key) {
|
||||
return `${dbName}:${key}`;
|
||||
}
|
||||
|
||||
async get(dbName = 'preferences', key) {
|
||||
const cacheKey = this.getCacheKey(dbName, key);
|
||||
if (this.cache.has(cacheKey)) {
|
||||
return this.cache.get(cacheKey);
|
||||
}
|
||||
|
||||
const db = this.getDatabase(dbName);
|
||||
const value = await db.getItem(key);
|
||||
this.cache.set(cacheKey, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
this.cache.set(key, value);
|
||||
return storage.set(key, value);
|
||||
async set(dbName = 'preferences', key, value) {
|
||||
const cacheKey = this.getCacheKey(dbName, key);
|
||||
this.cache.set(cacheKey, value);
|
||||
|
||||
const db = this.getDatabase(dbName);
|
||||
return db.setItem(key, value);
|
||||
}
|
||||
|
||||
async remove(key) {
|
||||
this.cache.delete(key);
|
||||
return storage.remove(key);
|
||||
async remove(dbName = 'preferences', key) {
|
||||
const cacheKey = this.getCacheKey(dbName, key);
|
||||
this.cache.delete(cacheKey);
|
||||
|
||||
const db = this.getDatabase(dbName);
|
||||
return db.removeItem(key);
|
||||
}
|
||||
|
||||
async clear(dbName = 'preferences') {
|
||||
const db = this.getDatabase(dbName);
|
||||
const keys = await db.keys();
|
||||
|
||||
keys.forEach(key => {
|
||||
const cacheKey = this.getCacheKey(dbName, key);
|
||||
this.cache.delete(cacheKey);
|
||||
});
|
||||
|
||||
return db.clear();
|
||||
}
|
||||
|
||||
async keys(dbName = 'preferences') {
|
||||
const db = this.getDatabase(dbName);
|
||||
return db.keys();
|
||||
}
|
||||
|
||||
async length(dbName = 'preferences') {
|
||||
const db = this.getDatabase(dbName);
|
||||
return db.length();
|
||||
}
|
||||
|
||||
async iterate(dbName = 'preferences', callback) {
|
||||
const db = this.getDatabase(dbName);
|
||||
return db.iterate(callback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ export default class IndexRoute extends Component {
|
||||
|
||||
<div class="col-start-2 row-start-3 flex items-center justify-center gap-4">
|
||||
<button command="show-modal" commandfor="modal-dialog-workflow"
|
||||
class="rounded-md px-2.5 py-1.5 text-white border border-white hover:bg-gray-950/10">
|
||||
class="rounded-md px-2.5 py-1.5 text-white border border-white hover:bg-gray-950/10" type="button">
|
||||
Open workflow dialog
|
||||
</button>
|
||||
|
||||
<button command="show-modal" commandfor="modal-dialog-yield"
|
||||
class="rounded-md px-2.5 py-1.5 text-white border border-white hover:bg-gray-950/10">
|
||||
class="rounded-md px-2.5 py-1.5 text-white border border-white hover:bg-gray-950/10" type="button">
|
||||
Open yielded dialog
|
||||
</button>
|
||||
</div>
|
||||
|
||||
+4
-1
@@ -88,5 +88,8 @@
|
||||
"ember": {
|
||||
"edition": "octane"
|
||||
},
|
||||
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
|
||||
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0",
|
||||
"dependencies": {
|
||||
"localforage": "^1.10.0"
|
||||
}
|
||||
}
|
||||
Generated
+23
@@ -7,6 +7,10 @@ settings:
|
||||
importers:
|
||||
|
||||
.:
|
||||
dependencies:
|
||||
localforage:
|
||||
specifier: ^1.10.0
|
||||
version: 1.10.0
|
||||
devDependencies:
|
||||
'@babel/core':
|
||||
specifier: ^7.28.4
|
||||
@@ -3682,6 +3686,9 @@ packages:
|
||||
resolution: {integrity: sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==}
|
||||
engines: {node: '>= 4'}
|
||||
|
||||
immediate@3.0.6:
|
||||
resolution: {integrity: sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==}
|
||||
|
||||
import-fresh@3.3.1:
|
||||
resolution: {integrity: sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ==}
|
||||
engines: {node: '>=6'}
|
||||
@@ -4088,6 +4095,9 @@ packages:
|
||||
resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==}
|
||||
engines: {node: '>= 0.8.0'}
|
||||
|
||||
lie@3.1.1:
|
||||
resolution: {integrity: sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw==}
|
||||
|
||||
lightningcss-android-arm64@1.30.2:
|
||||
resolution: {integrity: sha512-BH9sEdOCahSgmkVhBLeU7Hc9DWeZ1Eb6wNS6Da8igvUwAe0sqROHddIlvU06q3WyXVEOYDZ6ykBZQnjTbmo4+A==}
|
||||
engines: {node: '>= 12.0.0'}
|
||||
@@ -4178,6 +4188,9 @@ packages:
|
||||
resolution: {integrity: sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==}
|
||||
engines: {node: '>=8.9.0'}
|
||||
|
||||
localforage@1.10.0:
|
||||
resolution: {integrity: sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==}
|
||||
|
||||
locate-path@2.0.0:
|
||||
resolution: {integrity: sha512-NCI2kiDkyR7VeEKm27Kda/iQHyKJe1Bu0FlTbYp3CqJu+9IFe9bLyAjMxf5ZDDbEg+iMPzB5zYyUTSm8wVTKmA==}
|
||||
engines: {node: '>=4'}
|
||||
@@ -10566,6 +10579,8 @@ snapshots:
|
||||
|
||||
ignore@7.0.5: {}
|
||||
|
||||
immediate@3.0.6: {}
|
||||
|
||||
import-fresh@3.3.1:
|
||||
dependencies:
|
||||
parent-module: 1.0.1
|
||||
@@ -11010,6 +11025,10 @@ snapshots:
|
||||
prelude-ls: 1.2.1
|
||||
type-check: 0.4.0
|
||||
|
||||
lie@3.1.1:
|
||||
dependencies:
|
||||
immediate: 3.0.6
|
||||
|
||||
lightningcss-android-arm64@1.30.2:
|
||||
optional: true
|
||||
|
||||
@@ -11080,6 +11099,10 @@ snapshots:
|
||||
emojis-list: 3.0.0
|
||||
json5: 2.2.3
|
||||
|
||||
localforage@1.10.0:
|
||||
dependencies:
|
||||
lie: 3.1.1
|
||||
|
||||
locate-path@2.0.0:
|
||||
dependencies:
|
||||
p-locate: 2.0.0
|
||||
|
||||
Reference in new issue
Block a user