-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.ts
89 lines (72 loc) · 2.62 KB
/
draft.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
function blueStar() {
console.log("blueStar(): factory evaluated");
// Target is the object to which the decorator is applied
// The propertyKey is the name of the method to which the decorator is applied.
/*
The descriptor is an object that contains information about the method. It has properties that can be used to modify the method's behavior.
A PropertyDescriptor typically includes:
value: The actual function of the method.
writable: Indicates if the property is writable.
enumerable: Indicates if the property is enumerable.
configurable: Indicates if the property can be redefined.
*/
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
if (!target.stickers) { // This is needed because there can be class that do not have the stickers property
target.stickers = [];
}
target.stickers.push('Blue Star');
console.log("blueStar(): called");
};
}
function redHeart() {
console.log("redHeart(): factory evaluated");
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
if (!target.stickers) {
target.stickers = [];
}
target.stickers.push('Red Heart');
console.log("redHeart(): called");
};
}
class ToyCar {
stickers: Array<string>;
@blueStar()
@redHeart()
drive() {
console.log("The toy car is driving!");
if (this.stickers) {
console.log("Stickers on the car:", this.stickers.join(', '));
}
}
}
const myToyCar = new ToyCar();
myToyCar.drive();
function user() {
console.log("Came To user factory!");
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log(`The method user() decorator is applied: ${propertyKey}`);
console.log(`The user: ${target.username}`);
}
}
function subscription() {
console.log("Came To subscription factory!");
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log(`The method subscription() decorator is applied: ${propertyKey}`);
if(!target.subscription) {
target.subscription = true;
}
console.log(`Setting subscription for the user: ${target.subscription}`);
}
}
class UserSubscription {
username: string;
subscription: boolean;
constructor(usrName: string) {
this.username = usrName;
}
@subscription()
@user()
userData() {}
}
const userSubscription = new UserSubscription("Kraken");
userSubscription.userData();