-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
SlotFill: replace valtio with custom ObservableMap #60879
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: -2.15 kB (0%) Total Size: 1.74 MB
ℹ️ View Unchanged
|
This makes the select block/focus metric 20-30% faster! Thank you so much for this change! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice little refactoring. It's clearer indeed.
Wow, nice! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another dependency down. Makes things much clearer, too.
Nice work @jsnajdr 🚀
}; | ||
} | ||
|
||
export function useObservableValue< K, V >( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not particularly convinced this belongs here, but if we don't need it outside of the component for now, that should be fine.
subscribe( name: K, listener: () => void ): () => void; | ||
}; | ||
|
||
export function observableMap< K, V >(): ObservableMap< K, V > { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be nice to add some inline docs and/or some tests.
[ map, name ] | ||
); | ||
return useSyncExternalStore( subscribe, getValue ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could use this for block refs too: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/block-list/use-block-props/use-block-refs.js
And maybe there are more places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me figure out what useBlockRefs
does 🙂
}, | ||
subscribe( name, listener ) { | ||
const list = listeners.get( name ) || []; | ||
listeners.set( name, [ ...list, listener ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it bad to mutate the array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is for getValue
to return a different array. Otherwise, useSyncExternalStore
won't know that data has changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just replaced the array with a Set
, doing .add
and .delete
operations on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is for
getValue
to return a different array.
It would be OK if the listeners
map contained an array that is mutated with .push
and .splice
. Instead of creating and setting a new immutable array with ...
and .filter
. But when I looked around at other event emitter implementation in Gutenberg, I found that using a Set
is best.
} | ||
list.add( listener ); | ||
|
||
return unsubscribe( name, listener ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be possible to avoid having to get the list again if it's inlined? Or can it disappear/change some other way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, will do in a followup 👍
This PR removes usage of the
valtio
library from theSlotFill
registry and replaces it with our ownObservableMap
object. It's a map where each entry (keyed by slotname
) can be individually and independently observed for changes by calling asubscribe( name, listener )
function. AuseObservableValue( name )
then observes just thename
slot in the registry and is unaffected by others.It was always unclear that this is actually what
valtio
is doing and one was left wondering what other magic is going on there.@Mamaduka and @jeryj has some questions recently (#59884 (comment)) about
SlotFill
details, hopefully this answers them at least partially.