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

Improve lifecycle handler types #1179

Merged
merged 3 commits into from
Jan 27, 2023

Conversation

shawnmcknight
Copy link
Member

📝 Description

This PR improves the Service and ServiceBroker lifecycle handler type definitions in the following ways:

Service Lifecycle Handlers

  1. The service lifecycle methods are called with a this value of the Service. The type was updated to have a this value of the Service.
  2. The created handler was not consistent in structure with the started and stopped handlers. The created handler was defined inline while the started and stopped handlers were aliased. This was adjusted so that both are now aliased.
  3. The StartedStoppedHandler type was rather specific in name, so to make it more generic it was renamed to ServiceAsyncLifecycleHandler. The counterpart for the created handler was then a ServiceSyncLifecycleHandler. These can then be reused as necessary without any naming confusion.
  4. A recent PR (Improves StartedStoppedHandler definition to return Promise with void[] #1173) had adjusted the previously named StartedStoppedHandler to allow returning Promise<void[]>. However, that is not the intent of these lifecycle methods -- they should return void | Promise<void>. Any other type being returned is not appropriate, including Promise<void[]>. The intent was to allow a return of Promise.all[...] but the preferred approach would be something like:
async started() {
  await Promise.all([...]);
  // implicit return of void
}

Broker Lifecycle Handlers

The main issue with the broker lifecycle handlers is that the started and stopped handlers do not allow returning Promise<void> and are expecting a return of void. Changes have been made to address that deficiency as well as make the types used consistent with the changes that were just made to the service lifecycle handlers.

Other Changes

Any other changes were unintentional and only a result of prettier auto-formatting making adjustments.

💎 Type of change

  • Bug fix (non-breaking change which fixes an issue)

🚦 How Has This Been Tested?

Created a test repo and set it up to work with these type changes. Confirmed the following:

  1. The service broker started and stopped handlers now allow passing a Promise returning function.
  2. The service created, started, and stopped handlers now infer this as the Service type rather than the ServiceSchema type.

🏁 Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have commented my code, particularly in hard-to-understand areas

@icebob
Copy link
Member

icebob commented Jan 24, 2023

ping @danieledraganti

@danieledraganti
Copy link
Contributor

@icebob fine by me; I proposed that only because I found that form (return Promise.all(...)) in the documentation somewhere when I set the service up first. What's proposed by @shawnmcknight makes sense; it should return the state of the event.

@shawnmcknight
Copy link
Member Author

I made one additional tweak to ensure that the generic for the settings schema type was passed through to the lifecycle handlers.

@shawnmcknight
Copy link
Member Author

And one more tweak to fix my previous tweak that caused CI failures.

@shawnmcknight shawnmcknight merged commit 16dbe71 into moleculerjs:master Jan 27, 2023
@shawnmcknight shawnmcknight deleted the fix-lifecycle-types branch January 27, 2023 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants