Skip to content

Commit

Permalink
deprecate tryInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Nov 7, 2020
1 parent 14b7c33 commit fa39a3b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 { _tryInvoke } from '@ember/-internals/utils';

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

Expand Down Expand Up @@ -1645,9 +1645,9 @@ if (!jQueryDisabled) {
expectDeprecation(() => {
let comp = owner.lookup('component:foo-bar');
runAppend(comp);
runTask(() => tryInvoke(component, 'destroy'));
runTask(() => _tryInvoke(component, 'destroy'));
}, 'Using this.$() in a component has been deprecated, consider using this.element');
runTask(() => tryInvoke(component, 'destroy'));
runTask(() => _tryInvoke(component, 'destroy'));
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { _tryInvoke } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import { EmberLocation, UpdateCallback } from './api';
import {
Expand Down Expand Up @@ -199,7 +199,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 _tryInvoke(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, _tryInvoke } 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(_tryInvoke(item, methodName, args)));

return ret;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export {
} from './lib/super';
export { default as inspect } from './lib/inspect';
export { default as lookupDescriptor } from './lib/lookup-descriptor';
export { canInvoke, tryInvoke } from './lib/invoke';
export { canInvoke, _tryInvoke, tryInvoke } from './lib/invoke';
export { default as makeArray } from './lib/make-array';
export { getName, setName } from './lib/name';
export { default as toString } from './lib/to-string';
Expand Down
25 changes: 25 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,11 +48,34 @@ 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',
}
);

_tryInvoke(obj, methodName, args);
}

export function _tryInvoke(
obj: any | undefined | null,
methodName: string,
args: Array<any | undefined | null>
) {
if (canInvoke(obj, methodName)) {
let method = obj[methodName];
Expand Down
12 changes: 8 additions & 4 deletions packages/@ember/-internals/utils/tests/try_invoke_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { moduleFor, AbstractTestCase as TestCase } from 'internal-test-helpers';

let obj;

function expectDeprecatedTryInvoke(obj, methodName, args) {
expectDeprecation(() => tryInvoke(obj, methodName, args), /Use of tryInvoke is deprecated/);
}

moduleFor(
'Ember.tryInvoke',
class extends TestCase {
Expand All @@ -24,25 +28,25 @@ moduleFor(
}

["@test should return undefined when the object doesn't exist"](assert) {
assert.equal(tryInvoke(undefined, 'aMethodThatDoesNotExist'), undefined);
assert.equal(expectDeprecatedTryInvoke(undefined, 'aMethodThatDoesNotExist'), undefined);
}

["@test should return undefined when asked to perform a method that doesn't exist on the object"](
assert
) {
assert.equal(tryInvoke(obj, 'aMethodThatDoesNotExist'), undefined);
assert.equal(expectDeprecatedTryInvoke(obj, 'aMethodThatDoesNotExist'), undefined);
}

['@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);
assert.equal(expectDeprecatedTryInvoke(obj, 'aMethodThatExists'), true);
}

['@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);
assert.equal(expectDeprecatedTryInvoke(obj, 'aMethodThatTakesArguments', [true, true]), true);
}
}
);

0 comments on commit fa39a3b

Please sign in to comment.