| 1 | # Runtime: `Runtime<R>` Removed |
| 2 | |
| 3 | In v3, `Runtime<R>` bundled a `Context<R>`, `RuntimeFlags`, and `FiberRefs` |
| 4 | into a single value used to execute effects: |
| 5 | |
| 6 | ```ts |
| 7 | // v3 |
| 8 | interface Runtime<in R> { |
| 9 | readonly context: Context.Context<R> |
| 10 | readonly runtimeFlags: RuntimeFlags |
| 11 | readonly fiberRefs: FiberRefs |
| 12 | } |
| 13 | ``` |
| 14 | |
| 15 | In v4, this type no longer exists and you can use `Context<R>` instead. |
| 16 | Run functions live directly on `Effect`, and the `Runtime` module is reduced to |
| 17 | process lifecycle utilities. |
| 18 | |
| 19 | ## `Runtime.runFork(runtime)` -> `Effect.runForkWith(services)` |
| 20 | |
| 21 | In v3, running an effect with dependencies usually meant pulling the current |
| 22 | runtime from `Effect.runtime<R>()` and calling `Runtime.runFork(runtime)` inside |
| 23 | the main effect. |
| 24 | |
| 25 | **v3** |
| 26 | |
| 27 | ```ts |
| 28 | import { Context, Effect, Runtime } from "effect" |
| 29 | |
| 30 | class Logger extends Context.Tag("Logger")<Logger, { |
| 31 | readonly log: (message: string) => void |
| 32 | }>() {} |
| 33 | |
| 34 | const program = Effect.gen(function*() { |
| 35 | const logger = yield* Logger |
| 36 | logger.log("Hello from Logger") |
| 37 | }) |
| 38 | |
| 39 | const main = Effect.gen(function*() { |
| 40 | const runtime = yield* Effect.runtime<Logger>() |
| 41 | return Runtime.runFork(runtime)(program) |
| 42 | }).pipe( |
| 43 | Effect.provideService(Logger, { |
| 44 | log: (message) => console.log(message) |
| 45 | }) |
| 46 | ) |
| 47 | |
| 48 | const fiber = Effect.runFork(main) |
| 49 | ``` |
| 50 | |
| 51 | In v4, use the same pattern with `Effect.context<R>()`, then run with |
| 52 | `Effect.runForkWith(services)`: |
| 53 | |
| 54 | **v4** |
| 55 | |
| 56 | ```ts |
| 57 | import { Context, Effect } from "effect" |
| 58 | |
| 59 | class Logger extends Context.Service<Logger, { |
| 60 | readonly log: (message: string) => void |
| 61 | }>()("Logger") {} |
| 62 | |
| 63 | const program = Effect.gen(function*() { |
| 64 | const logger = yield* Logger |
| 65 | logger.log("Hello from Logger") |
| 66 | }) |
| 67 | |
| 68 | const main = Effect.gen(function*() { |
| 69 | const services = yield* Effect.context<Logger>() |
| 70 | return Effect.runForkWith(services)(program) |
| 71 | }).pipe( |
| 72 | Effect.provideContext(Context.make(Logger, { |
| 73 | log: (message) => console.log(message) |
| 74 | })) |
| 75 | ) |
| 76 | |
| 77 | const fiber = Effect.runFork(main) |
| 78 | ``` |
| 79 | |
| 80 | If your effect has no service requirements, use `Effect.runFork(effect)`. |
| 81 | |
| 82 | ## `Runtime` Module Contents |
| 83 | |
| 84 | The `Runtime` module now only contains: |
| 85 | |
| 86 | - `Teardown` — interface for handling process exit |
| 87 | - `defaultTeardown` — default teardown implementation |
| 88 | - `makeRunMain` — creates platform-specific main runners |