@sankhyalabs/core 5.20.0-dev.54 → 5.20.0-dev.56
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 +135 -135
- package/.docs/classes/LockManager.md +191 -0
- package/.docs/classes/SelectionInfo.md +12 -12
- package/.docs/classes/ServiceUtils.md +67 -0
- package/.docs/classes/SilentException.md +193 -0
- package/.docs/enumerations/ChangeOperation.md +4 -4
- package/.docs/enumerations/LockManagerOperation.md +21 -0
- package/.docs/enumerations/SelectionMode.md +2 -2
- package/.docs/enumerations/StorageType.md +37 -0
- package/.docs/globals.md +5 -0
- package/.docs/interfaces/DUActionInterceptor.md +1 -1
- package/.docs/interfaces/PageRequest.md +3 -3
- 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/dist/dataunit/DataUnit.js +4 -2
- package/dist/dataunit/DataUnit.js.map +1 -1
- package/dist/exceptions/SilentException.d.ts +14 -0
- package/dist/exceptions/SilentException.js +13 -0
- package/dist/exceptions/SilentException.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/CacheManager/index.d.ts +52 -0
- package/dist/utils/CacheManager/index.js +101 -0
- package/dist/utils/CacheManager/index.js.map +1 -0
- package/dist/utils/CacheManager/interfaces/index.d.ts +5 -0
- package/dist/utils/CacheManager/interfaces/index.js +7 -0
- package/dist/utils/CacheManager/interfaces/index.js.map +1 -0
- package/dist/utils/LockManager.d.ts +46 -0
- package/dist/utils/LockManager.js +141 -0
- package/dist/utils/LockManager.js.map +1 -0
- package/dist/utils/ServiceUtils.d.ts +24 -0
- package/dist/utils/ServiceUtils.js +40 -0
- package/dist/utils/ServiceUtils.js.map +1 -0
- package/package.json +1 -1
- package/reports/test-report.xml +89 -89
- package/src/dataunit/DataUnit.ts +7 -4
- package/src/exceptions/SilentException.ts +25 -0
- package/src/index.ts +10 -1
- package/src/utils/CacheManager/index.ts +103 -0
- package/src/utils/CacheManager/interfaces/index.ts +5 -0
- package/src/utils/LockManager.ts +157 -0
- package/src/utils/ServiceUtils.ts +36 -0
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ import WarningException from "./exceptions/WarningException.js";
|
|
|
20
20
|
import WaitingChangeException from "./exceptions/WaitingChangeException.js";
|
|
21
21
|
import ErrorException from "./exceptions/ErrorException.js";
|
|
22
22
|
import ServiceCanceledException from "./exceptions/ServiceCanceledException.js";
|
|
23
|
+
import SilentException from "./exceptions/SilentException.js";
|
|
23
24
|
import { ErrorTracking } from "./traking/ErrorTraking.js";
|
|
24
25
|
import { PaginationInfo } from "./dataunit/loading/PaginationInfo.js";
|
|
25
26
|
import { LoadDataRequest } from "./dataunit/loading/LoadDataRequest.js";
|
|
@@ -40,10 +41,15 @@ import { IRepositoryIndex } from "./repository/indexeddb/IRepositoryIndex.js"
|
|
|
40
41
|
import { FieldComparator } from "./dataunit/sorting/FieldComparator.js";
|
|
41
42
|
import { KeyboardManager } from "./utils/KeyboardManager/index.js";
|
|
42
43
|
import { SearchUtils } from "./utils/SearchUtils.js";
|
|
44
|
+
import { ServiceUtils } from "./utils/ServiceUtils.js";
|
|
45
|
+
import { StorageType } from "./utils/CacheManager/index.js";
|
|
43
46
|
import OverflowWatcher, { OnOverflowCallBack, OverflowDirection, OverFlowWatcherParams, OVERFLOWED_CLASS_NAME } from "./utils/OverflowWatcher/index.js";
|
|
47
|
+
import {LockManager, LockManagerOperation} from "./utils/LockManager.js";
|
|
44
48
|
|
|
45
49
|
/*Classes públicas no pacote*/
|
|
46
50
|
export {
|
|
51
|
+
LockManager,
|
|
52
|
+
LockManagerOperation,
|
|
47
53
|
StringUtils,
|
|
48
54
|
MaskFormatter,
|
|
49
55
|
NumberUtils,
|
|
@@ -108,11 +114,14 @@ export {
|
|
|
108
114
|
defaultDataLoader,
|
|
109
115
|
KeyboardManager,
|
|
110
116
|
SearchUtils,
|
|
117
|
+
ServiceUtils,
|
|
118
|
+
StorageType,
|
|
111
119
|
OverflowWatcher,
|
|
112
120
|
OnOverflowCallBack,
|
|
113
121
|
OverflowDirection,
|
|
114
122
|
OverFlowWatcherParams,
|
|
115
123
|
OVERFLOWED_CLASS_NAME,
|
|
116
124
|
DataUnitEventOptions,
|
|
117
|
-
ServiceCanceledException
|
|
125
|
+
ServiceCanceledException,
|
|
126
|
+
SilentException,
|
|
118
127
|
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { StorageType } from './interfaces/index.js';
|
|
2
|
+
|
|
3
|
+
export * from "./interfaces/index.js";
|
|
4
|
+
|
|
5
|
+
export class CacheManager {
|
|
6
|
+
/**
|
|
7
|
+
* Nome da chave utilizada para armazenar o cache no armazenamento.
|
|
8
|
+
*/
|
|
9
|
+
private static readonly storageKey = 'cacheManager';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Estrutura de armazenamento em memória.
|
|
13
|
+
*/
|
|
14
|
+
private static inMemoryCache = new Map<string, any>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Recupera ou define o valor no cache.
|
|
18
|
+
*
|
|
19
|
+
* @param key Identificador único para armazenar e recuperar o valor.
|
|
20
|
+
* @param fetchCallback Função que retorna uma `Promise` com o valor a ser armazenado no cache caso ele não exista ou tenha expirado.
|
|
21
|
+
* @param storageType Tipo de armazenamento: `'inMemoryCache'`, `'sessionStorage'` ou `'localStorage'`.
|
|
22
|
+
* @returns Uma `Promise` com o valor armazenado ou obtido via `fetchCallback`.
|
|
23
|
+
*/
|
|
24
|
+
public static async getOrSet<T>(
|
|
25
|
+
key: string,
|
|
26
|
+
fetchCallback: () => Promise<T>,
|
|
27
|
+
storageType: StorageType = StorageType.IN_MEMORY_CACHE
|
|
28
|
+
): Promise<T> {
|
|
29
|
+
const cache = this.getCache(storageType);
|
|
30
|
+
|
|
31
|
+
if (cache[key]) {
|
|
32
|
+
return cache[key].data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const data = await fetchCallback();
|
|
36
|
+
cache[key] = { data };
|
|
37
|
+
this.saveCache(cache, storageType);
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Remove uma entrada específica do cache.
|
|
43
|
+
*
|
|
44
|
+
* @param key Identificador único da entrada a ser removida.
|
|
45
|
+
* @param storageType Tipo de armazenamento: `'inMemoryCache'`, `'sessionStorage'` ou `'localStorage'`.
|
|
46
|
+
*/
|
|
47
|
+
public static clear(key: string, storageType: StorageType = StorageType.IN_MEMORY_CACHE): void {
|
|
48
|
+
const cache = this.getCache(storageType);
|
|
49
|
+
delete cache[key];
|
|
50
|
+
this.saveCache(cache, storageType);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Remove todas as entradas do cache.
|
|
55
|
+
*
|
|
56
|
+
* @param storageType Tipo de armazenamento: `'inMemoryCache'`, `'sessionStorage'` ou `'localStorage'`.
|
|
57
|
+
*/
|
|
58
|
+
public static clearAll(storageType: StorageType = StorageType.IN_MEMORY_CACHE): void {
|
|
59
|
+
if (storageType === StorageType.IN_MEMORY_CACHE) {
|
|
60
|
+
this.inMemoryCache.clear();
|
|
61
|
+
} else {
|
|
62
|
+
this.getStorage(storageType).removeItem(this.storageKey);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Obtém o cache armazenado no armazenamento especificado.
|
|
68
|
+
* @param storageType Tipo de armazenamento: `'inMemoryCache'`, `'sessionStorage'` ou `'localStorage'`.
|
|
69
|
+
* @returns Um objeto representando o cache armazenado.
|
|
70
|
+
*/
|
|
71
|
+
private static getCache(storageType: StorageType): Record<string, any> {
|
|
72
|
+
if (storageType === StorageType.IN_MEMORY_CACHE) {
|
|
73
|
+
return Object.fromEntries(this.inMemoryCache);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const storage = this.getStorage(storageType);
|
|
77
|
+
const cache = storage.getItem(this.storageKey);
|
|
78
|
+
return cache ? JSON.parse(cache) : {};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Salva o cache no armazenamento especificado.
|
|
83
|
+
* @param cache O objeto representando o cache a ser salvo.
|
|
84
|
+
* @param storageType Tipo de armazenamento: `'inMemoryCache'`, `'sessionStorage'` ou `'localStorage'`.
|
|
85
|
+
*/
|
|
86
|
+
private static saveCache(cache: Record<string, any>, storageType: StorageType): void {
|
|
87
|
+
if (storageType === StorageType.IN_MEMORY_CACHE) {
|
|
88
|
+
this.inMemoryCache = new Map(Object.entries(cache));
|
|
89
|
+
} else {
|
|
90
|
+
const storage = this.getStorage(storageType);
|
|
91
|
+
storage.setItem(this.storageKey, JSON.stringify(cache));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Retorna o armazenamento correspondente ao tipo especificado.
|
|
97
|
+
* @param storageType Tipo de armazenamento: `'sessionStorage'` ou `'localStorage'`.
|
|
98
|
+
* @returns O objeto de armazenamento correspondente.
|
|
99
|
+
*/
|
|
100
|
+
private static getStorage(storageType: StorageType): Storage {
|
|
101
|
+
return storageType === StorageType.LOCAL_STORAGE ? localStorage : sessionStorage;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { StringUtils } from "./StringUtils.js";
|
|
2
|
+
/**
|
|
3
|
+
* Define os tipos de operação que o locker pode controlar
|
|
4
|
+
*/
|
|
5
|
+
export enum LockManagerOperation {
|
|
6
|
+
/**
|
|
7
|
+
Operação de lock utilizada para controlar cliques nos botoes da taskbar.
|
|
8
|
+
*/
|
|
9
|
+
TASKBAR_CLICK = "taskbar_click"
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type Lock = {
|
|
13
|
+
promise?: Promise<unknown>,
|
|
14
|
+
resolve?: () => void,
|
|
15
|
+
done: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class LockManager{
|
|
19
|
+
private static _locks = new Map<string, Array<Lock>>();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Nome do atributo que será utilizado para controlar contexto de locks nos elementos da DOM.
|
|
23
|
+
*/
|
|
24
|
+
public static ATTRIBUTE_NAME = "data-locker-manger-context-id";
|
|
25
|
+
|
|
26
|
+
private static buildContextID(): string{
|
|
27
|
+
return StringUtils.generateUUID();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private static buildLockerID(ctxId:string|HTMLElement, operation: LockManagerOperation): string | undefined{
|
|
31
|
+
if(ctxId == undefined) return undefined;
|
|
32
|
+
|
|
33
|
+
let resolvedID:any = ctxId;
|
|
34
|
+
|
|
35
|
+
if(resolvedID instanceof HTMLElement){
|
|
36
|
+
resolvedID = (ctxId as HTMLElement).getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
37
|
+
if(!resolvedID) return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return `${resolvedID}_${operation}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private static findExistingCtxId = (element: HTMLElement): string | null => {
|
|
44
|
+
let currentElement: HTMLElement | null = element;
|
|
45
|
+
|
|
46
|
+
while (currentElement) {
|
|
47
|
+
if (currentElement.hasAttribute(LockManager.ATTRIBUTE_NAME)) {
|
|
48
|
+
return currentElement.getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const childWithCtxId = Array.from(currentElement.children).find(child =>
|
|
52
|
+
(child instanceof HTMLElement) && child.hasAttribute(LockManager.ATTRIBUTE_NAME)
|
|
53
|
+
) as HTMLElement | undefined;
|
|
54
|
+
|
|
55
|
+
if (childWithCtxId) {
|
|
56
|
+
return childWithCtxId.getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
currentElement = currentElement.parentElement;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private static traverseAndAddAttr = (element: HTMLElement, ctxId: string): void => {
|
|
66
|
+
if (element.tagName.startsWith('EZ-') || element.tagName.startsWith('SNK-')) {
|
|
67
|
+
element.setAttribute(LockManager.ATTRIBUTE_NAME, ctxId);
|
|
68
|
+
}
|
|
69
|
+
Array.from(element.children).forEach((child) => {
|
|
70
|
+
if (child instanceof HTMLElement) {
|
|
71
|
+
LockManager.traverseAndAddAttr(child, ctxId);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Cria um contexto de locker, caso nao exista, para todos elementos pais iniciados com ez- ou snk-.
|
|
78
|
+
*
|
|
79
|
+
* @param startElement - Elemento de de onde o lock deve começar.
|
|
80
|
+
*
|
|
81
|
+
* @returns - O id do locker, que pode ser usado para iniciar ou aguardar um lock do contexto.
|
|
82
|
+
*/
|
|
83
|
+
public static addLockManagerCtxId(startElement: HTMLElement): string {
|
|
84
|
+
try{
|
|
85
|
+
if (!startElement) {
|
|
86
|
+
console.error("Elemento inicial não fornecido.");
|
|
87
|
+
return "";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const ctxId = LockManager.findExistingCtxId(startElement) ?? LockManager.buildContextID();
|
|
91
|
+
|
|
92
|
+
let currentElement: HTMLElement | null = startElement;
|
|
93
|
+
|
|
94
|
+
while (currentElement && currentElement.tagName != 'BODY') {
|
|
95
|
+
LockManager.traverseAndAddAttr(currentElement, ctxId);
|
|
96
|
+
currentElement = currentElement.parentElement;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return ctxId;
|
|
100
|
+
}catch(err){
|
|
101
|
+
console.warn(`Erro ao registrar locks para o elemento: ${startElement?.tagName}`, err);
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Inicia um locker baseado em um contexto e uma operação.
|
|
108
|
+
*
|
|
109
|
+
* @param id - Pode ser um ID do contexto de locker, ou, o elemento contendo um contexto de locker.
|
|
110
|
+
* @param operation - Operação do contexto que o lock deve ser feito.
|
|
111
|
+
*
|
|
112
|
+
* @returns - Uma função que fara a liberação do lock.
|
|
113
|
+
*/
|
|
114
|
+
public static lock(id:string|HTMLElement, operation:LockManagerOperation): any{
|
|
115
|
+
const lockerId = LockManager.buildLockerID(id, operation);
|
|
116
|
+
|
|
117
|
+
if(!lockerId) return () => {};
|
|
118
|
+
|
|
119
|
+
const lock:Lock = { done: false };
|
|
120
|
+
const promise = new Promise<void>(resolve => lock.resolve = resolve);
|
|
121
|
+
lock.promise = promise;
|
|
122
|
+
|
|
123
|
+
const currentLocks = LockManager._locks.get(lockerId) ?? [];
|
|
124
|
+
currentLocks.push(lock);
|
|
125
|
+
|
|
126
|
+
LockManager._locks.set(lockerId, currentLocks);
|
|
127
|
+
|
|
128
|
+
return () => {
|
|
129
|
+
lock.done = true;
|
|
130
|
+
lock.resolve?.();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Aguarda todos os lockers de um contexto e operação serem resolvidos.
|
|
136
|
+
*
|
|
137
|
+
* @param id - Pode ser um ID do contexto de locker, ou, o elemento contendo um contexto de locker.
|
|
138
|
+
* @param operation - Operação do contexto que devera aguardar.
|
|
139
|
+
*
|
|
140
|
+
* @returns - Promise que será resolvida quando todos lockers forem finalizados.
|
|
141
|
+
*/
|
|
142
|
+
public static async whenResolve(id:string|HTMLElement, operation:LockManagerOperation): Promise<void> {
|
|
143
|
+
const lockerId = LockManager.buildLockerID(id, operation);
|
|
144
|
+
|
|
145
|
+
if(!lockerId) return;
|
|
146
|
+
|
|
147
|
+
while (LockManager._locks.get(lockerId)?.length) {
|
|
148
|
+
const locks:Array<Lock> = LockManager._locks.get(lockerId) ?? [];
|
|
149
|
+
await Promise.all(locks.map(lock => lock.promise));
|
|
150
|
+
|
|
151
|
+
//Aguarda listeners da tela reagirem as mudancas de estado do dataunit
|
|
152
|
+
await new Promise(resolve => setTimeout(resolve, 200));
|
|
153
|
+
|
|
154
|
+
LockManager._locks.set(lockerId, locks.filter(lock => !lock.done));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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.IN_MEMORY_CACHE
|
|
32
|
+
): Promise<T> {
|
|
33
|
+
const cacheKey = `${identifier}`;
|
|
34
|
+
return CacheManager.getOrSet(cacheKey, fetchFunction, storageType);
|
|
35
|
+
}
|
|
36
|
+
}
|