-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[Glimmer2] Add mut helper and update readonly helper #13541
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
Changes from all commits
70c5d8c
c7ec5b6
d638e2d
4fd086e
e1c6a4b
5ad48d9
e44bde1
dbd3c33
8d06db5
e1c7826
e9f9ba1
9066be0
b1ea106
1478c13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { assert } from 'ember-metal/debug'; | ||
| import { InternalHelperReference, UPDATE } from '../utils/references'; | ||
| import { INVOKE } from 'ember-htmlbars/keywords/closure-action'; | ||
| import { MUTABLE_REFERENCE } from 'ember-htmlbars/keywords/mut'; | ||
| import { isConst } from 'glimmer-reference'; | ||
|
|
||
| export default { | ||
| isInternalHelper: true, | ||
| toReference(args) { | ||
| let ref = args.positional.at(0); | ||
|
|
||
| assert('You can only pass a path to mut', !isConst(ref)); | ||
|
|
||
| if (!ref[MUTABLE_REFERENCE]) { | ||
| return new MutHelperReference(mut, args); | ||
| } | ||
| return ref; | ||
| } | ||
| }; | ||
|
|
||
| function mut({ positional }) { | ||
| return positional.at(0).value(); | ||
| } | ||
|
|
||
| class MutHelperReference extends InternalHelperReference { | ||
| constructor() { | ||
| super(...arguments); | ||
| this[MUTABLE_REFERENCE] = true; | ||
| } | ||
|
|
||
| [UPDATE](val) { | ||
| let argRef = this.args.positional.at(0); | ||
| argRef[UPDATE](val); | ||
| } | ||
|
|
||
| [INVOKE](val) { | ||
| this[UPDATE](val); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,44 @@ | ||
| import { helper } from '../helper'; | ||
| import { CachedReference, UnboundReference, UPDATE, READONLY } from '../utils/references'; | ||
| import { CURRENT_TAG, DirtyableTag, combine, isConst } from 'glimmer-reference'; | ||
|
|
||
| function readonly(args) { | ||
| return args[0]; | ||
| } | ||
| export default { | ||
| isInternalHelper: true, | ||
| toReference(args) { | ||
| return ReadOnlyReference.create(args.positional.at(0)); | ||
| } | ||
| }; | ||
|
|
||
| export class ReadOnlyReference extends CachedReference { | ||
| static create(arg) { | ||
| if (isConst(arg)) { | ||
| return new UnboundReference(arg); | ||
| } else { | ||
| return new ReadOnlyReference(arg); | ||
| } | ||
| } | ||
|
|
||
| constructor(arg) { | ||
| super(); | ||
| this.arg = arg; | ||
| this.unboundTag = new DirtyableTag(CURRENT_TAG.value()); | ||
| this.tag = combine([arg.tag, this.unboundTag]); | ||
| this.unboundValue = undefined; | ||
| this[READONLY] = true; | ||
| } | ||
|
|
||
| export default helper(readonly); | ||
| [UPDATE](val) { | ||
| this.unboundValue = val; | ||
| this.unboundTag.dirty(); | ||
| } | ||
|
|
||
| compute() { | ||
| let { tag, unboundTag, unboundValue, arg } = this; | ||
| let unboundTagValue = unboundTag.value(); | ||
|
|
||
| if (tag.validate(unboundTagValue) && !arg.tag.validate(unboundTagValue)) { | ||
| return unboundValue; | ||
| } else { | ||
| return arg.value(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,10 @@ import { CONSTANT_TAG } from 'glimmer-reference'; | |
| import { assert } from 'ember-metal/debug'; | ||
| import EmptyObject from 'ember-metal/empty_object'; | ||
| import { ARGS } from '../component'; | ||
| import { MUTABLE_CELL } from 'ember-views/compat/attrs-proxy'; | ||
| import { MUTABLE_REFERENCE } from 'ember-htmlbars/keywords/mut'; | ||
| import { ACTION } from 'ember-htmlbars/keywords/closure-action'; | ||
| import { UPDATE } from 'ember-glimmer/utils/references'; | ||
|
|
||
| export default function processArgs(args, positionalParamsDefinition) { | ||
| if (!positionalParamsDefinition || positionalParamsDefinition.length === 0 || args.positional.length === 0) { | ||
|
|
@@ -48,15 +52,32 @@ class SimpleArgs { | |
| for (let i = 0, l = keys.length; i < l; i++) { | ||
| let name = keys[i]; | ||
| let value = attrs[name]; | ||
| let ref = namedArgs.get(name); | ||
|
|
||
| args[name] = namedArgs.get(name); | ||
| if (ref[MUTABLE_REFERENCE] && !ref[ACTION]) { | ||
| attrs[name] = new MutableCell(ref, value); | ||
| } | ||
|
|
||
| args[name] = ref; | ||
| props[name] = value; | ||
| } | ||
|
|
||
| return { attrs, props }; | ||
| } | ||
| } | ||
|
|
||
| class MutableCell { | ||
| constructor(ref, value) { | ||
| this._ref = ref; | ||
| this[MUTABLE_CELL] = true; | ||
| this.value = value; | ||
| } | ||
|
|
||
| update(val) { | ||
| this._ref[UPDATE](val); | ||
|
||
| } | ||
| } | ||
|
|
||
| class RestArgs { | ||
| static create(args, restArgName) { | ||
| assert(`You cannot specify positional parameters and the hash argument \`${restArgName}\`.`, !args.named.has(restArgName)); | ||
|
|
||
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.
This looks like an ember-internal thing and as far as I can tell no internal package really calls INVOKE, should we remove this? It's a symbol anyway so it can't be called externally.
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.
In general, it is used (as private API) by ember-concurrency and ember-route-action-helper addons.