2024-03-15 22:48:28 +01:00
|
|
|
import { useEffect, useState } from "react"
|
|
|
|
|
2024-03-16 00:36:44 +01:00
|
|
|
import type { Presenter } from "@/presentation/presenters/_Presenter"
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-04-08 23:21:36 +02:00
|
|
|
export const usePresenterState = <State>(
|
|
|
|
presenter: Presenter<State>,
|
|
|
|
): State => {
|
|
|
|
const [state, setState] = useState<State>(presenter.initialState)
|
2024-03-15 22:48:28 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-08 23:21:36 +02:00
|
|
|
const presenterSubscription = (state: State): void => {
|
2024-03-15 22:48:28 +01:00
|
|
|
setState(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
presenter.subscribe(presenterSubscription)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
return presenter.unsubscribe(presenterSubscription)
|
|
|
|
}
|
|
|
|
}, [presenter])
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|