forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreference.ts
More file actions
55 lines (49 loc) · 1.77 KB
/
preference.ts
File metadata and controls
55 lines (49 loc) · 1.77 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
import store from 'store';
import { getLogger } from './logger';
import { IPublicModelPreference } from '@felce/lowcode-types';
const logger = getLogger({ level: 'warn', bizName: 'Preference' });
const STORAGE_KEY_PREFIX = 'ale';
/**
* used to store user preferences, such as pinned status of a pannel.
* save to local storage.
*/
export default class Preference implements IPublicModelPreference {
getStorageKey(key: string, module?: string): string {
const moduleKey = module || '__inner__';
return `${STORAGE_KEY_PREFIX}_${moduleKey}.${key}`;
}
set(key: string, value: any, module?: string): void {
if (!key || typeof key !== 'string' || key.length === 0) {
logger.error('Invalid key when setting preference', key);
return;
}
const storageKey = this.getStorageKey(key, module);
logger.debug('storageKey:', storageKey, 'set with value:', value);
store.set(storageKey, value);
}
get(key: string, module: string): any {
if (!key || typeof key !== 'string' || key.length === 0) {
logger.error('Invalid key when getting from preference', key);
return;
}
const storageKey = this.getStorageKey(key, module);
const result = store.get(storageKey);
logger.debug('storageKey:', storageKey, 'get with result:', result);
return result;
}
/**
* check if local storage contain certain key
*
* @param {string} key
* @param {string} module
*/
contains(key: string, module: string): boolean {
if (!key || typeof key !== 'string' || key.length === 0) {
logger.error('Invalid key when getting from preference', key);
return false;
}
const storageKey = this.getStorageKey(key, module);
const result = store.get(storageKey);
return !(result === undefined || result === null);
}
}