Skip to content

fix(vanilla, react): use experimental_use and some refactors #545

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

Merged
merged 21 commits into from
Oct 15, 2022
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "./index.js",
"types": "./index.d.ts",
"typesVersions": {
"<4.0": {
"<4.5": {
"esm/*": [
"ts3.4/*"
],
Expand Down Expand Up @@ -73,7 +73,7 @@
"test:coverage:watch": "jest --watch",
"copy": "shx cp -r dist/src/* dist/esm && shx mv dist/src/* dist && shx rm -rf dist/{src,tests} && downlevel-dts dist dist/ts3.4 && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined; this.jest=undefined;\"",
"patch-macro-vite": "shx cp dist/esm/macro/vite.d.ts dist/macro/ && shx mkdir dist/ts3.4/macro && shx cp dist/ts3.4/esm/macro/vite.d.ts dist/ts3.4/macro/",
"patch-ts3.4": "shx sed -i 's/^declare type Snapshot<T> =/declare type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<infer V> ? Snapshot2<V> : { readonly [K in keyof T]: Snapshot2<T[K]> }; type Snapshot2<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<infer V> ? V : { readonly [K in keyof T]: T[K] };declare type _Snapshot<T> =/' 'dist/ts3.4/**/*.d.ts'"
"patch-ts3.4": "node -e \"require('shelljs').find('dist/ts3.4/**/*.d.ts').forEach(f=>require('fs').appendFileSync(f,'declare type Awaited<T> = T extends Promise<infer V> ? V : T;'))\"; shx sed -i 's/^declare type Snapshot<T> =/declare type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : { readonly [K in keyof T]: Snapshot2<T[K]> }; type Snapshot2<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : { readonly [K in keyof T]: T[K] };declare type _Snapshot<T> =/' 'dist/ts3.4/**/*.d.ts'"
},
"engines": {
"node": ">=12.7.0"
Expand Down
50 changes: 46 additions & 4 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { useCallback, useDebugValue, useEffect, useMemo, useRef } from 'react'
/// <reference types="react/experimental" />

import {
experimental_use as use,
useCallback,
useDebugValue,
useEffect,
useMemo,
useRef,
} from 'react'
import {
affectedToPathList,
// affectedToPathList,
createProxy as createProxyToCompare,
getUntracked,
isChanged,
} from 'proxy-compare'
// import { useSyncExternalStore } from 'use-sync-external-store/shim'
Expand All @@ -14,6 +24,38 @@ import type { INTERNAL_Snapshot as Snapshot } from './vanilla'

const { useSyncExternalStore } = useSyncExternalStoreExports

// customized version of affectedToPathList
// we need to avoid invoking getters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi ahhh, okay, this change (skipping getters in the output of affectedToPathList) is why I've been confused.

I've been watching the output of useAffectedDebugValue (maybe a little too religiously in retrospect) and was very confused by the lack of getters.

This made me think "valtio is not working" / "doesn't know my getters were invoked", when obviously I could tell it did, b/c the re-renders still worked correctly.

I finally reproduced the behavior in proxy-compare and "get it"--getters (and method invocations) are always in the affected map, and are always handled correctly by isChanged, it is merely affectedToPathList that is leaving them off.

What was the rationale here? I'm sure it makes sense, just curious b/c it tripped me up. Wondering if I can work the rationale/behavior in a doc/"gotchas" update.

Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, it loops infinitely and hangs, AFAIR.

const affectedToPathList = (
obj: unknown,
affected: WeakMap<object, unknown>
) => {
const list: (string | symbol)[][] = []
const seen = new WeakSet()
const walk = (x: unknown, path?: (string | symbol)[]) => {
if (seen.has(x as object)) {
// for object with cycles
return
}
let used: Set<string | symbol> | undefined
if (typeof x === 'object' && x !== null) {
seen.add(x)
used = affected.get(getUntracked(x) || x) as any
}
if (used) {
used.forEach((key) => {
if ('value' in (Object.getOwnPropertyDescriptor(x, key) || {})) {
walk((x as any)[key], path ? [...path, key] : [key])
}
})
} else if (path) {
list.push(path)
}
}
walk(obj)
return list
}

const useAffectedDebugValue = (
state: object,
affected: WeakMap<object, unknown>
Expand Down Expand Up @@ -119,7 +161,7 @@ export function useSnapshot<T extends object>(
[proxyObject, notifyInSync]
),
() => {
const nextSnapshot = snapshot(proxyObject)
const nextSnapshot = snapshot(proxyObject, use)
try {
if (
!inRender &&
Expand All @@ -140,7 +182,7 @@ export function useSnapshot<T extends object>(
}
return nextSnapshot
},
() => snapshot(proxyObject)
() => snapshot(proxyObject, use)
)
inRender = false
const currAffected = new WeakMap()
Expand Down
Loading