Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Logger.pretty for runMain, and support dual usage #3506

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/friendly-maps-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/platform-node-shared": patch
"@effect/platform-browser": patch
"@effect/platform": patch
---

use Logger.pretty for runMain, and support dual usage
37 changes: 3 additions & 34 deletions packages/platform-browser/src/internal/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import type { RunMain } from "@effect/platform/Runtime"
import * as Cause from "effect/Cause"
import * as Effect from "effect/Effect"
import * as FiberRef from "effect/FiberRef"
import * as HashSet from "effect/HashSet"
import * as Logger from "effect/Logger"
import { makeRunMain } from "@effect/platform/Runtime"

/** @internal */
const useStructuredLogger = Effect.locallyWith(FiberRef.currentLoggers, (loggers) => {
if (HashSet.has(loggers, Logger.defaultLogger)) {
const set = HashSet.remove(loggers, Logger.defaultLogger)
return HashSet.add(set, Logger.withConsoleLog(Logger.structuredLogger))
}

return loggers
})

/** @internal */
export const runMain: RunMain = (
effect,
options
) => {
const _effect = useStructuredLogger(effect)

const fiber = Effect.runFork(
options?.disableErrorReporting === true ?
_effect :
Effect.tapErrorCause(_effect, (cause) => {
if (Cause.isInterruptedOnly(cause)) {
return Effect.void
}
return Effect.logError(cause)
})
)

export const runMain = makeRunMain(({ fiber }) => {
addEventListener("beforeunload", () => {
fiber.unsafeInterruptAsFork(fiber.id())
})
}
})
26 changes: 6 additions & 20 deletions packages/platform-node-shared/src/internal/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { defaultTeardown, type RunMain } from "@effect/platform/Runtime"
import * as Cause from "effect/Cause"
import * as Effect from "effect/Effect"
import { makeRunMain } from "@effect/platform/Runtime"

/** @internal */
export const runMain: RunMain = (
effect,
options
) => {
const teardown = options?.teardown ?? defaultTeardown
export const runMain = makeRunMain(({
fiber,
teardown
}) => {
const keepAlive = setInterval(() => {}, 2 ** 31 - 1)

const fiber = Effect.runFork(
options?.disableErrorReporting === true ?
effect :
Effect.tapErrorCause(effect, (cause) => {
if (Cause.isInterruptedOnly(cause)) {
return Effect.void
}
return Effect.logError(cause)
})
)

fiber.addObserver((exit) => {
clearInterval(keepAlive)
teardown(exit, (code) => {
Expand All @@ -36,4 +22,4 @@ export const runMain: RunMain = (

process.once("SIGINT", onSigint)
process.once("SIGTERM", onSigint)
}
})
72 changes: 69 additions & 3 deletions packages/platform/src/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
* @since 1.0.0
*/
import * as Cause from "effect/Cause"
import type * as Effect from "effect/Effect"
import * as Effect from "effect/Effect"
import * as Exit from "effect/Exit"
import type * as Fiber from "effect/Fiber"
import type * as FiberId from "effect/FiberId"
import * as FiberRef from "effect/FiberRef"
import * as FiberRefs from "effect/FiberRefs"
import { dual } from "effect/Function"
import * as HashSet from "effect/HashSet"
import * as Logger from "effect/Logger"

/**
* @category model
Expand All @@ -29,11 +36,70 @@ export const defaultTeardown: Teardown = <E, A>(
* @since 1.0.0
*/
export interface RunMain {
(
options?: {
readonly disableErrorReporting?: boolean | undefined
readonly disablePrettyLogger?: boolean | undefined
readonly teardown?: Teardown | undefined
}
): <E, A>(effect: Effect.Effect<A, E>) => void
<E, A>(
effect: Effect.Effect<A, E>,
options?: {
readonly disableErrorReporting?: boolean
readonly teardown?: Teardown
readonly disableErrorReporting?: boolean | undefined
readonly disablePrettyLogger?: boolean | undefined
readonly teardown?: Teardown | undefined
}
): void
}

const addPrettyLogger = (refs: FiberRefs.FiberRefs, fiberId: FiberId.Runtime) => {
const loggers = FiberRefs.getOrDefault(refs, FiberRef.currentLoggers)
if (!HashSet.has(loggers, Logger.defaultLogger)) {
return refs
}
return FiberRefs.updateAs(refs, {
fiberId,
fiberRef: FiberRef.currentLoggers,
value: loggers.pipe(
HashSet.remove(Logger.defaultLogger),
HashSet.add(Logger.prettyLogger())
)
})
}

/**
* @category constructors
* @since 1.0.0
*/
export const makeRunMain = (
f: <E, A>(
options: {
readonly fiber: Fiber.RuntimeFiber<A, E>
readonly teardown: Teardown
}
) => void
): RunMain =>
dual((args) => Effect.isEffect(args[0]), (effect: Effect.Effect<any, any>, options?: {
readonly disableErrorReporting?: boolean | undefined
readonly disablePrettyLogger?: boolean | undefined
readonly teardown?: Teardown | undefined
}) => {
const fiber = options?.disableErrorReporting === true
? Effect.runFork(effect, {
updateRefs: options?.disablePrettyLogger === true ? undefined : addPrettyLogger
})
: Effect.runFork(
Effect.tapErrorCause(effect, (cause) => {
if (Cause.isInterruptedOnly(cause)) {
return Effect.void
}
return Effect.logError(cause)
}),
{
updateRefs: options?.disablePrettyLogger === true ? undefined : addPrettyLogger
}
)
const teardown = options?.teardown ?? defaultTeardown
return f({ fiber, teardown })
})