-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixins.ts
77 lines (68 loc) · 2.25 KB
/
mixins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Sprite } from "./class";
export type Constructor = new (...args: any[]) => {};
export type ConstructorWithArgs<T> = new (...args: T[]) => {};
export type ConstructorWithArgsObject<T = {}> = new (...args: any[]) => T;
export type ConstructorWithArgsAndReturn<T, R> = new (...args: T[]) => R;
export type ConstructorWithReturn<R> = new (...args: any[]) => R;
export type Positionable = ConstructorWithArgsObject<{ setPos: (x: number, y: number) => void }>;
export type Spritable = ConstructorWithArgsObject<Sprite>;
export type Loggable = ConstructorWithArgsObject<{ print: () => void }>;
export function Scale<TBase extends Constructor>(Base: TBase) {
return class Scaling extends Base {
// Mixins may not declare private/protected properties
// however, you can use ES2020 private fields
_scale = 1;
setScale(scale: number) {
this._scale = scale;
}
get scale(): number {
return this._scale;
}
};
}
export function Jumpable<TBase extends Positionable>(Base: TBase) {
return class Jumpable extends Base {
jump() {
// This mixin will only work if it is passed a base
// class which has setPos defined because of the
// Positionable constraint.
this.setPos(0, 20);
}
};
}
export function applyMixins(derivedCtor: any, constructors: any[]) {
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
Object.create(null)
);
});
});
}
{
const EightBitSprite = Scale(Sprite);
const flappySprite = new EightBitSprite("Bird");
flappySprite.setScale(0.8);
console.log(flappySprite.scale);
}
{
class Jumpable {
jump() { }
}
class Duckable {
duck() { }
}
// Including the base
class Sprite {
x = 0;
y = 0;
}
interface Sprite extends Jumpable, Duckable { }
applyMixins(Sprite, [Jumpable, Duckable]);
let player = new Sprite();
player.jump();
console.log(player.x, player.y);
}