Skip to content

Latest commit

 

History

History
70 lines (54 loc) · 1.89 KB

2.initialization.md

File metadata and controls

70 lines (54 loc) · 1.89 KB

Initialize a new instance

Instantiation

The plugandplay export is a function which accepts an option object with the properties:

  • args ([...any])
    Array of arguments passed to plugins when they are initialized as a function.
  • chain (any)
    Value returned by Plug-And-Play register function, default to the Plug-And-Play instance.
  • parent (plugandplay)
    Parent Plug and Play instance to inherit plugins, unless sorted with after and before, child hooks are executed before the parent hooks by default.
  • plugins ([plugin])
    List of plugins to be initially registered. Plugins can later be registered with the register function.

Note, all options are optional.

For example:

plugandplay({
  args: [config, state]
  plugins: [(config, state) => ({
    hooks: {
      'my:hook': () => {}
    }
  })]
  parent: parentPlugAndPlayInstance
})

Option args

The args option defines an array of arguments used to instantiate a plugin when it is defined as a function. Thus, only plugins defined as functions leverage this option.

It is commonly used to pass a configuration object or a state to your plugins.

Assuming you wish to pass a configuration to your plugins, in this case the print-config plugin, you may initialize your library with:

import { plugandplay } from 'plug-and-play'
import printConfig from "my-package/plugins/print-config.js"

const config = {
  'hero': 'gollum'
}

export default function engine () {
  const plugins = new plugandplay({
    args: [config]
    plugins: printConfig
  })
  // ...
}

Registered plugins, like the print-config plugin discoverable somewhere with my-package/plugins/print-config.js, receive the config object on instantiation:

export default function (config) {
  return {
    hooks: {
      'my:hook': () => {
        console.log(`Configuration is ${JSON.stringify(config)}`)
      }
    }
  }
}