Botmation Documentation

Injects

Injects

Botmation's pattern of composing BotAction's has support for injecting other dependencies to help complete various tasks.

Injecting dependencies is a good pattern for Unit-Testing as they can be mocked easily

BotAction Interface

Let's take a look at the actual BotAction Function interface:

interface BotAction<R = void|AbortLineSignal, I extends Array<any> = any[]> extends Function {
(page: Page, ...injects: I) : Promise<R>
}

See, there is a second param, after page called injects which, by default, is a spread array of any. It's optional, you don't have to use it. But, it's useful. All Assembly Lines supports the injects system.

If many BotAction's in a line need an object(s), value(s), service(s), etc. then inject them. Let's see how:

const service = new ServiceA()
const todaysDate = new Date()
await chain(
// ... actions
)(page, service, todaysDate)

The assembled BotAction's in the chain() will be called with service and todaysDate as the second and third parameters.

Inject()() BotAction

Now, what if you want to compose a line of actions, but with new injects? You can use the inject()() BotAction to pass in new injects. The higher level injects will be appended to the new ones.

A few BotAction's are composed with inject()() such as files()() and indexedDBStore()(). Those composed BotAction's inject specific injects from the first inject() call, and append the other injects from the higher context, for the assembled BotAction's.

Here's an example:

const generalService = new GeneralService()
const specialService = new SpecialService()
await chain(
inject(specialService)(
goTo('http://example.com/special-page.html'),
doSomethingSpecial()
// these actions have `specialService` as their 1st inject
// and `generalService` as their 2nd inject
),
log('some message') // this log() BotAction will NOT have `specialService` injected
// but, will still have `generalService` as its 1st inject
)(page, generalService)

This gives granular control of what's passed in the spreaded injects array for assembled BotAction's in various scopes.

Edit this page on GitHub
Baby Bot