Skip to content

Commit

Permalink
svelte alias method
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Garrett committed Feb 1, 2019
1 parent d0d779b commit 29f5c2a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
93 changes: 59 additions & 34 deletions packages/@ember/-internals/metal/lib/mixin.ts
Expand Up @@ -13,6 +13,7 @@ import {
wrap,
} from '@ember/-internals/utils';
import { assert, deprecate } from '@ember/debug';
import { ALIAS_METHOD } from '@ember/deprecated-features';
import { assign } from '@ember/polyfills';
import { DEBUG } from '@glimmer/env';
import { ComputedProperty, ComputedPropertyGetter, ComputedPropertySetter } from './computed';
Expand Down Expand Up @@ -301,28 +302,37 @@ function mergeMixins(
}
}

function followMethodAlias(
let followMethodAlias: (
obj: object,
alias: Alias,
descs: { [key: string]: any },
values: { [key: string]: any }
) {
let altKey = alias.methodName;
let possibleDesc;
let desc = descs[altKey];
let value = values[altKey];

if (desc !== undefined || value !== undefined) {
// do nothing
} else if ((possibleDesc = descriptorFor(obj, altKey)) !== undefined) {
desc = possibleDesc;
value = undefined;
} else {
desc = undefined;
value = obj[altKey];
}
) => { desc: any; value: any };

if (ALIAS_METHOD) {
followMethodAlias = function(
obj: object,
alias: Alias,
descs: { [key: string]: any },
values: { [key: string]: any }
) {
let altKey = alias.methodName;
let possibleDesc;
let desc = descs[altKey];
let value = values[altKey];

if (desc !== undefined || value !== undefined) {
// do nothing
} else if ((possibleDesc = descriptorFor(obj, altKey)) !== undefined) {
desc = possibleDesc;
value = undefined;
} else {
desc = undefined;
value = obj[altKey];
}

return { desc, value };
return { desc, value };
};
}

function updateObserversAndListeners(
Expand Down Expand Up @@ -387,10 +397,12 @@ export function applyMixin(obj: { [key: string]: any }, mixins: Mixin[]) {
desc = descs[key];
value = values[key];

while (value && value instanceof Alias) {
let followed = followMethodAlias(obj, value, descs, values);
desc = followed.desc;
value = followed.value;
if (ALIAS_METHOD) {
while (value && value instanceof AliasImpl) {
let followed = followMethodAlias(obj, value, descs, values);
desc = followed.desc;
value = followed.value;
}
}

if (desc === undefined && value === undefined) {
Expand Down Expand Up @@ -703,8 +715,17 @@ function _keys(mixin: Mixin, ret = new Set(), seen = new Set()) {
return ret;
}

class Alias {
constructor(public methodName: string) {}
declare class Alias {
public methodName: string;
constructor(methodName: string);
}

let AliasImpl: typeof Alias;

if (ALIAS_METHOD) {
AliasImpl = class AliasImpl {
constructor(public methodName: string) {}
} as typeof Alias;
}

/**
Expand Down Expand Up @@ -737,17 +758,21 @@ class Alias {
@param {String} methodName name of the method to alias
@public
*/
export function aliasMethod(methodName: string): Alias {
deprecate(
`You attempted to alias '${methodName}, but aliasMethod has been deprecated. Consider extracting the method into a shared utility function.`,
false,
{
id: 'object.alias-method',
until: '4.0.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_object-alias-method',
}
);
return new Alias(methodName);
export let aliasMethod: (methodName: string) => any;

if (ALIAS_METHOD) {
aliasMethod = function aliasMethod(methodName: string): Alias {
deprecate(
`You attempted to alias '${methodName}, but aliasMethod has been deprecated. Consider extracting the method into a shared utility function.`,
false,
{
id: 'object.alias-method',
until: '4.0.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_object-alias-method',
}
);
return new AliasImpl(methodName);
};
}

// ..........................................................
Expand Down
1 change: 1 addition & 0 deletions packages/@ember/deprecated-features/index.ts
Expand Up @@ -10,3 +10,4 @@ export const ROUTER_EVENTS = !!'3.9.0';
export const TRANSITION_STATE = !!'3.9.0';
export const COMPONENT_MANAGER_STRING_LOOKUP = !!'4.0.0';
export const JQUERY_INTEGRATION = !!'3.9.0';
export const ALIAS_METHOD = !!'4.0.0';

0 comments on commit 29f5c2a

Please sign in to comment.