-
-
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.
[BUGFIX lts] Use args proxy for modifier managers.
Prior to this change, all custom modifiers **always** consumed every argument on install/update/destroy. This was because `reifyArgs` specifically reads all of them, and the actual usage was not consumption based.
- Loading branch information
Showing
5 changed files
with
208 additions
and
122 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
98 changes: 98 additions & 0 deletions
98
packages/@ember/-internals/glimmer/lib/utils/args-proxy.ts
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,98 @@ | ||
import { CUSTOM_TAG_FOR } from '@ember/-internals/metal'; | ||
import { HAS_NATIVE_PROXY } from '@ember/-internals/utils'; | ||
import { assert } from '@ember/debug'; | ||
import { DEBUG } from '@glimmer/env'; | ||
import { Arguments, CapturedNamedArguments } from '@glimmer/interfaces'; | ||
import { valueForRef } from '@glimmer/reference'; | ||
import { Tag, track } from '@glimmer/validator'; | ||
|
||
function tagForNamedArg<NamedArgs extends CapturedNamedArguments, K extends keyof NamedArgs>( | ||
namedArgs: NamedArgs, | ||
key: K | ||
): Tag { | ||
return track(() => valueForRef(namedArgs[key])); | ||
} | ||
|
||
export let namedArgsProxyFor: ( | ||
namedArgs: CapturedNamedArguments, | ||
debugName?: string | ||
) => Arguments['named']; | ||
|
||
if (HAS_NATIVE_PROXY) { | ||
namedArgsProxyFor = <NamedArgs extends CapturedNamedArguments>( | ||
namedArgs: NamedArgs, | ||
debugName?: string | ||
) => { | ||
let getTag = (key: keyof Arguments) => tagForNamedArg(namedArgs, key); | ||
|
||
let handler: ProxyHandler<{}> = { | ||
get(_target, prop) { | ||
let ref = namedArgs[prop as string]; | ||
|
||
if (ref !== undefined) { | ||
return valueForRef(ref); | ||
} else if (prop === CUSTOM_TAG_FOR) { | ||
return getTag; | ||
} | ||
}, | ||
|
||
has(_target, prop) { | ||
return namedArgs[prop as string] !== undefined; | ||
}, | ||
|
||
ownKeys(_target) { | ||
return Object.keys(namedArgs); | ||
}, | ||
|
||
getOwnPropertyDescriptor(_target, prop) { | ||
assert( | ||
'args proxies do not have real property descriptors, so you should never need to call getOwnPropertyDescriptor yourself. This code exists for enumerability, such as in for-in loops and Object.keys()', | ||
namedArgs[prop as string] !== undefined | ||
); | ||
|
||
return { | ||
enumerable: true, | ||
configurable: true, | ||
}; | ||
}, | ||
}; | ||
|
||
if (DEBUG) { | ||
handler.set = function(_target, prop) { | ||
assert( | ||
`You attempted to set ${debugName}#${String( | ||
prop | ||
)} on a components arguments. Component arguments are immutable and cannot be updated directly, they always represent the values that are passed to your component. If you want to set default values, you should use a getter instead` | ||
); | ||
|
||
return false; | ||
}; | ||
} | ||
|
||
return new Proxy({}, handler); | ||
}; | ||
} else { | ||
namedArgsProxyFor = <NamedArgs extends CapturedNamedArguments>(namedArgs: NamedArgs) => { | ||
let getTag = (key: keyof Arguments) => tagForNamedArg(namedArgs, key); | ||
|
||
let proxy = {}; | ||
|
||
Object.defineProperty(proxy, CUSTOM_TAG_FOR, { | ||
configurable: false, | ||
enumerable: false, | ||
value: getTag, | ||
}); | ||
|
||
Object.keys(namedArgs).forEach(name => { | ||
Object.defineProperty(proxy, name, { | ||
enumerable: true, | ||
configurable: true, | ||
get() { | ||
return valueForRef(namedArgs[name]); | ||
}, | ||
}); | ||
}); | ||
|
||
return proxy; | ||
}; | ||
} |
Oops, something went wrong.