-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useSyncExternalStore } from '@wordpress/element'; | ||
|
||
export type ObservableMap< K, V > = { | ||
get( name: K ): V | undefined; | ||
set( name: K, value: V ): void; | ||
delete( name: K ): void; | ||
subscribe( name: K, listener: () => void ): () => void; | ||
}; | ||
|
||
export function observableMap< K, V >(): ObservableMap< K, V > { | ||
const map = new Map< K, V >(); | ||
const listeners = new Map< K, Set< () => void > >(); | ||
|
||
function callListeners( name: K ) { | ||
const list = listeners.get( name ); | ||
if ( ! list ) { | ||
return; | ||
} | ||
for ( const listener of list ) { | ||
listener(); | ||
} | ||
} | ||
|
||
function unsubscribe( name: K, listener: () => void ) { | ||
return () => { | ||
const list = listeners.get( name ); | ||
if ( ! list ) { | ||
return; | ||
} | ||
|
||
list.delete( listener ); | ||
if ( list.size === 0 ) { | ||
listeners.delete( name ); | ||
} | ||
}; | ||
} | ||
|
||
return { | ||
get( name ) { | ||
return map.get( name ); | ||
}, | ||
set( name, value ) { | ||
map.set( name, value ); | ||
callListeners( name ); | ||
}, | ||
delete( name ) { | ||
map.delete( name ); | ||
callListeners( name ); | ||
}, | ||
subscribe( name, listener ) { | ||
let list = listeners.get( name ); | ||
if ( ! list ) { | ||
list = new Set(); | ||
listeners.set( name, list ); | ||
} | ||
list.add( listener ); | ||
|
||
return unsubscribe( name, listener ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, will do in a followup 👍 |
||
}, | ||
}; | ||
} | ||
|
||
export function useObservableValue< K, V >( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
map: ObservableMap< K, V >, | ||
name: K | ||
): V | undefined { | ||
const [ subscribe, getValue ] = useMemo( | ||
() => [ | ||
( listener: () => void ) => map.subscribe( name, listener ), | ||
() => map.get( name ), | ||
], | ||
[ map, name ] | ||
); | ||
return useSyncExternalStore( subscribe, getValue ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Let me figure out what |
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.