Introduce storage cache, auto-update values in components

This commit is contained in:
raucao committed 2025-10-29 21:12:16 +01:00
1 parent 541634d5f4
commit f4b07f497a
2 files changed
+12 -13

No files matched your search

+2 -11
View File
@@ -1,21 +1,12 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { service } from '@ember/service';
export default class GreetingComponent extends Component {
@service clock;
@service storage;
@tracked name = null;
constructor() {
super(...arguments);
this.loadName();
this.storage.remove("prefs:name", "Satoshi")
}
async loadName() {
this.name = (await this.storage.get("prefs:name")) || "Râu";
get name() {
return this.storage.cache.get('prefs:name') ?? "Râu";
}
get greeting() {
+10 -2
View File
@@ -1,4 +1,5 @@
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';
@@ -20,16 +21,23 @@ export default class StorageService extends Service {
isExtension = isExtension;
platform = platform;
@tracked cache = new Map();
async get(key) {
const value = await storage.get(key);
return this.isExtension ? value : JSON.parse(value);
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);
return value;
}
async set(key, value) {
this.cache.set(key, value);
return storage.set(key, value);
}
async remove(key) {
this.cache.delete(key);
return storage.remove(key);
}
}