2025-05-30 19:59:02 +00:00
|
|
|
import { Component } from "preact"
|
2022-10-26 13:11:51 +08:00
|
|
|
|
2025-05-30 19:59:02 +00:00
|
|
|
export const Stateful = ({ getInitialState, handlers, render }) => {
|
2022-10-26 13:11:51 +08:00
|
|
|
return class extends Component {
|
|
|
|
|
constructor() {
|
|
|
|
|
super()
|
|
|
|
|
this.compState = getInitialState()
|
|
|
|
|
this.handlers = Object.fromEntries(
|
2025-05-30 19:59:02 +00:00
|
|
|
Object.entries(handlers).map(([name, h]) => [
|
|
|
|
|
name,
|
|
|
|
|
this.makeHandler(h),
|
|
|
|
|
]),
|
2022-10-26 13:11:51 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeHandler(h) {
|
|
|
|
|
return (...args) => {
|
|
|
|
|
this.compState = h(this.compState, ...args)
|
|
|
|
|
this.forceUpdate()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return render(this.props, this.compState, this.handlers)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|