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

Documentation for tx2.action doesn't match PM2 documentation #10

Open
joeskeen opened this issue Feb 26, 2024 · 2 comments
Open

Documentation for tx2.action doesn't match PM2 documentation #10

joeskeen opened this issue Feb 26, 2024 · 2 comments

Comments

@joeskeen
Copy link

In the PM2 documentation it has an example of how to use tx2.action:

tx2.action('world', function(param, reply) {
  console.log(param)
  reply({success : param})
})

But this doesn't match any of the documented call signatures in the documentation for tx2:

tx2.action('run_query', (cb) => {
  cb({ success: true })
})
tx2.action('run_query', arg1, (cb) => {
  cb({ success: arg1 })
})

The TypeScript typings for this package, available in https://github.com/DefinitelyTyped/DefinitelyTyped/, only allow usage as documented:

    action(action_name: string, callback: (cb: (data: any) => void) => void): void;
    action<T extends object>(
        action_name: string,
        options: T,
        callback: (options: T, cb: (data: any) => void) => void,
    ): void;

(see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/tx2/index.d.ts#L36)

Thus this TypeScript code is invalid, even though when I run it as JS it works.

tx2.action(job.name, (param, reply) => { reply(param) });

The documentation and typings need to be updated if this is a supported use-case as the PM2 docs would suggest.

CC @sasial-dev

@joeskeen
Copy link
Author

joeskeen commented Feb 26, 2024

My current workaround is thus:

type Tx2Action = (name: string, handler: (param: any, reply: (result: any) => void) => void) => void;

const action = tx2.action.bind(tx2) as Tx2Action;

action(job.name, (param, reply) => {
  reply(param);
});

Those are some pretty nasty typings though... way too many nested function types. Perhaps something like this would be better:

type ActionCallback = (result: any) => void;
type ActionHandlerNoParam = (reply: ActionCallback) => void;
type ActionHandlerWithParam = (param: string, reply: ActionCallback) => void;
type ActionHandler = ActionHandlerNoParam | ActionHandlerWithParam;

export type Tx2Action = (name: string, handler: ActionHandler) => void;

@joeskeen
Copy link
Author

Also CC @zoernert - looks like your PR #7 may be related?

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

No branches or pull requests

1 participant