MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeUsers

Function makeUsers

packages/effect/test/ui/Query.test.ts:49–92  ·  view source on GitHub ↗
(options: {
  readonly staleTime: Duration.Input
  readonly idleTimeToLive?: Duration.Input
  readonly refetchInterval?: Query.RefetchInterval<
    { readonly id: string },
    User,
    string
  >
  readonly reactivityKeys?: (
    args: { readonly id: string }
  ) => Reactivity.KeysInput
})

Source from the content-addressed store, hash-verified

47
48// A query whose fetcher takes 10 ms and does what `db.mode` says.
49const makeUsers = (options: {
50 readonly staleTime: Duration.Input
51 readonly idleTimeToLive?: Duration.Input
52 readonly refetchInterval?: Query.RefetchInterval<
53 { readonly id: string },
54 User,
55 string
56 >
57 readonly reactivityKeys?: (
58 args: { readonly id: string }
59 ) => Reactivity.KeysInput
60}) => {
61 const db = {
62 calls: 0,
63 name: "Ann",
64 mode: "ok" as "ok" | "fail" | "die" | "interrupt"
65 }
66 class GetUser extends Query.Service<GetUser>()(
67 "users/GetUser",
68 {
69 args: { id: Schema.String },
70 success: User,
71 error: Schema.String,
72 tags: [Users],
73 ...options
74 }
75 ) {
76 static readonly layer = this.toLayer(Effect.fnUntraced(function*({ id }) {
77 db.calls++
78 yield* Effect.sleep(10)
79 if (db.mode === "die") return yield* Effect.die("boom")
80 // A Fail next to an Interrupt is still a Failure, and holds only the Fail.
81 if (db.mode === "fail") {
82 return yield* Effect.failCause(
83 Cause.combine(Cause.fail("nope"), Cause.interrupt())
84 )
85 }
86 if (db.mode === "interrupt") return yield* Effect.interrupt
87 return { id, name: db.name }
88 }))
89 }
90 const provide = Effect.provide(Layer.merge(GetUser.layer, TestQuery.layer()))
91 return { db, GetUser, provide }
92}
93
94// Watches through the public `changes` Stream, and records what it emits.
95const watch = Effect.fnUntraced(

Callers 1

Query.test.tsFile · 0.70

Calls 3

mergeMethod · 0.65
provideMethod · 0.45
layerMethod · 0.45

Tested by

no test coverage detected