Skip to content

Commit

Permalink
Move namespace arg to first position in proxify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Jul 4, 2024
1 parent 7baddca commit 46fa744
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ directive(
( { context: { Provider }, props: { children } } ) => {
executionProof( 'context' );
const value = {
[ namespace ]: proxifyState(
{
attribute: 'from context',
text: 'from context',
},
namespace
),
[ namespace ]: proxifyState( namespace, {
attribute: 'from context',
text: 'from context',
} ),
};
return h( Provider, { value }, children );
},
Expand Down
4 changes: 2 additions & 2 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default () => {

const ns = defaultEntry!.namespace;
const currentValue = useRef( {
[ ns ]: proxifyState( {}, ns ),
[ ns ]: proxifyState( ns, {} ),
} );

// No change should be made if `defaultEntry` does not exist.
Expand Down Expand Up @@ -608,7 +608,7 @@ export default () => {
const itemProp =
suffix === 'default' ? 'item' : kebabToCamelCase( suffix );
const itemContext = {
[ namespace ]: proxifyState( {}, namespace ),
[ namespace ]: proxifyState( namespace, {} ),
};
const mergedContext = proxifyContext(
itemContext,
Expand Down
9 changes: 6 additions & 3 deletions packages/interactivity/src/proxies/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const ignore = new WeakSet< object >();

const supported = new Set( [ Object, Array ] );

export const getProxy = < T extends object >(
export const createProxy = < T extends object >(
namespace: string,
obj: T,
handlers?: ProxyHandler< T >,
namespace?: string
handlers: ProxyHandler< T >
): T => {
if ( ! shouldProxy( obj ) ) {
throw Error( 'This object can be proxified.' );
Expand All @@ -21,6 +21,9 @@ export const getProxy = < T extends object >(
return objToProxy.get( obj ) as T;
};

export const getProxy = < T extends object >( obj: T ): T =>
objToProxy.get( obj ) as T;

export const getProxyNs = ( proxy: object ): string => proxyToNs.get( proxy )!;

export const shouldProxy = ( val: any ): val is Object | Array< unknown > => {
Expand Down
12 changes: 6 additions & 6 deletions packages/interactivity/src/proxies/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { signal, type Signal } from '@preact/signals';
/**
* Internal dependencies
*/
import { getProxy, shouldProxy } from './registry';
import { createProxy, getProxy, shouldProxy } from './registry';
import { PropSignal } from './signals';
import { setNamespace, resetNamespace } from '../hooks';

Expand Down Expand Up @@ -78,7 +78,7 @@ const stateHandlers: ProxyHandler< object > = {
const value = Reflect.get( target, key, receiver );
prop.setValue(
shouldProxy( value )
? proxifyState( value, prop.namespace )
? proxifyState( prop.namespace, value )
: value
);
}
Expand Down Expand Up @@ -124,7 +124,7 @@ const stateHandlers: ProxyHandler< object > = {
} else {
prop.setValue(
shouldProxy( value )
? proxifyState( value, prop.namespace )
? proxifyState( prop.namespace, value )
: value
);
}
Expand Down Expand Up @@ -168,9 +168,9 @@ const stateHandlers: ProxyHandler< object > = {
};

export const proxifyState = < T extends object >(
obj: T,
namespace: string
): T => getProxy( obj, stateHandlers, namespace ) as T;
namespace: string,
obj: T
): T => createProxy( namespace, obj, stateHandlers ) as T;

export const peek = < T extends object, K extends keyof T >(
obj: T,
Expand Down
14 changes: 7 additions & 7 deletions packages/interactivity/src/proxies/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { getProxy, getProxyNs, shouldProxy } from './registry';
import { createProxy, getProxyNs, shouldProxy } from './registry';
import { setNamespace, resetNamespace } from '../hooks';
import { withScope } from '../utils';

Expand All @@ -20,7 +20,7 @@ const storeHandlers: ProxyHandler< object > = {
if ( typeof result === 'undefined' && storeRoots.has( receiver ) ) {
const obj = {};
Reflect.set( target, key, obj );
return proxifyStore( obj, ns );
return proxifyStore( ns, obj, true );
}

// Check if the property is a function. If it is, add the store
Expand All @@ -36,20 +36,20 @@ const storeHandlers: ProxyHandler< object > = {

// Check if the property is an object. If it is, proxyify it.
if ( isObject( result ) && shouldProxy( result ) ) {
return proxifyStore( result, ns );
return proxifyStore( ns, result, true );
}

return result;
},
};

export const proxifyStore = < T extends object >(
obj: T,
namespace: string,
isRoot = false
obj: T,
isNotRoot = false
): T => {
const proxy = getProxy( obj, storeHandlers, namespace );
if ( proxy && isRoot ) {
const proxy = createProxy( namespace, obj, storeHandlers );
if ( proxy && ! isNotRoot ) {
storeRoots.add( proxy );
}
return proxy as T;
Expand Down
Loading

0 comments on commit 46fa744

Please sign in to comment.