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

Function queryClass

packages/effect/src/ui/Query.ts:557–713  ·  view source on GitHub ↗
(def: QueryDef)

Source from the content-addressed store, hash-verified

555 * statics that look the cache up. An infinite query adds its own.
556 */
557const queryClass = (def: QueryDef) => {
558 const Base = Context.Service<any, Fetcher>()(def.name)
559 class QueryService extends Base {
560 static readonly [TypeId]: any = def
561 static readonly args = def.args
562 static readonly success = def.success
563 static readonly error = def.error
564 static toLayer(
565 build: Fetcher | Effect.Effect<Fetcher, any, any>
566 ): Layer.Layer<any, any, any> {
567 return Layer.effect(
568 Base,
569 Effect.gen(function*() {
570 const scope = yield* Effect.scope
571 const captured = yield* Effect.context<never>()
572 const fetcher = Effect.isEffect(build) ? yield* build : build
573 // The fetch's context wins, keeping its cache, span and host runtime.
574 const provide = (context: Context.Context<never>) => Context.merge(captured, context)
575 const built = makeBuild((args, cursor) => {
576 const out: Effect.Effect<any, any> | Stream.Stream<any, any> = fetcher(args, cursor)
577 return Effect.isEffect(out)
578 ? Effect.updateContext(out, provide)
579 : Stream.updateContext(out, provide) as any
580 })
581 yield* Scope.addFinalizer(scope, closeBuild(built))
582 return built.fetcher
583 })
584 )
585 }
586 static get(args: unknown) {
587 return withCache((cache, context) =>
588 cache.get(
589 def,
590 args,
591 fetcherOf(context, Base),
592 Context.get(context, CurrentRun),
593 Context.getOrUndefined(context, Tracer.ParentSpan)
594 )
595 )
596 }
597 static prefetch(args: unknown) {
598 return withCache((cache, context) =>
599 Effect.sync(() => {
600 cache.prefetch(def, args, fetcherOf(context, Base))
601 })
602 )
603 }
604 static invalidate(args?: unknown) {
605 return withCache((cache, context) =>
606 Effect.sync(() => {
607 const span = Context.getOrUndefined(context, Tracer.ParentSpan)
608 cache.invalidate([
609 args === undefined ? QueryService : entryTarget(QueryService, args)
610 ], { byHand: true, parent: span && queryCore.spanContextOf(span) })
611 })
612 )
613 }
614 static changes(args: unknown) {

Callers 3

ServiceFunction · 0.85
InfiniteServiceFunction · 0.85
StreamServiceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected