@sankhyalabs/core 0.0.0-bugfix-dev-KB-74224.2 → 0.0.0-bugfix-dev-KB-76362.0
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/.docs/classes/Change.md +11 -11
- package/.docs/classes/DataUnit.md +32 -24
- package/.docs/classes/SelectionInfo.md +12 -12
- package/.docs/enumerations/ChangeOperation.md +4 -4
- package/.docs/enumerations/SelectionMode.md +2 -2
- package/.docs/globals.md +0 -2
- package/.docs/interfaces/DUActionInterceptor.md +1 -1
- package/.docs/interfaces/PageRequest.md +3 -3
- package/.docs/interfaces/PaginationInfo.md +13 -0
- package/.docs/interfaces/QuickFilter.md +3 -3
- package/.docs/interfaces/Record.md +4 -4
- package/.docs/interfaces/SavedRecord.md +5 -5
- package/.docs/interfaces/WaitingChange.md +3 -3
- package/.docs/type-aliases/DataUnitEventOptions.md +1 -1
- package/bun.lockb +0 -0
- package/dist/dataunit/DataUnit.d.ts +4 -4
- package/dist/dataunit/DataUnit.js +25 -12
- package/dist/dataunit/DataUnit.js.map +1 -1
- package/dist/dataunit/loading/PaginationInfo.d.ts +4 -0
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/reports/test-report.xml +66 -66
- package/src/dataunit/DataUnit.ts +25 -13
- package/src/dataunit/loading/PaginationInfo.ts +5 -0
- package/src/index.ts +0 -4
- package/.docs/classes/ServiceUtils.md +0 -67
- package/.docs/enumerations/StorageType.md +0 -27
- package/dist/utils/CacheManager/index.d.ts +0 -70
- package/dist/utils/CacheManager/index.js +0 -106
- package/dist/utils/CacheManager/index.js.map +0 -1
- package/dist/utils/CacheManager/interfaces/index.d.ts +0 -4
- package/dist/utils/CacheManager/interfaces/index.js +0 -6
- package/dist/utils/CacheManager/interfaces/index.js.map +0 -1
- package/dist/utils/ServiceUtils.d.ts +0 -24
- package/dist/utils/ServiceUtils.js +0 -40
- package/dist/utils/ServiceUtils.js.map +0 -1
- package/src/utils/CacheManager/index.ts +0 -109
- package/src/utils/CacheManager/interfaces/index.ts +0 -4
- package/src/utils/ServiceUtils.ts +0 -36
package/src/dataunit/DataUnit.ts
CHANGED
|
@@ -40,7 +40,7 @@ export default class DataUnit {
|
|
|
40
40
|
|
|
41
41
|
private _uuid: string;
|
|
42
42
|
private _name: string;
|
|
43
|
-
private _observers:
|
|
43
|
+
private _observers: Map<string, (action: DataUnitAction, options?: DataUnitEventOptions) => void>;
|
|
44
44
|
private _sortingProvider?: SortingProvider;
|
|
45
45
|
private _filterProviders: Map<string, FilterProvider>;
|
|
46
46
|
private _stateManager: StateManager;
|
|
@@ -81,7 +81,7 @@ export default class DataUnit {
|
|
|
81
81
|
SnapshotReducer
|
|
82
82
|
]
|
|
83
83
|
);
|
|
84
|
-
this._observers =
|
|
84
|
+
this._observers = new Map();
|
|
85
85
|
this._filterProviders = new Map<string, FilterProvider>();
|
|
86
86
|
this._sortingProvider = undefined;
|
|
87
87
|
this._defaultSorting = [];
|
|
@@ -119,7 +119,7 @@ export default class DataUnit {
|
|
|
119
119
|
public releaseCallbacks(){
|
|
120
120
|
if(!this._allowReleaseCallbacks) return;
|
|
121
121
|
|
|
122
|
-
this._observers =
|
|
122
|
+
this._observers = new Map();
|
|
123
123
|
this._filterProviders = new Map<string, FilterProvider>();
|
|
124
124
|
this._sortingProvider = undefined;
|
|
125
125
|
this._interceptors = new Map();
|
|
@@ -1579,10 +1579,7 @@ export default class DataUnit {
|
|
|
1579
1579
|
this._stateManager.process(action);
|
|
1580
1580
|
this?._parentDataUnit?.dispatchAction(Action.CHILD_CHANGED, { srcAction: action, srcDataUnit: this });
|
|
1581
1581
|
this._observers.forEach(f => {
|
|
1582
|
-
|
|
1583
|
-
if some observer throws exceptions,
|
|
1584
|
-
should be continued
|
|
1585
|
-
*/
|
|
1582
|
+
//if some observer throws exceptions, should be continued
|
|
1586
1583
|
try {
|
|
1587
1584
|
f(action, options);
|
|
1588
1585
|
} catch (e) {
|
|
@@ -1621,10 +1618,17 @@ export default class DataUnit {
|
|
|
1621
1618
|
* Ela vai ser chamada sempre que uma ação for despachada (dispatchAction()).
|
|
1622
1619
|
*
|
|
1623
1620
|
* @param observer - Função que recebe como parâmetro as ações que serão monitoradas.
|
|
1624
|
-
*
|
|
1621
|
+
* @param uuid - Identificador do observer. Quando não informado, será gerado um identificador aleatório.
|
|
1625
1622
|
*/
|
|
1626
|
-
public subscribe(observer: (action: DataUnitAction, options?:DataUnitEventOptions) => void | Promise<void
|
|
1627
|
-
|
|
1623
|
+
public subscribe(observer: (action: DataUnitAction, options?: DataUnitEventOptions) => void | Promise<void>, uuid?: string): string {
|
|
1624
|
+
if (uuid) {
|
|
1625
|
+
this._observers.set(uuid, observer);
|
|
1626
|
+
} else {
|
|
1627
|
+
uuid = StringUtils.generateUUID();
|
|
1628
|
+
this._observers.set(uuid, observer);
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
return uuid;
|
|
1628
1632
|
}
|
|
1629
1633
|
|
|
1630
1634
|
/**
|
|
@@ -1632,10 +1636,18 @@ export default class DataUnit {
|
|
|
1632
1636
|
* Remove um observer existente.
|
|
1633
1637
|
*
|
|
1634
1638
|
* @param observer - Observer que se deseja remover.
|
|
1635
|
-
*
|
|
1639
|
+
* @param uuid - Identificador do observer. Quando não informado o delete removera com base no equals do observer.
|
|
1636
1640
|
*/
|
|
1637
|
-
public unsubscribe(observer: Function) {
|
|
1638
|
-
|
|
1641
|
+
public unsubscribe(observer: Function, uuid?: string) {
|
|
1642
|
+
if (uuid) {
|
|
1643
|
+
this._observers.delete(uuid);
|
|
1644
|
+
} else {
|
|
1645
|
+
this._observers.forEach((valor, chave) => {
|
|
1646
|
+
if (valor == observer) {
|
|
1647
|
+
this._observers.delete(chave);
|
|
1648
|
+
}
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1639
1651
|
}
|
|
1640
1652
|
|
|
1641
1653
|
/**
|
|
@@ -18,4 +18,9 @@ export interface PaginationInfo {
|
|
|
18
18
|
|
|
19
19
|
/** Se ainda existem mais registros */
|
|
20
20
|
hasMore: boolean;
|
|
21
|
+
|
|
22
|
+
/** Informa se o carregamento de dados em background está sendo executado
|
|
23
|
+
* Caso o dataunit não tenha carga paralela o valor será indefinido
|
|
24
|
+
*/
|
|
25
|
+
loadingInProgress?: boolean;
|
|
21
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -40,8 +40,6 @@ import { IRepositoryIndex } from "./repository/indexeddb/IRepositoryIndex.js"
|
|
|
40
40
|
import { FieldComparator } from "./dataunit/sorting/FieldComparator.js";
|
|
41
41
|
import { KeyboardManager } from "./utils/KeyboardManager/index.js";
|
|
42
42
|
import { SearchUtils } from "./utils/SearchUtils.js";
|
|
43
|
-
import { ServiceUtils } from "./utils/ServiceUtils.js";
|
|
44
|
-
import { StorageType } from "./utils/CacheManager/index.js";
|
|
45
43
|
import OverflowWatcher, { OnOverflowCallBack, OverflowDirection, OverFlowWatcherParams, OVERFLOWED_CLASS_NAME } from "./utils/OverflowWatcher/index.js";
|
|
46
44
|
|
|
47
45
|
/*Classes públicas no pacote*/
|
|
@@ -110,8 +108,6 @@ export {
|
|
|
110
108
|
defaultDataLoader,
|
|
111
109
|
KeyboardManager,
|
|
112
110
|
SearchUtils,
|
|
113
|
-
ServiceUtils,
|
|
114
|
-
StorageType,
|
|
115
111
|
OverflowWatcher,
|
|
116
112
|
OnOverflowCallBack,
|
|
117
113
|
OverflowDirection,
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
[**@sankhyalabs/core**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@sankhyalabs/core](../globals.md) / ServiceUtils
|
|
6
|
-
|
|
7
|
-
# Class: ServiceUtils
|
|
8
|
-
|
|
9
|
-
## Constructors
|
|
10
|
-
|
|
11
|
-
### new ServiceUtils()
|
|
12
|
-
|
|
13
|
-
> **new ServiceUtils**(): [`ServiceUtils`](ServiceUtils.md)
|
|
14
|
-
|
|
15
|
-
#### Returns
|
|
16
|
-
|
|
17
|
-
[`ServiceUtils`](ServiceUtils.md)
|
|
18
|
-
|
|
19
|
-
## Methods
|
|
20
|
-
|
|
21
|
-
### useCacheWithService()
|
|
22
|
-
|
|
23
|
-
> `static` **useCacheWithService**\<`T`\>(`identifier`, `fetchFunction`, `storageType`): `Promise`\<`T`\>
|
|
24
|
-
|
|
25
|
-
Auxilia no uso do CacheManager, gerando automaticamente uma chave de cache com base no identificador.
|
|
26
|
-
|
|
27
|
-
#### Type parameters
|
|
28
|
-
|
|
29
|
-
• **T**
|
|
30
|
-
|
|
31
|
-
Tipo do dado a ser retornado.
|
|
32
|
-
|
|
33
|
-
#### Parameters
|
|
34
|
-
|
|
35
|
-
• **identifier**: `string`
|
|
36
|
-
|
|
37
|
-
Identificadores únicos usados para compor a chave de cache.
|
|
38
|
-
|
|
39
|
-
• **fetchFunction**
|
|
40
|
-
|
|
41
|
-
Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
42
|
-
|
|
43
|
-
• **storageType**: [`StorageType`](../enumerations/StorageType.md)= `StorageType.SESSION_STORAGE`
|
|
44
|
-
|
|
45
|
-
Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
46
|
-
|
|
47
|
-
#### Returns
|
|
48
|
-
|
|
49
|
-
`Promise`\<`T`\>
|
|
50
|
-
|
|
51
|
-
Uma `Promise` com o valor armazenado ou obtido via `fetchFunction`.
|
|
52
|
-
|
|
53
|
-
#### Example
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
const actions = await useCacheWithService(
|
|
57
|
-
`${this.entityName} - ${this.resourceID}`,
|
|
58
|
-
async () => {
|
|
59
|
-
return await fetchActionsFromAPI();
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
console.log(actions);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
#### Source
|
|
66
|
-
|
|
67
|
-
src/utils/ServiceUtils.ts:28
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
[**@sankhyalabs/core**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@sankhyalabs/core](../globals.md) / StorageType
|
|
6
|
-
|
|
7
|
-
# Enumeration: StorageType
|
|
8
|
-
|
|
9
|
-
## Enumeration Members
|
|
10
|
-
|
|
11
|
-
### LOCAL\_STORAGE
|
|
12
|
-
|
|
13
|
-
> **LOCAL\_STORAGE**: `"localStorage"`
|
|
14
|
-
|
|
15
|
-
#### Source
|
|
16
|
-
|
|
17
|
-
src/utils/CacheManager/interfaces/index.ts:3
|
|
18
|
-
|
|
19
|
-
***
|
|
20
|
-
|
|
21
|
-
### SESSION\_STORAGE
|
|
22
|
-
|
|
23
|
-
> **SESSION\_STORAGE**: `"sessionStorage"`
|
|
24
|
-
|
|
25
|
-
#### Source
|
|
26
|
-
|
|
27
|
-
src/utils/CacheManager/interfaces/index.ts:2
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { StorageType } from './interfaces/index.js';
|
|
2
|
-
export * from "./interfaces/index.js";
|
|
3
|
-
/**
|
|
4
|
-
* Gerenciador de cache com suporte a `sessionStorage` e `localStorage`.
|
|
5
|
-
*/
|
|
6
|
-
export declare class CacheManager {
|
|
7
|
-
/**
|
|
8
|
-
* Nome da chave utilizada para armazenar o cache no armazenamento.
|
|
9
|
-
*/
|
|
10
|
-
private static readonly storageKey;
|
|
11
|
-
/**
|
|
12
|
-
* Recupera ou define o valor no cache.
|
|
13
|
-
*
|
|
14
|
-
* @template T Tipo do dado a ser cacheado.
|
|
15
|
-
* @param key Identificador único para armazenar e recuperar o valor.
|
|
16
|
-
* @param fetchCallback Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
17
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
18
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchCallback`.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* const data = await CacheManager.getOrSet('uniqueKey', async () => {
|
|
23
|
-
* return await fetchDataFromAPI();
|
|
24
|
-
* }, 'sessionStorage');
|
|
25
|
-
* console.log(data);
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
static getOrSet<T>(key: string, fetchCallback: () => Promise<T>, storageType?: StorageType): Promise<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Remove uma entrada específica do cache.
|
|
31
|
-
*
|
|
32
|
-
* @param key Identificador único da entrada a ser removida.
|
|
33
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```typescript
|
|
37
|
-
* CacheManager.clear('my-cache-key', 'localStorage');
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
static clear(key: string, storageType?: StorageType): void;
|
|
41
|
-
/**
|
|
42
|
-
* Remove todas as entradas do cache.
|
|
43
|
-
*
|
|
44
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```typescript
|
|
48
|
-
* CacheManager.clearAll('localStorage');
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
static clearAll(storageType?: StorageType): void;
|
|
52
|
-
/**
|
|
53
|
-
* Obtém o cache armazenado no armazenamento especificado.
|
|
54
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
55
|
-
* @returns Um objeto representando o cache armazenado.
|
|
56
|
-
*/
|
|
57
|
-
private static getCache;
|
|
58
|
-
/**
|
|
59
|
-
* Salva o cache no armazenamento especificado.
|
|
60
|
-
* @param cache O objeto representando o cache a ser salvo.
|
|
61
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
62
|
-
*/
|
|
63
|
-
private static saveCache;
|
|
64
|
-
/**
|
|
65
|
-
* Retorna o armazenamento correspondente ao tipo especificado.
|
|
66
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
67
|
-
* @returns O objeto de armazenamento correspondente.
|
|
68
|
-
*/
|
|
69
|
-
private static getStorage;
|
|
70
|
-
}
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { StorageType } from './interfaces/index.js';
|
|
11
|
-
export * from "./interfaces/index.js";
|
|
12
|
-
/**
|
|
13
|
-
* Gerenciador de cache com suporte a `sessionStorage` e `localStorage`.
|
|
14
|
-
*/
|
|
15
|
-
export class CacheManager {
|
|
16
|
-
/**
|
|
17
|
-
* Recupera ou define o valor no cache.
|
|
18
|
-
*
|
|
19
|
-
* @template T Tipo do dado a ser cacheado.
|
|
20
|
-
* @param key Identificador único para armazenar e recuperar o valor.
|
|
21
|
-
* @param fetchCallback Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
22
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
23
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchCallback`.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const data = await CacheManager.getOrSet('uniqueKey', async () => {
|
|
28
|
-
* return await fetchDataFromAPI();
|
|
29
|
-
* }, 'sessionStorage');
|
|
30
|
-
* console.log(data);
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
static getOrSet(key, fetchCallback, storageType = StorageType.SESSION_STORAGE) {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const cache = this.getCache(storageType);
|
|
36
|
-
if (cache[key]) {
|
|
37
|
-
return cache[key].data;
|
|
38
|
-
}
|
|
39
|
-
const data = yield fetchCallback();
|
|
40
|
-
cache[key] = { data };
|
|
41
|
-
this.saveCache(cache, storageType);
|
|
42
|
-
return data;
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Remove uma entrada específica do cache.
|
|
47
|
-
*
|
|
48
|
-
* @param key Identificador único da entrada a ser removida.
|
|
49
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* CacheManager.clear('my-cache-key', 'localStorage');
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
static clear(key, storageType = StorageType.SESSION_STORAGE) {
|
|
57
|
-
const cache = this.getCache(storageType);
|
|
58
|
-
delete cache[key];
|
|
59
|
-
this.saveCache(cache, storageType);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Remove todas as entradas do cache.
|
|
63
|
-
*
|
|
64
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* CacheManager.clearAll('localStorage');
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
static clearAll(storageType = StorageType.SESSION_STORAGE) {
|
|
72
|
-
this.getStorage(storageType).removeItem(this.storageKey);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Obtém o cache armazenado no armazenamento especificado.
|
|
76
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
77
|
-
* @returns Um objeto representando o cache armazenado.
|
|
78
|
-
*/
|
|
79
|
-
static getCache(storageType) {
|
|
80
|
-
const storage = this.getStorage(storageType);
|
|
81
|
-
const cache = storage.getItem(this.storageKey);
|
|
82
|
-
return cache ? JSON.parse(cache) : {};
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Salva o cache no armazenamento especificado.
|
|
86
|
-
* @param cache O objeto representando o cache a ser salvo.
|
|
87
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
88
|
-
*/
|
|
89
|
-
static saveCache(cache, storageType) {
|
|
90
|
-
const storage = this.getStorage(storageType);
|
|
91
|
-
storage.setItem(this.storageKey, JSON.stringify(cache));
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Retorna o armazenamento correspondente ao tipo especificado.
|
|
95
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
96
|
-
* @returns O objeto de armazenamento correspondente.
|
|
97
|
-
*/
|
|
98
|
-
static getStorage(storageType) {
|
|
99
|
-
return storageType === StorageType.LOCAL_STORAGE ? localStorage : sessionStorage;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Nome da chave utilizada para armazenar o cache no armazenamento.
|
|
104
|
-
*/
|
|
105
|
-
CacheManager.storageKey = 'cacheManager';
|
|
106
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/CacheManager/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAEnD,cAAc,uBAAuB,CAAC;AAEtC;;GAEG;AACH,MAAM,OAAO,YAAY;IAMrB;;;;;;;;;;;;;;;;OAgBG;IACI,MAAM,CAAO,QAAQ,CACxB,GAAW,EACX,aAA+B,EAC/B,cAA2B,WAAW,CAAC,eAAe;;YAEtD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;YAEzC,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE;gBACZ,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;aAC1B;YAED,MAAM,IAAI,GAAG,MAAM,aAAa,EAAE,CAAC;YACnC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;YACtB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;YAEnC,OAAO,IAAI,CAAC;QAChB,CAAC;KAAA;IAED;;;;;;;;;;OAUG;IACI,MAAM,CAAC,KAAK,CAAC,GAAW,EAAE,cAA2B,WAAW,CAAC,eAAe;QACnF,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACzC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;;;OASG;IACI,MAAM,CAAC,QAAQ,CAAC,cAA2B,WAAW,CAAC,eAAe;QACzE,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7D,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,QAAQ,CAAC,WAAwB;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QAC7C,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC/C,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,SAAS,CAAC,KAA0B,EAAE,WAAwB;QACzE,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QAC7C,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,UAAU,CAAC,WAAwB;QAC9C,OAAO,WAAW,KAAK,WAAW,CAAC,aAAa,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC;IACrF,CAAC;;AAnGD;;GAEG;AACqB,uBAAU,GAAG,cAAc,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/CacheManager/interfaces/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,WAGX;AAHD,WAAY,WAAW;IACnB,iDAAkC,CAAA;IAClC,6CAA8B,CAAA;AAClC,CAAC,EAHW,WAAW,KAAX,WAAW,QAGtB"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { StorageType } from './CacheManager/interfaces/index.js';
|
|
2
|
-
export declare class ServiceUtils {
|
|
3
|
-
/**
|
|
4
|
-
* Auxilia no uso do CacheManager, gerando automaticamente uma chave de cache com base no identificador.
|
|
5
|
-
*
|
|
6
|
-
* @template T Tipo do dado a ser retornado.
|
|
7
|
-
* @param identifier Identificadores únicos usados para compor a chave de cache.
|
|
8
|
-
* @param fetchFunction Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
9
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
10
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchFunction`.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const actions = await useCacheWithService(
|
|
15
|
-
* `${this.entityName} - ${this.resourceID}`,
|
|
16
|
-
* async () => {
|
|
17
|
-
* return await fetchActionsFromAPI();
|
|
18
|
-
* }
|
|
19
|
-
* );
|
|
20
|
-
* console.log(actions);
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
static useCacheWithService<T>(identifier: string, fetchFunction: () => Promise<T>, storageType?: StorageType): Promise<T>;
|
|
24
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { CacheManager } from './CacheManager/index.js';
|
|
11
|
-
import { StorageType } from './CacheManager/interfaces/index.js';
|
|
12
|
-
export class ServiceUtils {
|
|
13
|
-
/**
|
|
14
|
-
* Auxilia no uso do CacheManager, gerando automaticamente uma chave de cache com base no identificador.
|
|
15
|
-
*
|
|
16
|
-
* @template T Tipo do dado a ser retornado.
|
|
17
|
-
* @param identifier Identificadores únicos usados para compor a chave de cache.
|
|
18
|
-
* @param fetchFunction Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
19
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
20
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchFunction`.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```typescript
|
|
24
|
-
* const actions = await useCacheWithService(
|
|
25
|
-
* `${this.entityName} - ${this.resourceID}`,
|
|
26
|
-
* async () => {
|
|
27
|
-
* return await fetchActionsFromAPI();
|
|
28
|
-
* }
|
|
29
|
-
* );
|
|
30
|
-
* console.log(actions);
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
static useCacheWithService(identifier, fetchFunction, storageType = StorageType.SESSION_STORAGE) {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const cacheKey = `${identifier}`;
|
|
36
|
-
return CacheManager.getOrSet(cacheKey, fetchFunction, storageType);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=ServiceUtils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceUtils.js","sourceRoot":"","sources":["../../src/utils/ServiceUtils.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAGjE,MAAM,OAAO,YAAY;IAGrB;;;;;;;;;;;;;;;;;;;OAmBG;IACI,MAAM,CAAO,mBAAmB,CACnC,UAAkB,EAClB,aAA+B,EAC/B,cAA2B,WAAW,CAAC,eAAe;;YAEtD,MAAM,QAAQ,GAAG,GAAG,UAAU,EAAE,CAAC;YACjC,OAAO,YAAY,CAAC,QAAQ,CAAC,QAAQ,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC;QACvE,CAAC;KAAA;CACJ"}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { StorageType } from './interfaces/index.js'
|
|
2
|
-
|
|
3
|
-
export * from "./interfaces/index.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Gerenciador de cache com suporte a `sessionStorage` e `localStorage`.
|
|
7
|
-
*/
|
|
8
|
-
export class CacheManager {
|
|
9
|
-
/**
|
|
10
|
-
* Nome da chave utilizada para armazenar o cache no armazenamento.
|
|
11
|
-
*/
|
|
12
|
-
private static readonly storageKey = 'cacheManager';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Recupera ou define o valor no cache.
|
|
16
|
-
*
|
|
17
|
-
* @template T Tipo do dado a ser cacheado.
|
|
18
|
-
* @param key Identificador único para armazenar e recuperar o valor.
|
|
19
|
-
* @param fetchCallback Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
20
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
21
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchCallback`.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* const data = await CacheManager.getOrSet('uniqueKey', async () => {
|
|
26
|
-
* return await fetchDataFromAPI();
|
|
27
|
-
* }, 'sessionStorage');
|
|
28
|
-
* console.log(data);
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
public static async getOrSet<T>(
|
|
32
|
-
key: string,
|
|
33
|
-
fetchCallback: () => Promise<T>,
|
|
34
|
-
storageType: StorageType = StorageType.SESSION_STORAGE
|
|
35
|
-
): Promise<T> {
|
|
36
|
-
const cache = this.getCache(storageType);
|
|
37
|
-
|
|
38
|
-
if (cache[key]) {
|
|
39
|
-
return cache[key].data;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const data = await fetchCallback();
|
|
43
|
-
cache[key] = { data };
|
|
44
|
-
this.saveCache(cache, storageType);
|
|
45
|
-
|
|
46
|
-
return data;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Remove uma entrada específica do cache.
|
|
51
|
-
*
|
|
52
|
-
* @param key Identificador único da entrada a ser removida.
|
|
53
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* CacheManager.clear('my-cache-key', 'localStorage');
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
public static clear(key: string, storageType: StorageType = StorageType.SESSION_STORAGE): void {
|
|
61
|
-
const cache = this.getCache(storageType);
|
|
62
|
-
delete cache[key];
|
|
63
|
-
this.saveCache(cache, storageType);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Remove todas as entradas do cache.
|
|
68
|
-
*
|
|
69
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* CacheManager.clearAll('localStorage');
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
public static clearAll(storageType: StorageType = StorageType.SESSION_STORAGE): void {
|
|
77
|
-
this.getStorage(storageType).removeItem(this.storageKey);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Obtém o cache armazenado no armazenamento especificado.
|
|
82
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
83
|
-
* @returns Um objeto representando o cache armazenado.
|
|
84
|
-
*/
|
|
85
|
-
private static getCache(storageType: StorageType ): Record<string, any> {
|
|
86
|
-
const storage = this.getStorage(storageType);
|
|
87
|
-
const cache = storage.getItem(this.storageKey);
|
|
88
|
-
return cache ? JSON.parse(cache) : {};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Salva o cache no armazenamento especificado.
|
|
93
|
-
* @param cache O objeto representando o cache a ser salvo.
|
|
94
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
95
|
-
*/
|
|
96
|
-
private static saveCache(cache: Record<string, any>, storageType: StorageType): void {
|
|
97
|
-
const storage = this.getStorage(storageType);
|
|
98
|
-
storage.setItem(this.storageKey, JSON.stringify(cache));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Retorna o armazenamento correspondente ao tipo especificado.
|
|
103
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
104
|
-
* @returns O objeto de armazenamento correspondente.
|
|
105
|
-
*/
|
|
106
|
-
private static getStorage(storageType: StorageType): Storage {
|
|
107
|
-
return storageType === StorageType.LOCAL_STORAGE ? localStorage : sessionStorage;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { CacheManager } from './CacheManager/index.js';
|
|
2
|
-
import { StorageType } from './CacheManager/interfaces/index.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export class ServiceUtils {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Auxilia no uso do CacheManager, gerando automaticamente uma chave de cache com base no identificador.
|
|
10
|
-
*
|
|
11
|
-
* @template T Tipo do dado a ser retornado.
|
|
12
|
-
* @param identifier Identificadores únicos usados para compor a chave de cache.
|
|
13
|
-
* @param fetchFunction Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
14
|
-
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`. O padrão é `'sessionStorage'`.
|
|
15
|
-
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchFunction`.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* const actions = await useCacheWithService(
|
|
20
|
-
* `${this.entityName} - ${this.resourceID}`,
|
|
21
|
-
* async () => {
|
|
22
|
-
* return await fetchActionsFromAPI();
|
|
23
|
-
* }
|
|
24
|
-
* );
|
|
25
|
-
* console.log(actions);
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
public static async useCacheWithService<T>(
|
|
29
|
-
identifier: string,
|
|
30
|
-
fetchFunction: () => Promise<T>,
|
|
31
|
-
storageType: StorageType = StorageType.SESSION_STORAGE
|
|
32
|
-
): Promise<T> {
|
|
33
|
-
const cacheKey = `${identifier}`;
|
|
34
|
-
return CacheManager.getOrSet(cacheKey, fetchFunction, storageType);
|
|
35
|
-
}
|
|
36
|
-
}
|