@@ -12,33 +12,46 @@ import {Fiber} from './Fiber'
12
12
import { Instruction , Tag } from './Instructions'
13
13
import { Ref } from './Ref'
14
14
15
- //#region Helper Functions
16
15
const Id = < A > ( _ : A ) : A => _
17
16
const ExitRef = < E = never , A = never > ( ) => Ref . of < Exit < E , A > > ( Exit . pending )
18
- //#endregion
19
17
20
- type AA < A1 , A2 > = A1 & A2 extends never ? never : [ A1 , A2 ]
18
+ type iRR < R1 , R2 > = R1 & R2 extends never ? R1 | R2 : R1 & R2
19
+ export type NoEnv = never
20
+ type iAA < A1 , A2 > = A1 & A2 extends never ? never : [ A1 , A2 ]
21
21
22
- export type Task < A > = FIO < Error , A >
23
- export type UIO < A > = FIO < never , A >
22
+ export type IO < E , A > = FIO < E , A >
23
+ export type Task < A > = IO < Error , A >
24
+ export type UIO < A > = IO < never , A >
24
25
25
- export class FIO < E1 = unknown , A1 = unknown > {
26
+ export class FIO < E1 = unknown , A1 = unknown , R1 = NoEnv > {
26
27
public get asInstruction ( ) : Instruction {
27
28
return this as Instruction
28
29
}
29
30
30
- public get fork ( ) : FIO < never , Fiber < E1 , A1 > > {
31
+ public get fork ( ) : FIO < never , Fiber < E1 , A1 > , R1 > {
31
32
return new FIO ( Tag . Fork , this )
32
33
}
33
34
34
- public get once ( ) : FIO < never , FIO < E1 , A1 > > {
35
- return Await . of < E1 , A1 > ( ) . map ( await => await . set ( this ) . and ( await . get ) )
35
+ public get env ( ) : FIO < never , R1 , R1 > {
36
+ return FIO . environment < R1 > ( )
36
37
}
37
38
38
- public get void ( ) : FIO < E1 , void > {
39
+ public get once ( ) : FIO < never , FIO < E1 , A1 > , R1 > {
40
+ return this . env . chain ( env =>
41
+ Await . of < E1 , A1 > ( ) . map ( await =>
42
+ await . set ( this . provide ( env ) ) . and ( await . get )
43
+ )
44
+ )
45
+ }
46
+
47
+ public get void ( ) : FIO < E1 , void , R1 > {
39
48
return this . const ( undefined )
40
49
}
41
50
51
+ public static access < R , A > ( cb : ( R : R ) => A ) : FIO < never , A , R > {
52
+ return new FIO ( Tag . Access , cb )
53
+ }
54
+
42
55
/**
43
56
* **NOTE:** The default type is set to `never` because it hard for typescript to infer the types based on how we use `res`.
44
57
* Using `never` will give users compile time error always while using.
@@ -67,17 +80,17 @@ export class FIO<E1 = unknown, A1 = unknown> {
67
80
return FIO . async ( ( rej , res , sh ) => cb ( res , sh ) )
68
81
}
69
82
70
- public static catch < E1 , A1 , E2 , A2 > (
71
- fa : FIO < E1 , A1 > ,
72
- aFe : ( e : E1 ) => FIO < E2 , A2 >
73
- ) : FIO < E2 , A2 > {
83
+ public static catch < E1 , A1 , R1 , E2 , A2 , R2 > (
84
+ fa : FIO < E1 , A1 , R1 > ,
85
+ aFe : ( e : E1 ) => FIO < E2 , A2 , R2 >
86
+ ) : FIO < E2 , A2 , iRR < R1 , R2 > > {
74
87
return new FIO ( Tag . Catch , fa , aFe )
75
88
}
76
89
77
- public static chain < E1 , A1 , E2 , A2 > (
78
- fa : FIO < E1 , A1 > ,
79
- aFb : ( a : A1 ) => FIO < E2 , A2 >
80
- ) : FIO < E1 | E2 , A2 > {
90
+ public static chain < E1 , A1 , R1 , E2 , A2 , R2 > (
91
+ fa : FIO < E1 , A1 , R1 > ,
92
+ aFb : ( a : A1 ) => FIO < E2 , A2 , R2 >
93
+ ) : FIO < E1 | E2 , A2 , iRR < R1 , R2 > > {
81
94
return new FIO ( Tag . Chain , fa , aFb )
82
95
}
83
96
@@ -104,6 +117,16 @@ export class FIO<E1 = unknown, A1 = unknown> {
104
117
)
105
118
}
106
119
120
+ public static environment < R1 = never > ( ) : FIO < never , R1 , R1 > {
121
+ return new FIO ( Tag . Environment )
122
+ }
123
+
124
+ public static flatten < E1 , A1 , R1 , E2 , A2 , R2 > (
125
+ fio : FIO < E1 , FIO < E2 , A2 , R2 > , R1 >
126
+ ) : FIO < E1 | E2 , A2 , iRR < R1 , R2 > > {
127
+ return fio . chain ( Id )
128
+ }
129
+
107
130
public static fromExit < E , A > ( exit : Exit < E , A > ) : FIO < E , A > {
108
131
return Exit . isSuccess ( exit )
109
132
? FIO . of ( exit [ 1 ] )
@@ -116,10 +139,10 @@ export class FIO<E1 = unknown, A1 = unknown> {
116
139
return FIO . resume ( cb )
117
140
}
118
141
119
- public static map < E1 , A1 , A2 > (
120
- fa : FIO < E1 , A1 > ,
142
+ public static map < E1 , A1 , R1 , A2 > (
143
+ fa : FIO < E1 , A1 , R1 > ,
121
144
ab : ( a : A1 ) => A2
122
- ) : FIO < E1 , A2 > {
145
+ ) : FIO < E1 , A2 , R1 > {
123
146
return new FIO ( Tag . Map , fa , ab )
124
147
}
125
148
@@ -180,35 +203,40 @@ export class FIO<E1 = unknown, A1 = unknown> {
180
203
public readonly i1 ?: unknown
181
204
) { }
182
205
183
- public and < E2 , A2 > ( aFb : FIO < E2 , A2 > ) : FIO < E1 | E2 , A2 > {
206
+ public and < E2 , A2 , R2 > ( aFb : FIO < E2 , A2 , R2 > ) : FIO < E1 | E2 , A2 , iRR < R1 , R2 > > {
184
207
return this . chain ( ( ) => aFb )
185
208
}
186
209
187
- public catch < E2 , A2 > ( aFb : ( e : E1 ) => FIO < E2 , A2 > ) : FIO < E2 , A1 | A2 > {
210
+ public catch < E2 , A2 , R2 > (
211
+ aFb : ( e : E1 ) => FIO < E2 , A2 , R2 >
212
+ ) : FIO < E2 , A1 | A2 , iRR < R1 , R2 > > {
188
213
return FIO . catch ( this , aFb )
189
214
}
190
215
191
- public chain < E2 , A2 > ( aFb : ( a : A1 ) => FIO < E2 , A2 > ) : FIO < E1 | E2 , A2 > {
216
+ public chain < E2 , A2 , R2 > (
217
+ aFb : ( a : A1 ) => FIO < E2 , A2 , R2 >
218
+ ) : FIO < E1 | E2 , A2 , iRR < R1 , R2 > > {
192
219
return FIO . chain ( this , aFb )
193
220
}
194
221
195
- public const < A2 > ( a : A2 ) : FIO < E1 , A2 > {
222
+ public const < A2 > ( a : A2 ) : FIO < E1 , A2 , R1 > {
196
223
return this . and ( FIO . of ( a ) )
197
224
}
198
225
199
- public delay ( duration : number ) : FIO < E1 , A1 > {
226
+ public delay ( duration : number ) : FIO < E1 , A1 , R1 > {
200
227
return FIO . timeout ( this , duration ) . chain ( Id )
201
228
}
202
229
203
- public map < A2 > ( ab : ( a : A1 ) => A2 ) : FIO < E1 , A2 > {
230
+ public map < A2 > ( ab : ( a : A1 ) => A2 ) : FIO < E1 , A2 , R1 > {
204
231
return FIO . map ( this , ab )
205
232
}
233
+ public provide = ( r1 : R1 ) : FIO < E1 , A1 > => new FIO ( Tag . Provide , this , r1 )
206
234
207
- public raceWith < E2 , A2 > (
208
- that : FIO < E2 , A2 > ,
235
+ public raceWith < E2 , A2 , R2 > (
236
+ that : FIO < E2 , A2 , R2 > ,
209
237
cb1 : ( exit : Exit < E1 , A1 > , fiber : Fiber < E2 , A2 > ) => UIO < void > ,
210
238
cb2 : ( exit : Exit < E2 , A2 > , fiber : Fiber < E1 , A1 > ) => UIO < void >
211
- ) : FIO < never , void > {
239
+ ) : FIO < never , void , iRR < R1 , R2 > > {
212
240
return this . fork . zip ( that . fork ) . chain ( ( [ f1 , f2 ] ) => {
213
241
const resume1 = f1 . resumeAsync ( exit => cb1 ( exit , f2 ) )
214
242
const resume2 = f2 . resumeAsync ( exit => cb2 ( exit , f1 ) )
@@ -217,25 +245,31 @@ export class FIO<E1 = unknown, A1 = unknown> {
217
245
} )
218
246
}
219
247
220
- public tap ( io : UIO < void > ) : FIO < E1 , A1 > {
248
+ public tap ( io : UIO < void > ) : FIO < E1 , A1 , R1 > {
221
249
return this . chain ( _ => io . const ( _ ) )
222
250
}
223
251
224
- public zip < E2 , A2 > ( that : FIO < E2 , A2 > ) : FIO < E1 | E2 , AA < A1 , A2 > > {
225
- return this . zipWith ( that , ( a , b ) => [ a , b ] ) as FIO < E1 | E2 , AA < A1 , A2 > >
252
+ public zip < E2 , A2 , R2 > (
253
+ that : FIO < E2 , A2 , R2 >
254
+ ) : FIO < E1 | E2 , iAA < A1 , A2 > , iRR < R1 , R2 > > {
255
+ return this . zipWith ( that , ( a , b ) => [ a , b ] ) as FIO <
256
+ E1 | E2 ,
257
+ iAA < A1 , A2 > ,
258
+ iRR < R1 , R2 >
259
+ >
226
260
}
227
261
228
- public zipWith < E2 , A2 , C > (
229
- that : FIO < E2 , A2 > ,
262
+ public zipWith < E2 , A2 , R2 , C > (
263
+ that : FIO < E2 , A2 , R2 > ,
230
264
c : ( a1 : A1 , a2 : A2 ) => C
231
- ) : FIO < E1 | E2 , C > {
265
+ ) : FIO < E1 | E2 , C , iRR < R1 , R2 > > {
232
266
return this . chain ( a1 => that . map ( a2 => c ( a1 , a2 ) ) )
233
267
}
234
268
235
- public zipWithPar < E2 , A2 , C > (
236
- that : FIO < E2 , A2 > ,
269
+ public zipWithPar < E2 , A2 , R2 , C > (
270
+ that : FIO < E2 , A2 , R2 > ,
237
271
c : ( e1 : Exit < E1 , A1 > , e2 : Exit < E2 , A2 > ) => C
238
- ) : FIO < void , C > {
272
+ ) : FIO < void , C , iRR < R1 , R2 > > {
239
273
// Create Caches
240
274
const cache = ExitRef < E1 , A1 > ( ) . zip ( ExitRef < E2 , A2 > ( ) )
241
275
0 commit comments