forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-application-operations.ts
More file actions
92 lines (75 loc) · 2.9 KB
/
demo-application-operations.ts
File metadata and controls
92 lines (75 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {inject} from '@angular/core';
import {ApplicationOperations} from '../projects/ng-devtools';
import {DirectivePosition, ElementPosition, SignalNodePosition} from '../projects/protocol';
import {LOCAL_STORAGE} from './local-storage';
const STORAGE_KEY = 'ng-dt-storage-sim';
export class DemoApplicationOperations extends ApplicationOperations {
private readonly localStorage = inject(LOCAL_STORAGE);
override viewSource(position: ElementPosition): void {
console.warn('viewSource() is not implemented because the demo app runs in an Iframe');
throw new Error('Not implemented in demo app.');
}
override selectDomElement(position: ElementPosition): void {
console.warn('selectDomElement() is not implemented because the demo app runs in an Iframe');
throw new Error('Not implemented in demo app.');
}
override inspect(directivePosition: DirectivePosition, keyPath: string[]): void {
console.warn('inspect() is not implemented because the demo app runs in an Iframe');
return;
}
override inspectSignal(position: SignalNodePosition): void {
console.warn('inspectSignal() is not implemented because the demo app runs in an Iframe');
return;
}
override viewSourceFromRouter(name: string, type: string): void {
console.warn(
'viewSourceFromRouter() is not implemented because the demo app runs in an Iframe',
);
throw new Error('Not implemented in demo app.');
}
override async setStorageItems(items: {[key: string]: unknown}): Promise<void> {
const currItems = this.getLsItems();
this.setLsItems({...currItems, ...items});
}
override async getStorageItems(items: string[]): Promise<{[key: string]: unknown}> {
const currItems = this.getLsItems();
const redundant = Object.keys(currItems).filter((prop) => !items.includes(prop));
for (const item of redundant) {
delete currItems[item];
}
return currItems;
}
override async removeStorageItems(items: string[]): Promise<void> {
const currItems = this.getLsItems();
for (const item of items) {
delete currItems[item];
}
this.setLsItems(currItems);
}
private getLsItems(): {[key: string]: unknown} {
const storage = this.localStorage.getItem(STORAGE_KEY);
try {
return JSON.parse(storage ?? '{}');
} catch {
this.localStorage.removeItem(STORAGE_KEY);
console.error(
'Unable to parse the data from the simulated storage. Cleaning the item and returning a default object',
);
return {};
}
}
private setLsItems(items: object) {
try {
this.localStorage.setItem(STORAGE_KEY, JSON.stringify(items));
} catch {
console.error('Unable to set item in the simulated storage.');
}
}
}