Skip to content

Commit

Permalink
Merge pull request #17845 from emberjs/cleanup/remove-deprecated-apis
Browse files Browse the repository at this point in the history
[CLEANUP] Removes various deprecated APIs
  • Loading branch information
rwjblue authored Apr 3, 2019
2 parents 0a23ccb + 630a2a8 commit 97d969d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 170 deletions.
84 changes: 10 additions & 74 deletions packages/@ember/-internals/meta/lib/meta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lookupDescriptor, symbol, toString } from '@ember/-internals/utils';
import { assert, deprecate } from '@ember/debug';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { Tag } from '@glimmer/reference';

Expand Down Expand Up @@ -63,14 +63,6 @@ const enum ListenerKind {
ADD = 0,
ONCE = 1,
REMOVE = 2,
REMOVE_ALL = 3,
}

interface RemoveAllListener {
event: string;
target: null;
method: null;
kind: ListenerKind.REMOVE_ALL;
}

interface StringListener {
Expand All @@ -87,7 +79,7 @@ interface FunctionListener {
kind: ListenerKind.ADD | ListenerKind.ONCE | ListenerKind.REMOVE;
}

type Listener = RemoveAllListener | StringListener | FunctionListener;
type Listener = StringListener | FunctionListener;

let currentListenerVersion = 1;

Expand Down Expand Up @@ -520,44 +512,6 @@ export class Meta {
this.pushListener(eventName, target, method, ListenerKind.REMOVE);
}

removeAllListeners(event: string) {
deprecate(
'The remove all functionality of removeListener and removeObserver has been deprecated. Remove each listener/observer individually instead.',
false,
{
id: 'events.remove-all-listeners',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_events-remove-all-listeners',
}
);

if (DEBUG) {
counters!.removeAllListenersCalls++;
}

let listeners = this.writableListeners();
let inheritedEnd = this._inheritedEnd;
// remove all listeners of event name
// adjusting the inheritedEnd if listener is below it
for (let i = listeners.length - 1; i >= 0; i--) {
let listener = listeners[i];
if (listener.event === event) {
listeners.splice(i, 1);
if (i < inheritedEnd) {
inheritedEnd--;
}
}
}
this._inheritedEnd = inheritedEnd;
// we put remove alls at start because rare and easy to check there
listeners.splice(inheritedEnd, 0, {
event,
target: null,
method: null,
kind: ListenerKind.REMOVE_ALL,
});
}

private pushListener(
event: string,
target: object | null,
Expand All @@ -579,28 +533,18 @@ export class Meta {
// found, even in the case of a function listener remove, because we may be
// attempting to add or remove listeners _before_ flattening has occured.
if (i === -1) {
deprecate(
'Adding function listeners to prototypes has been deprecated. Convert the listener to a string listener, or add it to the instance instead.',
!(this.isPrototypeMeta(this.source) && typeof method === 'function'),
{
id: 'events.inherited-function-listeners',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_events-inherited-function-listeners',
}
assert(
'You cannot add function listeners to prototypes. Convert the listener to a string listener, or add it to the instance instead.',
!(this.isPrototypeMeta(this.source) && typeof method === 'function')
);

deprecate(
'You attempted to remove a function listener which did not exist on the instance, which means it was an inherited prototype listener, or you attempted to remove it before it was added. Prototype function listeners have been deprecated, and attempting to remove a non-existent function listener this will error in the future.',
assert(
'You attempted to remove a function listener which did not exist on the instance, which means you may have attempted to remove it before it was added.',
!(
!this.isPrototypeMeta(this.source) &&
typeof method === 'function' &&
kind === ListenerKind.REMOVE
),
{
id: 'events.inherited-function-listeners',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_events-inherited-function-listeners',
}
)
);

listeners.push({
Expand All @@ -623,10 +567,6 @@ export class Meta {
} else {
// update own listener
listener.kind = kind;

// TODO: Remove this when removing REMOVE_ALL, it won't be necessary
listener.target = target;
listener.method = method;
}
}
}
Expand Down Expand Up @@ -744,7 +684,7 @@ export class Meta {
for (let index = 0; index < listeners.length; index++) {
let listener = listeners[index];

// REMOVE and REMOVE_ALL listeners are placeholders that tell us not to
// REMOVE listeners are placeholders that tell us not to
// inherit, so they never match. Only ADD and ONCE can match.
if (
listener.event === eventName &&
Expand Down Expand Up @@ -976,11 +916,7 @@ function indexOfListener(
for (let i = listeners.length - 1; i >= 0; i--) {
let listener = listeners[i];

if (
listener.event === event &&
((listener.target === target && listener.method === method) ||
listener.kind === ListenerKind.REMOVE_ALL)
) {
if (listener.event === event && (listener.target === target && listener.method === method)) {
return i;
}
}
Expand Down
26 changes: 0 additions & 26 deletions packages/@ember/-internals/meta/tests/listeners_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,5 @@ moduleFor(
'one reopen call after mutating parents and flattening out of order'
);
}

['@test REMOVE_ALL does not interfere with future adds'](assert) {
expectDeprecation(() => {
let t = {};
let m = meta({});

m.addToListeners('hello', t, 'm', 0);
let matching = m.matchingListeners('hello');

assert.equal(matching.length, 3);
assert.equal(matching[0], t);

// Remove all listeners
m.removeAllListeners('hello');

matching = m.matchingListeners('hello');
assert.equal(matching, undefined);

m.addToListeners('hello', t, 'm', 0);
matching = m.matchingListeners('hello');

// listener was added back successfully
assert.equal(matching.length, 3);
assert.equal(matching[0], t);
});
}
}
);
26 changes: 15 additions & 11 deletions packages/@ember/-internals/metal/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,30 @@ export function addListener(
export function removeListener(
obj: object,
eventName: string,
target: object | null,
method?: Function | string
targetOrFunction: object | Function | null,
functionOrName?: string | Function
): void {
assert(
'You must pass at least an object and event name to removeListener',
Boolean(obj) && Boolean(eventName)
'You must pass at least an object, event name, and method or target and method/method name to removeListener',
Boolean(obj) &&
Boolean(eventName) &&
(typeof targetOrFunction === 'function' ||
(typeof targetOrFunction === 'object' && Boolean(functionOrName)))
);

if (!method && 'function' === typeof target) {
method = target;
let target, method;

if (typeof targetOrFunction === 'object') {
target = targetOrFunction;
method = functionOrName!;
} else {
target = null;
method = targetOrFunction;
}

let m = metaFor(obj);

if (method === undefined) {
m.removeAllListeners(eventName);
} else {
m.removeFromListeners(eventName, target, method);
}
m.removeFromListeners(eventName, target, method);
}

/**
Expand Down
18 changes: 0 additions & 18 deletions packages/@ember/-internals/metal/tests/events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,6 @@ moduleFor(
assert.equal(hasListeners(obj, 'event!'), true, 'has listeners');
}

['@test calling removeListener without method should remove all listeners'](assert) {
expectDeprecation(() => {
let obj = {};
function F() {}
function F2() {}

assert.equal(hasListeners(obj, 'event!'), false, 'no listeners at first');

addListener(obj, 'event!', F);
addListener(obj, 'event!', F2);

assert.equal(hasListeners(obj, 'event!'), true, 'has listeners');
removeListener(obj, 'event!');

assert.equal(hasListeners(obj, 'event!'), false, 'has no more listeners');
}, /The remove all functionality of removeListener and removeObserver has been deprecated/);
}

['@test a listener can be added as part of a mixin'](assert) {
let triggered = 0;
let MyMixin = Mixin.create({
Expand Down
24 changes: 7 additions & 17 deletions packages/@ember/-internals/runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
arrayContentWillChange,
arrayContentDidChange,
} from '@ember/-internals/metal';
import { assert, deprecate } from '@ember/debug';
import { assert } from '@ember/debug';
import Enumerable from './enumerable';
import compare from '../compare';
import { ENV } from '@ember/-internals/environment';
Expand Down Expand Up @@ -1601,28 +1601,18 @@ if (ENV.EXTEND_PROTOTYPES.Array) {
NativeArray.apply(Array.prototype);

A = function(arr) {
deprecate(
'`new A()` has been deprecated, please update to calling A as a function: `A()`',
!(this instanceof A),
{
id: 'array.new-array-wrapper',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_array-new-array-wrapper',
}
assert(
'You cannot create an Ember Array with `new A()`, please update to calling A as a function: `A()`',
!(this instanceof A)
);

return arr || [];
};
} else {
A = function(arr) {
deprecate(
'`new A()` has been deprecated, please update to calling A as a function: `A()`',
!(this instanceof A),
{
id: 'array.new-array-wrapper',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_array-new-array-wrapper',
}
assert(
'You cannot create an Ember Array with `new A()`, please update to calling A as a function: `A()`',
!(this instanceof A)
);

if (!arr) {
Expand Down
27 changes: 10 additions & 17 deletions packages/@ember/-internals/runtime/lib/system/core_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
DEBUG_INJECTION_FUNCTIONS,
} from '@ember/-internals/metal';
import ActionHandler from '../mixins/action_handler';
import { assert, deprecate } from '@ember/debug';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

const reopen = Mixin.prototype.reopen;
Expand All @@ -38,12 +38,12 @@ const factoryMap = new WeakMap();

const prototypeMixinMap = new WeakMap();

const DELAY_INIT = Object.freeze({});

let PASSED_FROM_CREATE;
let initCalled; // only used in debug builds to enable the proxy trap

// using DEBUG here to avoid the extraneous variable when not needed
if (DEBUG) {
PASSED_FROM_CREATE = Symbol();
initCalled = new WeakSet();
}

Expand Down Expand Up @@ -269,19 +269,12 @@ class CoreObject {
let m = meta(self);
m.setInitializing();

if (properties !== DELAY_INIT) {
deprecate(
'using `new` with EmberObject has been deprecated. Please use `create` instead, or consider using native classes without extending from EmberObject.',
false,
{
id: 'object.new-constructor',
until: '3.9.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_object-new-constructor',
}
);

initialize(self, properties);
}
assert(
`An EmberObject based class, ${
this.constructor
}, was not instantiated correctly. You may have either used \`new\` instead of \`.create()\`, or not passed arguments to your call to super in the constructor: \`super(...arguments)\`. If you are trying to use \`new\`, consider using native classes without extending from EmberObject.`,
properties[PASSED_FROM_CREATE]
);

// only return when in debug builds and `self` is the proxy created above
if (DEBUG && self !== this) {
Expand Down Expand Up @@ -756,7 +749,7 @@ class CoreObject {
*/
static create(props, extra) {
let C = this;
let instance = new C(DELAY_INIT);
let instance = DEBUG ? new C(Object.freeze({ [PASSED_FROM_CREATE]: true })) : new C();

if (extra === undefined) {
initialize(instance, props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { moduleFor, AbstractTestCase, buildOwner } from 'internal-test-helpers';
moduleFor(
'Ember.CoreObject',
class extends AbstractTestCase {
['@test throws deprecation with new (one arg)']() {
expectDeprecation(() => {
['@test throws an error with new (one arg)']() {
expectAssertion(() => {
new CoreObject({
firstName: 'Stef',
lastName: 'Penner',
});
}, /using `new` with EmberObject has been deprecated/);
}, /You may have either used `new` instead of `.create\(\)`/);
}

['@test throws deprecation with new (> 1 arg)']() {
expectDeprecation(() => {
['@test throws an error with new (> 1 arg)']() {
expectAssertion(() => {
new CoreObject(
{
firstName: 'Stef',
Expand All @@ -26,7 +26,7 @@ moduleFor(
other: 'name',
}
);
}, /using `new` with EmberObject has been deprecated/);
}, /You may have either used `new` instead of `.create\(\)`/);
}

['@test toString should be not be added as a property when calling toString()'](assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ moduleFor(
}

['@test new Ember.A'](assert) {
expectDeprecation(() => {
expectAssertion(() => {
assert.deepEqual(new A([1, 2]), [1, 2], 'array values were not be modified');
assert.deepEqual(new A(), [], 'returned an array with no arguments');
assert.deepEqual(new A(null), [], 'returned an array with a null argument');
Expand Down

0 comments on commit 97d969d

Please sign in to comment.