-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: Refactor context proxies (#64713)
* Move `proxifyContext` to proxies folder * Move context handlers outside * Remove unused code * Update comment * Add TODO comment * Remove unused code and rename `k` arg to `key` * Replace `updateContext` with `deepMerge` * Throw when calling `proxifyContext` on a context proxy * Add unit tests * Update changelog * Added new tests to validate behavior with proxified state. Also included more descriptive error messages for re-proxification attempts. --------- Co-authored-by: DAreRodz <darerodz@git.wordpress.org> Co-authored-by: michalczaplinski <czapla@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
- Loading branch information
1 parent
93af2dd
commit 636710b
Showing
5 changed files
with
369 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const contextObjectToProxy = new WeakMap(); | ||
const contextObjectToFallback = new WeakMap(); | ||
const contextProxies = new WeakSet(); | ||
|
||
const descriptor = Reflect.getOwnPropertyDescriptor; | ||
|
||
// TODO: Use the proxy registry to avoid multiple proxies on the same object. | ||
const contextHandlers: ProxyHandler< object > = { | ||
get: ( target, key ) => { | ||
const fallback = contextObjectToFallback.get( target ); | ||
// Always subscribe to prop changes in the current context. | ||
const currentProp = target[ key ]; | ||
|
||
/* | ||
* Return the value from `target` if it exists, or from `fallback` | ||
* otherwise. This way, in the case the property doesn't exist either in | ||
* `target` or `fallback`, it also subscribes to changes in the parent | ||
* context. | ||
*/ | ||
return key in target ? currentProp : fallback[ key ]; | ||
}, | ||
set: ( target, key, value ) => { | ||
const fallback = contextObjectToFallback.get( target ); | ||
|
||
// If the property exists in the current context, modify it. Otherwise, | ||
// add it to the current context. | ||
const obj = key in target || ! ( key in fallback ) ? target : fallback; | ||
obj[ key ] = value; | ||
|
||
return true; | ||
}, | ||
ownKeys: ( target ) => [ | ||
...new Set( [ | ||
...Object.keys( contextObjectToFallback.get( target ) ), | ||
...Object.keys( target ), | ||
] ), | ||
], | ||
getOwnPropertyDescriptor: ( target, key ) => | ||
descriptor( target, key ) || | ||
descriptor( contextObjectToFallback.get( target ), key ), | ||
}; | ||
|
||
/** | ||
* Wrap a context object with a proxy to reproduce the context stack. The proxy | ||
* uses the passed `inherited` context as a fallback to look up for properties | ||
* that don't exist in the given context. Also, updated properties are modified | ||
* where they are defined, or added to the main context when they don't exist. | ||
* | ||
* @param current Current context. | ||
* @param inherited Inherited context, used as fallback. | ||
* | ||
* @return The wrapped context object. | ||
*/ | ||
export const proxifyContext = ( | ||
current: object, | ||
inherited: object = {} | ||
): object => { | ||
if ( contextProxies.has( current ) ) { | ||
throw Error( 'This object cannot be proxified.' ); | ||
} | ||
// Update the fallback object reference when it changes. | ||
contextObjectToFallback.set( current, inherited ); | ||
if ( ! contextObjectToProxy.has( current ) ) { | ||
const proxy = new Proxy( current, contextHandlers ); | ||
contextObjectToProxy.set( current, proxy ); | ||
contextProxies.add( proxy ); | ||
} | ||
return contextObjectToProxy.get( current ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
636710b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 636710b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10908245521
📝 Reported issues:
/test/e2e/specs/editor/blocks/navigation-colors.spec.js