Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): make Snapshot<T> use read-only collections #850

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const isObject = (x: unknown): x is object =>

type AnyFunction = (...args: any[]) => any

type AsRef = { $$valtioRef: true }

type ProxyObject = object

type Path = (string | symbol)[]
Expand All @@ -25,19 +23,20 @@ type SnapshotIgnore =
| Set<any>
| WeakMap<any, any>
| WeakSet<any>
| AsRef
| Error
| RegExp
| AnyFunction
| Primitive

type Snapshot<T> = T extends SnapshotIgnore
? T
: T extends Promise<unknown>
? Awaited<T>
: T extends object
? { readonly [K in keyof T]: Snapshot<T[K]> }
: T
type Snapshot<T> = T extends { $$valtioSnapshot: infer S }
? S
: T extends SnapshotIgnore
? T
: T extends Promise<unknown>
? Awaited<T>
: T extends object
? { readonly [K in keyof T]: Snapshot<T[K]> }
: T

/**
* This is not a public API.
Expand Down Expand Up @@ -403,9 +402,9 @@ export function snapshot<T extends object>(
return createSnapshot(target, ensureVersion(), handlePromise) as Snapshot<T>
}

export function ref<T extends object>(obj: T): T & AsRef {
export function ref<T extends object>(obj: T) {
refSet.add(obj)
return obj as T & AsRef
return obj as T & { $$valtioSnapshot: T }
}

export const unstable_buildProxyFunction = buildProxyFunction
8 changes: 4 additions & 4 deletions src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ type InternalProxyMap<K, V> = Map<K, V> & {
* state.set(key, "value")
* state.get(key) //undefined
*/
export function proxyMap<K, V>(
entries?: Iterable<readonly [K, V]> | null,
): Map<K, V> {
export function proxyMap<K, V>(entries?: Iterable<readonly [K, V]> | null) {
const map: InternalProxyMap<K, V> = proxy({
data: Array.from(entries || []) as KeyValRecord<K, V>[],
has(key) {
Expand Down Expand Up @@ -107,5 +105,7 @@ export function proxyMap<K, V>(
})
Object.seal(map)

return map as Map<K, V>
return map as unknown as Map<K, V> & {
$$valtioSnapshot: Omit<Map<K, V>, 'set' | 'delete' | 'clear'>
}
}
6 changes: 4 additions & 2 deletions src/vanilla/utils/proxySet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type InternalProxySet<T> = Set<T> & {
* set: proxySet()
* })
*/
export function proxySet<T>(initialValues?: Iterable<T> | null): Set<T> {
export function proxySet<T>(initialValues?: Iterable<T> | null) {
const set: InternalProxySet<T> = proxy({
data: Array.from(new Set(initialValues)),
has(value) {
Expand Down Expand Up @@ -92,5 +92,7 @@ export function proxySet<T>(initialValues?: Iterable<T> | null): Set<T> {

Object.seal(set)

return set as Set<T>
return set as unknown as Set<T> & {
$$valtioSnapshot: Omit<Set<T>, 'add' | 'delete' | 'clear'>
}
}
8 changes: 2 additions & 6 deletions tests/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ describe('snapsoht typings', () => {
undefined: undefined
bool: boolean
someFunction(): number
ref: {
$$valtioRef: true
}
ref: { x: unknown } & { $$valtioSnapshot: { x: unknown } }
}>,
{
readonly string: string
Expand All @@ -111,9 +109,7 @@ describe('snapsoht typings', () => {
readonly undefined: undefined
readonly bool: boolean
readonly someFunction: () => number
readonly ref: {
$$valtioRef: true
}
readonly ref: { x: unknown }
}
>
>(true)
Expand Down
Loading