mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
add default configuration values
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
export const defaultConfiguration: {[key in DefaultConfigKey]: any} = {
|
||||
"first-color": "#3b82ff",
|
||||
"second-color": "#ff4444"
|
||||
}
|
||||
|
||||
export type DefaultConfigKey = "first-color" | "second-color";
|
||||
@@ -1,4 +1,5 @@
|
||||
import { BehaviorSubject } from "rxjs";
|
||||
import { DefaultConfigKey, defaultConfiguration } from "renderer/config/default-configuration.config";
|
||||
|
||||
export class ConfigurationService {
|
||||
|
||||
@@ -9,24 +10,35 @@ export class ConfigurationService {
|
||||
this.subscribers = new Map<string, BehaviorSubject<any>[]>();
|
||||
}
|
||||
|
||||
private emitChange(key: string){
|
||||
if(this.subscribers.has(key)){
|
||||
const val = this.get(key);
|
||||
this.subscribers.get(key).forEach(sub => sub.next(val));
|
||||
}
|
||||
}
|
||||
|
||||
public static getInstance(): ConfigurationService{
|
||||
if(!ConfigurationService.instance){ ConfigurationService.instance = new ConfigurationService(); }
|
||||
return ConfigurationService.instance;
|
||||
}
|
||||
|
||||
public get<Type>(key: string): Type{
|
||||
public get<Type>(key: string | DefaultConfigKey): Type{
|
||||
const t = JSON.parse(window.localStorage.getItem(key));
|
||||
if(!t){ return defaultConfiguration[key as DefaultConfigKey]; }
|
||||
return t;
|
||||
}
|
||||
|
||||
public set(key: string, value: any){
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
if(this.subscribers.has(key)){
|
||||
this.subscribers.get(key).forEach(sub => sub.next(value));
|
||||
}
|
||||
this.emitChange(key);
|
||||
}
|
||||
|
||||
public watch(key: string): BehaviorSubject<any>{
|
||||
public delete(key: string){
|
||||
window.localStorage.setItem(key, null);
|
||||
this.emitChange(key);
|
||||
}
|
||||
|
||||
public watch(key: DefaultConfigKey | string): BehaviorSubject<any>{
|
||||
const newSub = new BehaviorSubject(this.get(key));
|
||||
if(!this.subscribers.has(key)){ this.subscribers.set(key, []); }
|
||||
this.subscribers.get(key).push(newSub);
|
||||
|
||||
Reference in New Issue
Block a user