Skip to content

Commit

Permalink
fix registry typing
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 30, 2023
1 parent 73fb42e commit 1e473a1
Show file tree
Hide file tree
Showing 19 changed files with 412 additions and 94 deletions.
21 changes: 20 additions & 1 deletion src/registry/action-catalog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import EndpointList = require("./endpoint-list");
import BrokerNode = require("./node");
import ServiceItem = require("./service-item");

import ServiceBroker = require("../service-broker");
import Registry = require("./registry");
import Strategy = require("../strategies/base");
import ActionEndpoint = require("./endpoint-action");

declare namespace ActionCatalog {
export interface ActionCatalogListOptions {
onlyLocal?: boolean;
Expand All @@ -12,17 +17,31 @@ declare namespace ActionCatalog {
withEndpoints?: boolean;
}

interface ActionEndpointList {
nodeID: string,
state: boolean,
available: boolean,
}

export interface ActionCatalogListResult {
name: string;
count: number;
hasLocal: boolean;
available: boolean;
action?: Omit<ActionSchema, "handler" | "remoteHandler" | "service">;
endpoints?: Pick<Endpoint, "id" | "state">[];
endpoints?: ActionEndpointList[];
}
}

declare class ActionCatalog {
registry: Registry;
broker: ServiceBroker;
actions: Map<string, any>;
StrategyFactory: Strategy;
EndpointFactory: typeof ActionEndpoint;

constructor(registry: Registry, broker: ServiceBroker, StrategyFactory: Strategy);

add(node: BrokerNode, service: ServiceItem, action: ActionSchema): EndpointList;

get(actionName: string): EndpointList | undefined;
Expand Down
35 changes: 25 additions & 10 deletions src/registry/action-catalog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand All @@ -11,18 +11,32 @@ const Strategies = require("../strategies");
const EndpointList = require("./endpoint-list");
const ActionEndpoint = require("./endpoint-action");

/**
* Import types
*
* @typedef {import("./action-catalog")} ActionCatalogClass
* @typedef {import("./action-catalog").ActionCatalogListOptions} ActionCatalogListOptions
* @typedef {import("./action-catalog").ActionCatalogListResult} ActionCatalogListResult
* @typedef {import("./registry")} Registry
* @typedef {import("./node")} Node
* @typedef {import("./service-item")} ServiceItem
* @typedef {import("../service").ActionSchema} ActionSchema
* @typedef {import("../service-broker")} ServiceBroker
* @typedef {import("../strategies/base")} BaseStrategy
*/
/**
* Catalog class to store service actions
*
* @class ActionCatalog
* @implements {ActionCatalogClass}
*/
class ActionCatalog {
/**
* Creates an instance of ActionCatalog.
*
* @param {Registry} registry
* @param {ServiceBroker} broker
* @param {Strategy} StrategyFactory
* @param {BaseStrategy} StrategyFactory
* @memberof ActionCatalog
*/
constructor(registry, broker, StrategyFactory) {
Expand All @@ -41,7 +55,8 @@ class ActionCatalog {
*
* @param {Node} node
* @param {ServiceItem} service
* @param {Action} action
* @param {ActionSchema} action
* @returns {EndpointList}
* @memberof ActionCatalog
*/
add(node, service, action) {
Expand Down Expand Up @@ -74,7 +89,7 @@ class ActionCatalog {
/**
* Get action by name
*
* @param {String} actionName
* @param {string} actionName
* @returns
* @memberof ActionCatalog
*/
Expand All @@ -85,8 +100,8 @@ class ActionCatalog {
/**
* Check the action is available (there is live endpoint)
*
* @param {String} actionName
* @returns {Boolean}
* @param {string} actionName
* @returns {boolean}
* @memberof ActionCatalog
*/
isAvailable(actionName) {
Expand All @@ -111,8 +126,8 @@ class ActionCatalog {
/**
* Remove action by name & nodeID
*
* @param {String} actionName
* @param {String} nodeID
* @param {string} actionName
* @param {string} nodeID
* @memberof ActionCatalog
*/
remove(actionName, nodeID) {
Expand All @@ -123,8 +138,8 @@ class ActionCatalog {
/**
* Get a filtered list of actions
*
* @param {Object} {onlyLocal = false, onlyAvailable = false, skipInternal = false, withEndpoints = false}
* @returns {Array}
* @param {ActionCatalogListOptions} {onlyLocal = false, onlyAvailable = false, skipInternal = false, withEndpoints = false}
* @returns {Array<ActionCatalogListResult>}
*
* @memberof ActionCatalog
*/
Expand Down
8 changes: 8 additions & 0 deletions src/registry/endpoint-action.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import ServiceBroker = require("../service-broker");
import Registry = require("./registry");
import Node = require("./node");

import Endpoint = require("./endpoint");
import type Service = require("../service");
import type { ActionSchema } from "../service";

declare class ActionEndpoint extends Endpoint {
service: Service;
action: ActionSchema;

constructor(registry: Registry, broker: ServiceBroker, node: Node, service: Service, action: any);

update(action: ActionSchema): void;
}
export = ActionEndpoint;
20 changes: 16 additions & 4 deletions src/registry/endpoint-action.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
/*
* moleculer
* Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

"use strict";

const Endpoint = require("./endpoint");

/**
* Import types
*
* @typedef {import("./registry")} Registry
* @typedef {import("../service-broker")} ServiceBroker
* @typedef {import("./endpoint-action")} ActionEndpointClass
* @typedef {import("./node")} Node
* @typedef {import("../service")} Service
* @typedef {import("../service").ActionSchema} ActionSchema
*/

/**
* Endpoint class for actions
*
* @class ActionEndpoint
* @extends {Endpoint}
* @implements {ActionEndpointClass}
*/
class ActionEndpoint extends Endpoint {
/**
* Creates an instance of ActionEndpoint.
* @param {Registry} registry
* @param {ServiceBroker} broker
* @param {Node} node
* @param {ServiceItem} service
* @param {any} action
* @param {Service} service
* @param {ActionSchema} action
* @memberof ActionEndpoint
*/
constructor(registry, broker, node, service, action) {
Expand All @@ -36,7 +48,7 @@ class ActionEndpoint extends Endpoint {
/**
* Update properties
*
* @param {any} action
* @param {ActionSchema} action
* @memberof ActionEndpoint
*/
update(action) {
Expand Down
12 changes: 10 additions & 2 deletions src/registry/endpoint-event.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import ServiceBroker = require("../service-broker");
import Registry = require("./registry");
import Node = require("./node");

import Endpoint = require("./endpoint");
import type Service = require("../service");
import type { EventSchema } from "../context";
import type { ServiceEvent } from "../service";

declare class EventEndpoint extends Endpoint {
service: Service;
event: EventSchema;
event: ServiceEvent;

constructor(registry: Registry, broker: ServiceBroker, node: Node, service: Service, event: ServiceEvent);

update(event: ServiceEvent): void;
}
export = EventEndpoint;
18 changes: 15 additions & 3 deletions src/registry/endpoint-event.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
/*
* moleculer
* Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

"use strict";

const Endpoint = require("./endpoint");

/**
* Import types
*
* @typedef {import("./registry")} Registry
* @typedef {import("../service-broker")} ServiceBroker
* @typedef {import("./endpoint-event")} EventEndpointClass
* @typedef {import("./node")} Node
* @typedef {import("../service")} Service
* @typedef {import("../service").ServiceEvent} ServiceEvent
*/

/**
* Endpoint class for events
*
* @class EventEndpoint
* @extends {Endpoint}
* @implements {EventEndpointClass}
*/
class EventEndpoint extends Endpoint {
/**
Expand All @@ -21,7 +33,7 @@ class EventEndpoint extends Endpoint {
* @param {ServiceBroker} broker
* @param {Node} node
* @param {Service} service
* @param {any} event
* @param {ServiceEvent} event
* @memberof EventEndpoint
*/
constructor(registry, broker, node, service, event) {
Expand All @@ -34,7 +46,7 @@ class EventEndpoint extends Endpoint {
/**
* Update properties
*
* @param {any} event
* @param {ServiceEvent} event
* @memberof EventEndpoint
*/
update(event) {
Expand Down
31 changes: 29 additions & 2 deletions src/registry/endpoint-list.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import BaseStrategy = require("../strategies/base");
import Node = require("./node");
import Endpoint = require("./endpoint");
import ActionEndpoint = require("./endpoint-action");
import EventEndpoint = require("./endpoint-event");
import ServiceItem = require("./service-item");
import { Registry, ServiceBroker } from "./event-catalog";
import Context = require("../context");

declare class EndpointList {
constructor(registry: Registry, broker: ServiceBroker, name: string, group: string, EndPointFactory?: typeof Endpoint, StrategyFactory?: BaseStrategy, strategyOptions?: Record<string, any>);

registry: Registry;
broker: ServiceBroker;
strategy: typeof BaseStrategy;
name: string;
group: string;
internal: boolean;
EndPointFactory: typeof Endpoint;
endpoints: (ActionEndpoint | EventEndpoint)[];
localEndpoints: (ActionEndpoint | EventEndpoint)[];

constructor(registry: Registry, broker: ServiceBroker, name: string, group: string, EndPointFactory?: typeof Endpoint, StrategyFactory?: typeof BaseStrategy, strategyOptions?: Record<string, any>);

add(node: Node, service: ServiceItem, data: any): Endpoint;
getFirst(): Endpoint | null;
select(list: Array<Endpoint>, ctx: Context): Endpoint | null;
next(ctx: Context): Endpoint | null;
nextLocal(ctx: Context): Endpoint | null;

hasAvailable(): boolean;
hasLocal(): boolean;
setLocalEndpoints(): void;
count(): number;

getEndpointByNodeID(nodeID: string): Endpoint | null;
hasNodeID(nodeID: string): boolean;
removeByService(service: ServiceItem): void;
removeByNodeID(nodeID: string): void;
}
export = EndpointList;
Loading

0 comments on commit 1e473a1

Please sign in to comment.