Skip to content

Commit

Permalink
deprecate tryInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Nov 12, 2020
1 parent 0213b78 commit 15e3eb2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { schedule } from '@ember/runloop';
import { set, setProperties } from '@ember/-internals/metal';
import { A as emberA } from '@ember/-internals/runtime';
import { getViewId, getViewElement, jQueryDisabled } from '@ember/-internals/views';
import { tryInvoke } from '@ember/-internals/utils';

import { Component } from '../../utils/helpers';

Expand Down Expand Up @@ -1645,9 +1644,9 @@ if (!jQueryDisabled) {
expectDeprecation(() => {
let comp = owner.lookup('component:foo-bar');
runAppend(comp);
runTask(() => tryInvoke(component, 'destroy'));
runTask(() => component.destroy?.());
}, 'Using this.$() in a component has been deprecated, consider using this.element');
runTask(() => tryInvoke(component, 'destroy'));
runTask(() => component.destroy?.());
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { history, location, userAgent, window } from '@ember/-internals/browser-
import { set } from '@ember/-internals/metal';
import { getOwner } from '@ember/-internals/owner';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { tryInvoke } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import { EmberLocation, UpdateCallback } from './api';
import {
Expand Down Expand Up @@ -199,7 +198,7 @@ function delegateToConcreteImplementation(methodName: string) {
"AutoLocation's detect() method should be called before calling any other hooks.",
Boolean(concreteImplementation)
);
return tryInvoke(concreteImplementation, methodName, args);
return concreteImplementation[methodName]?.(...args);
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@ember/-internals/runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { DEBUG } from '@glimmer/env';
import { PROXY_CONTENT } from '@ember/-internals/metal';
import { setEmberArray, HAS_NATIVE_PROXY, tryInvoke } from '@ember/-internals/utils';
import { setEmberArray, HAS_NATIVE_PROXY } from '@ember/-internals/utils';
import {
get,
set,
Expand Down Expand Up @@ -1347,7 +1347,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
invoke(methodName, ...args) {
let ret = A();

this.forEach((item) => ret.push(tryInvoke(item, methodName, args)));
this.forEach((item) => ret.push(item[methodName]?.(...args)));

This comment has been minimized.

Copy link
@janwerkhoven

janwerkhoven May 22, 2021

My Fastboot setup seems to trip over this change with SyntaxError: Unexpected token '.' at ]?.(....

"ember-source": "~3.27.1",
"ember-cli-fastboot": "^2.2.3",
"fastboot-app-server": "^3.0.0",

Reverting just ember-source back to:

"ember-source": "~3.23.1",

Solved the issue... Suggestions?

This comment has been minimized.

Copy link
@janwerkhoven

janwerkhoven May 22, 2021

I've created a more detail issue here: #19550


return ret;
},
Expand Down
17 changes: 17 additions & 0 deletions packages/@ember/-internals/utils/lib/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { deprecate } from '@ember/debug';

/**
Checks to see if the `methodName` exists on the `obj`.
Expand Down Expand Up @@ -46,12 +48,27 @@ export function canInvoke(obj: any | null | undefined, methodName: string): obj
@param {Array} [args] The arguments to pass to the method
@return {*} the return value of the invoked method or undefined if it cannot be invoked
@public
@deprecated Use Javascript's optional chaining instead.
*/
export function tryInvoke(
obj: any | undefined | null,
methodName: string,
args: Array<any | undefined | null>
) {
deprecate(
`Use of tryInvoke is deprecated. Instead, consider using JavaScript's optional chaining.`,
false,
{
id: 'ember-utils.try-invoke',
until: '4.0.0',
for: 'ember-source',
since: {
available: '3.24.0',
},
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-utils-try-invoke',
}
);

if (canInvoke(obj, methodName)) {
let method = obj[methodName];
return method.apply(obj, args);
Expand Down
20 changes: 16 additions & 4 deletions packages/@ember/-internals/utils/tests/try_invoke_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,37 @@ moduleFor(
}

["@test should return undefined when the object doesn't exist"](assert) {
assert.equal(tryInvoke(undefined, 'aMethodThatDoesNotExist'), undefined);
expectDeprecation(
() => assert.equal(tryInvoke(undefined, 'aMethodThatDoesNotExist'), undefined),
/Use of tryInvoke is deprecated/
);
}

["@test should return undefined when asked to perform a method that doesn't exist on the object"](
assert
) {
assert.equal(tryInvoke(obj, 'aMethodThatDoesNotExist'), undefined);
expectDeprecation(
() => assert.equal(tryInvoke(obj, 'aMethodThatDoesNotExist'), undefined),
/Use of tryInvoke is deprecated/
);
}

['@test should return what the method returns when asked to perform a method that exists on the object'](
assert
) {
assert.equal(tryInvoke(obj, 'aMethodThatExists'), true);
expectDeprecation(
() => assert.equal(tryInvoke(obj, 'aMethodThatExists'), true),
/Use of tryInvoke is deprecated/
);
}

['@test should return what the method returns when asked to perform a method that takes arguments and exists on the object'](
assert
) {
assert.equal(tryInvoke(obj, 'aMethodThatTakesArguments', [true, true]), true);
expectDeprecation(
() => assert.equal(tryInvoke(obj, 'aMethodThatTakesArguments', [true, true]), true),
/Use of tryInvoke is deprecated/
);
}
}
);

0 comments on commit 15e3eb2

Please sign in to comment.