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

Convert lock unlock to generics #66682

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions packages/components/src/private-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import { Tabs } from './tabs';
import { kebabCase } from './utils/strings';
import { lock } from './lock-unlock';

export const privateApis = {};
lock( privateApis, {
const privateData = {
__experimentalPopoverLegacyPositionToPlacement,
createPrivateSlotFill,
ComponentsContext,
Tabs,
Theme,
Menu,
kebabCase,
} );
};

export const privateApis = {} as typeof privateData;

lock( privateApis, privateData );
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/core-data/src/hooks/use-entity-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export function useEntityRecordsWithPermissions< RecordType >(

const permissions = useSelect(
( select ) => {
// @ts-expect-error getEntityRecordsPermissions doesn't exist in the core store types
const { getEntityRecordsPermissions } = unlock(
select( coreStore )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function AddFilterMenu( {
} ) {
const inactiveFilters = filters.filter( ( filter ) => ! filter.isVisible );
return (
<Menu trigger={ trigger }>
<Menu
// @ts-expect-error trigger prop exists on Menu component
trigger={ trigger }
>
{ inactiveFilters.map( ( filter ) => {
return (
<Menu.Item
Expand Down
18 changes: 11 additions & 7 deletions packages/private-apis/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,16 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
* @param object The object to bind the private data to.
* @param privateData The private data to bind to the object.
*/
function lock( object: Record< symbol, WeakKey >, privateData: unknown ) {
function lock< T >( object: T, privateData: unknown ) {
if ( ! object ) {
throw new Error( 'Cannot lock an undefined object.' );
}
if ( ! ( __private in object ) ) {
object[ __private ] = {};
const _object = object as Record< symbol, WeakKey >;

if ( ! ( __private in _object ) ) {
_object[ __private ] = {};
}
lockedData.set( object[ __private ], privateData );
lockedData.set( _object[ __private ], privateData );
}

/**
Expand All @@ -170,17 +172,19 @@ function lock( object: Record< symbol, WeakKey >, privateData: unknown ) {
* @param object The object to unlock the private data from.
* @return The private data bound to the object.
*/
function unlock( object: Record< symbol, WeakKey > ) {
function unlock< T >( object: T ): T {
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved
if ( ! object ) {
throw new Error( 'Cannot unlock an undefined object.' );
}
if ( ! ( __private in object ) ) {
const _object = object as Record< symbol, WeakKey >;

if ( ! ( __private in _object ) ) {
throw new Error(
'Cannot unlock an object that was not locked before. '
);
}

return lockedData.get( object[ __private ] );
return lockedData.get( _object[ __private ] );
}

const lockedData = new WeakMap();
Expand Down
Loading