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