-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Clean Up Workspace Event Subscription API #3418
Conversation
This branch uses EventKit, an ultra-simple library for implementing events. The object implementing the methods maintains its own emitter object rather than doing a mixin like Emissary encourages. This will make it easier for us to deprecate ::on on the object itself. Unlike emissary, the EventKit Emitter implements a super minimalistic API that only allows one value to be emitted and always returns a Disposable from subscriptions.
These have behavior semantics, invoking the observer immediately with the current value of the observed property.
This eliminates our reliance on the Sequence object for informing us of changes
Now that emissary’s Subscriber no longer conflicts with the name
I like this a lot a lot. ❤️ Excited about event kit. I was seeing emissary as a major source of garbage while profiling fnr. Event DeprecationsThere are a few events on How are we going to deprecate them? We can provide an override API ConventionsA note for later: the API conventions you've written should go in the api-guidelines doc when this is merged. Really good things! |
f499721
to
877fa40
Compare
|
||
# Public: Invoke the given callback with all current and future items. | ||
# | ||
# * `callback` {Function} to be called with current and future items. |
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.
Did you want to do the param(s) to this callback?
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.
✔️
This allows this package to be upgraded on master if needed before we merge atom/atom#3418.
We can handle this through ::onDidAddTextEditor when the copy is added back to the pane.
Conflicts: docs/your-first-package.md
Clean Up Workspace Event Subscription API
In #3378 I discussed the idea of integrating RxJS observables for our event system. But in the interest of getting to the API freeze faster, I think we should avoid pulling a complex framework into core at this time. This PR focuses on simplifying and clarifying the events API without introducing observables, but the proposed changes are compatible with adding RxJS observables in the future.
The goal of this PR is to reform all the event APIs to use methods instead of strings for all the objects involved in everything in workspace.
Tasks
::on
API Conventions
::on
..dispose()
on the returned disposable the subscription is cancelled.onDid[Verb][Noun]
oronWill[Verb][Noun]
:onDidActivate
onDidAddItem
onWillDestroyItem
onDidChange[PropertyName]
:onDidChangeActiveItem
observe[PropertyName]
:observeActiveItem
observe[CollectionName]
, which will invoke the callback with all current and future elements:observePanes
Usage Guidelines
I've created a simple npm called event-kit. The goal is to provide a few building blocks for implementing and consuming evented APIs while maintaining maximal simplicity.
Implementing Evented APIs
We want to move away from mixins and toward composition. Toward that end,
event-kit
exports a simpleEmitter
object that can be used to streamline evented APIs. Rather than mixing it in, you construct anEmitter
instance and use it internally. Here's a sample of how you use it:This is similar to the existing emitter from emissary, but it's maximally simple and omits several features such as namespaces and support for multiple event arguments. The
::on
method also returnsDisposable
instances which unsubscribe when.dispose()
is called on them.Using emitters via instances instead of mixins ensures users won't call
::on
directly and gives us leverage for deprecating existing direct usages of::on
.If needed,
Disposable
can be used directly. You build a disposable with a function that is called when the disposable is disposed. This intentionally follows the same API as RxJS.Consuming Evented APIs
We want to encourage users to dispose of subscriptions when they are done with them, and we now think the best way to do this is to make it explicit. So now rather than using the
Subscriber
mixin, we will encourage users to build their ownCompositeDisposable
instance and add subscriptions to it.Users will need to remember to dispose of the subscriptions, but we think that making it more explicit will help clarify what is going on in examples and increase the likelihood that package authors clean up.