@@ -5,21 +5,22 @@ import type { TypeFunction } from '../type-function/__.js'
5
5
import type { Context , Extension } from './Extension.js'
6
6
7
7
/**
8
- * A chain. Type level function with extensions.
9
- * When called, it returns itself with the given extensions.
8
+ * A builder definition.
9
+ * Technically it is a type level function with additional properties.
10
+ * When called, returns itself with the given extensions attached as a property.
10
11
*/
11
12
export interface Definition_ < $Extensions extends [ ...Extension [ ] ] = [ ...Extension [ ] ] > extends TypeFunction . Fn {
12
13
extensions : $Extensions
13
14
return : Definition_ < AssertExtends < this[ 'params' ] , Extension [ ] > >
14
15
}
15
16
16
- type InvokeDefinition < _ extends Definition_ , $Arguments extends [ ...Extension [ ] ] > = TypeFunction . Call <
17
- _ ,
17
+ type InvokeDefinition < $Definition extends Definition_ , $Arguments extends [ ...Extension [ ] ] > = TypeFunction . Call <
18
+ $Definition ,
18
19
$Arguments
19
20
>
20
21
21
22
/**
22
- * An empty chain definition . Useful for creation of new chains .
23
+ * A definition literal, empty . Useful for creation of new builders .
23
24
*/
24
25
export type Empty = Definition_ < [ ] >
25
26
@@ -29,107 +30,169 @@ export type Empty = Definition_<[]>
29
30
export type Create < $Extensions extends [ ...Extension [ ] ] > = AddExtensions < Empty , $Extensions >
30
31
31
32
/**
32
- * Extend the definition with many extensions. Refer to `Extend` for more details.
33
+ * Extend the definition with many extensions. See { @link AddExtension} for details.
33
34
*/
34
35
// dprint-ignore
35
- export type AddExtensions < $Chain_ extends Definition_ , $Extensions extends [ ...Extension [ ] ] > =
36
+ export type AddExtensions < $Definition extends Definition_ , $Extensions extends [ ...Extension [ ] ] > =
36
37
$Extensions extends [ infer $FirstExtension extends Extension , ...infer $RestExtensions extends Extension [ ] ]
37
- ? AddExtensions < AddExtension < $Chain_ , $FirstExtension > , $RestExtensions >
38
- : $Chain_
38
+ ? AddExtensions < AddExtension < $Definition , $FirstExtension > , $RestExtensions >
39
+ : $Definition
39
40
40
41
/**
41
42
* Extend the definition. During materialization the extension can contribute properties
42
43
* that make up the final builder.
43
44
*/
44
45
// dprint-ignore
45
- export type AddExtension < $Chain_ extends Definition_ , $Extension_ extends Extension > =
46
- InvokeDefinition < $Chain_ , [ ...$Chain_ [ 'extensions' ] , $Extension_ ] >
46
+ export type AddExtension < $definition_ extends Definition_ , $Extension_ extends Extension > =
47
+ InvokeDefinition < $definition_ , [ ...$definition_ [ 'extensions' ] , $Extension_ ] >
47
48
48
49
//
49
50
// Materialize Utilities
50
51
//
51
52
53
+ /**
54
+ * Materialize the definition using each extension's generic context type.
55
+ *
56
+ * Each extension will be invoked with its own generic context type.
57
+ * Then results will be merged to produce the builder object.
58
+ *
59
+ * All extensions' generic context types will be merged to produce
60
+ * the builder object context property.
61
+ */
52
62
// dprint-ignore
53
- export type MaterializeGeneric < $Chain_ extends Definition_ > =
63
+ export type MaterializeGeneric < $Definition extends Definition_ > =
54
64
Simplify <
55
65
Private . Add <
56
66
{
57
- chain : $Chain_ ,
58
- context : Tuple . IntersectItems <
59
- MaterializeExtensionsGenericContext < $Chain_ [ 'extensions' ] >
60
- >
67
+ definition : $Definition ,
68
+ context : MaterializeGeneric_Context < $Definition >
61
69
} ,
62
- Tuple . IntersectItems <
63
- MaterializeExtensionsGeneric < $Chain_ , $Chain_ [ 'extensions' ] >
64
- >
70
+ MaterializeGeneric_Extensions < $Definition >
65
71
>
66
72
>
67
73
68
74
// dprint-ignore
69
- type MaterializeExtensionsGeneric < $Chain_ extends Definition_ , $Extensions extends [ ...Extension [ ] ] > = {
70
- [ $Index in keyof $Extensions ] : Extension . Invoke < $Extensions [ $Index ] , {
71
- chain : $Chain_ ,
72
- context : $Extensions [ $Index ] [ 'context' ]
73
- } >
74
- }
75
+ type MaterializeGeneric_Extensions < $Definition extends Definition_ > =
76
+ Tuple . IntersectItems <
77
+ MaterializeGeneric_Extensions_ <
78
+ $Definition ,
79
+ $Definition [ 'extensions' ]
80
+ >
81
+ >
75
82
// dprint-ignore
76
- type MaterializeExtensionsGenericContext < $Extensions extends [ ...Extension [ ] ] > = {
83
+ type MaterializeGeneric_Extensions_ <
84
+ $Definition extends Definition_ ,
85
+ $Extensions extends [ ...Extension [ ] ]
86
+ > = {
87
+ [ $Index in keyof $Extensions ] :
88
+ Extension . Invoke < $Extensions [ $Index ] , {
89
+ definition : $Definition ,
90
+ context : $Extensions [ $Index ] [ 'context' ] ,
91
+ } >
92
+ }
93
+
94
+ type MaterializeGeneric_Context < $Definition extends Definition_ > = Tuple . IntersectItems <
95
+ MaterializeGeneric_Context_ < $Definition [ 'extensions' ] >
96
+ >
97
+ type MaterializeGeneric_Context_ < $Extensions extends [ ...Extension [ ] ] > = {
77
98
[ $Index in keyof $Extensions ] : $Extensions [ $Index ] [ 'context' ]
78
99
}
79
100
101
+ //
102
+ //
103
+ // ---------------------------------------------------------------------------------------------
104
+ //
105
+ //
106
+
107
+ /**
108
+ * Materialize the definition using each extension's empty context type.
109
+ *
110
+ * Each extension will be invoked with its own empty context.
111
+ * Then results will be merged to produce the builder object.
112
+ *
113
+ * All extensions' empty context types will be merged to produce
114
+ * the builder object context property.
115
+ */
80
116
// dprint-ignore
81
- export type MaterializeSpecific < $Chain_ extends Definition_ > =
117
+ export type MaterializeEmpty < $Definition extends Definition_ > =
82
118
Simplify <
83
119
Private . Add <
84
120
{
85
- chain : $Chain_ ,
86
- context : Tuple . IntersectItems <
87
- MaterializeExtensionsInitialContext < $Chain_ [ 'extensions' ] >
88
- >
121
+ definition : $Definition ,
122
+ context : MaterializeEmpty_Context < $Definition > ,
89
123
} ,
90
124
Tuple . IntersectItems <
91
- MaterializeExtensionsInitial < $Chain_ , $Chain_ [ 'extensions' ] >
125
+ MaterializeEmpty_Extensions < $Definition , $Definition [ 'extensions' ] >
92
126
>
93
127
>
94
128
>
95
129
96
130
// dprint-ignore
97
- type MaterializeExtensionsInitial < $Chain_ extends Definition_ , $Extensions extends [ ...Extension [ ] ] > = {
131
+ type MaterializeEmpty_Extensions <
132
+ $Definition extends Definition_ ,
133
+ $Extensions extends [ ...Extension [ ] ]
134
+ > = {
98
135
[ $Index in keyof $Extensions ] : Extension . Invoke < $Extensions [ $Index ] , {
99
- chain : $Chain_ ,
136
+ definition : $Definition ,
100
137
context : $Extensions [ $Index ] [ 'contextEmpty' ]
101
138
} >
102
139
}
103
140
// dprint-ignore
104
- type MaterializeExtensionsInitialContext < $Extensions extends [ ...Extension [ ] ] > = {
141
+ type MaterializeEmpty_Context < $Definition extends Definition_ > =
142
+ Tuple . IntersectItems <
143
+ MaterializeEmpty_Context_ < $Definition [ 'extensions' ] >
144
+ >
145
+ // dprint-ignore
146
+ type MaterializeEmpty_Context_ < $Extensions extends [ ...Extension [ ] ] > = {
105
147
[ $Index in keyof $Extensions ] : $Extensions [ $Index ] [ 'contextEmpty' ]
106
148
}
107
149
150
+ //
151
+ //
152
+ // ---------------------------------------------------------------------------------------------
153
+ //
154
+ //
155
+
156
+ /**
157
+ * Materialize the definition with a new context.
158
+ *
159
+ * Each extension will be invoked with the given context.
160
+ * Then results will be merged to produce the builder object.
161
+ *
162
+ * The given context will be used as-is for the builder object context property.
163
+ */
108
164
// dprint-ignore
109
- export type MaterializeWithNewContext < $Chain_ extends Definition_ , $Context extends Context > =
110
- // Simplify<
111
- Private . Add <
112
- {
113
- chain : $Chain_ ,
114
- context : $Context
115
- } ,
116
- Tuple . IntersectItems <
117
- MaterializeExtensionsWithNewState <
118
- $Chain_ ,
119
- $Context ,
120
- $Chain_ [ 'extensions' ]
121
- >
165
+ export type MaterializeWith <
166
+ $Definition extends Definition_ ,
167
+ $Context extends Context
168
+ > =
169
+
170
+ Private . Add <
171
+ {
172
+ definition : $Definition ,
173
+ context : $Context
174
+ } ,
175
+ Tuple . IntersectItems <
176
+ MaterializeWith_Extensions <
177
+ $Definition ,
178
+ $Definition [ 'extensions' ] ,
179
+ $Context
122
180
>
123
181
>
124
- // >
182
+ >
125
183
126
- type MaterializeExtensionsWithNewState <
127
- $Chain_ extends Definition_ ,
128
- $Context extends Context ,
184
+ // dprint-ignore
185
+ type MaterializeWith_Extensions <
186
+ $Definition extends Definition_ ,
129
187
$Extensions extends [ ...Extension [ ] ] ,
188
+ $Context extends Context ,
130
189
> = {
131
- [ $Index in keyof $Extensions ] : Extension . Invoke <
132
- $Extensions [ $Index ] ,
133
- { chain : $Chain_ ; context : $Context }
134
- >
190
+ [ $Index in keyof $Extensions ] :
191
+ Extension . Invoke <
192
+ $Extensions [ $Index ] ,
193
+ {
194
+ definition : $Definition
195
+ context : $Context
196
+ }
197
+ >
135
198
}
0 commit comments