@dynamic-labs/react-hooks 2.1.0-alpha.0 → 2.1.0-alpha.10

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/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import { jsx } from 'react/jsx-runtime';
2
- import { useRef, useState, useCallback, useEffect, useMemo, useContext, createContext } from 'react';
1
+ import { useRef, useState, useCallback, useMemo } from 'react';
3
2
  import { effect, stop } from '@vue/reactivity';
4
3
 
5
4
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -1546,7 +1545,7 @@ for (var COLLECTION_NAME in DOMIterables) {
1546
1545
 
1547
1546
  handlePrototype(DOMTokenListPrototype, 'DOMTokenList');
1548
1547
 
1549
- 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);
1550
1549
  /**
1551
1550
  * Watches for changes in the source and calls the callback when the source changes
1552
1551
  * @param source - The source to watch for changes
@@ -1554,9 +1553,12 @@ const isObject = value => typeof value === 'object' && value !== null && value !
1554
1553
  * @returns The unsubscribe function
1555
1554
  */
1556
1555
  const watch = (source, callback) => {
1556
+ let isWatching = false;
1557
1557
  const runner = effect(source, {
1558
+ onTrack: () => isWatching = true,
1558
1559
  scheduler: callback
1559
1560
  });
1561
+ if (!isWatching) return undefined;
1560
1562
  return () => {
1561
1563
  stop(runner);
1562
1564
  };
@@ -1575,20 +1577,19 @@ const useReactivityProxy = target => {
1575
1577
  const proxyObjectCacheRef = useRef(new Map());
1576
1578
  // Rerender count to force rerender when the target changes
1577
1579
  const [, setRerenderCount] = useState(0);
1578
- const rerender = useCallback(() => setRerenderCount(rerenderCount => rerenderCount + 1), [setRerenderCount]);
1579
1580
  const unsubscribeAll = useCallback(() => {
1580
1581
  unsubscribeFunctionsRef.current.forEach(unsubscribeFunction => unsubscribeFunction());
1581
1582
  unsubscribeFunctionsRef.current.clear();
1582
1583
  }, []);
1584
+ const rerender = useCallback(() => {
1585
+ unsubscribeAll();
1586
+ setRerenderCount(rerenderCount => rerenderCount + 1);
1587
+ }, [setRerenderCount, unsubscribeAll]);
1583
1588
  /**
1584
1589
  * On every render it cleans the previous subscriptions.
1585
1590
  * This is necessary because different renders can have different subscriptions.
1586
1591
  */
1587
1592
  unsubscribeAll();
1588
- /**
1589
- * Unsubscribe all subscriptions when the component is unmounted
1590
- */
1591
- useEffect(() => () => unsubscribeAll(), [unsubscribeAll]);
1592
1593
  const addSubscriptionProxy = (target, proxyCacheKey) => {
1593
1594
  // Check if object path is already cached
1594
1595
  if (proxyObjectCacheRef.current.has(proxyCacheKey)) {
@@ -1600,9 +1601,13 @@ const useReactivityProxy = target => {
1600
1601
  const value = Reflect.get(target, prop, receiver);
1601
1602
  // Subscribe to the accessed attribute
1602
1603
  const unsubscribeFunction = watch(() => Reflect.get(target, prop, receiver), rerender);
1603
- unsubscribeFunctionsRef.current.add(unsubscribeFunction);
1604
+ if (unsubscribeFunction) {
1605
+ unsubscribeFunctionsRef.current.add(unsubscribeFunction);
1606
+ }
1607
+ const isReactiveValue = Boolean(unsubscribeFunction);
1604
1608
  // If the value is an object, we need to create a proxy for it too
1605
- 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) {
1606
1611
  const propCachePath = `${proxyCacheKey}.${String(prop)}`;
1607
1612
  return addSubscriptionProxy(value, propCachePath);
1608
1613
  }
@@ -1621,28 +1626,4 @@ const useReactivityProxy = target => {
1621
1626
  }, [target]);
1622
1627
  };
1623
1628
 
1624
- const dynamicClientContext = /*#__PURE__*/createContext(undefined);
1625
- const DynamicClientProvider = ({
1626
- client,
1627
- children
1628
- }) => jsx(dynamicClientContext.Provider, {
1629
- value: {
1630
- client
1631
- },
1632
- children: children
1633
- });
1634
- const useDynamicClient = ({
1635
- watch: _watch = true
1636
- } = {}) => {
1637
- const context = useContext(dynamicClientContext);
1638
- if (context === undefined) {
1639
- throw new Error('useDynamicClient must be used within a DynamicClientProvider');
1640
- }
1641
- const reactiveClient = useReactivityProxy(context.client);
1642
- if (_watch) {
1643
- return reactiveClient;
1644
- }
1645
- return context.client;
1646
- };
1647
-
1648
- export { DynamicClientProvider, useDynamicClient };
1629
+ export { useReactivityProxy as useReactiveClient };
package/package.json CHANGED
@@ -1,14 +1,22 @@
1
1
  {
2
2
  "name": "@dynamic-labs/react-hooks",
3
- "version": "2.1.0-alpha.0",
4
- "peerDependencies": {
5
- "react": "^18.0.0",
6
- "@vue/reactivity": "^3.4.21",
7
- "@dynamic-labs/client": "2.1.0-alpha.0"
8
- },
9
- "module": "./index.js",
3
+ "version": "2.1.0-alpha.10",
10
4
  "main": "./index.js",
11
- "type": "module",
5
+ "module": "./index.js",
12
6
  "types": "./src/index.d.ts",
13
- "dependencies": {}
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.d.ts",
11
+ "import": "./src/index.js",
12
+ "require": "./src/index.cjs"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "dependencies": {
17
+ "@vue/reactivity": "3.4.21"
18
+ },
19
+ "peerDependencies": {
20
+ "react": "^18.0.0"
21
+ }
14
22
  }
package/src/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- export type { DefaultClient } from './lib/types/DefaultClient';
2
- export { DynamicClientProvider, useDynamicClient, } from './lib/context/DynamicClientContext';
1
+ export { useReactivityProxy as useReactiveClient } from './hooks/useReactivityProxy';
@@ -1,11 +0,0 @@
1
- import { FC, PropsWithChildren } from 'react';
2
- import { DefaultClient } from '../types/DefaultClient';
3
- type DynamicClientProviderProps = {
4
- client: DefaultClient;
5
- };
6
- export declare const DynamicClientProvider: FC<PropsWithChildren<DynamicClientProviderProps>>;
7
- type UseDynamicClientProps = {
8
- watch?: boolean;
9
- };
10
- export declare const useDynamicClient: ({ watch, }?: UseDynamicClientProps) => DefaultClient;
11
- export {};
@@ -1,2 +0,0 @@
1
- import { createClient } from '@dynamic-labs/client';
2
- export type DefaultClient = ReturnType<typeof createClient>;