Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Implement stacks for scopes and namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Oct 9, 2023
1 parent 97cac9d commit 96631ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
28 changes: 14 additions & 14 deletions assets/js/interactivity/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ import { nsPathParser } from './vdom';
// Main context.
const context = createContext( {} );

let currentScope = null;
let currentNamespace = null;
let scopeStack: any[] = [];
let namespaceStack: string[] = [];

export const getContext = < T extends object >(
namespace: string = currentNamespace
): T => {
return currentScope.context[ namespace ];
export const getContext = < T extends object >( namespace?: string ): T => {
const [ currentNamespace ] = namespaceStack.slice( -1 );
return getScope()?.context[ namespace || currentNamespace ];
};

export const getElementRef = () => currentScope.ref.current;
export const getElementRef = () => getScope()?.ref.current;

export const getScope = () => scopeStack.slice( -1 )[ 0 ];

export const getScope = () => currentScope;
export const setScope = ( scope ) => {
currentScope = scope;
scopeStack.push( scope );
};
export const resetScope = () => {
currentScope = null;
scopeStack.pop();
};

export const setNamespace = ( namespace ) => {
currentNamespace = namespace;
export const setNamespace = ( namespace: string ) => {
namespaceStack.push( namespace );
};
export const resetNamespace = () => {
currentNamespace = null;
namespaceStack.pop();
};

// WordPress Directives.
Expand Down Expand Up @@ -144,7 +144,7 @@ export const directive = ( name, callback, { priority = 10 } = {} ) => {
const resolve = ( path, namespace ) => {
let current = {
...stores.get( namespace ),
context: currentScope.context[ namespace ],
context: getScope().context[ namespace ],
};
path.split( '.' ).forEach( ( p ) => ( current = current[ p ] ) );
return current;
Expand Down
14 changes: 9 additions & 5 deletions assets/js/interactivity/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
*/
import { deepSignal } from 'deepsignal';
import { computed } from '@preact/signals';
import { getScope, setScope, setNamespace, resetNamespace } from './hooks';
import {
getScope,
setScope,
resetScope,
setNamespace,
resetNamespace,
} from './hooks';

const isObject = ( item: unknown ): boolean =>
!! item && typeof item === 'object' && ! Array.isArray( item );
Expand Down Expand Up @@ -77,13 +83,12 @@ const handlers = {
scope.getters.set(
getter,
computed( () => {
const prevScope = getScope();
setNamespace( ns );
setScope( scope );
try {
return getter.call( target );
} finally {
setScope( prevScope );
resetScope();
resetNamespace();
}
} )
Expand All @@ -105,13 +110,12 @@ const handlers = {
let it: IteratorResult< any >;

while ( true ) {
const prevScope = getScope();
setNamespace( ns );
setScope( scope );
try {
it = gen.next( value );
} finally {
setScope( prevScope );
resetScope();
resetNamespace();
}

Expand Down

0 comments on commit 96631ce

Please sign in to comment.