-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.ts
227 lines (220 loc) Β· 6.65 KB
/
io.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { Either, Left, Right } from '../either';
import { None, Option, Some } from '../option';
import { Throwable } from '../types';
import { memoize as memo, withCancel, withDelay, withTimeout } from '../utils';
interface IOAsync<R, A> {
ap: <B>(applicative: IOAsync<R, (a: A) => B | Promise<B>>) => IOAsync<R, B>;
map: <B>(f: (a: A) => B | Promise<B>) => IOAsync<R, B>;
forEach: <B = void>(f: (a: A) => B | Promise<B>) => IOAsync<R, A>;
flatMap: <B>(f: (a: A) => IOAsync<R, B>) => IOAsync<R, B>;
flatMapL: <S, B>(
f: (a: A) => IOAsync<S, B>,
local: (env: R) => S,
) => IOAsync<R, B>;
memoize: () => IOAsync<R, A>;
provide: (env: R) => IOAsync<void, A>;
provideDefault: (env: R) => IOAsync<R | void, A>;
either: () => IOAsync<R, Either<Throwable, A>>;
option: () => IOAsync<R, Option<A>>;
recover: <B>(handle: (t: Throwable) => B | Promise<B>) => IOAsync<R, A | B>;
recoverWith: <B>(io: IOAsync<R, B>) => IOAsync<R, A | B>;
retry: (amount: number) => IOAsync<R, A>;
delay: (ms: number) => IOAsync<R, A>;
timeout: (ms: number) => IOAsync<R, A>;
cancel: () => IOAsync<R, never>;
transform: <B>(convert: (io: IOAsync<R, A>) => B) => B;
access: <B>(f: (env: R) => B | Promise<B>) => IOAsync<R, B>;
local: <S = R>(f: (env: S) => R) => IOAsync<S, A>;
run: (env: R) => Promise<A>;
inspect: () => string;
}
interface IO<R, A> {
ap: <B>(applicative: IO<R, (a: A) => B>) => IO<R, B>;
map: <B>(f: (a: A) => B) => IO<R, B>;
forEach: <B = void>(f: (a: A) => B) => IO<R, A>;
flatMap: <B>(f: (a: A) => IO<R, B>) => IO<R, B>;
flatMapL: <S, B>(f: (a: A) => IO<S, B>, local: (env: R) => S) => IO<R, B>;
memoize: () => IO<R, A>;
provide: (env: R) => IO<void, A>;
provideDefault: (env: R) => IO<R | void, A>;
either: () => IO<R, Either<Throwable, A>>;
option: () => IO<R, Option<A>>;
recover: <B>(handle: (t: Throwable) => B) => IO<R, A | B>;
recoverWith: <B>(io: IO<R, B>) => IO<R, A | B>;
retry: (amount: number) => IO<R, A>;
transform: <B>(convert: (io: IO<R, A>) => B) => B;
access: <B>(f: (env: R) => B) => IO<R, B>;
local: <S = R>(f: (env: S) => R) => IO<S, A>;
async: () => IOAsync<R, A>;
run: (env: R) => A;
inspect: () => string;
}
const IOAsync = <R = void, A = unknown>(
fa: (env: R) => A | Promise<A>,
): IOAsync<R, A> => {
return {
ap: (applicative) =>
IOAsync((env) => applicative.map(async (f) => f(await fa(env))).run(env)),
map: (f) => IOAsync(async (env) => f(await fa(env))),
forEach: (f) =>
IOAsync(async (env) => {
f(await fa(env));
return await fa(env);
}),
flatMap: (f) => IOAsync(async (env) => f(await fa(env)).run(env)),
flatMapL: (f, local) =>
IOAsync(async (env) => f(await fa(env)).run(local(env))),
memoize: () => IOAsync(memo((env) => IOAsync(fa).run(env))),
provide: (env) => IOAsync(() => fa(env)),
provideDefault: (env) => IOAsync((newEnv) => fa(newEnv ?? env)),
either: () =>
IOAsync(async (env) => {
try {
return Right(await fa(env));
} catch (e) {
return Left(e as Throwable);
}
}),
option: () =>
IOAsync(async (env) => {
try {
return Some(await fa(env));
} catch {
return None;
}
}),
recover: (handle) =>
IOAsync(async (env) => {
try {
return await fa(env);
} catch (e) {
return handle(e as Throwable);
}
}),
recoverWith: (alternative) =>
IOAsync(async (env) => {
try {
return await fa(env);
} catch {
return alternative.run(env);
}
}),
retry: (amount) =>
IOAsync(async (env) => {
if (amount > 0) {
try {
return await fa(env);
} catch {
return IOAsync(fa)
.retry(amount - 1)
.run(env);
}
} else {
return fa(env);
}
}),
delay: (ms) =>
IOAsync(async (env) => {
const { delay } = withDelay(async () => fa(env));
return await delay(ms);
}),
timeout: (ms) =>
IOAsync(async (env) => {
const { timeout } = withTimeout(async () => fa(env));
return await timeout(ms);
}),
cancel: () =>
IOAsync(async (env) => {
const { cancel } = withCancel(async () => fa(env));
return await cancel();
}),
transform: (convert) => convert(IOAsync(fa)),
access: (f) => IOAsync((env) => f(env)),
local: (f) => IOAsync((env) => IOAsync(fa).run(f(env))),
run: async (env) => await fa(env),
inspect: () => `IOAsync(${fa})`,
};
};
IOAsync.of = <A>(a: A) => IOAsync(() => a);
IOAsync.failure = (t: Throwable) =>
IOAsync(() => {
throw t;
});
IOAsync.success = <A>(a: A) => IOAsync(() => a);
const IO = <R = void, A = unknown>(fa: (env: R) => A): IO<R, A> => {
return {
ap: (applicative) =>
IO((env) => applicative.map((f) => f(fa(env))).run(env)),
map: (f) => IO((env) => f(fa(env))),
forEach: (f) =>
IO((env) => {
f(fa(env));
return fa(env);
}),
flatMap: (f) => IO((env) => f(fa(env)).run(env)),
flatMapL: (f, local) => IO((env) => f(fa(env)).run(local(env))),
memoize: () => IO(memo((env) => IO(fa).run(env))),
provide: (env) => IO(() => fa(env)),
provideDefault: (env) => IO((newEnv) => fa(newEnv ?? env)),
either: () =>
IO((env) => {
try {
return Right(fa(env));
} catch (e) {
return Left(e as Throwable);
}
}),
option: () =>
IO((env) => {
try {
return Some(fa(env));
} catch {
return None;
}
}),
recover: (handle) =>
IO((env) => {
try {
return fa(env);
} catch (e) {
return handle(e as Throwable);
}
}),
recoverWith: (alternative) =>
IO((env) => {
try {
return fa(env);
} catch {
return alternative.run(env);
}
}),
retry: (amount) =>
IO((env) => {
if (amount > 0) {
try {
return fa(env);
} catch {
return IO(fa)
.retry(amount - 1)
.run(env);
}
} else {
return fa(env);
}
}),
transform: (convert) => convert(IO(fa)),
access: (f) => IO((env) => f(env)),
local: (f) => IO((env) => IO(fa).run(f(env))),
async: () => IOAsync((env) => IO(fa).run(env)),
run: (env) => fa(env),
inspect: () => `IO(${fa})`,
};
};
IO.of = <A>(a: A) => IO(() => a);
IO.failure = (t: Throwable) =>
IO(() => {
throw t;
});
IO.success = <A>(a: A) => IO(() => a);
IO.async = IOAsync;
export { IO, IOAsync };