Skip to content

Commit

Permalink
Make internal helpers implement Helper interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode authored and webark committed Oct 6, 2016
1 parent 9c1502a commit ae015a0
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 164 deletions.
18 changes: 9 additions & 9 deletions packages/ember-glimmer/lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class Environment extends GlimmerEnvironment {
'-input-type': inputTypeHelper,
'-normalize-class': normalizeClassHelper,
'-html-safe': htmlSafeHelper,
'-get-dynamic-var': { glimmerNativeHelper: getDynamicVar }
'-get-dynamic-var': getDynamicVar
};
}

Expand Down Expand Up @@ -288,23 +288,23 @@ export default class Environment extends GlimmerEnvironment {
assert('The first argument passed into `lookupHelper` should be an array', Array.isArray(nameParts));

let name = nameParts[0];
let helper = this.builtInHelpers[name];

if (helper) {
return helper;
}

let blockMeta = symbolTable.getMeta();
let owner = blockMeta.owner;
let options = blockMeta.moduleName && { source: `template:${blockMeta.moduleName}` } || {};

let helper = this.builtInHelpers[name] ||
owner.lookup(`helper:${name}`, options) ||
owner.lookup(`helper:${name}`);
helper = owner.lookup(`helper:${name}`, options) || owner.lookup(`helper:${name}`);

// TODO: try to unify this into a consistent protocol to avoid wasteful closure allocations
if (helper.isInternalHelper) {
return (vm, args) => helper.toReference(args, this, symbolTable);
} else if (helper.isHelperInstance) {
if (helper.isHelperInstance) {
return (vm, args) => SimpleHelperReference.create(helper.compute, args);
} else if (helper.isHelperFactory) {
return (vm, args) => ClassBasedHelperReference.create(helper, vm, args);
} else if (helper.glimmerNativeHelper) {
return helper.glimmerNativeHelper;
} else {
throw new Error(`${nameParts} is not a helper`);
}
Expand Down
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ function classHelper({ positional }) {
return value;
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(classHelper, args);
}
};
export default function(vm, args) {
return new InternalHelperReference(classHelper, args);
}
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/-html-safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ function htmlSafe({ positional }) {
return new SafeString(path.value());
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(htmlSafe, args);
}
};
export default function(vm, args) {
return new InternalHelperReference(htmlSafe, args);
}
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/-input-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ function inputTypeHelper({ positional, named }) {
return '-text-field';
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(inputTypeHelper, args);
}
};
export default function(vm, args) {
return new InternalHelperReference(inputTypeHelper, args);
}
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/-normalize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ function normalizeClass({ positional, named }) {
}
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(normalizeClass, args);
}
};
export default function(vm, args) {
return new InternalHelperReference(normalizeClass, args);
}
10 changes: 3 additions & 7 deletions packages/ember-glimmer/lib/helpers/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,9 @@ export class ClosureActionReference extends CachedReference {
}
}

export default {
isInternalHelper: true,

toReference(args) {
return ClosureActionReference.create(args);
}
};
export default function(vm, args) {
return ClosureActionReference.create(args);
}

