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:
// new instance of Thing!
Why is this cool? Because when you call the function again, it returns the same instance:
// new instance // same instance!
Everywhere you see thing
, you know exactly what it is.
You can create multiple instances using a key:
// new instance of Thing "a" // same instance of Thing "a" // new instance of Thing "b" // same instance of Thing "b"
One options API to rule them all
Classes can receive options as well:
// new instance with options // update instance with options
And yes, options work with keys:
// new "a" instance with options // update "b" instance with options
Do one thing and do it well
Introducing then
:
// do the thing
Think of then
as the single entry point to executing any post-initialization logic.
And oh yeah, its chainable:
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// class {} // initialize options {} // update options {} // returns definite instance, promise, or value
Now let's use the class we made:
// calls `constructor(options)` // calls `set(options)` // calls `then()`
Dependency autoloading
Its easy to autoload other definite
classes in your project.
Configure a definite
builder:
// lib/component.js// autoload: `/../components` `/../models`
And use it in your classes:
// components/hello.js// { thiscomponents // new instance of `components/thing` thismodels // new instance of `models/other-thing` } Hello
Mixins
Its easy to extend your definite
classes:
// lib/component.js// let { superoptions } { superoptions } { return super...args } mixins: mixin
Then use it to define your classes:
// components/hello.js// {} {} {}) 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.