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

[BUGFIX release] Deprecate immediateObserver #11677

Merged
merged 1 commit into from
Jul 7, 2015
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
20 changes: 15 additions & 5 deletions packages/ember-debug/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,25 @@ Ember.deprecate = function(message, test, options) {

@method deprecateFunc
@param {String} message A description of the deprecation.
@param {Object} [options] The options object for Ember.deprecate.
@param {Function} func The new function called to replace its deprecated counterpart.
@return {Function} a new function that wrapped the original function with a deprecation warning
@private
*/
Ember.deprecateFunc = function(message, func) {
return function() {
Ember.deprecate(message);
return func.apply(this, arguments);
};
Ember.deprecateFunc = function(...args) {
if (args.length === 3) {
let [message, options, func] = args;
return function() {
Ember.deprecate(message, false, options);
return func.call(this, arguments);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just spotted this. It should be func.apply(this, arguments), not call :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 0081ebd

};
} else {
let [message, func] = args;
return function() {
Ember.deprecate(message);
return func.apply(this, arguments);
};
}
};


Expand Down
12 changes: 6 additions & 6 deletions packages/ember-metal/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ import {
Mixin,
aliasMethod,
_beforeObserver,
immediateObserver,
_immediateObserver,
mixin,
observer,
required
Expand Down Expand Up @@ -310,20 +310,20 @@ Ember.cacheFor = cacheFor;
Ember.addObserver = addObserver;
Ember.observersFor = observersFor;
Ember.removeObserver = removeObserver;
Ember.addBeforeObserver = Ember.deprecateFunc('Ember.addBeforeObserver is deprecated and will be removed in the near future. See http://emberjs.com/deprecations/v1.x/#toc_beforeobserver', _addBeforeObserver);
Ember.addBeforeObserver = Ember.deprecateFunc('Ember.addBeforeObserver is deprecated and will be removed in the near future.', { url: 'http://emberjs.com/deprecations/v1.x/#toc_beforeobserver' }, _addBeforeObserver);
Ember._suspendBeforeObserver = _suspendBeforeObserver;
Ember._suspendBeforeObservers = _suspendBeforeObservers;
Ember._suspendObserver = _suspendObserver;
Ember._suspendObservers = _suspendObservers;
Ember.beforeObserversFor = Ember.deprecateFunc('Ember.beforeObserversFor is deprecated and will be removed in the near future. See http://emberjs.com/deprecations/v1.x/#toc_beforeobserver', _beforeObserversFor);
Ember.removeBeforeObserver = Ember.deprecateFunc('Ember.removeBeforeObserver is deprecated and will be removed in the near future. See http://emberjs.com/deprecations/v1.x/#toc_beforeobserver', _removeBeforeObserver);
Ember.beforeObserversFor = Ember.deprecateFunc('Ember.beforeObserversFor is deprecated and will be removed in the near future.', { url: 'http://emberjs.com/deprecations/v1.x/#toc_beforeobserver' }, _beforeObserversFor);
Ember.removeBeforeObserver = Ember.deprecateFunc('Ember.removeBeforeObserver is deprecated and will be removed in the near future.', { url: 'http://emberjs.com/deprecations/v1.x/#toc_beforeobserver' }, _removeBeforeObserver);

Ember.IS_BINDING = IS_BINDING;
Ember.required = required;
Ember.aliasMethod = aliasMethod;
Ember.observer = observer;
Ember.immediateObserver = immediateObserver;
Ember.beforeObserver = Ember.deprecateFunc('Ember.beforeObserver is deprecated and will be removed in the near future. See http://emberjs.com/deprecations/v1.x/#toc_beforeobserver', _beforeObserver);
Ember.immediateObserver = _immediateObserver;
Ember.beforeObserver = Ember.deprecateFunc('Ember.beforeObserver is deprecated and will be removed in the near future.', { url: 'http://emberjs.com/deprecations/v1.x/#toc_beforeobserver' }, _beforeObserver);
Ember.mixin = mixin;
Ember.Mixin = Mixin;

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ export function observer(...args) {
@return func
@private
*/
export function immediateObserver() {
export function _immediateObserver() {
Ember.deprecate('Usage of `Ember.immediateObserver` is deprecated, use `Ember.observer` instead.');

for (var i=0, l=arguments.length; i<l; i++) {
Expand Down
22 changes: 12 additions & 10 deletions packages/ember-metal/tests/observer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
mixin,
observer,
_beforeObserver,
immediateObserver
_immediateObserver
} from 'ember-metal/mixin';
import run from 'ember-metal/run_loop';
import {
Expand Down Expand Up @@ -1062,7 +1062,7 @@ testBoth('immediate observers should fire synchronously', function(get, set) {
// trigger deferred behavior
run(function() {
mixin = Mixin.create({
fooDidChange: immediateObserver('foo', function() {
fooDidChange: _immediateObserver('foo', function() {
observerCalled++;
equal(get(this, 'foo'), 'barbaz', 'newly set value is immediately available');
})
Expand Down Expand Up @@ -1094,12 +1094,14 @@ if (Ember.EXTEND_PROTOTYPES) {
// explicitly create a run loop so we do not inadvertently
// trigger deferred behavior
run(function() {
mixin = Mixin.create({
fooDidChange: function() {
observerCalled++;
equal(get(this, 'foo'), 'barbaz', 'newly set value is immediately available');
}.observesImmediately('{foo,bar}')
});
expectDeprecation(function() {
mixin = Mixin.create({
fooDidChange: function() {
observerCalled++;
equal(get(this, 'foo'), 'barbaz', 'newly set value is immediately available');
}.observesImmediately('{foo,bar}')
});
}, /Function#observesImmediately is deprecated. Use Function#observes instead/);

mixin.apply(obj);

Expand Down Expand Up @@ -1128,7 +1130,7 @@ testBoth('immediate observers watching multiple properties via brace expansion f
// trigger deferred behavior
run(function() {
mixin = Mixin.create({
fooDidChange: immediateObserver('{foo,bar}', function() {
fooDidChange: _immediateObserver('{foo,bar}', function() {
observerCalled++;
equal(get(this, 'foo'), 'barbaz', 'newly set value is immediately available');
})
Expand All @@ -1153,7 +1155,7 @@ testBoth('immediate observers watching multiple properties via brace expansion f
testBoth('immediate observers are for internal properties only', function(get, set) {
expectDeprecation(/Usage of `Ember.immediateObserver` is deprecated, use `Ember.observer` instead./);
expectAssertion(function() {
immediateObserver('foo.bar', function() { return this; });
_immediateObserver('foo.bar', function() { return this; });
}, 'Immediate observers must observe internal properties only, not properties on other objects.');
});

Expand Down
34 changes: 18 additions & 16 deletions packages/ember-runtime/lib/ext/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.Function) {
});
```

In the future this method may become asynchronous. If you want to ensure
synchronous behavior, use `observesImmediately`.
In the future this method may become asynchronous.

See `Ember.observer`.

Expand All @@ -110,6 +109,21 @@ if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.Function) {
return observer.apply(this, args);
};


FunctionPrototype._observesImmediately = function () {
Ember.assert('Immediate observers must observe internal properties only, ' +
'not properties on other objects.', function checkIsInternalProperty() {
for (var i = 0, l = arguments.length; i < l; i++) {
if (arguments[i].indexOf('.') !== -1) {
return false;
}
}
return true;
});

// observes handles property expansion
return this.observes(...arguments);
};
/**
The `observesImmediately` extension of Javascript's Function prototype is
available when `Ember.EXTEND_PROTOTYPES` or
Expand All @@ -134,22 +148,10 @@ if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.Function) {

@method observesImmediately
@for Function
@deprecated
@private
*/
FunctionPrototype.observesImmediately = function () {
Ember.assert('Immediate observers must observe internal properties only, ' +
'not properties on other objects.', function checkIsInternalProperty() {
for (var i = 0, l = arguments.length; i < l; i++) {
if (arguments[i].indexOf('.') !== -1) {
return false;
}
}
return true;
});

// observes handles property expansion
return this.observes(...arguments);
};
FunctionPrototype.observesImmediately = Ember.deprecateFunc('Function#observesImmediately is deprecated. Use Function#observes instead', FunctionPrototype._observesImmediately);

/**
The `observesBefore` extension of Javascript's Function prototype is
Expand Down