strvcom / atlas.js

A component-based Node.js library to reduce boilerplate and provide sane project structure 🍻
BSD 3-Clause "New" or "Revised" License
106 stars 9 forks source link

Support stateless components #53

Open robertrossmann opened 6 years ago

robertrossmann commented 6 years ago

In many situations, a Hook or Action could be completely stateless (just a bunch of functions), and so the requirement to write such components as classes becomes unnecessary and even counter-intuitive.

If a component is to be shared between multiple possible instances of Atlas in the same process then the component must be written as a class, otherwise it's not clear how to get hold of the correct instance of Atlas class which should be used to handle the request or where to get the reference to other components.

For all other use cases (single-Atlas-instance apps, 95% of normal use cases), one can do the following:

// path to project root
import atlas from '..'
// example path to a place where other stateless components exist
import { notifier } from '.'

// This is a stateless action component
export default {
  async createAccount(info) {
    // Example how to access other components from this component
    const user = await atlas.services.database.users.insert(info)
    // Example how to use other stateless components
    await notifier.sendEmail('welcome-message', user.email, user)
  }
}
robertrossmann commented 6 years ago

@thunderball1 Is the code example above somewhat similar to what you are after?