Skip to content

Commit

Permalink
Expose privateActionsOf() and privateSelectorsOf() instead of getStor…
Browse files Browse the repository at this point in the history
…e() on the registry object
  • Loading branch information
adamziel committed Jan 26, 2023
1 parent 86b6270 commit 8067b46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
32 changes: 0 additions & 32 deletions docs/contributors/code/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,38 +330,6 @@ function MyComponent() {
}
```

Remember to always register the private actions and selectors on the **registered** store.

Sometimes that's easy:
```js
export const store = createReduxStore( STORE_NAME, storeConfig() );

register( store );

unlock( registeredStore ).registerPrivateActions({
// ...
});
```

However, some packages call both `createReduxStore` **and** `registerStore`. In this case,
always choose the store that gets registered:

```js
export const store = createReduxStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );

const registeredStore = registerStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );

unlock( registeredStore ).registerPrivateActions({
// ...
});
```

##### Experimental functions, classes, and variables

```js
Expand Down
46 changes: 32 additions & 14 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
};
stores[ name ] = store;
store.subscribe( globalListener );

// Copy private actions and selectors from the parent store.
if ( parent ) {
try {
unlock( store.store ).registerPrivateActions(
unlock( parent ).privateActionsOf( name )
);
unlock( store.store ).registerPrivateSelectors(
unlock( parent ).privateSelectorsOf( name )
);
} catch ( e ) {
// unlock() throws if store.store was not locked.
// The error indicates there's nothing to do here so let's
// ignore it.
}
}
}

/**
Expand Down Expand Up @@ -281,19 +297,6 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
const store = createReduxStore( storeName, options ).instantiate(
registry
);
// If creating a sub store, we need to copy the private actions and selectors
// from the parent store.
if ( parent ) {
try {
const getParentStore = unlock( parent ).getRegisteredStore;
unlock( store.store ).registerPrivateActions(
unlock( getParentStore( storeName ).store ).privateActions
);
unlock( store.store ).registerPrivateSelectors(
unlock( getParentStore( storeName ).store ).privateSelectors
);
} catch ( e ) {}
}
registerStoreInstance( storeName, store );
return store.store;
}
Expand Down Expand Up @@ -350,7 +353,22 @@ export function createRegistry( storeConfigs = {}, parent = null ) {

const registryWithPlugins = withPlugins( registry );
lock( registryWithPlugins, {
getRegisteredStore: ( name ) => stores[ name ],
privateActionsOf: ( name ) => {
try {
return unlock( stores[ name ].store ).privateActions;
} catch ( e ) {
// unlock() throws an error the store was not locked – this means
// there no private actions are available
return {};
}
},
privateSelectorsOf: ( name ) => {
try {
return unlock( stores[ name ].store ).privateSelectors;
} catch ( e ) {
return {};
}
},
} );
return registryWithPlugins;
}

0 comments on commit 8067b46

Please sign in to comment.