Skip to content

Commit

Permalink
added jsdoc comments for proxyMap and proxySet (#974)
Browse files Browse the repository at this point in the history
* added jsdoc comments for proxyMap and proxySet

* fixed spacing in jsdoc comment
  • Loading branch information
overthemike authored Oct 16, 2024
1 parent 131df82 commit 2a96acf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ type InternalProxyObject<K, V> = Map<K, V> & {
toJSON: () => Map<K, V>
}

/**
* proxyMap
*
* This is to create a proxy which mimic the native Map behavior.
* The API is the same as Map API
*
* @example
* import { proxyMap } from 'valtio/utils'
* const state = proxyMap([["key", "value"]])
*
* // can be used inside a proxy as well
* const state = proxy({
* count: 1,
* map: proxyMap()
* })
*
* // When using an object as a key, you can wrap it with `ref` so it's not proxied
* // this is useful if you want to preserve the key equality
* import { ref } from 'valtio'
*
* const key = ref({})
* state.set(key, "value")
* state.get(key) //value
*
* const key = {}
* state.set(key, "value")
* state.get(key) //undefined
*/
export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
const initialData: Array<V> = []
let initialIndex = 0
Expand Down
15 changes: 15 additions & 0 deletions src/vanilla/utils/proxySet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ type InternalProxySet<T> = Set<T> & {
union: (other: Set<T>) => Set<T>
}

/**
* proxySet
*
* This is to create a proxy which mimic the native Set behavior.
* The API is the same as Set API
*
* @example
* import { proxySet } from 'valtio/utils'
* const state = proxySet([1,2,3])
* // can be used inside a proxy as well
* const state = proxy({
* count: 1,
* set: proxySet()
* })
*/
export function proxySet<T>(initialValues?: Iterable<T> | null) {
const initialData: T[] = []
const indexMap = new Map<T, number>()
Expand Down

0 comments on commit 2a96acf

Please sign in to comment.