Deprecate Observers
Summary
Deprecate the observer helper from @ember/object and the addObserver and removeObserver functions from @ember/object/observers.
The Observable mixin and the methods it adds to EmberObject are already deprecated by RFC 1116 and RFC 1234.
Motivation
Observers have not been recommended since Octane replaced them with autotracking, and eslint-plugin-ember's no-observers rule has flagged them for years.
Removing them shrinks Ember's internals (see Exploration).
Transition Path
Calling a deprecated API logs a deprecation that targets until: '8.0.0'.
No API directly replaces observers. The fix depends on what the observer does, and the deprecation guide has an example of each:
| Observer used to… | Replace with |
|---|---|
| derive state from other state | a native getter over @tracked properties |
| perform a side effect | an explicit call where the change happens |
| react to classic component arguments | a function called from the template |
Ecosystem Impact
- eslint-plugin-ember already ships
no-observers, so no new lint rules are needed. - Several libraries use observers and need to migrate (see Library Usage).
- The published types for
@ember/object/observersand theobserverexport from@ember/objectare marked@deprecated.
How We Teach This
The current guides do not cover observers, so they need no changes. The work is:
- publish the deprecation guide below at deprecations.emberjs.com
- mark these APIs deprecated in the API docs, linking to the guide
- mention the deprecation in the release blog post
Deprecation Guide
deprecate(message, false, {
id: 'deprecate-ember-object-observers',
until: '8.0.0',
for: 'ember-source',
url: 'https://deprecations.emberjs.com/id/deprecate-ember-object-observers',
since: { available: '7.x', enabled: '7.x' },
});
What is deprecated
| | API | status |
| - | --- | ------ |
| 🌐 | observer from @ember/object | deprecated |
| 🌐 | addObserver / removeObserver from @ember/object/observers | deprecated |
The Observable mixin and its methods, including this.addObserver, are covered by the Mixins and EmberObject deprecations.
Migration
Observers that derive state
// before
import EmberObject, { observer } from '@ember/object';
const Person = EmberObject.extend({
firstName: null,
lastName: null,
fullName: null,
fullNameDidChange: observer('firstName', 'lastName', function () {
this.set('fullName', `${this.firstName} ${this.lastName}`);
}),
});
// after
import { tracked } from '@glimmer/tracking';
class Person {
@tracked firstName;
@tracked lastName;
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
The getter computes fullName when it is read, instead of storing a copy each time a dependency changes.
Observers that perform side effects
// before
import EmberObject, { observer } from '@ember/object';
const User = EmberObject.extend({
lastLogin: null,
lastLoginDidChange: observer('lastLogin', function () {
this.analytics.track('login', this.lastLogin);
}),
});
user.set('lastLogin', new Date());
// after
import { tracked } from '@glimmer/tracking';
class User {
@tracked lastLogin;
login() {
this.lastLogin = new Date();
this.analytics.track('login', this.lastLogin);
}
}
user.login();
Run the side effect where the change happens, not in a callback that watches for it.
Observers on component arguments
// before
import Component from '@ember/component';
import { observer } from '@ember/object';
export default Component.extend({
titleDidChange: observer('title', function () {
document.title = this.title;
}),
});
// after
function setTitle(title) {
document.title = title;
}
<template>
{{setTitle @title}}
</template>
The template calls setTitle again whenever @title changes.
Alternatively, wrap it in effect from reactiveweb so that it runs after rendering.
[!NOTE] Setting tracked state at all in an effect-like primitive is dangerous and can cause an infinite loop -- even if it's a slower infinite loop.
addObserver / removeObserver
// before
import { addObserver, removeObserver } from '@ember/object/observers';
addObserver(session, 'user', this, 'userDidChange');
removeObserver(session, 'user', this, 'userDidChange');
If the observer updates the UI, make user a @tracked property and read it in the template or in a getter.
The template re-renders when it changes, with no observer:
<template>
{{#if @session.user}}
<Dashboard @user={{@session.user}} />
{{else}}
<Login />
{{/if}}
</template>
If the observer runs code when the value changes, call that code where the value is set. You can also call it from the template that reads the value, as in the previous section.
Drawbacks
Code that reacted to a change from anywhere must now run either where the change happens or from a template that reads the value. Libraries that relied on observers for this need a new design.
Alternatives
- Do nothing. This keeps two reactivity systems in place indefinitely.
Unresolved questions
None.
Appendix
Exploration
emberjs/ember.js#20937, merged on Ember's next branch, removes the observer helper along with the inlined Observable methods, deleting 3,096 lines across 122 files.
Library Usage
These libraries use observers and need to migrate:
- ember-concurrency's
waitForProperty - ember-animated's task
.observes()modifier and<AnimatedEach> - EmberData's Inspector data adapter and its legacy compatibility layer, which forwards
addObserver/removeObserveron records