@etsoo/shared 1.0.85 → 1.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/StorageUtils.ts +1 -0
- package/__tests__/Utils.ts +1 -0
- package/__tests__/WindowStorageTests.ts +16 -0
- package/lib/cjs/storage/IStorage.d.ts +31 -6
- package/lib/cjs/storage/WindowStorage.d.ts +31 -6
- package/lib/cjs/storage/WindowStorage.js +56 -10
- package/lib/mjs/storage/IStorage.d.ts +31 -6
- package/lib/mjs/storage/WindowStorage.d.ts +31 -6
- package/lib/mjs/storage/WindowStorage.js +56 -10
- package/package.json +1 -1
- package/src/storage/IStorage.ts +35 -6
- package/src/storage/WindowStorage.ts +79 -14
|
@@ -11,6 +11,7 @@ test('Tests for all', () => {
|
|
|
11
11
|
expect(StorageUtils.getSessionData('string1', '')).toBe('');
|
|
12
12
|
expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
|
|
13
13
|
expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
|
|
14
|
+
expect(StorageUtils.getSessionData('number1', 0)).toBe(0);
|
|
14
15
|
expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
|
|
15
16
|
});
|
|
16
17
|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -107,6 +107,7 @@ test('Tests for parseString', () => {
|
|
|
107
107
|
expect(Utils.parseString<boolean>('')).toBeUndefined();
|
|
108
108
|
expect(Utils.parseString<number>(undefined)).toBeUndefined();
|
|
109
109
|
expect(Utils.parseString('3.14', 0)).toBe(3.14);
|
|
110
|
+
expect(Utils.parseString(null, 0)).toBe(0);
|
|
110
111
|
expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
|
|
111
112
|
new Date('2021/4/13')
|
|
112
113
|
);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WindowStorage } from '../src/storage/WindowStorage';
|
|
2
|
+
|
|
3
|
+
// Arrange
|
|
4
|
+
const storage = new WindowStorage([], (_field, data, _index) => data);
|
|
5
|
+
|
|
6
|
+
test('Tests for getInstanceCount', () => {
|
|
7
|
+
expect(storage.getInstanceCount()).toBe(0);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('Tests for updateInstanceCount / getInstanceCount', () => {
|
|
11
|
+
// Current index -1
|
|
12
|
+
storage.updateInstanceCount(true);
|
|
13
|
+
|
|
14
|
+
// Always make sure it starts with 0
|
|
15
|
+
expect(storage.getInstanceCount()).toBe(0);
|
|
16
|
+
});
|
|
@@ -2,32 +2,57 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Current instance index
|
|
7
|
+
*/
|
|
8
|
+
readonly instanceIndex: number;
|
|
5
9
|
/**
|
|
6
10
|
* Get data
|
|
7
11
|
* @param key Key name
|
|
12
|
+
* @param persistance From the persisted data
|
|
8
13
|
*/
|
|
9
|
-
getData<T>(key: string): T | undefined;
|
|
14
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
10
15
|
/**
|
|
11
16
|
* Get data with default value
|
|
12
17
|
* @param key Key name
|
|
13
18
|
* @param defaultValue Default value
|
|
19
|
+
* @param persistance From the persisted data
|
|
14
20
|
*/
|
|
15
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
21
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
16
22
|
/**
|
|
17
|
-
* Get
|
|
23
|
+
* Get object data
|
|
18
24
|
* @param key Key name
|
|
25
|
+
* @param persistance From the persisted data
|
|
19
26
|
*/
|
|
20
|
-
getObject<T extends {}>(key: string): T | undefined;
|
|
27
|
+
getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
|
|
21
28
|
/**
|
|
22
29
|
* Set data
|
|
23
30
|
* @param key Key name
|
|
24
31
|
* @param data Data, null for removal
|
|
32
|
+
* @param persistance Persist the data, false will stop persistance
|
|
25
33
|
*/
|
|
26
|
-
setData(key: string, data: unknown): void;
|
|
34
|
+
setData(key: string, data: unknown, persistance?: boolean): void;
|
|
35
|
+
/**
|
|
36
|
+
* Get current instance count
|
|
37
|
+
* @returns Current instance count
|
|
38
|
+
*/
|
|
39
|
+
getInstanceCount(): number;
|
|
40
|
+
/**
|
|
41
|
+
* Update instance count
|
|
42
|
+
* @param removed Is removed?
|
|
43
|
+
* @returns Current instance count
|
|
44
|
+
*/
|
|
45
|
+
updateInstanceCount(removed: boolean): number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Storage init callback
|
|
49
|
+
*/
|
|
50
|
+
export interface IStorageInitCallback {
|
|
51
|
+
(field: string, data: string | null, instanceIndex: number): string | null;
|
|
27
52
|
}
|
|
28
53
|
/**
|
|
29
54
|
* Storage constructor interface
|
|
30
55
|
*/
|
|
31
56
|
export interface IStorageConstructor {
|
|
32
|
-
new (globalFields: string[]): IStorage;
|
|
57
|
+
new (globalFields: string[], callback: IStorageInitCallback): IStorage;
|
|
33
58
|
}
|
|
@@ -1,35 +1,60 @@
|
|
|
1
|
-
import { IStorage } from './IStorage';
|
|
1
|
+
import { IStorage, IStorageInitCallback } from './IStorage';
|
|
2
2
|
/**
|
|
3
3
|
* Window storage
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
5
|
*/
|
|
6
6
|
export declare class WindowStorage implements IStorage {
|
|
7
7
|
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Instance count field name
|
|
10
|
+
*/
|
|
11
|
+
private readonly instanceCountField;
|
|
12
|
+
private _instanceIndex;
|
|
13
|
+
/**
|
|
14
|
+
* Current instance index
|
|
15
|
+
*/
|
|
16
|
+
get instanceIndex(): number;
|
|
8
17
|
/**
|
|
9
18
|
* Constructor
|
|
10
19
|
* @param globalFields Global fields
|
|
20
|
+
* @param callback Field and data callback
|
|
11
21
|
*/
|
|
12
|
-
constructor(globalFields: string[]);
|
|
22
|
+
constructor(globalFields: string[], callback: IStorageInitCallback);
|
|
13
23
|
/**
|
|
14
24
|
* Get data
|
|
15
25
|
* @param key Key name
|
|
26
|
+
* @param persistance From the persisted data
|
|
16
27
|
*/
|
|
17
|
-
getData<T>(key: string): T | undefined;
|
|
28
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
18
29
|
/**
|
|
19
30
|
* Get data with default value
|
|
20
31
|
* @param key Key name
|
|
21
32
|
* @param defaultValue Default value
|
|
33
|
+
* @param persistance From the persisted data
|
|
22
34
|
*/
|
|
23
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
35
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
24
36
|
/**
|
|
25
37
|
* Get object data
|
|
26
38
|
* @param key Key name
|
|
39
|
+
* @param persistance From the persisted data
|
|
27
40
|
*/
|
|
28
|
-
getObject<T extends {}>(key: string): T | undefined;
|
|
41
|
+
getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
|
|
29
42
|
/**
|
|
30
43
|
* Set data
|
|
31
44
|
* @param key Key name
|
|
32
45
|
* @param data Data, null for removal
|
|
46
|
+
* @param persistance To the persisted data, false will stop persistance
|
|
47
|
+
*/
|
|
48
|
+
setData(key: string, data: unknown, persistance?: boolean): void;
|
|
49
|
+
/**
|
|
50
|
+
* Get current instance count
|
|
51
|
+
* @returns Current instance count
|
|
52
|
+
*/
|
|
53
|
+
getInstanceCount(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Update instance count
|
|
56
|
+
* @param removed Is removed?
|
|
57
|
+
* @returns Current instance count
|
|
33
58
|
*/
|
|
34
|
-
|
|
59
|
+
updateInstanceCount(removed: boolean): number;
|
|
35
60
|
}
|
|
@@ -11,9 +11,16 @@ class WindowStorage {
|
|
|
11
11
|
/**
|
|
12
12
|
* Constructor
|
|
13
13
|
* @param globalFields Global fields
|
|
14
|
+
* @param callback Field and data callback
|
|
14
15
|
*/
|
|
15
|
-
constructor(globalFields) {
|
|
16
|
+
constructor(globalFields, callback) {
|
|
16
17
|
this.globalFields = globalFields;
|
|
18
|
+
/**
|
|
19
|
+
* Instance count field name
|
|
20
|
+
*/
|
|
21
|
+
this.instanceCountField = 'EtsooSmartERPInstanceCount';
|
|
22
|
+
// Init instance index
|
|
23
|
+
this._instanceIndex = this.getInstanceCount();
|
|
17
24
|
if (globalFields.length == 0)
|
|
18
25
|
return;
|
|
19
26
|
// Copy global fields to session storage where first item does not exist
|
|
@@ -22,20 +29,30 @@ class WindowStorage {
|
|
|
22
29
|
if (firsItem)
|
|
23
30
|
return;
|
|
24
31
|
globalFields.forEach((field) => {
|
|
25
|
-
const data = localStorage.getItem(field);
|
|
26
|
-
if (data
|
|
32
|
+
const data = callback(field, localStorage.getItem(field), this._instanceIndex);
|
|
33
|
+
if (data == null)
|
|
34
|
+
sessionStorage.removeItem(field);
|
|
35
|
+
else
|
|
27
36
|
sessionStorage.setItem(field, data);
|
|
28
|
-
}
|
|
29
37
|
});
|
|
30
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Current instance index
|
|
41
|
+
*/
|
|
42
|
+
get instanceIndex() {
|
|
43
|
+
return this._instanceIndex;
|
|
44
|
+
}
|
|
31
45
|
/**
|
|
32
46
|
* Get data
|
|
33
47
|
* @param key Key name
|
|
34
48
|
* @param defaultValue Default value
|
|
49
|
+
* @param persistance From the persisted data
|
|
35
50
|
*/
|
|
36
|
-
getData(key, defaultValue) {
|
|
51
|
+
getData(key, defaultValue, persistance) {
|
|
37
52
|
// Get storage
|
|
38
|
-
const data =
|
|
53
|
+
const data = persistance
|
|
54
|
+
? localStorage.getItem(key)
|
|
55
|
+
: sessionStorage.getItem(key);
|
|
39
56
|
// No default value
|
|
40
57
|
if (defaultValue == null)
|
|
41
58
|
return Utils_1.Utils.parseString(data);
|
|
@@ -45,10 +62,13 @@ class WindowStorage {
|
|
|
45
62
|
/**
|
|
46
63
|
* Get object data
|
|
47
64
|
* @param key Key name
|
|
65
|
+
* @param persistance From the persisted data
|
|
48
66
|
*/
|
|
49
|
-
getObject(key) {
|
|
67
|
+
getObject(key, persistance) {
|
|
50
68
|
// Get storage
|
|
51
|
-
const data =
|
|
69
|
+
const data = persistance
|
|
70
|
+
? localStorage.getItem(key)
|
|
71
|
+
: sessionStorage.getItem(key);
|
|
52
72
|
if (data == null)
|
|
53
73
|
return undefined;
|
|
54
74
|
return JSON.parse(data);
|
|
@@ -57,12 +77,38 @@ class WindowStorage {
|
|
|
57
77
|
* Set data
|
|
58
78
|
* @param key Key name
|
|
59
79
|
* @param data Data, null for removal
|
|
80
|
+
* @param persistance To the persisted data, false will stop persistance
|
|
60
81
|
*/
|
|
61
|
-
setData(key, data) {
|
|
82
|
+
setData(key, data, persistance) {
|
|
83
|
+
if (persistance) {
|
|
84
|
+
StorageUtils_1.StorageUtils.setLocalData(key, data);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
62
87
|
StorageUtils_1.StorageUtils.setSessionData(key, data);
|
|
63
|
-
if (this.globalFields.includes(key)) {
|
|
88
|
+
if (persistance !== false && this.globalFields.includes(key)) {
|
|
64
89
|
StorageUtils_1.StorageUtils.setLocalData(key, data);
|
|
65
90
|
}
|
|
66
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Get current instance count
|
|
94
|
+
* @returns Current instance count
|
|
95
|
+
*/
|
|
96
|
+
getInstanceCount() {
|
|
97
|
+
const count = this.getData(this.instanceCountField, 0, true);
|
|
98
|
+
// Make sure starting from 0
|
|
99
|
+
if (count < 0)
|
|
100
|
+
return 0;
|
|
101
|
+
return count;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Update instance count
|
|
105
|
+
* @param removed Is removed?
|
|
106
|
+
* @returns Current instance count
|
|
107
|
+
*/
|
|
108
|
+
updateInstanceCount(removed) {
|
|
109
|
+
const count = this.getInstanceCount() + (removed ? -1 : 1);
|
|
110
|
+
this.setData(this.instanceCountField, count, true);
|
|
111
|
+
return count;
|
|
112
|
+
}
|
|
67
113
|
}
|
|
68
114
|
exports.WindowStorage = WindowStorage;
|
|
@@ -2,32 +2,57 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Current instance index
|
|
7
|
+
*/
|
|
8
|
+
readonly instanceIndex: number;
|
|
5
9
|
/**
|
|
6
10
|
* Get data
|
|
7
11
|
* @param key Key name
|
|
12
|
+
* @param persistance From the persisted data
|
|
8
13
|
*/
|
|
9
|
-
getData<T>(key: string): T | undefined;
|
|
14
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
10
15
|
/**
|
|
11
16
|
* Get data with default value
|
|
12
17
|
* @param key Key name
|
|
13
18
|
* @param defaultValue Default value
|
|
19
|
+
* @param persistance From the persisted data
|
|
14
20
|
*/
|
|
15
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
21
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
16
22
|
/**
|
|
17
|
-
* Get
|
|
23
|
+
* Get object data
|
|
18
24
|
* @param key Key name
|
|
25
|
+
* @param persistance From the persisted data
|
|
19
26
|
*/
|
|
20
|
-
getObject<T extends {}>(key: string): T | undefined;
|
|
27
|
+
getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
|
|
21
28
|
/**
|
|
22
29
|
* Set data
|
|
23
30
|
* @param key Key name
|
|
24
31
|
* @param data Data, null for removal
|
|
32
|
+
* @param persistance Persist the data, false will stop persistance
|
|
25
33
|
*/
|
|
26
|
-
setData(key: string, data: unknown): void;
|
|
34
|
+
setData(key: string, data: unknown, persistance?: boolean): void;
|
|
35
|
+
/**
|
|
36
|
+
* Get current instance count
|
|
37
|
+
* @returns Current instance count
|
|
38
|
+
*/
|
|
39
|
+
getInstanceCount(): number;
|
|
40
|
+
/**
|
|
41
|
+
* Update instance count
|
|
42
|
+
* @param removed Is removed?
|
|
43
|
+
* @returns Current instance count
|
|
44
|
+
*/
|
|
45
|
+
updateInstanceCount(removed: boolean): number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Storage init callback
|
|
49
|
+
*/
|
|
50
|
+
export interface IStorageInitCallback {
|
|
51
|
+
(field: string, data: string | null, instanceIndex: number): string | null;
|
|
27
52
|
}
|
|
28
53
|
/**
|
|
29
54
|
* Storage constructor interface
|
|
30
55
|
*/
|
|
31
56
|
export interface IStorageConstructor {
|
|
32
|
-
new (globalFields: string[]): IStorage;
|
|
57
|
+
new (globalFields: string[], callback: IStorageInitCallback): IStorage;
|
|
33
58
|
}
|
|
@@ -1,35 +1,60 @@
|
|
|
1
|
-
import { IStorage } from './IStorage';
|
|
1
|
+
import { IStorage, IStorageInitCallback } from './IStorage';
|
|
2
2
|
/**
|
|
3
3
|
* Window storage
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
5
|
*/
|
|
6
6
|
export declare class WindowStorage implements IStorage {
|
|
7
7
|
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Instance count field name
|
|
10
|
+
*/
|
|
11
|
+
private readonly instanceCountField;
|
|
12
|
+
private _instanceIndex;
|
|
13
|
+
/**
|
|
14
|
+
* Current instance index
|
|
15
|
+
*/
|
|
16
|
+
get instanceIndex(): number;
|
|
8
17
|
/**
|
|
9
18
|
* Constructor
|
|
10
19
|
* @param globalFields Global fields
|
|
20
|
+
* @param callback Field and data callback
|
|
11
21
|
*/
|
|
12
|
-
constructor(globalFields: string[]);
|
|
22
|
+
constructor(globalFields: string[], callback: IStorageInitCallback);
|
|
13
23
|
/**
|
|
14
24
|
* Get data
|
|
15
25
|
* @param key Key name
|
|
26
|
+
* @param persistance From the persisted data
|
|
16
27
|
*/
|
|
17
|
-
getData<T>(key: string): T | undefined;
|
|
28
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
18
29
|
/**
|
|
19
30
|
* Get data with default value
|
|
20
31
|
* @param key Key name
|
|
21
32
|
* @param defaultValue Default value
|
|
33
|
+
* @param persistance From the persisted data
|
|
22
34
|
*/
|
|
23
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
35
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
24
36
|
/**
|
|
25
37
|
* Get object data
|
|
26
38
|
* @param key Key name
|
|
39
|
+
* @param persistance From the persisted data
|
|
27
40
|
*/
|
|
28
|
-
getObject<T extends {}>(key: string): T | undefined;
|
|
41
|
+
getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
|
|
29
42
|
/**
|
|
30
43
|
* Set data
|
|
31
44
|
* @param key Key name
|
|
32
45
|
* @param data Data, null for removal
|
|
46
|
+
* @param persistance To the persisted data, false will stop persistance
|
|
47
|
+
*/
|
|
48
|
+
setData(key: string, data: unknown, persistance?: boolean): void;
|
|
49
|
+
/**
|
|
50
|
+
* Get current instance count
|
|
51
|
+
* @returns Current instance count
|
|
52
|
+
*/
|
|
53
|
+
getInstanceCount(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Update instance count
|
|
56
|
+
* @param removed Is removed?
|
|
57
|
+
* @returns Current instance count
|
|
33
58
|
*/
|
|
34
|
-
|
|
59
|
+
updateInstanceCount(removed: boolean): number;
|
|
35
60
|
}
|
|
@@ -8,9 +8,16 @@ export class WindowStorage {
|
|
|
8
8
|
/**
|
|
9
9
|
* Constructor
|
|
10
10
|
* @param globalFields Global fields
|
|
11
|
+
* @param callback Field and data callback
|
|
11
12
|
*/
|
|
12
|
-
constructor(globalFields) {
|
|
13
|
+
constructor(globalFields, callback) {
|
|
13
14
|
this.globalFields = globalFields;
|
|
15
|
+
/**
|
|
16
|
+
* Instance count field name
|
|
17
|
+
*/
|
|
18
|
+
this.instanceCountField = 'EtsooSmartERPInstanceCount';
|
|
19
|
+
// Init instance index
|
|
20
|
+
this._instanceIndex = this.getInstanceCount();
|
|
14
21
|
if (globalFields.length == 0)
|
|
15
22
|
return;
|
|
16
23
|
// Copy global fields to session storage where first item does not exist
|
|
@@ -19,20 +26,30 @@ export class WindowStorage {
|
|
|
19
26
|
if (firsItem)
|
|
20
27
|
return;
|
|
21
28
|
globalFields.forEach((field) => {
|
|
22
|
-
const data = localStorage.getItem(field);
|
|
23
|
-
if (data
|
|
29
|
+
const data = callback(field, localStorage.getItem(field), this._instanceIndex);
|
|
30
|
+
if (data == null)
|
|
31
|
+
sessionStorage.removeItem(field);
|
|
32
|
+
else
|
|
24
33
|
sessionStorage.setItem(field, data);
|
|
25
|
-
}
|
|
26
34
|
});
|
|
27
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Current instance index
|
|
38
|
+
*/
|
|
39
|
+
get instanceIndex() {
|
|
40
|
+
return this._instanceIndex;
|
|
41
|
+
}
|
|
28
42
|
/**
|
|
29
43
|
* Get data
|
|
30
44
|
* @param key Key name
|
|
31
45
|
* @param defaultValue Default value
|
|
46
|
+
* @param persistance From the persisted data
|
|
32
47
|
*/
|
|
33
|
-
getData(key, defaultValue) {
|
|
48
|
+
getData(key, defaultValue, persistance) {
|
|
34
49
|
// Get storage
|
|
35
|
-
const data =
|
|
50
|
+
const data = persistance
|
|
51
|
+
? localStorage.getItem(key)
|
|
52
|
+
: sessionStorage.getItem(key);
|
|
36
53
|
// No default value
|
|
37
54
|
if (defaultValue == null)
|
|
38
55
|
return Utils.parseString(data);
|
|
@@ -42,10 +59,13 @@ export class WindowStorage {
|
|
|
42
59
|
/**
|
|
43
60
|
* Get object data
|
|
44
61
|
* @param key Key name
|
|
62
|
+
* @param persistance From the persisted data
|
|
45
63
|
*/
|
|
46
|
-
getObject(key) {
|
|
64
|
+
getObject(key, persistance) {
|
|
47
65
|
// Get storage
|
|
48
|
-
const data =
|
|
66
|
+
const data = persistance
|
|
67
|
+
? localStorage.getItem(key)
|
|
68
|
+
: sessionStorage.getItem(key);
|
|
49
69
|
if (data == null)
|
|
50
70
|
return undefined;
|
|
51
71
|
return JSON.parse(data);
|
|
@@ -54,11 +74,37 @@ export class WindowStorage {
|
|
|
54
74
|
* Set data
|
|
55
75
|
* @param key Key name
|
|
56
76
|
* @param data Data, null for removal
|
|
77
|
+
* @param persistance To the persisted data, false will stop persistance
|
|
57
78
|
*/
|
|
58
|
-
setData(key, data) {
|
|
79
|
+
setData(key, data, persistance) {
|
|
80
|
+
if (persistance) {
|
|
81
|
+
StorageUtils.setLocalData(key, data);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
59
84
|
StorageUtils.setSessionData(key, data);
|
|
60
|
-
if (this.globalFields.includes(key)) {
|
|
85
|
+
if (persistance !== false && this.globalFields.includes(key)) {
|
|
61
86
|
StorageUtils.setLocalData(key, data);
|
|
62
87
|
}
|
|
63
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Get current instance count
|
|
91
|
+
* @returns Current instance count
|
|
92
|
+
*/
|
|
93
|
+
getInstanceCount() {
|
|
94
|
+
const count = this.getData(this.instanceCountField, 0, true);
|
|
95
|
+
// Make sure starting from 0
|
|
96
|
+
if (count < 0)
|
|
97
|
+
return 0;
|
|
98
|
+
return count;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update instance count
|
|
102
|
+
* @param removed Is removed?
|
|
103
|
+
* @returns Current instance count
|
|
104
|
+
*/
|
|
105
|
+
updateInstanceCount(removed) {
|
|
106
|
+
const count = this.getInstanceCount() + (removed ? -1 : 1);
|
|
107
|
+
this.setData(this.instanceCountField, count, true);
|
|
108
|
+
return count;
|
|
109
|
+
}
|
|
64
110
|
}
|
package/package.json
CHANGED
package/src/storage/IStorage.ts
CHANGED
|
@@ -2,36 +2,65 @@
|
|
|
2
2
|
* Storage interface
|
|
3
3
|
*/
|
|
4
4
|
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Current instance index
|
|
7
|
+
*/
|
|
8
|
+
readonly instanceIndex: number;
|
|
9
|
+
|
|
5
10
|
/**
|
|
6
11
|
* Get data
|
|
7
12
|
* @param key Key name
|
|
13
|
+
* @param persistance From the persisted data
|
|
8
14
|
*/
|
|
9
|
-
getData<T>(key: string): T | undefined;
|
|
15
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
10
16
|
|
|
11
17
|
/**
|
|
12
18
|
* Get data with default value
|
|
13
19
|
* @param key Key name
|
|
14
20
|
* @param defaultValue Default value
|
|
21
|
+
* @param persistance From the persisted data
|
|
15
22
|
*/
|
|
16
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
23
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
17
24
|
|
|
18
25
|
/**
|
|
19
|
-
* Get
|
|
26
|
+
* Get object data
|
|
20
27
|
* @param key Key name
|
|
28
|
+
* @param persistance From the persisted data
|
|
21
29
|
*/
|
|
22
|
-
getObject<T extends {}>(key: string): T | undefined;
|
|
30
|
+
getObject<T extends {}>(key: string, persistance?: boolean): T | undefined;
|
|
23
31
|
|
|
24
32
|
/**
|
|
25
33
|
* Set data
|
|
26
34
|
* @param key Key name
|
|
27
35
|
* @param data Data, null for removal
|
|
36
|
+
* @param persistance Persist the data, false will stop persistance
|
|
28
37
|
*/
|
|
29
|
-
setData(key: string, data: unknown): void;
|
|
38
|
+
setData(key: string, data: unknown, persistance?: boolean): void;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get current instance count
|
|
42
|
+
* @returns Current instance count
|
|
43
|
+
*/
|
|
44
|
+
getInstanceCount(): number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Update instance count
|
|
48
|
+
* @param removed Is removed?
|
|
49
|
+
* @returns Current instance count
|
|
50
|
+
*/
|
|
51
|
+
updateInstanceCount(removed: boolean): number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Storage init callback
|
|
56
|
+
*/
|
|
57
|
+
export interface IStorageInitCallback {
|
|
58
|
+
(field: string, data: string | null, instanceIndex: number): string | null;
|
|
30
59
|
}
|
|
31
60
|
|
|
32
61
|
/**
|
|
33
62
|
* Storage constructor interface
|
|
34
63
|
*/
|
|
35
64
|
export interface IStorageConstructor {
|
|
36
|
-
new (globalFields: string[]): IStorage;
|
|
65
|
+
new (globalFields: string[], callback: IStorageInitCallback): IStorage;
|
|
37
66
|
}
|
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
import { StorageUtils } from '../StorageUtils';
|
|
2
2
|
import { Utils } from '../Utils';
|
|
3
|
-
import { IStorage } from './IStorage';
|
|
3
|
+
import { IStorage, IStorageInitCallback } from './IStorage';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Window storage
|
|
7
7
|
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
8
8
|
*/
|
|
9
9
|
export class WindowStorage implements IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Instance count field name
|
|
12
|
+
*/
|
|
13
|
+
private readonly instanceCountField = 'EtsooSmartERPInstanceCount';
|
|
14
|
+
|
|
15
|
+
private _instanceIndex: number;
|
|
16
|
+
/**
|
|
17
|
+
* Current instance index
|
|
18
|
+
*/
|
|
19
|
+
get instanceIndex() {
|
|
20
|
+
return this._instanceIndex;
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
/**
|
|
11
24
|
* Constructor
|
|
12
25
|
* @param globalFields Global fields
|
|
26
|
+
* @param callback Field and data callback
|
|
13
27
|
*/
|
|
14
|
-
constructor(
|
|
28
|
+
constructor(
|
|
29
|
+
private globalFields: string[],
|
|
30
|
+
callback: IStorageInitCallback
|
|
31
|
+
) {
|
|
32
|
+
// Init instance index
|
|
33
|
+
this._instanceIndex = this.getInstanceCount();
|
|
34
|
+
|
|
15
35
|
if (globalFields.length == 0) return;
|
|
16
36
|
|
|
17
37
|
// Copy global fields to session storage where first item does not exist
|
|
@@ -20,34 +40,46 @@ export class WindowStorage implements IStorage {
|
|
|
20
40
|
if (firsItem) return;
|
|
21
41
|
|
|
22
42
|
globalFields.forEach((field) => {
|
|
23
|
-
const data =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
43
|
+
const data = callback(
|
|
44
|
+
field,
|
|
45
|
+
localStorage.getItem(field),
|
|
46
|
+
this._instanceIndex
|
|
47
|
+
);
|
|
48
|
+
if (data == null) sessionStorage.removeItem(field);
|
|
49
|
+
else sessionStorage.setItem(field, data);
|
|
27
50
|
});
|
|
28
51
|
}
|
|
29
52
|
|
|
30
53
|
/**
|
|
31
54
|
* Get data
|
|
32
55
|
* @param key Key name
|
|
56
|
+
* @param persistance From the persisted data
|
|
33
57
|
*/
|
|
34
|
-
getData<T>(key: string): T | undefined;
|
|
58
|
+
getData<T>(key: string, persistance?: boolean): T | undefined;
|
|
35
59
|
|
|
36
60
|
/**
|
|
37
61
|
* Get data with default value
|
|
38
62
|
* @param key Key name
|
|
39
63
|
* @param defaultValue Default value
|
|
64
|
+
* @param persistance From the persisted data
|
|
40
65
|
*/
|
|
41
|
-
getData<T>(key: string, defaultValue: T): T;
|
|
66
|
+
getData<T>(key: string, defaultValue: T, persistance?: boolean): T;
|
|
42
67
|
|
|
43
68
|
/**
|
|
44
69
|
* Get data
|
|
45
70
|
* @param key Key name
|
|
46
71
|
* @param defaultValue Default value
|
|
72
|
+
* @param persistance From the persisted data
|
|
47
73
|
*/
|
|
48
|
-
getData<T>(
|
|
74
|
+
getData<T>(
|
|
75
|
+
key: string,
|
|
76
|
+
defaultValue?: T,
|
|
77
|
+
persistance?: boolean
|
|
78
|
+
): T | undefined {
|
|
49
79
|
// Get storage
|
|
50
|
-
const data =
|
|
80
|
+
const data = persistance
|
|
81
|
+
? localStorage.getItem(key)
|
|
82
|
+
: sessionStorage.getItem(key);
|
|
51
83
|
|
|
52
84
|
// No default value
|
|
53
85
|
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
@@ -59,11 +91,16 @@ export class WindowStorage implements IStorage {
|
|
|
59
91
|
/**
|
|
60
92
|
* Get object data
|
|
61
93
|
* @param key Key name
|
|
94
|
+
* @param persistance From the persisted data
|
|
62
95
|
*/
|
|
63
|
-
getObject<T extends {}>(key: string) {
|
|
96
|
+
getObject<T extends {}>(key: string, persistance?: boolean) {
|
|
64
97
|
// Get storage
|
|
65
|
-
const data =
|
|
98
|
+
const data = persistance
|
|
99
|
+
? localStorage.getItem(key)
|
|
100
|
+
: sessionStorage.getItem(key);
|
|
101
|
+
|
|
66
102
|
if (data == null) return undefined;
|
|
103
|
+
|
|
67
104
|
return <T>JSON.parse(data);
|
|
68
105
|
}
|
|
69
106
|
|
|
@@ -71,11 +108,39 @@ export class WindowStorage implements IStorage {
|
|
|
71
108
|
* Set data
|
|
72
109
|
* @param key Key name
|
|
73
110
|
* @param data Data, null for removal
|
|
111
|
+
* @param persistance To the persisted data, false will stop persistance
|
|
74
112
|
*/
|
|
75
|
-
setData(key: string, data: unknown) {
|
|
113
|
+
setData(key: string, data: unknown, persistance?: boolean) {
|
|
114
|
+
if (persistance) {
|
|
115
|
+
StorageUtils.setLocalData(key, data);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
76
119
|
StorageUtils.setSessionData(key, data);
|
|
77
|
-
if (this.globalFields.includes(key)) {
|
|
120
|
+
if (persistance !== false && this.globalFields.includes(key)) {
|
|
78
121
|
StorageUtils.setLocalData(key, data);
|
|
79
122
|
}
|
|
80
123
|
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Get current instance count
|
|
127
|
+
* @returns Current instance count
|
|
128
|
+
*/
|
|
129
|
+
getInstanceCount() {
|
|
130
|
+
const count = this.getData(this.instanceCountField, 0, true);
|
|
131
|
+
// Make sure starting from 0
|
|
132
|
+
if (count < 0) return 0;
|
|
133
|
+
return count;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Update instance count
|
|
138
|
+
* @param removed Is removed?
|
|
139
|
+
* @returns Current instance count
|
|
140
|
+
*/
|
|
141
|
+
updateInstanceCount(removed: boolean) {
|
|
142
|
+
const count = this.getInstanceCount() + (removed ? -1 : 1);
|
|
143
|
+
this.setData(this.instanceCountField, count, true);
|
|
144
|
+
return count;
|
|
145
|
+
}
|
|
81
146
|
}
|