-
Notifications
You must be signed in to change notification settings - Fork 18
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
Feature: Events #76
Feature: Events #76
Conversation
Thanks for this first draft ! I believe we can use the "new" https://developer.mozilla.org/en-US/docs/Web/API/EventTarget API instead of implementing our own class ! I also think we should have both Regarding adding code to the injector, it's fine to get it working first, and then we will probably want to move that logic outside of Danet core and implement it in its own module. But that will require change in Danet so let's first focus on making this work ! |
Sounds great @Sorikairox , I'll work on these changes 👍🏼 |
@Sorikairox changes done! I'm also planning to add some test cases, but I'm not sure if I'll have the time 'til the weekend |
a753b28
to
e50a7c3
Compare
@marco-souza Thanks for your efforts! To make this (and the future Queue/Pubsub module) standalone, I need to make Danet's injector a singleton, modify modules handling (instantiate them instead of relying on their constructor type), allow us to use a similar pattern to |
@Sorikairox Nice, thank you for taking care of that! Let me know when you have it so I can rebase and adjust this PR. By the way, do you think the |
@marco-souza Yeah it looks good, if it works, then go for it ! ❤️ |
@marco-souza Main branch now contains (if I did that well) everything required to make it standalone. import { injector } from 'danet/mod.ts'
function registerAvailableEventListeners(injectableInstance: any) {
const methods = Object.getOwnPropertyNames(Type.prototype);
for (const method of methods) {
const target = Type.prototype[method];
const eventListenerMedatada = MetadataHelper.getMetadata<
{ channel: string }
>(
eventListenerMetadataKey,
target,
);
if (!eventListenerMedatada) continue;
const { channel } = eventListenerMedatada;
const emmiter = this.resolved.get(EventEmitter)?.() as EventEmitter;
emmiter.subscribe(channel, target);
}
}
@Module({
injectables: [EventEmitter],
})
export class EventEmitterModule implements OnAppBoostrap {
onAppBoostrap() {
const injectables = injector.injectables;
for (const injectable of injectables) {
registerAvailableEventListeners(injectable);
}
}
} So you should be able to remove the code from the injector, and once it works, we can take everything to its own repository 😄 |
Sidenote, I noticed that there is a typo on |
Co-authored-by: Thomas Cruveilher <38007824+Sorikairox@users.noreply.github.com>
- separate module and service file
@Sorikairox changes done! I had to expose Let me know what you think about this approach. |
@Sorikairox tests and docs added! 🎉 I think now I can move this PR to "ready for review" |
Codecov ReportAttention:
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #76 +/- ##
==========================================
+ Coverage 85.84% 86.65% +0.80%
==========================================
Files 47 54 +7
Lines 1561 1663 +102
Branches 163 176 +13
==========================================
+ Hits 1340 1441 +101
- Misses 219 220 +1
Partials 2 2 ☔ View full report in Codecov by Sentry. |
I managed to make it work with const methods = Object.getOwnPropertyNames(injectableInstance.constructor.prototype); |
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.
Submitted code change to use injectables !
Awesome @Sorikairox, thank you for the changes! 🙏🏼 I didn't know we could use |
Description
This Pull Request provides a simple observer implementation, allowing you to subscribe and listen for various events that occur in your application. Events serve as a great way to decouple various aspects of your application, since a single event can have multiple listeners that do not depend on each other.
This PR implements
EventEmitter
module fordanet
, trying to keep a similar interface with the Nest.JS events.Issue Ticket Number
Relates to
Type of change
not work as expected)
Checklist:
deno lint
ANDdeno fmt
ANDdeno task test
and got noerrors.
in CONTRIBUTING.md
Pull Requests for the same
update/change?