Avalander / yuki

1 stars 0 forks source link

Modules #6

Open Avalander opened 4 years ago

Avalander commented 4 years ago

I've been thinking how to handle commands that need to do some asynchronous initialization and maybe a module system could be a good thing to have in place for that. Plus, it would provide a standard way to group commands with related functionality.

A quick draft of the API for a module could look like this:

Option A

module.exports.init = ({ client, settings, memory, store }) => {
  // Do some asynchronous initialization.
  // Return a Promise to verify that everything went well.
}

module.exports.commands = [
  // It makes sense to use modules to group related commands, so we expect this to be a list.
  (text, message, { client, settings, memory }) => {
    // Maybe we can send some things only to the `init` function, like the `client` object.
    // ...
  }
]

module.exports.shutdown = () => {
  // Do some potentially asynchronous clean up
}

Only the commands property would be required, init and shutdown can be implemented only when we need to do some initialization and/or clean up.

Option B

Another option is to have the function init return the list of commands. Than way, we could grab de client, settings and other things from there instead of having to send it as a third argument to the command. Then, the property commands would only be required when the function init is not implemented.

module.exports.init = ({ client, settings, memory, store }) => {
  // Do initialization
  // Returns either a list of commands or a Promise that resolves to a list of commands.
  return [
    (text, message) => { ... }
  ]
}

module.exports.shutdown = () => {
  // Optional function for clean up. Implementation is not required.
}

Option C

Or we might as well have the module export a factory function that would do any required initialization and return/resolve a list of commands and potentially a shutdown function.

module.exports = ({ client, settings, memory, store }) => {
  // Do initialization
  // Return or resolve
  return {
    // The property commands is required.
    commands: [
      (text, message) => { ... }
    ],
    // The property shutdown is optional.
    shutdown: () => { ... }
  }
}
Avalander commented 4 years ago

A couple other things we need to consider:

Avalander commented 4 years ago

@SavagePixie do you have any thoughts on this?

arturjzapater commented 4 years ago
Avalander commented 4 years ago

I'm also leaning more towards option C, the more I think about it. It feels a bit awkward that the commands and shutdown function are accessible before initialising the module.