@sankhyalabs/core 5.20.0-dev.79 → 5.20.0-dev.80
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/src/utils/LockManager.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StringUtils } from "./StringUtils.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Define os tipos de operação que o locker pode controlar
|
|
4
|
+
*/
|
|
5
5
|
export enum LockManagerOperation {
|
|
6
6
|
/**
|
|
7
7
|
Operação de lock utilizada para controlar cliques nos botoes da taskbar.
|
|
@@ -19,7 +19,7 @@ type Lock = {
|
|
|
19
19
|
done: boolean
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class LockManager{
|
|
22
|
+
export class LockManager {
|
|
23
23
|
private static _locks = new Map<string, Array<Lock>>();
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -27,18 +27,18 @@ export class LockManager{
|
|
|
27
27
|
*/
|
|
28
28
|
public static ATTRIBUTE_NAME = "data-locker-manger-context-id";
|
|
29
29
|
|
|
30
|
-
private static buildContextID(): string{
|
|
30
|
+
private static buildContextID(): string {
|
|
31
31
|
return StringUtils.generateUUID();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
private static buildLockerID(ctxId:string|HTMLElement, operation: LockManagerOperation): string | undefined{
|
|
35
|
-
if(ctxId == undefined) return undefined;
|
|
34
|
+
private static buildLockerID(ctxId: string | HTMLElement, operation: LockManagerOperation): string | undefined {
|
|
35
|
+
if (ctxId == undefined) return undefined;
|
|
36
36
|
|
|
37
|
-
let resolvedID:any = ctxId;
|
|
37
|
+
let resolvedID: any = ctxId;
|
|
38
38
|
|
|
39
|
-
if(resolvedID instanceof HTMLElement){
|
|
39
|
+
if (resolvedID instanceof HTMLElement) {
|
|
40
40
|
resolvedID = (ctxId as HTMLElement).getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
41
|
-
if(!resolvedID) return undefined;
|
|
41
|
+
if (!resolvedID) return undefined;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
return `${resolvedID}_${operation}`;
|
|
@@ -46,23 +46,23 @@ export class LockManager{
|
|
|
46
46
|
|
|
47
47
|
private static findExistingCtxId = (element: HTMLElement): string | null => {
|
|
48
48
|
let currentElement: HTMLElement | null = element;
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
while (currentElement) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
if (currentElement.hasAttribute(LockManager.ATTRIBUTE_NAME)) {
|
|
52
|
+
return currentElement.getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const childWithCtxId = Array.from(currentElement.children).find(child =>
|
|
56
|
+
(child instanceof HTMLElement) && child.hasAttribute(LockManager.ATTRIBUTE_NAME)
|
|
57
|
+
) as HTMLElement | undefined;
|
|
58
|
+
|
|
59
|
+
if (childWithCtxId) {
|
|
60
|
+
return childWithCtxId.getAttribute(LockManager.ATTRIBUTE_NAME);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
currentElement = currentElement.parentElement;
|
|
64
64
|
}
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
68
68
|
|
|
@@ -76,37 +76,31 @@ export class LockManager{
|
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
};
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Cria um contexto de locker, caso nao exista, para todos elementos pais iniciados com ez- ou snk-.
|
|
82
|
+
*
|
|
83
|
+
* @param startElement - Elemento de de onde o lock deve começar.
|
|
84
|
+
*
|
|
85
|
+
* @returns - O id do locker, que pode ser usado para iniciar ou aguardar um lock do contexto.
|
|
86
|
+
*/
|
|
87
87
|
public static addLockManagerCtxId(startElement: HTMLElement): string {
|
|
88
|
-
try{
|
|
88
|
+
try {
|
|
89
89
|
if (!startElement) {
|
|
90
90
|
console.error("Elemento inicial não fornecido.");
|
|
91
91
|
return "";
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
const ctxId = LockManager.findExistingCtxId(startElement) ?? LockManager.buildContextID();
|
|
95
|
-
|
|
96
95
|
let currentElement: HTMLElement | null = startElement;
|
|
97
|
-
|
|
98
|
-
while (currentElement && currentElement.tagName != 'BODY') {
|
|
99
|
-
LockManager.traverseAndAddAttr(currentElement, ctxId);
|
|
100
|
-
currentElement = currentElement.parentElement;
|
|
101
|
-
}
|
|
102
|
-
|
|
96
|
+
LockManager.traverseAndAddAttr(currentElement, ctxId);
|
|
103
97
|
return ctxId;
|
|
104
|
-
}catch(err){
|
|
98
|
+
} catch (err) {
|
|
105
99
|
console.warn(`Erro ao registrar locks para o elemento: ${startElement?.tagName}`, err);
|
|
106
100
|
return "";
|
|
107
101
|
}
|
|
108
102
|
}
|
|
109
|
-
|
|
103
|
+
|
|
110
104
|
/**
|
|
111
105
|
* Reseta todos os locks existentes para um determinado contexto e operação de forma assíncrona
|
|
112
106
|
* @param id - ID do contexto ou elemento HTML contendo contexto
|
|
@@ -116,7 +110,7 @@ export class LockManager{
|
|
|
116
110
|
public static async resetLocks(id: string | HTMLElement, operation: LockManagerOperation): Promise<void> {
|
|
117
111
|
const lockerId = this.buildLockerID(id, operation);
|
|
118
112
|
if (!lockerId) return;
|
|
119
|
-
|
|
113
|
+
|
|
120
114
|
const currentLocks = this._locks.get(lockerId);
|
|
121
115
|
|
|
122
116
|
if (currentLocks?.length) {
|
|
@@ -126,11 +120,11 @@ export class LockManager{
|
|
|
126
120
|
return lock.promise;
|
|
127
121
|
}));
|
|
128
122
|
|
|
129
|
-
this._locks.delete(lockerId);
|
|
123
|
+
this._locks.delete(lockerId);
|
|
130
124
|
}
|
|
131
125
|
}
|
|
132
|
-
|
|
133
|
-
|
|
126
|
+
|
|
127
|
+
|
|
134
128
|
/**
|
|
135
129
|
* Inicia um locker baseado em um contexto e uma operação.
|
|
136
130
|
*
|
|
@@ -139,12 +133,12 @@ export class LockManager{
|
|
|
139
133
|
*
|
|
140
134
|
* @returns - Uma função que fara a liberação do lock.
|
|
141
135
|
*/
|
|
142
|
-
public static lock(id:string|HTMLElement, operation:LockManagerOperation): () => void {
|
|
136
|
+
public static lock(id: string | HTMLElement, operation: LockManagerOperation): () => void {
|
|
143
137
|
const lockerId = LockManager.buildLockerID(id, operation);
|
|
144
138
|
|
|
145
|
-
if(!lockerId) return () => {};
|
|
139
|
+
if (!lockerId) return () => { };
|
|
146
140
|
|
|
147
|
-
const lock:Lock = { done: false };
|
|
141
|
+
const lock: Lock = { done: false };
|
|
148
142
|
const promise = new Promise<void>(resolve => lock.resolve = resolve);
|
|
149
143
|
lock.promise = promise;
|
|
150
144
|
|
|
@@ -166,42 +160,42 @@ export class LockManager{
|
|
|
166
160
|
*
|
|
167
161
|
* @returns - Promise que será resolvida quando todos lockers forem finalizados.
|
|
168
162
|
*/
|
|
169
|
-
public static async whenResolve(id:string|HTMLElement, operation:LockManagerOperation, debounce?: number, timeOut?: number): Promise<void> {
|
|
163
|
+
public static async whenResolve(id: string | HTMLElement, operation: LockManagerOperation, debounce?: number, timeOut?: number): Promise<void> {
|
|
170
164
|
const lockerId = LockManager.buildLockerID(id, operation);
|
|
171
165
|
|
|
172
|
-
if(!lockerId) return;
|
|
166
|
+
if (!lockerId) return;
|
|
167
|
+
|
|
168
|
+
if (debounce) await new Promise(resolve => setTimeout(resolve, debounce));
|
|
173
169
|
|
|
174
|
-
if(debounce) await new Promise(resolve => setTimeout(resolve, debounce));
|
|
175
|
-
|
|
176
170
|
const startTime = Date.now();
|
|
177
|
-
|
|
171
|
+
|
|
178
172
|
while (LockManager._locks.get(lockerId)?.length) {
|
|
179
|
-
|
|
173
|
+
|
|
180
174
|
if (timeOut && Date.now() - startTime >= timeOut) {
|
|
181
175
|
await this.resetLocks(id, operation);
|
|
182
176
|
return;
|
|
183
177
|
}
|
|
184
178
|
|
|
185
|
-
const locks:Array<Lock> = LockManager._locks.get(lockerId) ?? [];
|
|
179
|
+
const locks: Array<Lock> = LockManager._locks.get(lockerId) ?? [];
|
|
186
180
|
await Promise.all(locks.map(lock => lock.promise));
|
|
187
|
-
|
|
181
|
+
|
|
188
182
|
//Aguarda listeners da tela reagirem as mudancas de estado do dataunit
|
|
189
183
|
await new Promise(resolve => setTimeout(resolve, debounce || 200));
|
|
190
|
-
|
|
184
|
+
|
|
191
185
|
LockManager._locks.set(lockerId, locks.filter(lock => !lock.done));
|
|
192
|
-
}
|
|
193
|
-
|
|
186
|
+
}
|
|
187
|
+
|
|
194
188
|
}
|
|
195
|
-
|
|
196
|
-
public static async whenHasLock(id:string|HTMLElement, operation:LockManagerOperation, timeOut?: number): Promise<void> {
|
|
189
|
+
|
|
190
|
+
public static async whenHasLock(id: string | HTMLElement, operation: LockManagerOperation, timeOut?: number): Promise<void> {
|
|
197
191
|
const lockerId = LockManager.buildLockerID(id, operation);
|
|
198
192
|
|
|
199
|
-
if(!lockerId) return;
|
|
193
|
+
if (!lockerId) return;
|
|
200
194
|
|
|
201
195
|
const startTime = Date.now();
|
|
202
196
|
|
|
203
197
|
while (!LockManager._locks.get(lockerId)?.length) {
|
|
204
|
-
|
|
198
|
+
|
|
205
199
|
if (timeOut && Date.now() - startTime >= timeOut) {
|
|
206
200
|
await this.resetLocks(id, operation);
|
|
207
201
|
return;
|