# Industry.js

Define factories from ES6 classes and immutably manipulate the class inheritance chain.

## Define the factory

Use Industry’s `factory` method to define a factory from an ES6 class:

```js
// hello.js
//
import { factory } from "industry"

class Hello {
  say() { return "hello" }
}

export factory(Hello)
```

## Use the factory

Import the factory and call it:

```js
// say_hello.js
//
import factory from "./hello"
factory() // undefined
```

## Nothing happened!

The factory function call returns undefined because we haven't told Industry what to do when we call this factory.

## Our first extension

Using the last example, let's create an extension that tells the factory function to return an instance of the class. We will also modify the return value of the `say` function.

```js
// hello.js
//
import { factory } from "industry"

class Hello {
  say() { return "world" }
}

let ext = Class =>
  class extends Class {
    static factory(...args) {
      super.factory(...args)
      return new this()
    }
    
    say() { return `hello ${super.say()}` }
  }

export default factory(Hello)
  .set("my_extension", ext)
```

Now our `factory()` call returns an instance of `Hello` and changes the return value of `say`:

```js
// say_hello.js
//
import factory from "./hello"
factory().say() // "hello world"
```

## How the extension works

An extension is a function that receives a class and returns a class. So we start by defining a function that receives a class as an argument with an implicit return:

```js
let ext = Class =>
```

In this case, `Class` is the class from the `factory(class {})` call. We refer to it as the "factory class".

Next, we extend the factory class to add our own static factory function:

```js
  class extends Class {
    static factory() {
```

The `static factory` function determines the return value of the factory function.

We call the superclass with the same arguments we received:

```js
      super.factory(...args)
```

It is important to keep the call going up the inheritance chain in order to execute the logic of the superclass and any other extensions in between.

At the end of the static factory, we return the new instance of our class:

```js
      return new this()
```

Here we extend the `say` function to add "hello" before "world":

```js
    say() { return `hello ${super.say()}` }
```

Industry provides a [similar API as Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) to modify the extension hierarchy of a factory:

```js
export default factory(Hello)
  .set("my_extension", ext)
```

For a continuation of the above extension, take a look at [Instance](https://github.com/invrs/industry-instance).

## Existing extensions

[Inverse Labs](https://inverse.com/labs) has already authored a few extensions that you can use out of the box:

* [Chain](https://github.com/invrs/industry-chain) - Chain synchronous or async methods using a common parameter namespace.
* [Exception](https://github.com/invrs/industry-exception) - Pretty error logging for StandardIO functions.
* [Functions](https://github.com/invrs/industry-functions) - Retrieve factory instance and class methods.
* [Include](https://github.com/invrs/industry-include) - Provides a dependency tree object to your method parameters.
* [Instance](https://github.com/invrs/industry-instance) - Defines a factory function that builds instances.
* [Pattern](https://github.com/invrs/industry-pattern) - Pattern matching on factory class and instance methods.
* [StandardIO](https://github.com/invrs/industry-standard-io) - Adds [StandardIO](https://github.com/invrs/standard-io) compliance to factory methods.
* [State](https://github.com/invrs/industry-state) - Immutable state for your factories.
