From b7ee8dbfc264fc1baf5ff05dbb01e7d8996b8d93 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 2 Nov 2022 10:11:24 +0100 Subject: [PATCH] unstableSubscribeStore: support store descriptors --- packages/data/src/registry.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/data/src/registry.js b/packages/data/src/registry.js index 65c2acead5091..92638b02768db 100644 --- a/packages/data/src/registry.js +++ b/packages/data/src/registry.js @@ -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, @@ -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 ) {