Skip to content

Commit 46ec304

Browse files
committed
improve: global runtime
1 parent 4a1ef8b commit 46ec304

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

frontend/composables/client.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { makeClient } from "@effect-app/vue/makeClient"
22
import { useToast } from "vue-toastification"
33
import { useIntl } from "./intl"
4-
import { runtime, type RT } from "~/plugins/runtime"
54
import type { Effect } from "effect-app"
65
import { clientFor as clientFor_ } from "#resources/lib"
76
import type { Requests } from "effect-app/client/clientFor"
@@ -18,19 +17,17 @@ export {
1817
mapHandler,
1918
} from "@effect-app/vue"
2019

21-
const rt = computed(() => runtime.value?.runtime)
22-
2320
export const run = <A, E>(
2421
effect: Effect.Effect<A, E, RT>,
2522
options?:
2623
| {
2724
readonly signal?: AbortSignal
2825
}
2926
| undefined,
30-
) => runtime.value!.runPromise(effect, options)
27+
) => runtime.runPromise(effect, options)
3128

3229
export const runSync = <A, E>(effect: Effect.Effect<A, E, RT>) =>
33-
runtime.value!.runSync(effect)
30+
runtime.runSync(effect)
3431

3532
export const clientFor = <M extends Requests>(m: M) => runSync(clientFor_(m))
3633
export const useOperationsClient = () => runSync(OperationsClient)
@@ -42,4 +39,4 @@ export const {
4239
useSafeMutation,
4340
useSafeMutationWithState,
4441
useSafeQuery,
45-
} = makeClient(useIntl, useToast, rt)
42+
} = makeClient(useIntl, useToast, shallowRef(runtime.runtime)) // TODO
Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import * as Runtime from "effect/Runtime"
55
import { Effect, Option } from "effect-app"
66
import { WebSdkLive } from "~/utils/observability"
77
import "effect-app/builtin"
8-
import { ref, shallowRef } from "vue"
8+
import { ref } from "vue"
99
import { HttpClient } from "effect-app/http"
1010
import { FetchHttpClient } from "@effect/platform"
1111
import { ApiClientFactory } from "effect-app/client/apiClientFactory"
12-
import { defineNuxtPlugin, useRuntimeConfig } from "nuxt/app"
12+
import { type useRuntimeConfig } from "nuxt/app"
1313

1414
export const versionMatch = ref(true)
1515

16-
export const runtime = shallowRef<ReturnType<typeof makeRuntime>>()
17-
1816
function makeRuntime(feVersion: string, disableTracing: boolean) {
1917
const apiLayers = ApiClientFactory.layer({
2018
url: "/api/api",
@@ -42,23 +40,26 @@ function makeRuntime(feVersion: string, disableTracing: boolean) {
4240
),
4341
)
4442

43+
const globalLayers = disableTracing
44+
? Layer.empty
45+
: WebSdkLive({
46+
serviceName: "effect-app-boilerplate-frontend",
47+
serviceVersion: feVersion,
48+
attributes: {},
49+
})
50+
4551
const rt: {
4652
runtime: Runtime.Runtime<RT>
4753
clean: () => void
4854
} = initializeSync(
4955
// TODO: tracing when deployed
5056
disableTracing
5157
? apiLayers
52-
: apiLayers.pipe(
53-
Layer.merge(
54-
WebSdkLive({
55-
serviceName: "effect-app-boilerplate-frontend",
56-
serviceVersion: feVersion,
57-
attributes: {},
58-
}),
59-
),
60-
),
58+
: apiLayers.pipe(Layer.provideMerge(globalLayers)),
6159
)
60+
61+
//Atom.runtime.addGlobalLayer(globalLayers)
62+
6263
return {
6364
...rt,
6465
runFork: Runtime.runFork(rt.runtime),
@@ -71,13 +72,25 @@ function makeRuntime(feVersion: string, disableTracing: boolean) {
7172
// TODO: make sure the runtime provides these
7273
export type RT = ApiClientFactory
7374

74-
export default defineNuxtPlugin(_ => {
75-
const config = useRuntimeConfig()
75+
/*
76+
We read the configuration from the global var sent by server, embedded in the html document.
77+
The reason for this is, that we want to have the configuration available before the Nuxt app is initialized.
78+
Otherwise we can only initialize the runtime in nuxt plugin, script setup or middleware,
79+
which means we cannot do anything with the runtime in the root of modules, etc.
7680
77-
const rt = makeRuntime(
78-
config.public.feVersion,
79-
config.public.env !== "local-dev" || !config.public.telemetry,
80-
)
81+
Now we can use things like clientFor, which leverage the runtime, and export clients directly from modules.
82+
*/
83+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84+
const config = (globalThis as any).__NUXT__.config as ReturnType<
85+
typeof useRuntimeConfig
86+
>
87+
const isRemote = config.public.env !== "local-dev"
88+
const disableTracing = !isRemote || !config.public.telemetry
8189

82-
runtime.value = rt
83-
})
90+
export const runtime = makeRuntime(
91+
config.public.feVersion,
92+
disableTracing,
93+
// config.public.env,
94+
// isRemote,
95+
// !isRemote && !config.public.telemetry,
96+
)

0 commit comments

Comments
 (0)