industry
v2.9.0
Published
Define factories from ES6 classes and immutably manipulate the class inheritance chain.
Downloads
31
Readme
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:
// hello.js
//
import { factory } from "industry"
class Hello {
say() { return "hello" }
}
export factory(Hello)
Use the factory
Import the factory and call it:
// 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.
// 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
:
// 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:
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:
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:
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:
return new this()
Here we extend the say
function to add "hello" before "world":
say() { return `hello ${super.say()}` }
Industry provides a similar API as Map to modify the extension hierarchy of a factory:
export default factory(Hello)
.set("my_extension", ext)
For a continuation of the above extension, take a look at Instance.
Existing extensions
Inverse Labs has already authored a few extensions that you can use out of the box:
- Chain - Chain synchronous or async methods using a common parameter namespace.
- Exception - Pretty error logging for StandardIO functions.
- Functions - Retrieve factory instance and class methods.
- Include - Provides a dependency tree object to your method parameters.
- Instance - Defines a factory function that builds instances.
- Pattern - Pattern matching on factory class and instance methods.
- StandardIO - Adds StandardIO compliance to factory methods.
- State - Immutable state for your factories.