-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
96 lines (67 loc) · 2.72 KB
/
example.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Mixin } from "./abstract_mixin";
//////////////////////////////////////////////////////////////////////////////////////////////////
// Mixin 1. As plain as a mixin can get.
//////////////////////////////////////////////////////////////////////////////////////////////////
export class MixinNumber extends Mixin {
myNumber: number;
MixinNumber() {
this.myNumber = 1;
}
MixinNumberDestroy() {
delete this.myNumber;
}
getNumber() {
return this.myNumber;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Mixin 2. Mixin that depends on the applier having a specific class (in this case another mixin).
//////////////////////////////////////////////////////////////////////////////////////////////////
export type MixinStringConf = {
defaultValue?: string;
}
export class MixinString extends Mixin {
myString: string;
MixinString(conf: MixinStringConf) {
if (Mixin.notApplicable(this, MixinString, MixinNumber)) return;
this.myString = conf.defaultValue;
}
MixinStringDestroy() {
delete this.myString;
}
getString() {
return this.myString || "My value: ";
}
joinWithNumber() {
this.myString = this.getString() + this.getNumber();
}
}
export interface MixinString extends MixinNumber { }
//////////////////////////////////////////////////////////////////////////////////////////////////
// Class 1. No mixins, only a class to derive from as an example of combining with mixins.
//////////////////////////////////////////////////////////////////////////////////////////////////
export abstract class BaseClass {
constructor() {
console.log("BaseClass is here!");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Class 2. Using mixins and extends BaseClass.
//////////////////////////////////////////////////////////////////////////////////////////////////
export class Combinator extends BaseClass {
constructor() {
super();
Mixin.mixinInits(this, Combinator, MixinNumber, { mixin: MixinString, conf: { defaultValue: "Fave nr is: " } as MixinStringConf });
}
destroy() {
this.mixinDestroys(Combinator);
}
}
export interface Combinator extends MixinNumber, MixinString { }
//////////////////////////////////////////////////////////////////////////////////////////////////
// Demo of using the mixin crafted class.
//////////////////////////////////////////////////////////////////////////////////////////////////
const combinator = new Combinator();
console.log(combinator.getString());
combinator.joinWithNumber();
console.log(combinator.getString());