42
packages/example/src/routes/time.tsx
Normal file
42
packages/example/src/routes/time.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Reffuse } from "@/reffuse"
|
||||
import { createFileRoute } from "@tanstack/react-router"
|
||||
import { Console, DateTime, Effect, Ref, Schedule, SubscriptionRef } from "effect"
|
||||
import { useMemo } from "react"
|
||||
|
||||
|
||||
export const Route = createFileRoute("/time")({
|
||||
component: Time
|
||||
})
|
||||
|
||||
function Time() {
|
||||
|
||||
const runtime = Reffuse.useRuntime()
|
||||
|
||||
const timeRef = useMemo(() => DateTime.now.pipe(
|
||||
Effect.flatMap(SubscriptionRef.make),
|
||||
runtime.runSync,
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
), [])
|
||||
|
||||
Reffuse.useFork(Effect.addFinalizer(() => Console.log("Component unmounted.")).pipe(
|
||||
Effect.flatMap(() => DateTime.now),
|
||||
Effect.flatMap(v => Ref.set(timeRef, v)),
|
||||
Effect.repeat(Schedule.spaced("1 second")),
|
||||
))
|
||||
|
||||
const [time] = Reffuse.useRefState(timeRef)
|
||||
|
||||
|
||||
return (
|
||||
<div className="container mx-auto">
|
||||
<p className="text-center">
|
||||
{DateTime.format(time, {
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
second: "numeric",
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user