definite
v1.2.8
Published
[![Build Status](https://travis-ci.org/invrs/definite.svg?branch=master)](https://travis-ci.org/invrs/definite)
Downloads
15
Readme
Definite
A pattern for writing concise and testable Javascript classes.
Definite supports mixins that can add option immutability, call stack graphing, and more.
Eliminate vague variables
First let's look at what its like to require and use a definite
class:
import thing from "./thing"
thing() // new instance of Thing!
Why is this cool? Because when you call the function again, it returns the same instance:
import thing from "./thing"
thing() // new instance
thing() // same instance!
Everywhere you see thing
, you know exactly what it is.
You can create multiple instances using a key:
import thing from "./thing"
thing("a") // new instance of Thing "a"
thing("a") // same instance of Thing "a"
thing("b") // new instance of Thing "b"
thing("b") // same instance of Thing "b"
One options API to rule them all
Classes can receive options as well:
import thing from "./thing"
thing({ hello: "world" }) // new instance with options
thing({ hello: "me" }) // update instance with options
And yes, options work with keys:
import thing from "./thing"
thing("a", { hello: "world" }) // new "a" instance with options
thing("a", { hello: "me" }) // update "b" instance with options
Do one thing and do it well
Introducing then
:
import thing from "./thing"
thing().then() // do the thing
Think of then
as the single entry point to executing any post-initialization logic.
And oh yeah, its chainable:
import thing from "./thing"
import otherThing from "./other-thing"
thing().then(otherThing(), () => {
console.log("did all the things!")
})
Above we passed multiple thenable parameters to then
to automatically chain them.
Thenable parameters can be promises, functions, or other definite
classes.
How to define a class
Let's look at how we define a skeleton definite
class:
// skeleton.js
//
import def from "definite"
export default def(class {
constructor(options) {} // initialize options
set(options) {} // update options
then(...args) {} // returns definite instance, promise, or value
})
Now let's use the class we made:
import skeleton from "./skeleton"
skeleton({ a: true }) // calls `constructor(options)`
skeleton({ a: false }) // calls `set(options)`
skeleton().then() // calls `then()`
Dependency autoloading
Its easy to autoload other definite
classes in your project.
Configure a definite
builder:
// lib/component.js
//
import definite from "definite"
export default definite({
autoload: [
`${__dirname}/../components`,
`${__dirname}/../models`
]
})
And use it in your classes:
// components/hello.js
//
import component from "../lib/component"
class Hello {
constructor() {
this.components.thing() // new instance of `components/thing`
this.models.otherThing() // new instance of `models/other-thing`
}
}
export default component(Hello)
Mixins
Its easy to extend your definite
classes:
// lib/component.js
//
import def from "definite"
let mixin = (Extend) =>
class extends Extend {
constructor(options) {
super(options)
}
set(options) {
super(options)
}
then(...args) {
return super(...args)
}
}
export default def({
mixins: [ mixin ]
})
Then use it to define your classes:
// components/hello.js
//
import component from "../lib/component"
class Hello {
constructor() {}
set(options) {}
then(...args) {}
})
export default component(Hello)
Definite runner
The def
command automatically requires your definite
class and executes then
on it:
def path/to/class -o "{ option: true }"
Use -o
to specify options to pass into the class constructor.