Skip to content
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] New @OnEvent() decorator #17

Closed
hakimio opened this issue Jan 2, 2024 · 9 comments · Fixed by #18
Closed

[FEATURE] New @OnEvent() decorator #17

hakimio opened this issue Jan 2, 2024 · 9 comments · Fixed by #18
Assignees
Labels
type: feature request New feature or request

Comments

@hakimio
Copy link

hakimio commented Jan 2, 2024

Description

Let's say you are emitting custom event with srv.emit('OrderedBook', someData) and you'd like to add a handler for this event. Right now you have to subscribe to the event manually by writing code like the following:

@ServiceLogic()
class MyService {
  
  @Inject(SRV) 
  private readonly srv: Service;

  constructor() {
    this.srv.on('OrderedBook', data => this.onOrderedBook(data));
  }
  
  onOrderedBook(data: OrderedBookData) {
    // TODO: implement me
  }
}

It would be nice if there was a decorator like @OnEvent('OrderedBook') which would reduce the amount of boilerplate code you need to write to subscribe to custom events.

Suggested solution

@ServiceLogic()
class MyService {
  
  @OnEvent('OrderedBook')
  onOrderedBook(data: OrderedBookData) {
    // TODO: implement me
  }
}
@hakimio hakimio added the type: feature request New feature or request label Jan 2, 2024
@hakimio hakimio changed the title [FEATURE] New "OnEvent" decorator [FEATURE] New OnEvent() decorator Jan 2, 2024
@hakimio hakimio changed the title [FEATURE] New OnEvent() decorator [FEATURE] New @OnEvent() decorator Jan 2, 2024
@dragolea
Copy link
Contributor

dragolea commented Jan 2, 2024

Hi Thomas,

Great new feature, I am currently on vacation till 15th of January, after my return I will start working on this new decorator.

Happy new year,
Daniel

@dragolea dragolea self-assigned this Jan 2, 2024
@hakimio
Copy link
Author

hakimio commented Jan 3, 2024

No worries, Daniel.
Have a great vacation and happy new year 🙂

@dragolea
Copy link
Contributor

Hi Thomas,
@OnEvent is ready.

https://github.com/dxfrontier/cds-ts-dispatcher/tree/v0.1.20?tab=readme-ov-file#onevent - docu
AND
https://github.com/dxfrontier/cds-ts-dispatcher/tree/v0.1.20?tab=readme-ov-file#unboundactions - where it should be placed.

@hakimio

@dragolea dragolea linked a pull request Jan 16, 2024 that will close this issue
@hakimio
Copy link
Author

hakimio commented Jan 16, 2024

Thank you @dragolea
Looks good 🙂

@hakimio
Copy link
Author

hakimio commented Jan 16, 2024

@dragolea on related note: would it be possible to use @OnEvent() to handle CAP special error event? Or should there be specialized @OnError() decorator?
cap-js/cds-types#9

@dragolea
Copy link
Contributor

dragolea commented Jan 16, 2024

I remember I tried to implement it at the beginning of this project but I was not able to find an overload for the .on error

Below we see all the overloads of the .on handler

  on<T extends Constructable>(eve: types.event, entity: T, handler: CRUDEventHandler.On<InstanceType<T>, InstanceType<T> | void | Error>): this
  on<F extends CdsFunction>(boundAction: F, service: string, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
  on<F extends CdsFunction>(unboundAction: F, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
  on(eve: types.event, entity: types.target, handler: OnEventHandler): this
  on(eve: types.event, handler: OnEventHandler): this

I remember I've tried to see if the last overload is working but something didn't. ( on(eve: types.event, handler: OnEventHandler): this)

I think you're referring to this error handler https://cap.cloud.sap/docs/node.js/core-services#srv-on-error

Were you able to find the overload in TS for this error handler?

@hakimio
Copy link
Author

hakimio commented Jan 16, 2024

It's coming soon to TS. The new types were just merged last week: cap-js/cds-types#9

@sblessing
Copy link
Contributor

@hakimio @dragolea: Lets open a new issue refererring to that PR. We'll get a release notification once its available.

@dragolea
Copy link
Contributor

dragolea commented Jan 16, 2024

@hakimio feel free to create a new issue for @OnError decorator
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants