forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperations.resources.ts
More file actions
86 lines (75 loc) · 2.67 KB
/
Operations.resources.ts
File metadata and controls
86 lines (75 loc) · 2.67 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
import { clientFor, S } from "api/lib.js"
import { Duration, Effect } from "effect-app"
import { NotFoundError } from "effect-app/client"
import { Operation, OperationFailure, OperationId } from "effect-app/Operations"
export class FindOperation extends S.Req<FindOperation>()("FindOperation", {
id: OperationId
}, { allowAnonymous: true, allowRoles: ["user"], success: S.NullOr(Operation) }) {}
// codegen:start {preset: meta, sourcePrefix: src/Operations/}
export const meta = { moduleName: "Operations.resources" } as const
// codegen:end
// Extensions
const opsClient = clientFor({ FindOperation, meta })
export function refreshAndWaitAForOperation<R2, E2, A2>(
refresh: Effect<A2, E2, R2>,
cb?: (op: Operation) => void
) {
return <R, E>(act: Effect<OperationId, E, R>) =>
Effect.tap(
waitForOperation(
Effect.tap(act, refresh),
cb
),
refresh
)
}
export function refreshAndWaitAForOperation_<R2, E2, A2, R, E>(
act: Effect<OperationId, E, R>,
refresh: Effect<A2, E2, R2>,
cb?: (op: Operation) => void
) {
return Effect.tap(
waitForOperation(
Effect.tap(act, refresh),
cb
),
refresh
)
}
export function refreshAndWaitForOperation<R2, E2, A2>(refresh: Effect<A2, E2, R2>, cb?: (op: Operation) => void) {
return <Req, R, E>(act: (req: Req) => Effect<OperationId, E, R>) => (req: Req) =>
refreshAndWaitAForOperation_(act(req), refresh, cb)
}
export function refreshAndWaitForOperation_<Req, R2, E2, A2, R, E>(
act: (req: Req) => Effect<OperationId, E, R>,
refresh: Effect<A2, E2, R2>,
cb?: (op: Operation) => void
) {
return (req: Req) => refreshAndWaitAForOperation_(act(req), refresh, cb)
}
export function waitForOperation<R, E>(
self: Effect<OperationId, E, R>,
cb?: (op: Operation) => void
) {
return Effect.andThen(self, (r) => _waitForOperation(r, cb))
}
export function waitForOperation_(cb?: (op: Operation) => void) {
return <Req, R, E>(self: (req: Req) => Effect<OperationId, E, R>) => (req: Req) =>
Effect.andThen(self(req), (r) => _waitForOperation(r, cb))
}
const isFailure = S.is(OperationFailure)
function _waitForOperation(id: OperationId, cb?: (op: Operation) => void) {
return Effect
.gen(function*() {
let r = yield* opsClient.FindOperation.handler({ id })
while (r) {
if (cb) cb(r)
const result = r.result
if (result) return isFailure(result) ? yield* Effect.fail(result) : yield* Effect.succeed(result)
yield* Effect.sleep(Duration.seconds(2))
r = yield* opsClient.FindOperation.handler({ id })
}
return yield* new NotFoundError({ type: "Operation", id })
})
// .pipe(Effect.provide(Layer.setRequestCaching(false)))
}