-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
RequestResolver.ts
196 lines (188 loc) · 6.21 KB
/
RequestResolver.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
/**
* @since 1.0.0
*/
import type * as Schema from "@effect/schema/Schema"
import type * as Serializable from "@effect/schema/Serializable"
import * as Deferred from "effect/Deferred"
import type * as Duration from "effect/Duration"
import * as Effect from "effect/Effect"
import * as Either from "effect/Either"
import { dual, pipe } from "effect/Function"
import * as Option from "effect/Option"
import type * as PrimaryKey from "effect/PrimaryKey"
import * as Queue from "effect/Queue"
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as Ref from "effect/Ref"
import * as Request from "effect/Request"
import * as RequestResolver from "effect/RequestResolver"
import type * as Scope from "effect/Scope"
import * as Persistence from "./Persistence.js"
interface DataLoaderItem<A extends Request.Request<any, any>> {
readonly request: A
readonly deferred: Deferred.Deferred<
Request.Request.Error<A>,
Request.Request.Success<A>
>
}
/**
* @since 1.0.0
* @category combinators
*/
export const dataLoader = dual<
(
options: {
readonly window: Duration.DurationInput
readonly maxBatchSize?: number
}
) => <A extends Request.Request<any, any>>(
self: RequestResolver.RequestResolver<A, never>
) => Effect.Effect<Scope.Scope, never, RequestResolver.RequestResolver<A, never>>,
<A extends Request.Request<any, any>>(
self: RequestResolver.RequestResolver<A, never>,
options: {
readonly window: Duration.DurationInput
readonly maxBatchSize?: number
}
) => Effect.Effect<Scope.Scope, never, RequestResolver.RequestResolver<A, never>>
>(2, <A extends Request.Request<any, any>>(
self: RequestResolver.RequestResolver<A, never>,
options: {
readonly window: Duration.DurationInput
readonly maxBatchSize?: number
}
) =>
Effect.gen(function*(_) {
const queue = yield* _(
Effect.acquireRelease(
Queue.unbounded<DataLoaderItem<A>>(),
Queue.shutdown
)
)
const batch = yield* _(Ref.make(ReadonlyArray.empty<DataLoaderItem<A>>()))
const takeOne = Effect.flatMap(Queue.take(queue), (item) => Ref.updateAndGet(batch, ReadonlyArray.append(item)))
const takeRest = takeOne.pipe(
Effect.repeat({
until: (items) =>
options.maxBatchSize !== undefined &&
items.length >= options.maxBatchSize
}),
Effect.timeout(options.window),
Effect.ignore,
Effect.zipRight(Ref.getAndSet(batch, ReadonlyArray.empty()))
)
yield* _(
takeOne,
Effect.zipRight(takeRest),
Effect.flatMap(
Effect.filter(({ deferred }) => Deferred.isDone(deferred), {
negate: true
})
),
Effect.flatMap(
Effect.forEach(
({ deferred, request }) =>
Effect.flatMap(Effect.exit(Effect.request(request, self)), (exit) => Deferred.complete(deferred, exit)),
{ batching: true, discard: true }
)
),
Effect.forever,
Effect.withRequestCaching(false),
Effect.interruptible,
Effect.forkScoped
)
return RequestResolver.fromEffect((request: A) =>
Effect.flatMap(
Deferred.make<Request.Request.Error<A>, Request.Request.Success<A>>(),
(deferred) =>
Queue.offer(queue, { request, deferred }).pipe(
Effect.zipRight(Deferred.await(deferred)),
Effect.onInterrupt(() => Deferred.interrupt(deferred))
)
)
)
}))
/**
* @since 1.0.0
* @category combinators
*/
export const persisted = dual<
(storeId: string) => <Req extends Schema.TaggedRequest.Any & PrimaryKey.PrimaryKey>(
self: RequestResolver.RequestResolver<Req, never>
) => Effect.Effect<
Persistence.ResultPersistence | Scope.Scope,
never,
RequestResolver.RequestResolver<Req, Serializable.WithResult.Context<Req>>
>,
<Req extends Schema.TaggedRequest.Any & PrimaryKey.PrimaryKey>(
self: RequestResolver.RequestResolver<Req, never>,
storeId: string
) => Effect.Effect<
Persistence.ResultPersistence | Scope.Scope,
never,
RequestResolver.RequestResolver<Req, Serializable.WithResult.Context<Req>>
>
>(2, <Req extends Schema.TaggedRequest.Any & PrimaryKey.PrimaryKey>(
self: RequestResolver.RequestResolver<Req, never>,
storeId: string
): Effect.Effect<
Persistence.ResultPersistence | Scope.Scope,
never,
RequestResolver.RequestResolver<Req, Serializable.WithResult.Context<Req>>
> =>
Effect.gen(function*(_) {
const storage = yield* _(
(yield* _(Persistence.ResultPersistence)).make(storeId)
)
const partition = (requests: ReadonlyArray<Req>) =>
storage.getMany(requests as any).pipe(
Effect.map(
ReadonlyArray.partitionMap((_, i) =>
Option.match(_, {
onNone: () => Either.left(requests[i]),
onSome: (_) => Either.right([requests[i], _] as const)
})
)
),
Effect.orElseSucceed(() => [requests, []] as const)
)
const set = (
request: Req,
result: Request.Request.Result<Req>
): Effect.Effect<any, never, void> => Effect.ignoreLogged(storage.set(request as any, result))
return RequestResolver.makeBatched((requests: Array<Req>) =>
Effect.flatMap(partition(requests), ([remaining, results]) => {
const completeCached = Effect.forEach(
results,
([request, result]) =>
Request.complete(request, result as any) as Effect.Effect<
never,
never,
void
>,
{ discard: true }
)
const completeUncached = pipe(
Effect.forEach(
remaining,
(request) => Effect.exit(Effect.request(request, self)),
{ batching: true }
),
Effect.flatMap((results) =>
Effect.forEach(
results,
(result, i) => {
const request = remaining[i]
return Effect.zipRight(
set(request, result as any),
Request.complete(request, result as any)
)
},
{ discard: true }
)
),
Effect.withRequestCaching(false)
)
return Effect.zipRight(completeCached, completeUncached)
})
)
}))