-
-
Notifications
You must be signed in to change notification settings - Fork 588
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
feat: Expand the type for ServiceSchema to allow for typed lifecycle handlers #1272
feat: Expand the type for ServiceSchema to allow for typed lifecycle handlers #1272
Conversation
…handlers See more in this PR: moleculerjs/moleculer-template-project-typescript#70
The tests use the latest template from https://github.com/moleculerjs/moleculer-template-project-typescript The service also provides the types for the lifecycle methods which now call the logger to demostrate that correct object is passed.
@icebob , Apologies for any confusion! I updated the test/typescript/hello-world/greeter.service.ts (i.e. service schema) to the one used in the typescript template repository, since I couldn't find any in-code uses of the hooks or any tests related to that schema. I must be missing something, most likely the actual test that use this code. Could you please point me to the tests you're referring to? I tried running all the tests specified in package.json which all pass fine (with the exception of memory leak tests, which I am guessing are unrelated). |
I've checked again the code and it looks the test can be fine, but you removed a lot of things from the original greeter service, e.g. the hooks which uppercaseing the context params. it would be good if you keep the same logic in the service file. If you should modify the test files, it means the changes contains breaking changes. |
@icebob You're absolutely right, sorry for that! I reverted the deletions and tweaked them to comply with the service definition. I also added the implementation for the |
Great, thanks! |
@marceliwac I've just updated moleculer versions and I'm encountering type errors as a result of this change. Can you clarify why the |
Refer moleculerjs/moleculer#1272 for more detail
Apologies for the late reply and thanks for bringing this up! You're right, these should in fact use type ServiceSyncLifecycleHandler<T> = (this: T) => void;
type ServiceAsyncLifecycleHandler<T> = (this: T) => void | Promise<void>;
interface ServiceSchema<S = ServiceSettingSchema, T = Service<S>> {
name: string;
version?: string | number;
settings?: S;
dependencies?: string | ServiceDependency | (string | ServiceDependency)[];
metadata?: any;
actions?: ServiceActionsSchema;
mixins?: Partial<ServiceSchema>[];
methods?: ServiceMethods;
hooks?: ServiceHooks;
events?: ServiceEvents;
created?: ServiceSyncLifecycleHandler<T> | ServiceSyncLifecycleHandler<T>[];
started?: ServiceAsyncLifecycleHandler<T> | ServiceAsyncLifecycleHandler<T>[];
stopped?: ServiceAsyncLifecycleHandler<T> | ServiceAsyncLifecycleHandler<T>[];
[name: string]: any;
} Ideally, if lifecycle handlers were nested within a separate property there would be a much cleaner solution that uses type ServiceSyncLifecycleHandler = () => void;
type ServiceAsyncLifecycleHandler = () => void | Promise<void>;
type ServiceLifecycle<T> = {
created?: ServiceSyncLifecycleHandler | ServiceSyncLifecycleHandler[];
started?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[];
stopped?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[];
};
interface ServiceSchema<S = ServiceSettingSchema, T = Service<S>> {
name: string;
version?: string | number;
settings?: S;
dependencies?: string | ServiceDependency | (string | ServiceDependency)[];
metadata?: any;
actions?: ServiceActionsSchema;
mixins?: Partial<ServiceSchema>[];
methods?: ServiceMethods;
hooks?: ServiceHooks;
events?: ServiceEvents;
lifecycle?: ServiceLifecycle & ThisType<T>;
[name: string]: any;
} But because the lifecycle handlers are direct properties of the service, this makes things slightly messier. type ServiceSyncLifecycleHandler = () => void;
type ServiceAsyncLifecycleHandler = () => void | Promise<void>;
type ServiceLifecycle<T> = {
created?: ServiceSyncLifecycleHandler | ServiceSyncLifecycleHandler[];
started?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[];
stopped?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[];
} & ThisType<T>;
interface ServiceSchema<S = ServiceSettingSchema, T = Service<S>> extends ServiceLifecycle<T> {
name: string;
version?: string | number;
settings?: S;
dependencies?: string | ServiceDependency | (string | ServiceDependency)[];
metadata?: any;
actions?: ServiceActionsSchema;
mixins?: Partial<ServiceSchema>[];
methods?: ServiceMethods;
hooks?: ServiceHooks;
// `created`, `started` and `stopped` are defined, but don't have the correct type for `this`
events?: ServiceEvents;
[name: string]: any;
} On a side note, I also think that the generic @icebob Any thoughts on this? Should I submit a PR? |
I have no idea due to lack of TS experience. @shawnmcknight ? |
what @marceliwac suggested would resolve our issues with the current version |
@shawnmcknight, @icebob, @MichaelErmer I've pushed the PR to address this. I would appreciate a second pair of eyes sanity checking it if you could find some time. Hopefully, the added TS tests should prevent similar issues in the future. I'm not 100% sure how to verify that |
This change expands the typings for the ServiceSchema, allowing it to accept the "service-this" type used by the lifecycle handlers in the typescript template. Please see this issue for a more complete explanation.
📝 Description
Change the type of the ServiceSchema to accept two generic types, latter of which is new and used to provide types for the lifecycle methods (created, started, stopped).
The typescript template will require a minor change if this change is accepted.
🎯 Relevant issues
moleculer-template-project-typescript #70
💎 Type of change
🚦 How Has This Been Tested?
The example code to verify that this change works is outlined in moleculer-template-project-typescript #70.
🏁 Checklist: