Skip to content

Commit 4f2f6e3

Browse files
authored
[docs]: TS-Doc comments added (#1087)
* docs: Added tsdoc comments * lint fixes
1 parent ccc47e4 commit 4f2f6e3

6 files changed

Lines changed: 123 additions & 10 deletions

File tree

src/react.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ import {
1515
import { snapshot, subscribe } from './vanilla.ts'
1616
import type { Snapshot } from './vanilla.ts'
1717

18+
/**
19+
* React hook to display affected paths in React DevTools for debugging
20+
*
21+
* This internal hook collects the paths that were accessed during render
22+
* and displays them in React DevTools to help with debugging render optimizations.
23+
*
24+
* @param {object} state - The state object being tracked
25+
* @param {WeakMap<object, unknown>} affected - WeakMap of accessed properties
26+
* @private
27+
*/
1828
const useAffectedDebugValue = (
1929
state: object,
2030
affected: WeakMap<object, unknown>,
@@ -87,8 +97,9 @@ type Options = {
8797
* > console.log(state)
8898
* { profile: { name: "new name" } }
8999
*
90-
* `useSnapshot()` depends on the original reference of the child proxy so if you replace it with a new one, the component
91-
* that is subscribed to the old proxy won't receive new updates because it is still subscribed to the old one.
100+
* `useSnapshot()` depends on the original reference of the child proxy so if you replace it with a new one, the
101+
* component that is subscribed to the old proxy won't receive new updates because it is still subscribed to
102+
* the old one.
92103
*
93104
* In this case we recommend the example C or D. On both examples you don't need to worry with re-render,
94105
* because it is render-optimized.

src/vanilla.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@ import { getUntracked, markToTrack } from 'proxy-compare'
33
const isObject = (x: unknown): x is object =>
44
typeof x === 'object' && x !== null
55

6+
/** Function type for any kind of function */
67
type AnyFunction = (...args: any[]) => any
78

9+
/** Object that can be proxied */
810
type ProxyObject = object
911

12+
/** Property access path as an array of property names/symbols */
1013
type Path = (string | symbol)[]
14+
15+
/**
16+
* Operation performed on a proxy object
17+
* - 'set': A property was set to a new value
18+
* - 'delete': A property was deleted
19+
*/
1120
type Op =
1221
| [op: 'set', path: Path, value: unknown, prevValue: unknown]
1322
| [op: 'delete', path: Path, prevValue: unknown]
23+
24+
/** Function called when a proxy object changes */
1425
type Listener = (op: Op, nextVersion: number) => void
1526

1627
export type INTERNAL_Op = Op
1728

29+
/** JavaScript primitive types */
1830
type Primitive = string | number | boolean | null | undefined | symbol | bigint
1931

32+
/** Types that should not be proxied in snapshots */
2033
type SnapshotIgnore =
2134
| Date
2235
| Map<any, any>
@@ -28,6 +41,11 @@ type SnapshotIgnore =
2841
| AnyFunction
2942
| Primitive
3043

44+
/**
45+
* Snapshot type that converts objects to readonly versions recursively
46+
*
47+
* @template T - Type to convert to a snapshot
48+
*/
3149
export type Snapshot<T> = T extends { $$valtioSnapshot: infer S }
3250
? S
3351
: T extends SnapshotIgnore
@@ -156,6 +174,14 @@ let canProxy: typeof canProxyDefault = canProxyDefault
156174
let createSnapshot: typeof createSnapshotDefault = createSnapshotDefault
157175
let createHandler: typeof createHandlerDefault = createHandlerDefault
158176

177+
/**
178+
* Creates a reactive proxy object that can be tracked for changes
179+
*
180+
* @template T - Type of the object to be proxied
181+
* @param {T} baseObject - The object to create a proxy for
182+
* @returns {T} A proxied version of the input object
183+
* @throws {Error} If the input is not an object
184+
*/
159185
export function proxy<T extends object>(baseObject: T = {} as T): T {
160186
if (!isObject(baseObject)) {
161187
throw new Error('object required')
@@ -266,11 +292,26 @@ export function proxy<T extends object>(baseObject: T = {} as T): T {
266292
return proxyObject
267293
}
268294

295+
/**
296+
* Gets the current version number of a proxy object
297+
*
298+
* @param {unknown} proxyObject - The proxy object to get the version of
299+
* @returns {number | undefined} The current version number, or undefined if not a proxy
300+
*/
269301
export function getVersion(proxyObject: unknown): number | undefined {
270302
const proxyState = proxyStateMap.get(proxyObject as object)
271303
return proxyState?.[1]()
272304
}
273305

306+
/**
307+
* Subscribes to changes in a proxy object
308+
*
309+
* @template T - Type of the proxy object
310+
* @param {T} proxyObject - The proxy object to subscribe to
311+
* @param {Function} callback - Function called when the proxy object changes
312+
* @param {boolean} [notifyInSync] - If true, notifications happen synchronously
313+
* @returns {Function} Unsubscribe function to stop listening for changes
314+
*/
274315
export function subscribe<T extends object>(
275316
proxyObject: T,
276317
callback: (unstable_ops: Op[]) => void,
@@ -307,6 +348,13 @@ export function subscribe<T extends object>(
307348
}
308349
}
309350

351+
/**
352+
* Creates an immutable snapshot of the current state of a proxy object
353+
*
354+
* @template T - Type of the proxy object
355+
* @param {T} proxyObject - The proxy object to create a snapshot from
356+
* @returns {Snapshot<T>} An immutable snapshot of the current state
357+
*/
310358
export function snapshot<T extends object>(proxyObject: T): Snapshot<T> {
311359
const proxyState = proxyStateMap.get(proxyObject as object)
312360
if (import.meta.env?.MODE !== 'production' && !proxyState) {
@@ -316,6 +364,16 @@ export function snapshot<T extends object>(proxyObject: T): Snapshot<T> {
316364
return createSnapshot(target, ensureVersion()) as Snapshot<T>
317365
}
318366

367+
/**
368+
* Marks an object to be excluded from proxying
369+
*
370+
* Objects marked with ref will be kept as references in snapshots
371+
* instead of being deeply copied.
372+
*
373+
* @template T - Type of the object to mark as a reference
374+
* @param {T} obj - The object to mark as a reference
375+
* @returns {T & { $$valtioSnapshot: T }} The same object with a type marker
376+
*/
319377
export function ref<T extends object>(obj: T) {
320378
refSet.add(obj)
321379
return obj as T & { $$valtioSnapshot: T }

src/vanilla/utils/deepClone.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const getDefaultRefSet = (): WeakSet<object> => {
1313
return defaultRefSet
1414
}
1515

16+
/**
17+
* Creates a deep clone of an object, maintaining proxy behavior for Maps and Sets
18+
*
19+
* @template T - Type of the object to clone
20+
* @param {T} obj - The object to clone
21+
* @param {Function} [getRefSet=getDefaultRefSet] - Function to get the set of reference objects
22+
* @returns {T} A deep clone of the input object
23+
*/
1624
export function deepClone<T>(
1725
obj: T,
1826
getRefSet: () => WeakSet<object> = getDefaultRefSet,

src/vanilla/utils/devtools.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ type Options = {
2222
} & Config
2323

2424
/**
25-
* devtools
25+
* Connects a proxy object to Redux DevTools Extension for state debugging
26+
*
27+
* This allows real-time monitoring and time-travel debugging of state changes
28+
* using the Redux DevTools browser extension.
2629
*
27-
* This is to connect with [Redux DevTools Extension](https://github.com/reduxjs/redux-devtools).
2830
* Limitation: Only plain objects/values are supported.
2931
*
32+
* @template T - Type of the proxy object
33+
* @param {T} proxyObject - The proxy object to connect to DevTools
34+
* @param {Options} [options] - Configuration options for the DevTools connection
35+
* @param {boolean} [options.enabled] - Explicitly enable or disable the connection
36+
* @param {string} [options.name=''] - Name to display in DevTools
37+
* @returns {Function|undefined} Unsubscribe function to disconnect from DevTools, or undefined if connection failed
38+
*
3039
* @example
3140
* import { devtools } from 'valtio/utils'
3241
* const state = proxy({ count: 0, text: 'hello' })

src/vanilla/utils/proxyMap.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ type InternalProxyObject<K, V> = Map<K, V> & {
1010
toJSON: () => Map<K, V>
1111
}
1212

13+
/**
14+
* Determines if an object is a proxy Map created with proxyMap
15+
*
16+
* @param {object} obj - The object to check
17+
* @returns {boolean} True if the object is a proxy Map, false otherwise
18+
*/
1319
export const isProxyMap = (obj: object): boolean => {
1420
return (
1521
Symbol.toStringTag in obj &&
@@ -19,10 +25,17 @@ export const isProxyMap = (obj: object): boolean => {
1925
}
2026

2127
/**
22-
* proxyMap
28+
* Creates a reactive Map that integrates with Valtio's proxy system
29+
*
30+
* This utility creates a Map-like object that works with Valtio's reactivity system,
31+
* allowing you to track changes to the Map in the same way as regular proxy objects.
32+
* The API is the same as the standard JavaScript Map.
2333
*
24-
* This is to create a proxy which mimic the native Map behavior.
25-
* The API is the same as Map API
34+
* @template K - Type of the Map keys
35+
* @template V - Type of the Map values
36+
* @param {Iterable<[K, V]>} [entries] - Initial key-value pairs to populate the Map
37+
* @returns {Map<K, V>} A proxy Map object that tracks changes
38+
* @throws {TypeError} If entries is not iterable
2639
*
2740
* @example
2841
* import { proxyMap } from 'valtio/utils'

src/vanilla/utils/proxySet.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ type InternalProxySet<T> = Set<T> & {
1818
isDisjointFrom: (other: Set<T>) => boolean
1919
}
2020

21+
/**
22+
* Determines if an object is a proxy Set created with proxySet
23+
*
24+
* @param {object} obj - The object to check
25+
* @returns {boolean} True if the object is a proxy Set, false otherwise
26+
*/
2127
export const isProxySet = (obj: object): boolean => {
2228
return (
2329
Symbol.toStringTag in obj &&
@@ -27,14 +33,22 @@ export const isProxySet = (obj: object): boolean => {
2733
}
2834

2935
/**
30-
* proxySet
36+
* Creates a reactive Set that integrates with Valtio's proxy system
3137
*
32-
* This is to create a proxy which mimic the native Set behavior.
33-
* The API is the same as Set API
38+
* This utility creates a Set-like object that works with Valtio's reactivity system,
39+
* allowing you to track changes to the Set in the same way as regular proxy objects.
40+
* The API extends the standard JavaScript Set with additional set operations like
41+
* union, intersection, difference, etc.
42+
*
43+
* @template T - Type of the Set elements
44+
* @param {Iterable<T>} [initialValues] - Initial values to populate the Set
45+
* @returns {Set<T>} A reactive proxy Set with extended methods
46+
* @throws {TypeError} If initialValues is not iterable
3447
*
3548
* @example
3649
* import { proxySet } from 'valtio/utils'
3750
* const state = proxySet([1,2,3])
51+
*
3852
* // can be used inside a proxy as well
3953
* const state = proxy({
4054
* count: 1,

0 commit comments

Comments
 (0)