forked from inversify/InversifyJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding_when_syntax.ts
More file actions
165 lines (133 loc) · 5.13 KB
/
binding_when_syntax.ts
File metadata and controls
165 lines (133 loc) · 5.13 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { interfaces } from '../interfaces/interfaces';
import { BindingOnSyntax } from './binding_on_syntax';
import {
namedConstraint,
taggedConstraint,
traverseAncerstors,
typeConstraint,
} from './constraint_helpers';
class BindingWhenSyntax<T> implements interfaces.BindingWhenSyntax<T> {
private readonly _binding: interfaces.Binding<T>;
constructor(binding: interfaces.Binding<T>) {
this._binding = binding;
}
public when(
constraint: (request: interfaces.Request) => boolean,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = constraint as interfaces.ConstraintFunction;
return new BindingOnSyntax<T>(this._binding);
}
public whenTargetNamed(
name: string | number | symbol,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = namedConstraint(name);
return new BindingOnSyntax<T>(this._binding);
}
public whenTargetIsDefault(): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) => {
if (request === null) {
return false;
}
const targetIsDefault: boolean =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
request.target !== null &&
!request.target.isNamed() &&
!request.target.isTagged();
return targetIsDefault;
};
return new BindingOnSyntax<T>(this._binding);
}
public whenTargetTagged(
tag: string | number | symbol,
value: unknown,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = taggedConstraint(tag)(value);
return new BindingOnSyntax<T>(this._binding);
}
public whenInjectedInto(
parent: NewableFunction | string,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && typeConstraint(parent)(request.parentRequest);
return new BindingOnSyntax<T>(this._binding);
}
public whenParentNamed(
name: string | number | symbol,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && namedConstraint(name)(request.parentRequest);
return new BindingOnSyntax<T>(this._binding);
}
public whenParentTagged(
tag: string | number | symbol,
value: unknown,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && taggedConstraint(tag)(value)(request.parentRequest);
return new BindingOnSyntax<T>(this._binding);
}
public whenAnyAncestorIs(
ancestor: NewableFunction | string,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && traverseAncerstors(request, typeConstraint(ancestor));
return new BindingOnSyntax<T>(this._binding);
}
public whenNoAncestorIs(
ancestor: NewableFunction | string,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null &&
!traverseAncerstors(request, typeConstraint(ancestor));
return new BindingOnSyntax<T>(this._binding);
}
public whenAnyAncestorNamed(
name: string | number | symbol,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && traverseAncerstors(request, namedConstraint(name));
return new BindingOnSyntax<T>(this._binding);
}
public whenNoAncestorNamed(
name: string | number | symbol,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null && !traverseAncerstors(request, namedConstraint(name));
return new BindingOnSyntax<T>(this._binding);
}
public whenAnyAncestorTagged(
tag: string | number | symbol,
value: unknown,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null &&
traverseAncerstors(request, taggedConstraint(tag)(value));
return new BindingOnSyntax<T>(this._binding);
}
public whenNoAncestorTagged(
tag: string | number | symbol,
value: unknown,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null &&
!traverseAncerstors(request, taggedConstraint(tag)(value));
return new BindingOnSyntax<T>(this._binding);
}
public whenAnyAncestorMatches(
constraint: (request: interfaces.Request) => boolean,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null &&
traverseAncerstors(request, constraint as interfaces.ConstraintFunction);
return new BindingOnSyntax<T>(this._binding);
}
public whenNoAncestorMatches(
constraint: (request: interfaces.Request) => boolean,
): interfaces.BindingOnSyntax<T> {
this._binding.constraint = (request: interfaces.Request | null) =>
request !== null &&
!traverseAncerstors(request, constraint as interfaces.ConstraintFunction);
return new BindingOnSyntax<T>(this._binding);
}
}
export { BindingWhenSyntax };