Skip to content

Add deprecation guide for Ember.Evented and @ember/object/events #1404

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
138 changes: 138 additions & 0 deletions content/ember/v6/evented.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---
title: Ember.Evented and @ember/object/events
until: 7.0.0
since: 6.6.0
---

The `Ember.Evented` mixin, the underlying `@ember/object/events` module (`addListener`, `removeListener`, `sendEvent`), and the `on()` function from `@ember/object/evented` are all deprecated.

These APIs provided a way for Ember objects to send and receive events. With modern JavaScript features and patterns, we recommend more explicit and standard approaches. For eventing, we recommend refactoring to use a modern asynchronous library like [emittery](https://www.npmjs.com/package/emittery) or (if you need to preserve synchronous semantics) a library such as [`nanoevents`](https://www.npmjs.com/package/nanoevents) or [`mitt`](https://www.npmjs.com/package/mitt).

> ⚠️ Important: `Ember.Evented` emits events *synchronously*. Changing to a library with asynchronous behavior, while recommended, may lead to subtle changes in your application's behavior.

Please note: The methods from `Evented` (`on`, `one`, `off`, `trigger`, `has`) were also available on `Ember.Component`, `Ember.Route`, and `Ember.Router`. While usage on these objects is deprecated, the methods will continue to be supported and not deprecated on the `RouterService`, since key parts of its functionality are difficult to reproduce without them.

### Replacing `Evented` with `emittery`

Choose a reason for hiding this comment

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

Oof, for any application at scale this is going to be a pretty huge change. Switching from synchronous events to asynchronous events is almost certain to not just be a drop-in replacement like this, and this deprecation guide doesn't mention that at all.

It looks like this deprecation guide is simultaneously doing two things:

  1. Describing the deprecation of Evented, and providing a possible path away from it
  2. Recommending that users make the architectural change of switching from synchronous event emitting to asynchronous event emitting.

I think these really need to be called out separately. Users should be aware that if they just follow this deprecation guide mechanically they'll be changing timing characteristics of their application in ways that can (and in many cases almost certainly will) break things. So I think we need to be more explicit that we're both recommending a new library to use and the change from synchronous to asynchronous events, and ideally offer up a recommendation of a synchronous eventing library for users that aren't ready to make that change but still need to deal with this deprecation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this callout. I would have thought existing events are async via the runloop. However, if we're sure this isn't the case, then we can definitely call it out in this guide.

Choose a reason for hiding this comment

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

Observers are async by default, @ember/object/events and Evented are synchronous

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 updated the guides to make this clear.


First, add `emittery` to your project:
```bash
npm install --save-dev emittery
# or
pnpm add --save-dev emittery
```

Here is an example of a session service that used `Evented`:

#### Before
```javascript
// app/services/session.js
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { tracked } from '@glimmer/tracking';

export default class SessionService extends Service.extend(Evented) {
@tracked user = null;

login(userData) {
this.user = userData;
this.trigger('loggedIn', userData);
}

logout() {
const oldUser = this.user;
this.user = null;
this.trigger('loggedOut', oldUser);
}
}
```

A consumer might use it like this:

```javascript
// app/components/some-component.js
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { registerDestructor } from '@ember/destroyable';

export default class SomeComponent extends Component {
@service session;

constructor(owner, args) {
super(owner, args);
this.session.on('loggedIn', this, 'handleLogin');

registerDestructor(this, () => {
this.session.off('loggedIn', this, 'handleLogin');
});
}

handleLogin(user) {
console.log(`User logged in: ${user.name}`);
// ... update component state
}
}
```

After refactoring to use `emittery`, the service manages its own event emitter and provides clear methods for subscribing.

#### After
```javascript
// app/services/session.js
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import Emittery from 'emittery';

export default class SessionService extends Service {
@tracked user = null;

#emitter = new Emittery();

login(userData) {
this.user = userData;
this.#emitter.emit('loggedIn', userData);
}

logout() {
const oldUser = this.user;
this.user = null;
this.#emitter.emit('loggedOut', oldUser);
}

// Public subscription methods
Copy link
Contributor

@NullVoxPopuli NullVoxPopuli Jun 23, 2025

Choose a reason for hiding this comment

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

also, maybe

on(event, callbark) {
  return this.#emitter.on(event, callbark);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

but what you have is easier to be type-safe

onLoggedIn(callback) {
return this.#emitter.on('loggedIn', callback);
}

onLoggedOut(callback) {
return this.#emitter.on('loggedOut', callback);
}
}
```

The listening object can then use `registerDestructor` from `@ember/destroyable` to tie the subscription's lifetime to its own. This removes the need for a `willDestroy` hook and manual cleanup.

```javascript
// app/components/some-component.js
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { registerDestructor } from '@ember/destroyable';

export default class SomeComponent extends Component {
@service session;

constructor(owner, args) {
super(owner, args);

const unsubscribe = this.session.onLoggedIn((user) => {
this.handleLogin(user);
});

registerDestructor(this, unsubscribe);
}

handleLogin(user) {
console.log(`User logged in: ${user.name}`);
// ... update component state
}
}
```