@dynamic-labs/react-hooks 2.1.0-alpha.1 → 2.1.0-alpha.3

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.
Files changed (2) hide show
  1. package/index.js +15 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useRef, useState, useCallback, useEffect, useMemo } from 'react';
1
+ import { useRef, useState, useCallback, useMemo } from 'react';
2
2
  import { effect, stop } from '@vue/reactivity';
3
3
 
4
4
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -1545,7 +1545,7 @@ for (var COLLECTION_NAME in DOMIterables) {
1545
1545
 
1546
1546
  handlePrototype(DOMTokenListPrototype, 'DOMTokenList');
1547
1547
 
1548
- const isObject = value => typeof value === 'object' && value !== null && value !== undefined;
1548
+ const isObject = value => typeof value === 'object' && value !== null && value !== undefined && typeof value !== 'function' && typeof value !== 'symbol' && typeof value !== 'bigint' && !Array.isArray(value) && !(value instanceof Date);
1549
1549
  /**
1550
1550
  * Watches for changes in the source and calls the callback when the source changes
1551
1551
  * @param source - The source to watch for changes
@@ -1553,9 +1553,12 @@ const isObject = value => typeof value === 'object' && value !== null && value !
1553
1553
  * @returns The unsubscribe function
1554
1554
  */
1555
1555
  const watch = (source, callback) => {
1556
+ let isWatching = false;
1556
1557
  const runner = effect(source, {
1558
+ onTrack: () => isWatching = true,
1557
1559
  scheduler: callback
1558
1560
  });
1561
+ if (!isWatching) return undefined;
1559
1562
  return () => {
1560
1563
  stop(runner);
1561
1564
  };
@@ -1574,20 +1577,19 @@ const useReactivityProxy = target => {
1574
1577
  const proxyObjectCacheRef = useRef(new Map());
1575
1578
  // Rerender count to force rerender when the target changes
1576
1579
  const [, setRerenderCount] = useState(0);
1577
- const rerender = useCallback(() => setRerenderCount(rerenderCount => rerenderCount + 1), [setRerenderCount]);
1578
1580
  const unsubscribeAll = useCallback(() => {
1579
1581
  unsubscribeFunctionsRef.current.forEach(unsubscribeFunction => unsubscribeFunction());
1580
1582
  unsubscribeFunctionsRef.current.clear();
1581
1583
  }, []);
1584
+ const rerender = useCallback(() => {
1585
+ unsubscribeAll();
1586
+ setRerenderCount(rerenderCount => rerenderCount + 1);
1587
+ }, [setRerenderCount, unsubscribeAll]);
1582
1588
  /**
1583
1589
  * On every render it cleans the previous subscriptions.
1584
1590
  * This is necessary because different renders can have different subscriptions.
1585
1591
  */
1586
1592
  unsubscribeAll();
1587
- /**
1588
- * Unsubscribe all subscriptions when the component is unmounted
1589
- */
1590
- useEffect(() => () => unsubscribeAll(), [unsubscribeAll]);
1591
1593
  const addSubscriptionProxy = (target, proxyCacheKey) => {
1592
1594
  // Check if object path is already cached
1593
1595
  if (proxyObjectCacheRef.current.has(proxyCacheKey)) {
@@ -1599,9 +1601,13 @@ const useReactivityProxy = target => {
1599
1601
  const value = Reflect.get(target, prop, receiver);
1600
1602
  // Subscribe to the accessed attribute
1601
1603
  const unsubscribeFunction = watch(() => Reflect.get(target, prop, receiver), rerender);
1602
- unsubscribeFunctionsRef.current.add(unsubscribeFunction);
1604
+ if (unsubscribeFunction) {
1605
+ unsubscribeFunctionsRef.current.add(unsubscribeFunction);
1606
+ }
1607
+ const isReactiveValue = Boolean(unsubscribeFunction);
1603
1608
  // If the value is an object, we need to create a proxy for it too
1604
- if (isObject(value)) {
1609
+ // but not when the value is reactive, if it is reactive we wanna stop creating proxies
1610
+ if (isObject(value) && !isReactiveValue) {
1605
1611
  const propCachePath = `${proxyCacheKey}.${String(prop)}`;
1606
1612
  return addSubscriptionProxy(value, propCachePath);
1607
1613
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/react-hooks",
3
- "version": "2.1.0-alpha.1",
3
+ "version": "2.1.0-alpha.3",
4
4
  "main": "./index.js",
5
5
  "module": "./index.js",
6
6
  "types": "./src/index.d.ts",