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

unstableSubscribeStore: support store descriptors #45481

Merged
merged 1 commit into from
Nov 3, 2022
Merged
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
20 changes: 14 additions & 6 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,18 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
/**
* Subscribe handler to a store.
*
* @param {string[]} storeName The store name.
* @param {Function} handler The function subscribed to the store.
* @param {string|StoreDescriptor} storeNameOrDescriptor The store name.
* @param {Function} handler The function subscribed to the store.
* @return {Function} A function to unsubscribe the handler.
*/
function __unstableSubscribeStore( storeName, handler ) {
if ( storeName in stores ) {
return stores[ storeName ].subscribe( handler );
function __unstableSubscribeStore( storeNameOrDescriptor, handler ) {
const storeName = isObject( storeNameOrDescriptor )
? storeNameOrDescriptor.name
: storeNameOrDescriptor;

const store = stores[ storeName ];
if ( store ) {
return store.subscribe( handler );
}

// Trying to access a store that hasn't been registered,
Expand All @@ -288,7 +293,10 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
return subscribe( handler );
}

return parent.__unstableSubscribeStore( storeName, handler );
return parent.__unstableSubscribeStore(
storeNameOrDescriptor,
handler
);
}

function batch( callback ) {
Expand Down