Skip to content

Commit

Permalink
Merge pull request #1179 from shawnmcknight/fix-lifecycle-types
Browse files Browse the repository at this point in the history
Improve lifecycle handler types
  • Loading branch information
shawnmcknight authored Jan 27, 2023
2 parents 07467b0 + 040c3d4 commit 16dbe71
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,7 @@ declare namespace Moleculer {
generateSnapshot(): HistogramMetricSnapshot[];

static generateLinearBuckets(start: number, width: number, count: number): number[];
static generateExponentialBuckets(
start: number,
factor: number,
count: number
): number[];
static generateExponentialBuckets(start: number, factor: number, count: number): number[];
}

namespace MetricTypes {
Expand Down Expand Up @@ -584,7 +580,7 @@ declare namespace Moleculer {
call<TResult, TParams>(
actionName: string,
params: TParams,
opts?: CallingOptions,
opts?: CallingOptions
): Promise<TResult>;

mcall<T>(
Expand Down Expand Up @@ -721,7 +717,11 @@ declare namespace Moleculer {
version?: string | number;
}

type StartedStoppedHandler = () => Promise<void[]> | Promise<void> | void;
type ServiceSyncLifecycleHandler<S = ServiceSettingSchema> = (this: Service<S>) => void;
type ServiceAsyncLifecycleHandler<S = ServiceSettingSchema> = (
this: Service<S>
) => void | Promise<void>;

interface ServiceSchema<S = ServiceSettingSchema> {
name: string;
version?: string | number;
Expand All @@ -734,9 +734,9 @@ declare namespace Moleculer {
hooks?: ServiceHooks;

events?: ServiceEvents;
created?: (() => void) | (() => void)[];
started?: StartedStoppedHandler | StartedStoppedHandler[];
stopped?: StartedStoppedHandler | StartedStoppedHandler[];
created?: ServiceSyncLifecycleHandler<S> | ServiceSyncLifecycleHandler<S>[];
started?: ServiceAsyncLifecycleHandler<S> | ServiceAsyncLifecycleHandler<S>[];
stopped?: ServiceAsyncLifecycleHandler<S> | ServiceAsyncLifecycleHandler<S>[];

[name: string]: any;
}
Expand Down Expand Up @@ -967,6 +967,9 @@ declare namespace Moleculer {
options?: GenericObject;
}

type BrokerSyncLifecycleHandler = (broker: ServiceBroker) => void;
type BrokerAsyncLifecycleHandler = (broker: ServiceBroker) => void | Promise<void>;

interface BrokerOptions {
namespace?: string | null;
nodeID?: string | null;
Expand Down Expand Up @@ -1030,9 +1033,9 @@ declare namespace Moleculer {
ContextFactory?: typeof Context;
Promise?: PromiseConstructorLike;

created?: (broker: ServiceBroker) => void;
started?: (broker: ServiceBroker) => void;
stopped?: (broker: ServiceBroker) => void;
created?: BrokerSyncLifecycleHandler;
started?: BrokerAsyncLifecycleHandler;
stopped?: BrokerAsyncLifecycleHandler;

/**
* If true, process.on("beforeExit/exit/SIGINT/SIGTERM", ...) handler won't be registered!
Expand Down

0 comments on commit 16dbe71

Please sign in to comment.