@@ -3,20 +3,33 @@ import { getUntracked, markToTrack } from 'proxy-compare'
33const isObject = ( x : unknown ) : x is object =>
44 typeof x === 'object' && x !== null
55
6+ /** Function type for any kind of function */
67type AnyFunction = ( ...args : any [ ] ) => any
78
9+ /** Object that can be proxied */
810type ProxyObject = object
911
12+ /** Property access path as an array of property names/symbols */
1013type 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+ */
1120type 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 */
1425type Listener = ( op : Op , nextVersion : number ) => void
1526
1627export type INTERNAL_Op = Op
1728
29+ /** JavaScript primitive types */
1830type Primitive = string | number | boolean | null | undefined | symbol | bigint
1931
32+ /** Types that should not be proxied in snapshots */
2033type 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+ */
3149export type Snapshot < T > = T extends { $$valtioSnapshot : infer S }
3250 ? S
3351 : T extends SnapshotIgnore
@@ -156,6 +174,14 @@ let canProxy: typeof canProxyDefault = canProxyDefault
156174let createSnapshot : typeof createSnapshotDefault = createSnapshotDefault
157175let 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+ */
159185export 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+ */
269301export 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+ */
274315export 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+ */
310358export 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+ */
319377export function ref < T extends object > ( obj : T ) {
320378 refSet . add ( obj )
321379 return obj as T & { $$valtioSnapshot : T }
0 commit comments