@okyrychenko-dev/react-action-guard 0.6.0 → 0.6.1
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/dist/index.cjs +161 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1200 -60
- package/dist/index.d.ts +1200 -60
- package/dist/index.js +161 -27
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -63,11 +63,38 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
63
63
|
},
|
|
64
64
|
// Actions
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
66
|
+
* Adds a new blocker or overwrites an existing one.
|
|
67
|
+
*
|
|
68
|
+
* If a blocker with the same ID already exists, it will be replaced and any
|
|
69
|
+
* existing timeout will be cleared. The blocker is active immediately after
|
|
70
|
+
* being added. Triggers middleware with 'add' action.
|
|
71
|
+
*
|
|
72
|
+
* @param id - Unique identifier for the blocker. Must be unique within the store.
|
|
73
|
+
* @param config - Blocker configuration. All fields are optional with defaults applied.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Basic usage
|
|
77
|
+
* ```ts
|
|
78
|
+
* store.getState().addBlocker('save-operation', {
|
|
79
|
+
* scope: 'form',
|
|
80
|
+
* reason: 'Saving data...',
|
|
81
|
+
* priority: 70
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* With timeout
|
|
87
|
+
* ```ts
|
|
88
|
+
* store.getState().addBlocker('api-call', {
|
|
89
|
+
* scope: 'global',
|
|
90
|
+
* reason: 'Loading...',
|
|
91
|
+
* timeout: 30000, // Auto-remove after 30 seconds
|
|
92
|
+
* onTimeout: (id) => console.warn(`${id} timed out`)
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @see {@link removeBlocker} to remove a blocker
|
|
97
|
+
* @see {@link updateBlocker} to update an existing blocker
|
|
71
98
|
*/
|
|
72
99
|
addBlocker: (id, config = {}) => {
|
|
73
100
|
const existingBlocker = get().activeBlockers.get(id);
|
|
@@ -115,11 +142,31 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
115
142
|
);
|
|
116
143
|
},
|
|
117
144
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
145
|
+
* Updates an existing blocker or creates it if it doesn't exist.
|
|
146
|
+
*
|
|
147
|
+
* Merges the provided configuration with existing blocker data. If the timeout
|
|
148
|
+
* is changed, the timeout timer is restarted. Triggers middleware with 'update' action.
|
|
149
|
+
*
|
|
150
|
+
* @param id - Identifier of the blocker to update
|
|
151
|
+
* @param config - Partial configuration to merge with existing blocker data
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* Update blocker reason
|
|
155
|
+
* ```ts
|
|
156
|
+
* store.getState().updateBlocker('save', {
|
|
157
|
+
* reason: 'Saving additional data...'
|
|
158
|
+
* });
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* Extend timeout
|
|
163
|
+
* ```ts
|
|
164
|
+
* store.getState().updateBlocker('api-call', {
|
|
165
|
+
* timeout: 60000 // Extend to 60 seconds
|
|
166
|
+
* });
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @see {@link addBlocker} to create a new blocker
|
|
123
170
|
*/
|
|
124
171
|
updateBlocker: (id, config = {}) => {
|
|
125
172
|
const existingBlocker = get().activeBlockers.get(id);
|
|
@@ -193,10 +240,23 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
193
240
|
);
|
|
194
241
|
},
|
|
195
242
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
243
|
+
* Removes a blocker from the store.
|
|
244
|
+
*
|
|
245
|
+
* Clears any associated timeout and triggers middleware with 'remove' action.
|
|
246
|
+
* Safe to call even if the blocker doesn't exist (no-op in that case).
|
|
247
|
+
*
|
|
248
|
+
* @param id - Identifier of the blocker to remove
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* // Add and remove a blocker
|
|
253
|
+
* store.getState().addBlocker('temp', { scope: 'form' });
|
|
254
|
+
* // ... later
|
|
255
|
+
* store.getState().removeBlocker('temp');
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @see {@link addBlocker} to add a blocker
|
|
259
|
+
* @see {@link clearAllBlockers} to remove all blockers at once
|
|
200
260
|
*/
|
|
201
261
|
removeBlocker: (id) => {
|
|
202
262
|
const prevBlocker = get().activeBlockers.get(id);
|
|
@@ -213,11 +273,30 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
213
273
|
}
|
|
214
274
|
},
|
|
215
275
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
276
|
+
* Checks if one or more scopes are currently blocked.
|
|
277
|
+
*
|
|
278
|
+
* Returns true if ANY blocker affects the specified scope(s). Global scope
|
|
279
|
+
* blockers will cause this to return true for any scope check.
|
|
280
|
+
*
|
|
281
|
+
* @param scope - Scope(s) to check. Defaults to "global" if not specified.
|
|
282
|
+
* Can be a single scope string or array of scopes.
|
|
283
|
+
* @returns true if any of the specified scopes are currently blocked
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* Single scope check
|
|
287
|
+
* ```ts
|
|
288
|
+
* const isFormBlocked = store.getState().isBlocked('form');
|
|
289
|
+
* console.log(isFormBlocked); // true or false
|
|
290
|
+
* ```
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* Multiple scopes check
|
|
294
|
+
* ```ts
|
|
295
|
+
* const isBlocked = store.getState().isBlocked(['form', 'navigation']);
|
|
296
|
+
* // Returns true if either 'form' OR 'navigation' has active blockers
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* @see {@link getBlockingInfo} for detailed blocker information
|
|
221
300
|
*/
|
|
222
301
|
isBlocked: (scope = DEFAULT_SCOPE) => {
|
|
223
302
|
const { activeBlockers } = get();
|
|
@@ -234,11 +313,37 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
234
313
|
return false;
|
|
235
314
|
},
|
|
236
315
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
316
|
+
* Gets detailed information about all blockers affecting a specific scope.
|
|
317
|
+
*
|
|
318
|
+
* Returns an array of blocker objects sorted by priority (highest first).
|
|
319
|
+
* Includes global blockers and scope-specific blockers. Each blocker object
|
|
320
|
+
* contains id, reason, priority, scope, timestamp, and timeout information.
|
|
321
|
+
*
|
|
239
322
|
* @param scope - Scope to get blocking information for
|
|
240
|
-
* @returns Array of
|
|
241
|
-
*
|
|
323
|
+
* @returns Array of {@link BlockerInfo} objects, sorted by priority descending
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* Display blocker details
|
|
327
|
+
* ```ts
|
|
328
|
+
* const blockers = store.getState().getBlockingInfo('form');
|
|
329
|
+
* blockers.forEach(blocker => {
|
|
330
|
+
* console.log(`${blocker.id}: ${blocker.reason}`);
|
|
331
|
+
* console.log(`Priority: ${blocker.priority}`);
|
|
332
|
+
* });
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* Get highest priority blocker
|
|
337
|
+
* ```ts
|
|
338
|
+
* const blockers = store.getState().getBlockingInfo('navigation');
|
|
339
|
+
* const topBlocker = blockers[0]; // Highest priority
|
|
340
|
+
* if (topBlocker) {
|
|
341
|
+
* console.log(`Top blocker: ${topBlocker.reason}`);
|
|
342
|
+
* }
|
|
343
|
+
* ```
|
|
344
|
+
*
|
|
345
|
+
* @see {@link isBlocked} for a simple boolean check
|
|
346
|
+
* @see {@link BlockerInfo} for the structure of returned objects
|
|
242
347
|
*/
|
|
243
348
|
getBlockingInfo: (scope) => {
|
|
244
349
|
const { activeBlockers } = get();
|
|
@@ -252,7 +357,18 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
252
357
|
return blockers.sort((a, b) => b.priority - a.priority);
|
|
253
358
|
},
|
|
254
359
|
/**
|
|
255
|
-
*
|
|
360
|
+
* Removes all active blockers from the store.
|
|
361
|
+
*
|
|
362
|
+
* Clears all timeouts and triggers middleware with 'clear' action.
|
|
363
|
+
* Useful for resetting the blocking state.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* // Clear all blockers on logout
|
|
368
|
+
* store.getState().clearAllBlockers();
|
|
369
|
+
* ```
|
|
370
|
+
*
|
|
371
|
+
* @see {@link clearBlockersForScope} to clear blockers for a specific scope
|
|
256
372
|
*/
|
|
257
373
|
clearAllBlockers: () => {
|
|
258
374
|
const { activeBlockers } = get();
|
|
@@ -273,10 +389,28 @@ var createUIBlockingActions = (set, get) => ({
|
|
|
273
389
|
}
|
|
274
390
|
},
|
|
275
391
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
392
|
+
* Removes all blockers that affect a specific scope.
|
|
393
|
+
*
|
|
394
|
+
* Clears timeouts for removed blockers and triggers middleware with
|
|
395
|
+
* 'clear_scope' action. Global scope blockers are NOT cleared by this method.
|
|
396
|
+
*
|
|
278
397
|
* @param scope - Scope to clear blockers for
|
|
279
|
-
*
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* Clear form blockers
|
|
401
|
+
* ```ts
|
|
402
|
+
* // Clear all blockers affecting 'form' scope
|
|
403
|
+
* store.getState().clearBlockersForScope('form');
|
|
404
|
+
* ```
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* Clear on navigation
|
|
408
|
+
* ```ts
|
|
409
|
+
* // Clear navigation blockers when user navigates away
|
|
410
|
+
* store.getState().clearBlockersForScope('navigation');
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @see {@link clearAllBlockers} to clear all blockers
|
|
280
414
|
*/
|
|
281
415
|
clearBlockersForScope: (scope) => {
|
|
282
416
|
const { activeBlockers } = get();
|