-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
wagenet
wants to merge
4
commits into
ember-learn:main
Choose a base branch
from
wagenet:events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, maybe on(event, callbark) {
return this.#emitter.on(event, callbark);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Evented
, and providing a possible path away from itI 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
andEvented
are synchronousThere was a problem hiding this comment.
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.