Last active
June 13, 2017 21:43
-
-
Save 3mcd/600bd4440994b6cce846f1e100e7f5a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Ctor<T> = new (...args: any[]) => T | |
abstract class BaseComponent { | |
abstract update(): IterableIterator<any> | |
protected children: BaseComponent[] = [] | |
protected getComponent(ctor: Ctor<BaseComponent>) { | |
return this.children.find(x => x instanceof ctor) | |
} | |
protected *send() { | |
} | |
} | |
function Component(...dependencies: Ctor<BaseComponent>[]) { | |
abstract class ComponentConstructor extends BaseComponent { | |
constructor() { | |
super() | |
for (let dependency of dependencies) { | |
const dep = new dependency() | |
this.children.push(dep) | |
} | |
} | |
protected getComponent(ctor: Ctor<BaseComponent>) { | |
const index = dependencies.indexOf(ctor) | |
if (index < 0) { | |
throw new TypeError(`Component ${ctor.name} not found.`) | |
} | |
return super.getComponent(ctor) | |
} | |
} | |
return ComponentConstructor | |
} | |
class A extends Component() { | |
*update() { | |
} | |
} | |
class B extends Component(A) { | |
*update() { | |
} | |
} | |
class C extends Component(B, A) { | |
*update() { | |
const c = this.getComponent(C) | |
} | |
} | |
const c = new C() | |
c.update().next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment