mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 23:56:51 +00:00
21 lines
487 B
TypeScript
21 lines
487 B
TypeScript
import type { DataModel } from '@mcschema/core'
|
|
import type { Inputs } from 'preact/hooks'
|
|
import { useEffect } from 'preact/hooks'
|
|
|
|
export function useModel(model: DataModel | undefined | null, invalidated: (model: DataModel) => unknown, inputs?: Inputs) {
|
|
const listener = {
|
|
invalidated() {
|
|
if (model) {
|
|
invalidated(model)
|
|
}
|
|
},
|
|
}
|
|
|
|
useEffect(() => {
|
|
model?.addListener(listener)
|
|
return () => {
|
|
model?.removeListener(listener)
|
|
}
|
|
}, [model, ...inputs ?? []])
|
|
}
|