export function createClosureAction(target, action, valuePath, actionArgs) {
let closureAction;
Expand Down
10 changes: 3 additions & 7 deletions packages/ember-glimmer/lib/helpers/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ function curryArgs(definition, newArgs) {
return mergedArgs;
}

export default {
isInternalHelper: true,

toReference(args, env, symbolTable) {
return ClosureComponentReference.create(args, symbolTable, env);
}
};
export default function(vm, args, symbolTable) {
return ClosureComponentReference.create(args, symbolTable, vm.env);
}
10 changes: 6 additions & 4 deletions packages/ember-glimmer/lib/helpers/concat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { helper } from '../helper';
import { InternalHelperReference } from '../utils/references';
import { normalizeTextValue } from 'glimmer-runtime';

/**
Expand All @@ -22,8 +22,10 @@ import { normalizeTextValue } from 'glimmer-runtime';
@for Ember.Templates.helpers
@since 1.13.0
*/
function concat(args) {
return args.map(normalizeTextValue).join('');
function concat({ positional }) {
return positional.value().map(normalizeTextValue).join('');
}

export default helper(concat);
export default function(vm, args) {
return new InternalHelperReference(concat, args);
}
13 changes: 5 additions & 8 deletions packages/ember-glimmer/lib/helpers/each-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ export function isEachIn(ref) {
return ref && ref[EACH_IN_REFERENCE];
}

export default {
isInternalHelper: true,
toReference(args) {
let ref = Object.create(args.positional.at(0));
ref[EACH_IN_REFERENCE] = true;
return ref;
}
};
export default function(vm, args) {
let ref = Object.create(args.positional.at(0));
ref[EACH_IN_REFERENCE] = true;
return ref;
}
10 changes: 3 additions & 7 deletions packages/ember-glimmer/lib/helpers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ import { CONSTANT_TAG, UpdatableTag, combine, isConst, referenceFromParts } from
@since 2.1.0
*/

export default {
isInternalHelper: true,

toReference(args) {
return GetHelperReference.create(args.positional.at(0), args.positional.at(1));
}
};
export default function(vm, args) {
return GetHelperReference.create(args.positional.at(0), args.positional.at(1));
}

class GetHelperReference extends CachedReference {
static create(sourceReference, pathReference) {
Expand Down
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
@public
*/

export default {
isInternalHelper: true,
toReference(args) {
return args.named;
}
};
export default function(vm, args) {
return args.named;
}
46 changes: 20 additions & 26 deletions packages/ember-glimmer/lib/helpers/if-unless.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,17 @@ class ConditionalHelperReference extends CachedReference {
@for Ember.Templates.helpers
@public
*/
export const inlineIf = {
isInternalHelper: true,
toReference({ positional: pargs }) {
switch (pargs.length) {
case 2: return ConditionalHelperReference.create(pargs.at(0), pargs.at(1), null);
case 3: return ConditionalHelperReference.create(pargs.at(0), pargs.at(1), pargs.at(2));
default:
assert(
'The inline form of the `if` helper expects two or three arguments, e.g. ' +
'`{{if trialExpired "Expired" expiryDate}}`.'
);
}
export function inlineIf(vm, { positional }) {
switch (positional.length) {
case 2: return ConditionalHelperReference.create(positional.at(0), positional.at(1), null);
case 3: return ConditionalHelperReference.create(positional.at(0), positional.at(1), positional.at(2));
default:
assert(
'The inline form of the `if` helper expects two or three arguments, e.g. ' +
'`{{if trialExpired "Expired" expiryDate}}`.'
);
}
};
}

/**
The inline `unless` helper conditionally renders a single property or string.
Expand All @@ -89,17 +86,14 @@ export const inlineIf = {
@for Ember.Templates.helpers
@public
*/
export const inlineUnless = {
isInternalHelper: true,
toReference({ positional: pargs }) {
switch (pargs.length) {
case 2: return ConditionalHelperReference.create(pargs.at(0), null, pargs.at(1));
case 3: return ConditionalHelperReference.create(pargs.at(0), pargs.at(2), pargs.at(1));
default:
assert(
'The inline form of the `unless` helper expects two or three arguments, e.g. ' +
'`{{unless isFirstLogin "Welcome back!"}}`.'
);
}
export function inlineUnless(vm, { positional }) {
switch (positional.length) {
case 2: return ConditionalHelperReference.create(positional.at(0), null, positional.at(1));
case 3: return ConditionalHelperReference.create(positional.at(0), positional.at(2), positional.at(1));
default:
assert(
'The inline form of the `unless` helper expects two or three arguments, e.g. ' +
'`{{unless isFirstLogin "Welcome back!"}}`.'
);
}
};
}
10 changes: 6 additions & 4 deletions packages/ember-glimmer/lib/helpers/loc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { helper } from '../helper';
import { InternalHelperReference } from '../utils/references';
import { String as StringUtils } from 'ember-runtime';

/**
Expand Down Expand Up @@ -38,8 +38,10 @@ import { String as StringUtils } from 'ember-runtime';
@see {Ember.String#loc}
@public
*/
function locHelper(params) {
return StringUtils.loc.apply(null, params);
function locHelper({ positional }) {
return StringUtils.loc.apply(null, positional.value());
}

export default helper(locHelper);
export default function(vm, args) {
return new InternalHelperReference(locHelper, args);
}
10 changes: 6 additions & 4 deletions packages/ember-glimmer/lib/helpers/log.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { helper } from '../helper';
import { InternalHelperReference } from '../utils/references';
/**
@module ember
@submodule ember-templates
Expand All @@ -19,8 +19,10 @@ import Logger from 'ember-console';
@param {Array} params
@public
*/
function log(params) {
Logger.log.apply(null, params);
function log({ positional }) {
Logger.log.apply(null, positional.value());
}

export default helper(log);
export default function(vm, args) {
return new InternalHelperReference(log, args);
}
58 changes: 27 additions & 31 deletions packages/ember-glimmer/lib/helpers/mut.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,32 @@ export function unMut(ref) {
return ref[SOURCE] || ref;
}

export default {
isInternalHelper: true,
export default function(vm, args) {
let rawRef = args.positional.at(0);

toReference(args) {
let rawRef = args.positional.at(0);

if (isMut(rawRef)) {
return rawRef;
}

// TODO: Improve this error message. This covers at least two distinct
// cases:
//
// 1. (mut "not a path") – passing a literal, result from a helper
// invocation, etc
//
// 2. (mut receivedValue) – passing a value received from the caller
// that was originally derived from a literal, result from a helper
// invocation, etc
//
// This message is alright for the first case, but could be quite
// confusing for the second case.
assert('You can only pass a path to mut', rawRef[UPDATE]);

let wrappedRef = Object.create(rawRef);

wrappedRef[SOURCE] = rawRef;
wrappedRef[INVOKE] = rawRef[UPDATE];
wrappedRef[MUT_REFERENCE] = true;

return wrappedRef;
if (isMut(rawRef)) {
return rawRef;
}
};

// TODO: Improve this error message. This covers at least two distinct
// cases:
//
// 1. (mut "not a path") – passing a literal, result from a helper
// invocation, etc
//
// 2. (mut receivedValue) – passing a value received from the caller
// that was originally derived from a literal, result from a helper
// invocation, etc
//
// This message is alright for the first case, but could be quite
// confusing for the second case.
assert('You can only pass a path to mut', rawRef[UPDATE]);

let wrappedRef = Object.create(rawRef);

wrappedRef[SOURCE] = rawRef;
wrappedRef[INVOKE] = rawRef[UPDATE];
wrappedRef[MUT_REFERENCE] = true;

return wrappedRef;
}
9 changes: 3 additions & 6 deletions packages/ember-glimmer/lib/helpers/query-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ function queryParams({ positional, named }) {
});
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(queryParams, args);
}
};
export default function(vm, args) {
return new InternalHelperReference(queryParams, args);
}
16 changes: 6 additions & 10 deletions packages/ember-glimmer/lib/helpers/readonly.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { UPDATE } from '../utils/references';
import { unMut } from './mut';

export default {
isInternalHelper: true,
export default function(vm, args) {
let ref = unMut(args.positional.at(0));

toReference(args) {
let ref = unMut(args.positional.at(0));
let wrapped = Object.create(ref);

let wrapped = Object.create(ref);
wrapped[UPDATE] = undefined;

wrapped[UPDATE] = undefined;

return wrapped;
}
};
return wrapped;
}
Loading

0 comments on commit ae015a0

Please sign in to comment.