@okyrychenko-dev/react-action-guard 1.0.2 → 1.0.4

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.js CHANGED
@@ -2,7 +2,7 @@ import { createStoreProvider, createShallowStore } from '@okyrychenko-dev/react-
2
2
  import { useStore } from 'zustand';
3
3
  import 'zustand/react/shallow';
4
4
  import { jsx } from 'react/jsx-runtime';
5
- import { useShallow } from 'zustand/shallow';
5
+ import { useShallow, shallow } from 'zustand/shallow';
6
6
  import { devtools } from 'zustand/middleware';
7
7
  import { useRef, useCallback, useEffect, useMemo, useState } from 'react';
8
8
 
@@ -528,6 +528,40 @@ function useAsyncAction(actionId, scope, options) {
528
528
  );
529
529
  return executeWithBlocking;
530
530
  }
531
+ function createBlockerConfig(config) {
532
+ return {
533
+ scope: config.scope,
534
+ reason: config.reason,
535
+ priority: config.priority
536
+ };
537
+ }
538
+ function normalizeScopeSnapshot(scope) {
539
+ if (scope === void 0) {
540
+ return [DEFAULT_SCOPE];
541
+ }
542
+ if (typeof scope === "string") {
543
+ return [scope];
544
+ }
545
+ return scope;
546
+ }
547
+ function getScopeSnapshotKey(scope) {
548
+ return JSON.stringify(normalizeScopeSnapshot(scope));
549
+ }
550
+ function toBlockerConfigSnapshot(config) {
551
+ return [
552
+ getScopeSnapshotKey(config.scope),
553
+ config.reason,
554
+ config.priority,
555
+ config.timestamp,
556
+ config.timeout,
557
+ config.onTimeout
558
+ ];
559
+ }
560
+ function areBlockerConfigsEqual(first, second) {
561
+ return shallow(toBlockerConfigSnapshot(first), toBlockerConfigSnapshot(second));
562
+ }
563
+
564
+ // src/hooks/useBlocker/useBlocker.ts
531
565
  function useBlocker(blockerId, config, isActive = true) {
532
566
  const { addBlocker, removeBlocker, updateBlocker } = useResolvedValue((state) => ({
533
567
  addBlocker: state.addBlocker,
@@ -550,22 +584,13 @@ function useBlocker(blockerId, config, isActive = true) {
550
584
  if (!isActive || !blockerId) {
551
585
  return;
552
586
  }
553
- if (lastConfigRef.current === config) {
587
+ if (lastConfigRef.current !== null && areBlockerConfigsEqual(lastConfigRef.current, config)) {
554
588
  return;
555
589
  }
556
590
  updateBlocker(blockerId, config);
557
591
  lastConfigRef.current = config;
558
592
  }, [blockerId, config, isActive, updateBlocker]);
559
593
  }
560
-
561
- // src/hooks/useBlocker/useBlocker.utils.ts
562
- function createBlockerConfig(config) {
563
- return {
564
- scope: config.scope,
565
- reason: config.reason,
566
- priority: config.priority
567
- };
568
- }
569
594
  function useBlockingInfo(scope = DEFAULT_SCOPE) {
570
595
  const store = useResolvedStoreApi();
571
596
  const activeBlockers = useStore(store, (state) => state.activeBlockers);