-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SlotFill: replace valtio with custom ObservableMap (#60879)
* SlotFill: replace valtio with custom ObservableMap * Fill PR number in changelog * Use Set for listeners Co-authored-by: jsnajdr <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: jeryj <[email protected]>
- Loading branch information
1 parent
4b08cf1
commit 058cc37
Showing
9 changed files
with
106 additions
and
111 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/components/src/slot-fill/bubbles-virtually/observable-map.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}, | ||
}; | ||
} | ||
|
||
export function useObservableValue< K, V >( | ||
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters