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