wip: optimizations
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import {
|
||||
Injectable,
|
||||
inject,
|
||||
signal,
|
||||
computed,
|
||||
type Signal
|
||||
} from '@angular/core';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { STORAGE_KEY_ICE_SERVERS } from '../../core/constants';
|
||||
import { jsonStorage } from '../persistence/json-storage.service';
|
||||
|
||||
export interface IceServerEntry {
|
||||
id: string;
|
||||
@@ -26,6 +28,7 @@ export class IceServerSettingsService {
|
||||
readonly entries: Signal<IceServerEntry[]>;
|
||||
readonly rtcIceServers: Signal<RTCIceServer[]>;
|
||||
|
||||
private readonly storageService = jsonStorage;
|
||||
private readonly _entries = signal<IceServerEntry[]>(this.load());
|
||||
|
||||
constructor() {
|
||||
@@ -90,33 +93,23 @@ export class IceServerSettingsService {
|
||||
}
|
||||
|
||||
private load(): IceServerEntry[] {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY_ICE_SERVERS);
|
||||
const parsed = this.storageService.read<unknown>(STORAGE_KEY_ICE_SERVERS, null);
|
||||
|
||||
if (!raw) {
|
||||
return [...DEFAULT_ENTRIES];
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(raw);
|
||||
|
||||
if (!Array.isArray(parsed) || parsed.length === 0) {
|
||||
return [...DEFAULT_ENTRIES];
|
||||
}
|
||||
|
||||
return parsed.filter(
|
||||
(entry: unknown): entry is IceServerEntry =>
|
||||
typeof entry === 'object'
|
||||
&& entry !== null
|
||||
&& typeof (entry as IceServerEntry).id === 'string'
|
||||
&& ((entry as IceServerEntry).type === 'stun' || (entry as IceServerEntry).type === 'turn')
|
||||
&& typeof (entry as IceServerEntry).urls === 'string'
|
||||
);
|
||||
} catch {
|
||||
if (!Array.isArray(parsed) || parsed.length === 0) {
|
||||
return [...DEFAULT_ENTRIES];
|
||||
}
|
||||
|
||||
return parsed.filter(
|
||||
(entry: unknown): entry is IceServerEntry =>
|
||||
typeof entry === 'object'
|
||||
&& entry !== null
|
||||
&& typeof (entry as IceServerEntry).id === 'string'
|
||||
&& ((entry as IceServerEntry).type === 'stun' || (entry as IceServerEntry).type === 'turn')
|
||||
&& typeof (entry as IceServerEntry).urls === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
private save(entries: IceServerEntry[]): void {
|
||||
localStorage.setItem(STORAGE_KEY_ICE_SERVERS, JSON.stringify(entries));
|
||||
this.storageService.write(STORAGE_KEY_ICE_SERVERS, entries);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user