Skip to content
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

[RFC 673] Deprecate tryInvoke #19254

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"broccoli-funnel": "^2.0.2",
"broccoli-merge-trees": "^4.2.0",
"chalk": "^4.0.0",
"ember-cli-babel": "^7.19.0",
"ember-cli-babel": "^7.23.0",
"ember-cli-get-component-path-option": "^1.0.0",
"ember-cli-is-package-missing": "^1.0.0",
"ember-cli-normalize-entity-name": "^1.0.0",
Expand Down
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)));

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/
);
}
}
);
Loading